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



当我尝试打印out1和out2时,我发现out2中的值在out1中不存在。但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]]]

您需要在一次会话运行中运行两个结果,因为您是随机生成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]]]

最新更新