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)>
不確定列表理解部分是否對大數據最有效
添加回答
舉報