【问题标题】:Tensorflow model's weight updated on sess.runTensorflow 模型的权重在 sess.run 上更新
【发布时间】:2018-06-20 09:47:42
【问题描述】:

当我运行 sess.run(不参考训练步骤)时,我在模型中的权重会更新,这让我很苦恼。

我尝试为我的模型提供变量以获得估计的输出,但是当我运行 sess.run 时,权重会更新。

### in the training phase ####
X_eval, Y_eval, O_eval, W_eval, cost_eval, train_step_eval = sess.run([X, Y, O_out, W, cost, train_step], feed_dict={X:x_batch , Y:y_batch})

### when the training is finished (closed for loop) ###
print(W_eval)
Y_out, W_eval2 = sess.run([O_out, W], feed_dict = {X:labeled_features[:,: - n_labels], Y:labeled_features[:,- n_labels :]})
print(W_eval2)

当我比较 W_eval 和 W_eval2 时,它们并不相同,我不明白为什么。 您能否指出正确的方向,为什么权重不一样?

'w3': array([[-2.9685912],
   [-3.215485 ],
   [ 3.8806837],
   [-3.331745 ],
   [-3.3904853]], dtype=float32

'w3': array([[-2.9700036],
   [-3.2168453],
   [ 3.8804765],
   [-3.3330843],
   [-3.3922129]], dtype=float32

提前谢谢你。

编辑添加了 W_eval 分配。

【问题讨论】:

  • 您的代码不完整。请说明您从哪里获得W_eval

标签: python-3.x tensorflow


【解决方案1】:

你的代码

### in the training phase ####
X_eval, Y_eval, O_eval, W_eval, cost_eval, train_step_eval = sess.run([X, Y, O_out, W, cost, train_step], feed_dict={X:x_batch , Y:y_batch})

### when the training is finished (closed for loop) ###
print(W_eval)
Y_out, W_eval2 = sess.run([O_out, W], feed_dict = {X:labeled_features[:,: - n_labels], Y:labeled_features[:,- n_labels :]})
print(W_eval2)

仍然执行train_step。一个更简单的理解发生了什么的版本是:

import tensorflow as tf

a = tf.get_variable('a', initializer=42.)

train_step = a.assign(a + 1)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    value, _ = sess.run([a, train_step])  # will update a
    print(value)
    value = sess.run([a])  # will not update a
    print(value)
    value = sess.run([a])  # will not update a
    print(value)

给出输出

42.0
[43.0]
[43.0]

要检查的另一件事是x_batch == labeled_features[:,: - n_labels] 是否成立。

【讨论】:

  • 为了清楚起见,您可能需要添加 W_eval 包含来自之前执行训练步骤的值,以防两者都通过相同的@987654327返回@调用。
  • 各位,非常感谢你们的时间和精力。现在我明白了,sess.run 实际上返回了权重/变量的先前迭代。这是我在任何地方都没有读过的东西,但现在我知道多亏了你,应该关注什么。非常感谢。
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多