यह पता लगाया। यह एक चौराहे की प्रक्रिया है, लेकिन यह केवल एक ही है जो मैं बता सकता हूं कि वास्तव में कार्य करता है। आपको पहले चर को अनपैक करने की आवश्यकता है, फिर नए वैरिएबल को अंत में जोड़ दें, फिर उन्हें एक साथ वापस पैक करें।
यदि आप पहले आयाम के साथ विस्तार कर रहे हैं, तो यह छोटा है: वास्तविक कोड की केवल 7 पंक्तियां।
#the first variable is 5x3
v1 = tf.Variable(tf.zeros([5, 3], dtype=tf.float32), "1")
#the second variable is 1x3
v2 = tf.Variable(tf.zeros([1, 3], dtype=tf.float32), "2")
#unpack the first variable into a list of size 3 tensors
#there should be 5 tensors in the list
change_shape = tf.unpack(v1)
#unpack the second variable into a list of size 3 tensors
#there should be 1 tensor in this list
change_shape_2 = tf.unpack(v2)
#for each tensor in the second list, append it to the first list
for i in range(len(change_shape_2)):
change_shape.append(change_shape_2[i])
#repack the list of tensors into a single tensor
#the shape of this resultant tensor should be [6, 3]
final = tf.pack(change_shape)
यदि आप दूसरे आयाम के साथ विस्तार करना चाहते हैं, तो यह कुछ हद तक अधिक हो जाता है।
#First variable, 5x3
v3 = tf.Variable(tf.zeros([5, 3], dtype=tf.float32))
#second variable, 5x1
v4 = tf.Variable(tf.zeros([5, 1], dtype=tf.float32))
#unpack tensors into lists of size 3 tensors and size 1 tensors, respectively
#both lists will hold 5 tensors
change = tf.unpack(v3)
change2 = tf.unpack(v4)
#for each tensor in the first list, unpack it into its own list
#this should make a 2d array of size 1 tensors, array will be 5x3
changestep2 = []
for i in range(len(change)):
changestep2.append(tf.unpack(change[i]))
#do the same thing for the second tensor
#2d array of size 1 tensors, array will be 5x1
change2step2 = []
for i in range(len(change2)):
change2step2.append(tf.unpack(change2[i]))
#for each tensor in the array, append it onto the corresponding array in the first list
for j in range(len(change2step2[i])):
changestep2[i].append(change2step2[i][j])
#pack the lists in the array back into tensors
changestep2[i] = tf.pack(changestep2[i])
#pack the list of tensors into a single tensor
#the shape of this resultant tensor should be [5, 4]
final2 = tf.pack(changestep2)
मुझे नहीं पता कि ऐसा करने का एक और अधिक प्रभावी तरीका है, लेकिन यह काम करता है, जहां तक यह जाता है। आगे के आयामों को बदलने के लिए आवश्यकतानुसार सूचियों की अधिक परतों की आवश्यकता होगी।
ध्यान दें कि tf.concat() टेंसर को जोड़ता है। उदाहरण के लिए, आपका उदाहरण 1 हो सकता है: v1 = tf.variable (... [5, 3] ...) v2 = tf.variable (... [1, 3] ...) अंतिम = टीएफ ।concat (0, [v1, v2]) आपका दूसरा उदाहरण किया जा सकता है: v1 = tf.variable (... [5, 3] ...) v2 = tf.variable (... [5 , 1] ...) अंतिम = tf.concat (1, [v1, v2]) मुझे लगता है कि vrv क्या सुझाव दिया गया है। – zfc