多次运行会话时结果不匹配

时间:2018-11-22 02:13:48

标签: tensorflow keras

当我尝试打印out1和out2时,我发现out1中不存在out2中的值。但是out2只是从out1中找到最大值。需要帮助

import tensorflow as tf
from keras import backend as K
box_class_probs = tf.random_normal([2, 2, 1, 2], mean=1, stddev=4, seed = 1)
max_ind_class=K.max(box_class_probs,axis=-1)

with tf.Session() as sess:
    out1=sess.run(box_class_probs)
    print(out1)
    out2=sess.run(max_ind_class)
    print(out2)

输出:

[[[[-2.24527287  6.93839502]]

  [[ 1.26131749 -8.77081585]]]


 [[[ 1.39699364  3.36489725]]

  [[ 3.37129188 -7.49171829]]]]
---------------------------------------------
---------------------------------------------
---------------------------------------------
[[[ 1.96837616]
  [ 3.06311464]]

 [[ 9.33515644]
  [ 6.58941841]]]

1 个答案:

答案 0 :(得分:0)

您需要在一次会话运行中同时运行两个结果,因为您是随机生成box_class_probs,并且根据随机种子(默认种子或内部种子),每次执行会话运行时它都会改变。而且,请记住,它总是更加一致,以使用K.get_session()获取当前的keras后端会话,然后在混合keras和tensorflow时运行代码。

sess = K.get_session()
out1, out2 = sess.run([box_class_probs, max_ind_class])
print(out1)
print(out2)

结果:

[[[[-2.2452729  6.938395 ]]

  [[ 1.2613175 -8.770817 ]]]


 [[[ 1.3969936  3.3648973]]

  [[ 3.3712919 -7.4917183]]]]
[[[6.938395 ]
  [1.2613175]]

 [[3.3648973]
  [3.3712919]]]