Tensorflow模型保存与读取
来源:互联网 发布:依然范特西 知乎 编辑:程序博客网 时间:2024/04/30 19:53
import tensorflow as tfimport numpy as npimport os#输入数据x_data = np.linspace(-1,1,300)[:, np.newaxis]noise = np.random.normal(0,0.05, x_data.shape)y_data = np.square(x_data)-0.5+noise#输入层xs = tf.placeholder(tf.float32, [None, 1])ys = tf.placeholder(tf.float32, [None, 1])#隐层W1 = tf.Variable(tf.random_normal([1,10]))b1 = tf.Variable(tf.zeros([1,10])+0.1)Wx_plus_b1 = tf.matmul(xs,W1) + b1output1 = tf.nn.relu(Wx_plus_b1)#输出层W2 = tf.Variable(tf.random_normal([10,1]))b2 = tf.Variable(tf.zeros([1,1])+0.1)Wx_plus_b2 = tf.matmul(output1,W2) + b2output2 = Wx_plus_b2#损失loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-output2),reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#模型保存加载工具saver = tf.train.Saver()#判断模型保存路径是否存在,不存在就创建if not os.path.exists('tmp/'): os.mkdir('tmp/')#初始化sess = tf.Session()if os.path.exists('tmp/checkpoint'): #判断模型是否存在 saver.restore(sess, 'tmp/model.ckpt') #存在就从模型中恢复变量else: init = tf.global_variables_initializer() #不存在就初始化变量 sess.run(init)#训练for i in range(1000): _,loss_value = sess.run([train_step,loss], feed_dict={xs:x_data,ys:y_data}) if(i%50==0): #每50次保存一次模型 save_path = saver.save(sess, 'tmp/model.ckpt') #保存模型到tmp/model.ckpt,注意一定要有一层文件夹,否则保存不成功!!! print("模型保存:%s 当前训练损失:%s"%(save_path, loss_value))
0 0
- Tensorflow模型保存与读取
- tensorflow-模型保存与读取
- sklearn、TensorFlow、keras模型保存与读取
- TensorFlow保存读取模型
- Tensorflow保存和读取模型
- 保存和读取 TensorFlow 模型
- [Deep Learning] TensorFlow模型、参数的保存与读取
- tensorflow 模型保存与加载
- tensorflow模型保存与可视化
- TensorFlow 模型保存与加载
- Tensorflow小样例-Saver模型保存读取
- 6.TensorFlow模型的保存和读取
- TensorFlow模型文件保存和读取
- tensorflow之保存模型与加载模型
- Tensorflow学习笔记--模型保存与调取
- 【深度学习】Tensorflow模型保存与恢复
- Tensorflow学习笔记-模型保存与加载
- tensorflow 模型的保存与恢复(Saver)
- 网络信息安全及防范策略
- 如何选择才是一个正确的选择啊?遇事不慌不忙,理性加冷静就行了的。
- Redis 网络架构及单线程模型
- 算法设计Week2 LeetCode #169 Majority Element
- 软件定义,软件开发,软件维护
- Tensorflow模型保存与读取
- Unity在移动端iphone/ipad上的触屏手势
- java内存中的对象研究
- Android学习——Android 系统中的UID、GID、GIDS与PID
- 6.1、Cube(多维数据集)、tuple(元组)、member(成员)
- java线程池
- js设计模式
- eclipse maven 搭建 SSM(Spring+SpringMVC+MyBatis)开发环境 和 MyBatis 自动生成的 maven 插件配置
- Linux命令大全