美文网首页
2018-12-23-tensorflow要点

2018-12-23-tensorflow要点

作者: HollyMeng | 来源:发表于2018-12-23 17:17 被阅读0次

保存检查点(checkpoint)

为了得到可以用来后续恢复模型以进一步训练或评估的检查点文件(checkpoint file),我们实例化一个tf.train.Saver

saver = tf.train.Saver()

在训练循环中,将定期调用saver.save()方法,向训练文件夹中写入包含了当前所有可训练变量值得检查点文件。

saver.save(sess, FLAGS.train_dir, global_step=step)

这样,我们以后就可以使用saver.restore()方法,重载模型的参数,继续训练。

saver.restore(sess, FLAGS.train_dir)

例:
保存变量
用tf.train.Saver()创建一个Saver来管理模型中的所有变量。

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  ..
  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print "Model saved in file: ", save_path

恢复变量
用同一个Saver对象来恢复变量。注意,当你从文件中恢复变量时,不需要事先对它们做初始化。

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")
  print "Model restored."
  # Do some work with the model
  ...

选择存储和恢复哪些变量

如果你不给tf.train.Saver()传入任何参数,那么saver将处理graph中的所有变量。其中每一个变量都以变量创建时传入的名称被保存。

有时候在检查点文件中明确定义变量的名称很有用。举个例子,你也许已经训练得到了一个模型,其中有个变量命名为"weights",你想把它的值恢复到一个新的变量"params"中。

有时候仅保存和恢复模型的一部分变量很有用。再举个例子,你也许训练得到了一个5层神经网络,现在想训练一个6层的新模型,可以将之前5层模型的参数导入到新模型的前5层中。

你可以通过给tf.train.Saver()构造函数传入Python字典,很容易地定义需要保持的变量及对应名称:键对应使用的名称,值对应被管理的变量。

注意:

  • 如果需要保存和恢复模型变量的不同子集,可以创建任意多个saver对象。同一个变量可被列入多个saver对象中,只有当saver的restore()函数被运行时,它的值才会发生改变。
  • 如果你仅在session开始时恢复模型变量的一个子集,你需要对剩下的变量执行初始化op。详情请见tf.initialize_variables()
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore only 'v2' using the name "my_v2"
saver = tf.train.Saver({"my_v2": v2})
# Use the saver object normally after that.
...

关于tensorflow中tensorborad No dashboards are active for the current data set.的解决办法

链接在这里:(https://stackoverflow.com/questions/47113472/tensorboard-error-no-dashboards-are-active-for-current-data-set)

our issue may be related to the drive you are attempting to start tensorboard from and the drive your logdir is on. Tensorboard uses a colon to separate the optional run name and the path in the logdir flag, so your path is being interpreted as \path\to\output\folder with name C. This can be worked around by either starting tensorboard from the same drive as your log directory or by providing an explicit run name, e.g. --logdir=mylogs:C:\path\to\output\folder.

什么意思呢。也就是说你的--logdir后面的文件和目录是通过冒号分割的,先写文件,再写路径:

附上我的代码:

python;gutter:true;">import tensorflow as tf
a=tf.constant(1,name="input_a")
b=tf.constant(2,name="input_b")
c=tf.multiply(a,b, name=‘mul_c‘)
d=tf.multiply(a,b, name=‘mul_d‘)
e=tf.add(c,d, name=‘add_e‘)
print(e)
sess=tf.Session()
sess.run(e)
writer=tf.summary.FileWriter(‘./my_graph‘,sess.graph)
然后在console上执行:

注意:tensorboard --logdir=file_name:path_name

tensorboard --logdir=logs:D:/Code/PycharmProjects/test/venv

image.png image.png

附上成功的代码:

import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 构造一个线性模型
#
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化变量
init = tf.initialize_all_variables()

# 启动图 (graph)
sess = tf.Session()
sess.run(init)

# 拟合平面
for step in range(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

# 得到最佳拟合结果 W: [[0.100  0.200]], b: [0.300]
summary_writer = tf.summary.FileWriter('logs', sess.graph)
image.png

相关文章

  • 2018-12-23-tensorflow要点

    保存检查点(checkpoint) 为了得到可以用来后续恢复模型以进一步训练或评估的检查点文件(checkpoin...

  • 一定不要点进来!!!!!!!!!

    一定不要点进来一定不要点进来一定不要点进来一定不要点进来一定不要点进来一定不要点进来一定不要点进来一定不要点进来一...

  • 手写Mybatis

    入门要点 框架要点

  • Unity3D开发特效组件之TrailRenderer(三)

    本节要点 小结 本节要点 小结

  • 原生js实现元素的拖拽

    demo地址 要点1:css 必须设置position要点2:鼠标事件的应用要点3:鼠标弹起,事件清空

  • 【拆书】学习一项技能的几个要点

    R: 要点一:克服恐惧 要点二:善于借人之势 要点三:刻意练习 要点四:揭掉标签 I:这四个片段讲得是如何学习一项...

  • 护肤小知识(不同肤质补水要点)

    不同肤质补水要点: 中性皮肤补水要点是洁面。 干性皮肤补水要点是面霜。 敏感性皮肤补水要点是夜间补水。 油性皮肤补...

  • bullet point

    bullet point : 要点 介绍上架产品时罗列的几个要点,要点的质量关系你产品的销量

  • 要点

    一、整体计划 抓整体,定计划 稳步前进 多实践,反复听 二、内容传播法则 生命期短,跟进要快 引导互动,维护互动,...

  • 要点

    1.关注体温的变化,体温是术后炎症的表现,如出现38度以上的高温, 采取以下措施。.服用美林,每次十毫升。第二。用...

网友评论

      本文标题:2018-12-23-tensorflow要点

      本文链接:https://www.haomeiwen.com/subject/rfknkqtx.html