繁体   English   中英

Tensorflow:如何使用特定索引连接张量

[英]Tensorflow: how to concat tensor with specific index

我有这样的张量:

tensor_a = [[[[255,255,255]]], [[[100,100,100]]]]
tensor_b = [[[[0.1,0.2]]], [[[0.3,0.4]]]]
tensor_c = [[[[1]]], [[[2]]]]

今天,我尝试将这些张量连接到tensor_d上,例如:

tensor_d = [[[[255,255,255,0.1,1]]], [[[100,100,100, 0.3, 2]]]]

但是我不知道如何吸引他们。

我试图使用for循环将张量追加到列表

但这太慢了(以tensor_a:(10,64,64,3)的形式)

你可以试试

tensor_a = [[[[255,255,255]]], [[[100,100,100]]]]
tensor_b = [[[[0.1,0.2]]], [[[0.3,0.4]]]] 
tensor_c = [[[[1]]], [[[2]]]]

tensor_d = [[[a[0][0] + [b[0][0][0]] + [c[0][0][0]]]] for a, b, c in zip( tensor_a, tensor_b, tensor_c)]
print(tensor_d) 

# [[[[255, 255, 255, 0.1, 1]]], [[[100, 100, 100, 0.3, 2]]]]

您可以使用张量操纵,例如tf.splittf.concat

import tensorflow as tf

# tensors
tensor_a = [[[[255, 255, 255]]], [[[100, 100, 100]]]]
tensor_b = [[[[0.1, 0.2]]], [[[0.3, 0.4]]]]
tensor_c = [[[[1]]], [[[2]]]]

# casting becuase date type should match in tf.concat
tensor_a = tf.cast(tensor_a, dtype=tf.float32)
tensor_c = tf.cast(tensor_c, dtype=tf.float32)

# split elements into one and the other at the last axis
b, _ = tf.split(value=tensor_b, num_or_size_splits=[1, -1], axis=-1)
c, _ = tf.split(value=tensor_c, num_or_size_splits=[1, -1], axis=-1)

# concatenate tensors at the last axis
tensors = tf.concat(values=[tensor_a, b, c], axis=-1)

sess = tf.Session()
result = sess.run(tensors)

print(result)
[[[[2.55e+02 2.55e+02 2.55e+02 1.00e-01 1.00e+00]]]


 [[[1.00e+02 1.00e+02 1.00e+02 3.00e-01 2.00e+00]]]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM