2 回答
TA贡献1802条经验 获得超5个赞
tf.concat
与tf.repeat
和一起使用tf.tile
import tensorflow as tf
import numpy as np
# Input
A = tf.convert_to_tensor(np.array([['a', 'b'], ['c', 'd']]))
B = tf.convert_to_tensor(np.array([['e', 'f'], ['g', 'h']]))
# Repeat, tile and concat
C = tf.concat([tf.repeat(A, repeats=tf.shape(A)[-1], axis=0),
tf.tile(B, multiples=[tf.shape(A)[0], 1])],
axis=-1)
# Reshape to requested shape
C = tf.reshape(C, [tf.shape(A)[0], tf.shape(A)[0], -1])
print(C)
>>> <tf.Tensor: shape=(2, 2, 4), dtype=string, numpy=
>>> array([[[b'a', b'b', b'e', b'f'],
>>> [b'a', b'b', b'g', b'h']],
>>> [[b'c', b'd', b'e', b'f'],
>>> [b'c', b'd', b'g', b'h']]], dtype=object)>
TA贡献1789条经验 获得超8个赞
试试这样:
A = np.array([['a', 'b'], ['c', 'd']])
B = np.array([['e', 'f'], ['g', 'h']])
C = np.array([np.concatenate((a, b), axis=0) for a in A for b in B])
您可以像这样轻松地将其转换为张量流
data =tf.convert_to_tensor(C, dtype=tf.string)
输出:
<tf.Tensor: shape=(4, 4), dtype=string, numpy=
array([[b'a', b'b', b'e', b'f'],
[b'a', b'b', b'g', b'h'],
[b'c', b'd', b'e', b'f'],
[b'c', b'd', b'g', b'h']], dtype=object)>
不确定列表理解部分是否对大数据最有效
添加回答
举报