添加dropout(tf.nn.dropout)会导致Nan

时间:2017-07-05 09:20:01

标签: tensorflow nan conv-neural-network

作为张力流动的初学者和CNN我正致力于情感识别以理解这些。

以下代码在删除dropout图层时有效,但在添加时会产生Nan。我已经搜索过,并且遇到了诸如降低学习率等解决方案。没有一个对我有用。
网:

def cnn(self, data):
    conv = tf.nn.conv2d(data, self.w_1, [1, 1, 1, 1], padding='SAME')
    hidden = tf.nn.relu(conv + self.b_1)
    pool = tf.nn.max_pool(hidden, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
    norm = tf.nn.lrn(pool, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
    conv = tf.nn.conv2d(norm, self.w_2, [1, 1, 1, 1], padding='SAME')
    hidden = tf.nn.relu(conv + self.b_2)
    pool = tf.nn.max_pool(hidden, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
    norm = tf.nn.lrn(pool, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
    list_shape = norm.get_shape().as_list()
    reshape = tf.reshape(pool, [list_shape[0], list_shape[1] * list_shape[2] * list_shape[3]])
    hidden = tf.nn.relu(tf.matmul(reshape, self.w_3) + self.b_3)
    hidden = tf.nn.relu(tf.matmul(hidden, self.w_4) + self.b_4)
    dropout = tf.nn.dropout(hidden, self.dropout_prob)
    return tf.matmul(dropout, self.w_5) + self.b_5

模特:

self.tf_x = tf.placeholder(tf.float32, shape=(self.batch_size, self.image_size, self.image_size, 1))
self.tf_y = tf.placeholder(tf.float32, shape=(self.batch_size, self.num_labels))
self.dropout_prob = tf.placeholder(tf.float32)

self.w_1 = tf.Variable(tf.truncated_normal([5, 5, 1, 64], stddev=0.1))
self.b_1 = tf.Variable(tf.zeros([64]))
self.w_2 = tf.Variable(tf.truncated_normal([9, 9, 64, 128], stddev=0.04))
self.b_2 = tf.Variable(tf.constant(1.0, shape=[128]))
self.w_3 = tf.Variable(tf.truncated_normal([self.image_size//4 * self.image_size//4 * 128, 392], stddev=0.1))
self.b_3 = tf.Variable(tf.constant(1.0, shape=(392,)))
self.w_4 = tf.Variable(tf.truncated_normal([392, 196], stddev=0.1))
self.b_4 = tf.Variable(tf.constant(1.0, shape=(196,)))
self.w_5 = tf.Variable(tf.truncated_normal([196, self.num_labels], stddev=0.04))
self.b_5 = tf.Variable(tf.constant(1.0, shape=[self.num_labels]))

self.logits = self.cnn(self.tf_x)
self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.tf_y, logits=self.logits))

self.optimizer = tf.train.AdamOptimizer(1e-6).minimize(self.loss)
self.train_pred = tf.nn.softmax(self.logits)
tf.summary.histogram('weights_1', self.w_1)
tf.summary.histogram('weights_2', self.w_2)
tf.summary.histogram('weights_3', self.w_3)
tf.summary.histogram('weights_4', self.w_4)
tf.summary.scalar('loss', self.loss)
self.merged = tf.summary.merge_all()

错误:

Traceback (most recent call last):
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 75, in <module>
main()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 64, in main
emotion_cnn.train_test_validate()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\Emotion.py", line 127, in train_test_validate
_,summary, l1, predictions1 = self.session.run([self.optimizer, self.merged, self.loss, self.train_pred], feed_dict=feed_dict1)
 File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
run_metadata_ptr)
 File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 965, in _run
feed_dict_string, options, run_metadata)
 File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 1015, in _do_run
target_list, options, run_metadata)
 File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
 tensorflow.python.framework.errors_impl.InvalidArgumentError: Nan in summary histogram for: weights_1
     [[Node: weights_1 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](weights_1/tag, Variable/read/_81)]]

Caused by op 'weights_1', defined at:
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 75, in <module>
main()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 64, in main
emotion_cnn.train_test_validate()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\Emotion.py", line 104, in train_test_validate
self.model()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\Emotion.py", line 82, in model
tf.summary.histogram('weights_1', self.w_1)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\summary\summary.py", line 203, in histogram
tag=scope.rstrip('/'), values=values, name=scope)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py", line 139, in _histogram_summary
name=name)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
op_def=op_def)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\framework\ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\framework\ops.py", line 1226, in __init__
self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Nan in summary histogram for: weights_1
     [[Node: weights_1 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](weights_1/tag, Variable/read/_81)]]

E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\cuda\cuda_gpu_executor.cc:637] Deallocating stream with pending work

0 个答案:

没有答案
相关问题