如何扩展 Tensorflow 变量

How to expand a Tensorflow Variable

有什么方法可以让 Tensorflow 变量变大吗?比如说,我想在训练过程中向神经网络的一层添加一个神经元。我该怎么做呢? 中的答案告诉我如何更改变量的形状,将其扩展以适应另一行权重,但我不知道如何初始化这些新权重。

我认为另一种解决方法可能涉及组合变量,例如首先在第二个变量中初始化权重,然后将其添加为第一个变量的新行或新列,但我找不到任何让我这样做的东西。

您可以通过多种方式完成此操作。

1) post () 中的第二个答案解释了如何通过使用 validate_shape=False 调用 'assign' 来更改变量的形状。例如,您可以做类似

# Assume var is [m, n] 
# Add the new 'data' of shape [1, n] with new values
new_neuron = tf.constant(...)  

# If concatenating to add a row, concat on the first dimension.
# If new_neuron was [m, 1], you would concat on the second dimension.
new_variable_data = tf.concat(0, [var, new_neuron])  # [m+1, n]

resize_var = tf.assign(var, new_variable_data, validate_shape=False)

那么当你运行resize_var时,'var'指向的数据现在会有更新的数据。

2) 您还可以创建一个较大的初始变量,并随着训练的进行在变量的不同区域调用 tf.slice,因为您可以动态更改 'begin' 和 'size'切片的属性。

想通了。这是一个迂回的过程,但这是我能说的唯一一个真正起作用的过程。您需要先解压变量,然后将新变量附加到末尾,然后将它们重新组合在一起。

如果沿着第一个维度展开,它会很短:只有 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扩展一个Tensorflow变量,你可以看到api_docs 了解详情。

    v1 = tf.Variable(tf.zeros([5,3]),dtype=tf.float32)
    v2 = tf.Variable(tf.zeros([1,3]),dtype=tf.float32)
    v3 = tf.concat(0,[v1, v2])