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