0

Loss turns to be NAN at the first round step in my tensorflow CNN.

1. Network :

3 hidden layers (2 convolutional layers +1 hidden fullconnect layer) + readout layer.

2. The 3 hidden layers :

a) Weights :
W = tf.Variable(tf.truncated_normal(wt,stddev=0.1,name='wights' ))

b) Bias :
b = tf.Variable( tf.fill([W.get_shape().as_list()[-1] ],0.9),name = 'biases' )

c) Activition:
ReLu

d) Dropout:
0.6

**loss turns to be nan even if dropout is 0.0

3. Readout layyer:
softmax

4: loss function:
tf.reduce_mean(-tf.reduce_sum(_lables * tf.log(_logist), reduction_indices=[1]))

5.optimizer:
tf.train.AdamOptimizer

learning_rate = 0.0005
**loss truns to be nan even if learning_rate = 0

wazz
  • 4,953
  • 5
  • 20
  • 34
u4lr
  • 41
  • 5

2 Answers2

1

Since we don't have the entire source code, it's hard to see the problem. However, you may try to use 'tf.nn.softmax_cross_entropy_with_logits' in your cost function. For example:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(predictions, labels))

You can find an entire code example using 'tf.nn.softmax_cross_entropy_with_logits' at https://github.com/nlintz/TensorFlow-Tutorials.

Sung Kim
  • 8,417
  • 9
  • 34
  • 42
  • thanks
    , it fixed with two steps:
    1. use xavier_initializer for weigths.
    2. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(predictions, labels)
    – u4lr Apr 20 '16 at 08:17
  • @user6223071 awesome! Could you also share the xavier_initializer part for others if you don't mind? – Sung Kim Apr 20 '16 at 08:21
  • 1
    xavier was implemented in tensorflow api v8.0. here is [https://www.tensorflow.org/versions/r0.8/api_docs/python/contrib.layers.html#xavier_initializer] – u4lr Apr 20 '16 at 08:24
1

So far I've hit two cases where nan can result:

  • The numbers get too large (perhaps you're performing square on a number, and the result is too large)
  • Or there exists an invalid input (some functions like sqrt and log don't take negative inputs, so they would return nan)
Jeffrey Sun
  • 7,789
  • 1
  • 24
  • 17