莫烦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 _ ...
随机推荐
- 002_logging
Java 中最通用的日志模块莫过于 Log4j 了,在 python 中,也自带了 logging 模块,该模块的用法其实和 Log4j 类似. Python 使用logging模块记录日志涉及四个主 ...
- WiFi-ESP8266入门http(3-4)网页一键配网(1若为普通wifi直连 2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信
网页一键配网(1若为普通wifi直连 2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信 工程连接:https://github.com/Dongvdong/ESP8266_H ...
- (二 -3) 天猫精灵接入Home Assistant-自动发现Mqtt设备--灯系列
参考网站 1该mqtt光平台可以让您控制您的MQTT启用灯.它支持设置亮度,色温,效果,闪烁,开/关,RGB颜色,过渡,XY颜色和白色值. https://www.home-assistant.io/ ...
- jvm 年轻代、年老代、永久代
关键字约定 Young generation –>新生代 Tenured / Old Generation –>老年代 Perm Area –>永久代 年轻代: 所有新生 ...
- Uncaught ReferenceError: layer is not defined
错误详细信息,如下: Uncaught ReferenceError: layer is not defined' 关键词就是not defined 未定义,那么解决方案就是给它定义. 原来的问题代码 ...
- XSS简介
xss又叫css,为了与前端的css区别,所以叫xss,即跨站脚本攻击. XSS原理解析 XSS攻击是在网页中嵌入客户端恶意脚本代码,恶意代码一般都是javascript编写的.想要深入研究XSS,必 ...
- 剑指offer——矩形覆盖
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 分析:斐波那契数列的变形 n=0,返回0 n=1,返回1 n=2,返回 ...
- Feature Extractor[DenseNet]
0.背景 随着CNN变得越来越深,人们发现会有梯度消失的现象.这个问题主要是单路径的信息和梯度的传播,其中的激活函数都是非线性的,从而特别是乘法就可以使得随着层数越深,假设将传统的神经网络的每一层看成 ...
- Generative Adversarial Nets[Introduction]
0. 背景 通过阅读书籍<Pro Deep Learning with TensorFlow: A Mathematical Approach to Advanced Artificial In ...
- SpringBoot + Shiro + shiro.ini 的踩坑记录
0.写在前面的话 好久没写博客了,诶,好多时候偷懒直接就抓网上的资料丢笔记里了,也就没有自己提炼,偷懒偷懒.然后最近参加了一个网络课程,要交作业的那种,为了能方便看下其他同学的作业,就写了个爬虫把作业 ...