2016-04-12 10 views

答えて

16

体重減少を加えたいすべての変数をコレクション名 'variables'に追加して、コレクション全体のL2ノルム減量を計算することができます。

# Create your variables 
    weights = tf.get_variable('weights', collections=['variables']) 

    with tf.variable_scope('weights_norm') as scope: 
    weights_norm = tf.reduce_sum(
     input_tensor = WEIGHT_DECAY_FACTOR*tf.pack(
      [tf.nn.l2_loss(i) for i in tf.get_collection('weights')] 
    ), 
     name='weights_norm' 
) 

    # Add the weight decay loss to another collection called losses 
    tf.add_to_collection('losses', weights_norm) 

    # Add the other loss components to the collection losses  
    # ... 

    # To calculate your total loss 
    tf.add_n(tf.get_collection('losses'), name='total_loss') 
+0

はalexnetの重量崩壊と同等のこの量の崩壊ですか? – LKM

+2

例の行6のテンソルフローv1.0 'tf.pack'で始まる注意は、' tf.stack'に名前が変更されました。 – Chrigi

+0

'tf.get_collection( 'weights')の代わりに' tf.get_collection( 'variables') 'でなければなりません。 –

3
get_variable(
name, 
shape=None, 
dtype=None, 
initializer=None, 
regularizer=None, 
trainable=True, 
collections=None, 
caching_device=None, 
partitioner=None, 
validate_shape=True, 
use_resource=None, 
custom_getter=None) 

これはtensorflow機能get_variableの使用です。あなたは簡単に体重減衰を行う正規化者を指定することができます。続き

は一例です:

weight_decay = tf.constant(0.0005, dtype=tf.float32) # your weight decay rate, must be a scalar tensor. 
W = tf.get_variable(name='weight', shape=[4, 4, 256, 512], regularizer=tf.contrib.layers.l2_regularizer(weight_decay)) 
関連する問題