Last active
September 25, 2016 22:10
-
-
Save yanji84/d811ed5b5579b98f5b0a1c5dccc6ee17 to your computer and use it in GitHub Desktop.
Create Tensorflow variable with weight regularization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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