【发布时间】:2017-08-30 14:11:39
【问题描述】:
我有一个神经网络,我在其中构建了自己的层,它给出了形状为 A = [10, 5] 的结果。
我想将结果提供给另一个层,该层接受形状为B = [10, 9, 5] 的输入。
输入B是基于之前的结果A,例如从A中选择9个不同的行10次,形成一个形状为[10, 9, 5]的新张量。
有没有办法做到这一点?
【问题讨论】:
标签: tensorflow reshape tensor
我有一个神经网络,我在其中构建了自己的层,它给出了形状为 A = [10, 5] 的结果。
我想将结果提供给另一个层,该层接受形状为B = [10, 9, 5] 的输入。
输入B是基于之前的结果A,例如从A中选择9个不同的行10次,形成一个形状为[10, 9, 5]的新张量。
有没有办法做到这一点?
【问题讨论】:
标签: tensorflow reshape tensor
for 循环会做:
a = tf.constant([[1, 2, 7], [3, 4, 8], [5, 6, 9]])
tensor_list = []
pick_times = 3
for i in range(pick_times):
pick_rows = [j for j in range(pick_times) if i != j]
tensor_list.append(tf.gather(a, pick_rows))
concated_tensor = tf.concat(tensor_list, 0)
result = tf.reshape(concated_tensor, [3, 2, 3])
【讨论】:
将张量 A(层的输出)转换为 numpy 数组:
a=sess.run(A.eval())
为了示例,我将使用:
a=np.random.uniform(0,5,[10])
然后:
#choose wich element will be left out
out = np.random.randint(5, size=[10])
#array of different output layers without one random element
b=[]
for i in range(10):
b.append(np.delete(a,out[i]))
#Stack them all together
B = tf.stack(b[:])
【讨论】: