python - cost function outputs 'nan' in tensorflow -
while studying tensorflow, faced problem.
cost function output 'nan'.
and, if find other wrong in source code let me know links it.
i trying send cost function value trained model, not working.
tf.reset_default_graph() tf.set_random_seed(777) x = tf.placeholder(tf.float32, [none, 20, 20, 3]) y = tf.placeholder(tf.float32, [none, 1]) tf.variable_scope('conv1') scope: w1 = tf.variable(tf.random_normal([4, 4, 3, 32], stddev=0.01), name='weight1') l1 = tf.nn.conv2d(x, w1, strides=[1, 1, 1, 1], padding='same') l1 = tf.nn.relu(l1) l1 = tf.nn.max_pool(l1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='same') l1 = tf.reshape(l1, [-1, 10 * 10 * 32]) w1_hist = tf.summary.histogram('conv_weight1', w1) l1_hist = tf.summary.histogram('conv_layer1', l1) tf.name_scope('fully_connected_layer1') scope: w2 = tf.get_variable('w2', shape=[10 * 10 * 32, 1], initializer=tf.contrib.layers.xavier_initializer()) b = tf.variable(tf.random_normal([1])) hypothesis = tf.matmul(l1, w2) + b w2_hist = tf.summary.histogram('fully_connected_weight1', w2) b_hist = tf.summary.histogram('fully_connected_bias', b) hypothesis_hist = tf.summary.histogram('hypothesis', hypothesis) tf.name_scope('cost') scope: cost = -tf.reduce_mean(y * tf.log(hypothesis) + (1 - y) * tf.log(1 - hypothesis)) cost_summary = tf.summary.scalar('cost', cost) tf.name_scope('train_optimizer') scope: optimizer = tf.train.adamoptimizer(learning_rate=0.0001).minimize(cost) predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, y), dtype=tf.float32)) accuracy_summary = tf.summary.scalar('accuracy', accuracy) train_data_batch, train_labels_batch = tf.train.batch([train_data, train_labels], enqueue_many=true , batch_size=100, allow_smaller_final_batch=true) tf.session() sess: # tensorboard --logdir=./logs/planesnet2_log merged_summary = tf.summary.merge_all() writer = tf.summary.filewriter('./logs/planesnet2_log') writer.add_graph(sess.graph) sess.run(tf.global_variables_initializer()) coord = tf.train.coordinator() threads = tf.train.start_queue_runners(coord=coord) total_cost = 0 step in range(20): x_batch, y_batch = sess.run([train_data_batch, train_labels_batch]) feed_dict = {x: x_batch, y: y_batch} _, cost_val = sess.run([optimizer, cost], feed_dict = feed_dict) total_cost += cost_val print('total_cost: ', total_cost, 'cost_val: ', cost_val) coord.request_stop() coord.join(threads)
you use cross entropy loss without sigmoid activation function hypothesis
, values not bounded in ]0,1]. log function not defined negative values , somes. add sigmoid , epsilon factor avoid negative or 0 values , should fine.
Comments
Post a Comment