莫烦theano学习自修第十天【保存神经网络及加载神经网络】
1. 为何保存神经网络
保存神经网络指的是保存神经网络的权重W及偏置b,权重W,和偏置b本身是一个列表,将这两个列表的值写到列表或者字典的数据结构中,使用pickle的数据结构将列表或者字典写入到文件中,保存神经网络只须保存权重W和偏置b,神经网络的结构下次再次定义为一样的,只需从文件中加载权重W和偏置b参数即可,无需重新训练神经网络
2. 代码实现:
from __future__ import print_function import numpy as np import theano import theano.tensor as T import pickle def compute_accuracy(y_target, y_predict): correct_prediction = np.equal(y_predict, y_target) accuracy = np.sum(correct_prediction)/len(correct_prediction) return accuracy rng = np.random # set random seed np.random.seed(100) N = 400 feats = 784 # generate a dataset: D = (input_values, target_class) D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2)) # Declare Theano symbolic variables x = T.dmatrix("x") y = T.dvector("y") # initialize the weights and biases w = theano.shared(rng.randn(feats), name="w") b = theano.shared(0., name="b") # Construct Theano expression graph p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) prediction = p_1 > 0.5 xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) cost = xent.mean() + 0.01 * (w ** 2).sum() gw, gb = T.grad(cost, [w, b]) # Compile learning_rate = 0.1 train = theano.function( inputs=[x, y], updates=((w, w - learning_rate * gw), (b, b - learning_rate * gb))) predict = theano.function(inputs=[x], outputs=prediction) # Training for i in range(500): train(D[0], D[1]) # save model with open('save/model.pickle', 'wb') as file: model = [w.get_value(), b.get_value()] pickle.dump(model, file) print(model[0][:10]) print("accuracy:", compute_accuracy(D[1], predict(D[0]))) # load model with open('save/model.pickle', 'rb') as file: model = pickle.load(file) w.set_value(model[0]) b.set_value(model[1]) print(w.get_value()[:10]) print("accuracy:", compute_accuracy(D[1], predict(D[0])))
莫烦theano学习自修第十天【保存神经网络及加载神经网络】的更多相关文章
- 莫烦theano学习自修第九天【过拟合问题与正规化】
如下图所示(回归的过拟合问题):如果机器学习得到的回归为下图中的直线则是比较好的结果,但是如果进一步控制减少误差,导致机器学习到了下图中的曲线,则100%正确的学习了训练数据,看似较好,但是如果换成另 ...
- 莫烦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 ...
- 莫烦theano学习自修第四天【激励函数】
1. 定义 激励函数通常用于隐藏层,是将特征值进行过滤或者激活的算法 2.常见的激励函数 1. sigmoid (1)sigmoid() (2)ultra_fast_sigmoid() (3)hard ...
随机推荐
- centos7修改系统语言为简体中文
centos7修改系统语言为简体中文 说明 自己装系统时一般都可以自定义选择系统语言.可是云端服务器一般都是安装好的镜像,默认系统语言为英文,对于初学者可能还会有搞不懂的计算机词汇.这里简单说一下ce ...
- Python:Day09
Ubantu忘记密码: 1.开机长按shift,进入界面后按e: 2.将红框中内改成如下并按F10重启: 3.输入passwd,然后用户名,然后重新输入密码: locale命令查看系统中是否有中文 a ...
- SQLAlchemy中的自引用
SQLALCHEMY采用adjacency list pattern来表示类的自引用. 例如,对于类Node自引用: class Node(Base): __tablename__='node' id ...
- 解决 Vim 的 quickfix 插件错误信息乱码问题
将以下代码插入 vim 配置文件即可, function! QfMakeConv() let qflist = getqflist() for i in q ...
- Pyhon流程控制
1.条件控制 Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else. 注意: 1.每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语 ...
- 隐写工具Hydan的安装使用方法
Hydan是可以在32位ELF二进制文件里隐藏信息的工具,主要原理是利用了i386指令中的冗余信息. 官网地址:http://www.crazyboy.com/hydan/ 但这个工具最后更新好像是在 ...
- Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
1. Hystrix Dashboard (断路器:hystrix 仪表盘) Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...
- .NET Core 2.1中的分层编译(预览)
如果您是.NET性能的粉丝,最近有很多好消息,例如.NET Core 2.1中的性能改进和宣布.NET Core 2.1,但我们还有更多的好消息.分层编译是一项重要的新特性功能,我们可以作为预览供任何 ...
- 开源后的.Net 如何选择使用
.NET是跨平台的开发栈.它有一个标准库,称为.NET Standard Library,其中包含了大量的APIs.这个标准库由各种.NET运行环境实现:.NET Framework..NET Co ...
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...