如何扩展Tensorflow变量

时间:2016-01-21 01:22:07

标签: tensorflow

有没有办法让Tensorflow变量更大?就像,让我们说我想在训练过程中将神经元添加到神经网络的一层。我该怎么做呢? This question中的答案告诉我如何更改变量的形状,将其展开以适应另一行权重,但我不知道如何初始化这些新权重。

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

3 个答案:

答案 0 :(得分:12)

有多种方法可以实现这一目标。

1)该帖子中的第二个答案(https://stackoverflow.com/a/33662680/5548115)解释了如何通过使用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'属性。

答案 1 :(得分:4)

只需使用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])

答案 2 :(得分:1)

想出来。这是一个迂回的过程,但它是我能说出的唯一一个实际功能的过程。您需要首先解压缩变量,然后将新变量追加到最后,然后将它们打包在一起。

如果你沿着第一维扩展,那就相当短:只有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)

我不知道是否有一种更有效的方法可以做到这一点,但就这一点而言,这是有效的。如有必要,更改更多维度将需要更多层列表。