2016-01-21 14 views
7

Tensorflow変数を大きくする方法はありますか?同様に、トレーニングの途中でニューラルネットワークのレイヤーにニューロンを追加したいとしましょう。それをどうやってやりますか? This questionの回答は、変数の形状を変更して、別の行の行に合わせて変数を拡張する方法を教えてくれましたが、これらの新しい重みを初期化する方法はわかりません。Tensorflow変数を拡張する方法

私は別の方法として、変数を結合することがあります。最初の変数を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) 

2番目のディメンションに沿って拡大したい場合は、少し長くなります。

#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) 

限りそれが行くように、これを行うためのより効率的な方法があるのか​​どうかは知りませんが、これは動作します。ディメンションをさらに変更するには、必要に応じてリストのレイヤーを増やす必要があります。

+3

tf.concat()はテンソルを連結することに注意してください。たとえば、あなたの例1は次のようになります: v1 = tf.variable(... [5,3] ...) v2 = tf.variable(... [1、3] ...) final = tf 。 v1 = tf.variable(... [5、3] ...) v2 = tf.variable(... [5] 、1] ...) final = tf.concat(1、[v1、v2]) 私はこれがvrvの提案だと思います。 – zfc

9

これを達成するにはさまざまな方法があります。

1)その投稿(https://stackoverflow.com/a/33662680/5548115)の2番目の答えは、validate_shape = Falseを指定して 'assign'を呼び出すことによって変数の形を変更する方法を説明しています。たとえば、あなたがresize_varを実行したときにその後、データは「VAR」で指さ

# 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) 

ような何かを行うことができ、今更新されたデータを持っています。

2)大きな初期変数を作成し、sliceの 'begin'と 'size'属性を動的に変更することができるので、トレーニングの進行とともに変数の異なる領域でtf.sliceを呼び出すこともできます。

+0

形状[m、1]の新しい変数を追加した場合、最終形状は[m、n + 1]になりますか? – Beez

+0

3次元の変数を作成するだけではパックできませんか?これがAPIの説明です。パックを実行するとエラーが発生します。サイズを同じサイズにしないと、サイズが互換性がないというエラーが表示されます。その場合、サイズ2の3番目のディメンションが追加されます。 – Beez

+0

パック。 (または希望の図形を作成したい次元に連結してください) 私はこれを反映する答えを編集しました。 。 – vrv

4

Tensorflow変数を展開するためにtf.concatを使用するだけで、詳細については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]) 
関連する問題