【发布时间】:2019-12-14 03:08:50
【问题描述】:
a = tf.constant([[1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1]])
mul = tf.constant([1, 3, 2])
result = []
for i in range(3):
print(a[i], mul[i])
result.append(tf.tile(a[i], [mul[i]]))
with tf.Session() as sess:
print([r.eval() for r in result])
正确结果:
[数组([1, 2, 3, 1]), 数组([4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1]), 数组([7 , 8, 9, 1, 7, 8, 9, 1])]
while run below with tf.map_fn, it will fail
c = tf.constant([[1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1]])
x = tf.constant([1, 3, 1])
def cc(b, t):
print(b.shape, t)
print(type(b), type(t))
return tf.tile(b, [t])
d = tf.map_fn(fn=lambda t: cc(t[0], t[1]), elems=(c, x))
这是错误跟踪:
Traceback(最近一次调用最后一次): 文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\util\nest.py”,第 297 行,位于 assert_same_structure expand_composites) ValueError: 这两个结构没有相同的嵌套结构。
第一个结构:
type=tuple str=(tf.int32, tf.int32)
第二个结构:
type=Tensor str=Tensor("map/while/Tile:0", shape=(?,), dtype=int32)
更具体地说:子结构"type=tuple str=(tf.int32, tf.int32)" 是一个序列,而子结构"type=Tensor str=Tensor("map/while/Tile:0", shape=(?,), dtype=int32)" 不是
【问题讨论】:
标签: python tensorflow