Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d816971

Browse files
Evan Lezarmartinwicke
authored andcommitted
Use tf.softmax_cross_entropy_with_logits to calculate loss (tensorflow#181)
* Use the tensorflow cross entropy function to prevent nan losses. * Correct double softmax and use mean for loss.
1 parent 05630a7 commit d816971

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

transformer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A Spatial Transformer Network implemented in Tensorflow 0.7 and based on [2].
1717
</div>
1818

1919
```python
20-
transformer(U, theta, downsample_factor=1)
20+
transformer(U, theta, out_size)
2121
```
2222

2323
#### Parameters

transformer/cluttered_mnist.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,17 @@
119119
# %% And finally our softmax layer:
120120
W_fc2 = weight_variable([n_fc, 10])
121121
b_fc2 = bias_variable([10])
122-
y_pred = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
122+
y_logits = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
123123

124124
# %% Define loss/eval/training functions
125-
cross_entropy = -tf.reduce_sum(y * tf.log(y_pred))
125+
cross_entropy = tf.reduce_mean(
126+
tf.nn.softmax_cross_entropy_with_logits(y_logits, y))
126127
opt = tf.train.AdamOptimizer()
127128
optimizer = opt.minimize(cross_entropy)
128129
grads = opt.compute_gradients(cross_entropy, [b_fc_loc2])
129130

130131
# %% Monitor accuracy
131-
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))
132+
correct_prediction = tf.equal(tf.argmax(y_logits, 1), tf.argmax(y, 1))
132133
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
133134

134135
# %% We now create a new session to actually perform the initialization the

0 commit comments

Comments
 (0)