莫烦theano学习自修第九天【过拟合问题与正规化】
如下图所示(回归的过拟合问题):如果机器学习得到的回归为下图中的直线则是比较好的结果,但是如果进一步控制减少误差,导致机器学习到了下图中的曲线,则100%正确的学习了训练数据,看似较好,但是如果换成另外一批数据,则不能较好的反应数据,造成过大的误差,这就是过拟合问题

再看下图这是分类问题的过拟合问题

2. 正规化方法
(1)l1正规化:使用权重绝对值和的方式惩罚误差
(2)l2正规化:使用权重平方和的方式惩罚误差
3. 代码实现:
from __future__ import print_function
import theano
from sklearn.datasets import load_boston
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt
class Layer(object):
def __init__(self, inputs, in_size, out_size, activation_function=None):
self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))
self.b = theano.shared(np.zeros((out_size, )) + 0.1)
self.Wx_plus_b = T.dot(inputs, self.W) + self.b
self.activation_function = activation_function
if activation_function is None:
self.outputs = self.Wx_plus_b
else:
self.outputs = self.activation_function(self.Wx_plus_b)
def minmax_normalization(data): # 对输入数据进行预处理,处理成大于0,小于1的范围
xs_max = np.max(data, axis=0)
xs_min = np.min(data, axis=0)
xs = (1 - 0) * (data - xs_min) / (xs_max - xs_min) + 0
return xs
# 获取波士顿房价数据
np.random.seed(100)
x_data = load_boston().data
# minmax normalization, rescale the inputs
x_data = minmax_normalization(x_data)
y_data = load_boston().target[:, np.newaxis]
# 进行交叉验证,将数据的一部分数据作为训练数据,另一部分作废测试数据
# cross validation, train test data split
x_train, y_train = x_data[:400], y_data[:400]
x_test, y_test = x_data[400:], y_data[400:]
x = T.dmatrix("x")
y = T.dmatrix("y")
l1 = Layer(x, 13, 50, T.tanh)
l2 = Layer(l1.outputs, 50, 1, None)
# the way to compute cost
cost = T.mean(T.square(l2.outputs - y)) # without regularization
# cost = T.mean(T.square(l2.outputs - y)) + 0.1 * ((l1.W ** 2).sum() + (l2.W ** 2).sum()) # with l2 regularization
# cost = T.mean(T.square(l2.outputs - y)) + 0.1 * (abs(l1.W).sum() + abs(l2.W).sum()) # with l1 regularization
gW1, gb1, gW2, gb2 = T.grad(cost, [l1.W, l1.b, l2.W, l2.b])
learning_rate = 0.01
train = theano.function(
inputs=[x, y],
updates=[(l1.W, l1.W - learning_rate * gW1),
(l1.b, l1.b - learning_rate * gb1),
(l2.W, l2.W - learning_rate * gW2),
(l2.b, l2.b - learning_rate * gb2)])
compute_cost = theano.function(inputs=[x, y], outputs=cost)
# record cost
train_err_list = []
test_err_list = []
learning_time = []
for i in range(1000):
train(x_train, y_train)
if i % 10 == 0:
# record cost
train_err_list.append(compute_cost(x_train, y_train))
test_err_list.append(compute_cost(x_test, y_test))
learning_time.append(i)
# plot cost history
plt.plot(learning_time, train_err_list, 'r-')
plt.plot(learning_time, test_err_list, 'b--')
plt.show()
莫烦theano学习自修第九天【过拟合问题与正规化】的更多相关文章
- 莫烦theano学习自修第十天【保存神经网络及加载神经网络】
1. 为何保存神经网络 保存神经网络指的是保存神经网络的权重W及偏置b,权重W,和偏置b本身是一个列表,将这两个列表的值写到列表或者字典的数据结构中,使用pickle的数据结构将列表或者字典写入到文件 ...
- 莫烦theano学习自修第八天【分类问题】
1. 代码实现 from __future__ import print_function import numpy as np import theano import theano.tensor ...
- 莫烦theano学习自修第七天【回归结果可视化】
1.代码实现 from __future__ import print_function import theano import theano.tensor as T import numpy as ...
- 莫烦theano学习自修第六天【回归】
1. 代码实现 from __future__ import print_function import theano import theano.tensor as T import numpy a ...
- 莫烦theano学习自修第五天【定义神经层】
1. 代码如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T ...
- 莫烦theano学习自修第三天【共享变量】
1. 代码实现 #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T i ...
- 莫烦theano学习自修第二天【激励函数】
1. 代码如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T ...
- 莫烦theano学习自修第一天【常量和矩阵的运算】
1. 代码实现如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ # 导入numpy模块,因为numpy是常用的计算模块 import numpy as ...
- 莫烦sklearn学习自修第九天【过拟合问题处理】
1. 过拟合问题可以通过调整机器学习的参数来完成,比如sklearn中通过调节gamma参数,将训练损失和测试损失降到最低 2. 代码实现(显示gamma参数对训练损失和测试损失的影响) from _ ...
随机推荐
- 100Mbps和100Mb/s有什么不同
100Mbps 和 100Mb/s 有什么不同 Mbps=Mbit/s即兆比特每秒.Million bits per second的缩写 传输速率是指设备的的数据交换能力,也叫“带宽”,单位是Mbps ...
- Linux 分卷压缩
例如,要将大文件夹 PYNQ 分卷压缩成 1G 的单元大小,如下命令(类似的可以指定 tar 的参数为 czf 而生产 .tar.gz 格式的压缩包:可以指定分卷大小例如 500M 等),压缩完成后, ...
- Python+自动化测试框架的设计编写
Python之一个简单的自动化测试框架:https://baijiahao.baidu.com/s?id=1578211870226409536&wfr=spider&for=pc h ...
- Luogu P1776 宝物筛选_NOI导刊2010提高(02)(多重背包模版)
传送门 多重背包板子题, 多重背包就是每种东西有好几个,可以把它拆分成一个一个的01背包 优化:二进制拆分(拆成1+2+4+8+16+...) 比如18=1+2+4+8+3,可以证明18以内的任何数都 ...
- object detection[YOLO]
这部分,我们来聊聊YOLO. YOLO:You Only Look Once,顾名思义,就是希望网络在训练过程中,一张图片只要看一次就行,不需要去多次观察,比如滑框啥的,从而从底层原理上就减少了很多的 ...
- Generative Adversarial Nets[Pre-WGAN]
本文来自<towards principled methods for training generative adversarial networks>,时间线为2017年1月,第一作者 ...
- Java关键字(五)——this
this 也是Java中的一个关键字,在<Java编程思想>第四版第五章5.4小节对 this 关键字是这样介绍的: this 关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用 ...
- .NET Core中复制源文件夹下的所有内容到新文件夹
.NET Core中没有原生的复制文件夹方法,我们可以自己写个: 新建一个.NET Core控制台项目,示例代码如下: using System; using System.IO; namespace ...
- Microsoft Artificial Intelligence Conference(2018.05.21)
时间:2018.05.21地点:北京嘉丽大酒店
- 使用hibernate造成的MySql 8小时问题解决方案
本文借鉴了网上的很多博客,在此不再声明 总结 1.增加 MySQL 的 wait_timeout 属性的值(不推荐) mysql5之前的版本,可以在jdbc连接的url中加入:autoReconnec ...