我想在张量的第一维中插入两个新值矩阵,我使用的是tensor_scatter_add方法,但是它给了我一个错误。
indices = tf.constant([[0], [2]])
updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]],
[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]]])
tensor = tf.ones([4, 5, 4])
updated = tf.tensor_scatter_add(tensor, indices, updates)
with tf.Session() as se:
print(ses.run(scatter))发布于 2019-12-09 10:59:23
只要更正这些行,您就会错误地输入这些代码,这就解决了代码中的问题:
tensor = tf.ones([4, 4, 4])
updated = tf.tensor_scatter_add(tensor, indices, updates)
with tf.Session() as se:
print(se.run(scatter))发布于 2019-12-09 10:52:02
tensor的内部二维必须与updates的内部二维相匹配。Dimension 0 in both shapes must be equal, but are 5 and 4。
tensor必须与updates具有相同的dtype,但在代码中不同。
在以下方面存在错误:
with tf.Session() as se:
print(ses.run(scatter))您将tf.Session()化名为se,但是调用ses而不是se,并将其传递给ses.run(),但它在任何地方都没有定义;se.run(updated)应该是正确的函数调用。
带有代码修复的片段:
这对你来说应该很好。
indices = tf.constant([[0], [2]])
updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]],
[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]]])
tensor = tf.ones([4, 4, 4], dtype=tf.int32)
updated = tf.tensor_scatter_nd_add(tensor, indices, updates)
with tf.Session() as se:
print(se.run(updated))https://stackoverflow.com/questions/59246382
复制相似问题