Выяснил это. Это своего рода обходной процесс, но это единственный, который я могу сказать, что на самом деле функционирует. Вам нужно сначала распаковать переменные, а затем добавить новую переменную до конца, а затем собрать обратно.
Если вы расширяетесь по первому измерению, оно довольно короткое: всего 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] ...) final = tf ,concat (0, [v1, v2]) Второй пример может быть выполнен: v1 = tf.variable (... [5, 3] ...) v2 = tf.variable (... [5 , 1] ...) final = tf.concat (1, [v1, v2]) Я думаю, это то, что предложил vrv. – zfc