Skip to content

Instantly share code, notes, and snippets.

@yanji84
Last active September 25, 2016 22:10
Show Gist options
  • Select an option

  • Save yanji84/d811ed5b5579b98f5b0a1c5dccc6ee17 to your computer and use it in GitHub Desktop.

Select an option

Save yanji84/d811ed5b5579b98f5b0a1c5dccc6ee17 to your computer and use it in GitHub Desktop.
Create Tensorflow variable with weight regularization
def variable_on_cpu(self, name, shape, initializer):
with tf.device('/cpu:0'):
var = tf.get_variable(name, shape, initializer=initializer)
return var
def variable_with_weight_decay(self, name, shape, stddev, wd):
var = self.variable_on_cpu(name, shape,
tf.truncated_normal_initializer(stddev=stddev))
if wd:
weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment