0
所以,我有这段代码:为什么变量不在这个程序中更新?
graph = tf.Graph()
with graph.as_default():
res = tf.Variable(
np.zeros((100, 100), dtype=np.float32)
)
mask = tf.placeholder(np.float32, (100, 100))
res = tf.add(res, mask)
init = tf.global_variables_initializer()
with tf.Session(graph=graph) as sess:
sess.run(init)
for i in range(100):
x1, x2, y1, y2 = np.random.randint(0, 100, 4)
x1, x2 = sorted((x1, x2))
y1, y2 = sorted((y1, y2))
c_mask = np.zeros((100, 100))
c_mask[x1:x2, y1:y2] = 255
new_pic = sess.run([res], feed_dict={mask:c_mask})[0]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(new_pic.astype('uint8'), cmap='gray')
fig.show()
基本上,它(至少应该)黑底上吸引100个随机白色矩形。但是我所得到的是这样的:
我不明白。它看起来像每次迭代res
只是重新设置黑色石板再次(图片上的矩形是绘制的最后一个,由坐标来判断)。我不是以某种方式保存它,或者我做错了什么?
这很有道理,是的,谢谢。我没有想到重写和修改张量之间可能有任何区别,但这似乎是合乎逻辑的。 – Akiiino