Tensorflow:成本不变

时间:2017-12-04 20:24:13

标签: tensorflow conv-neural-network

我在以下结构中构建CNN。输入为32 * 32 * 3图片和10个类别的单热标签。

inputs = tf.placeholder(tf.float32, [None, 32, 32, 3], name = "input")
targets = tf.placeholder(tf.float32, [None, 10], name = "targets")

layer_1_filter = tf.layers.conv2d(inputs = inputs,
                              filters = 64,
                              kernel_size = (2, 2),
                              strides = (1, 1),
                              padding = "same",
                              activation= tf.nn.relu)

layer_2_pooling = tf.layers.max_pooling2d(inputs = layer_1_filter,
                                      pool_size = (2 * 2),
                                      strides =1 * 1,
                                      padding = 'same')

layer_3_filter = tf.layers.conv2d(inputs = layer_2_pooling,
                              filters = 128,
                              kernel_size = (4, 4),
                              strides = (1, 1),
                              padding = "same",
                              activation= tf.nn.relu)

layer_4_pooling = tf.layers.max_pooling2d(inputs = layer_3_filter,
                                      pool_size = (2 * 2),
                                      strides = 1 * 1,
                                      padding = 'same')

sha = np.prod(layer_4_pooling.get_shape().as_list()[1:])
layer_5_reshape = tf.reshape(tensor= layer_4_pooling,
                         shape = [-1, sha])

layer_6_fc = tf.contrib.layers.fully_connected(inputs = layer_5_reshape,
                                           num_outputs = 1024)
layer_6_fc = tf.nn.dropout(layer_6_fc, keep_prob)  # Faster with drop out

layer_7_fc2 = tf.contrib.layers.fully_connected(inputs = layer_6_fc,
                                           num_outputs = 512)

layer_8_fc3 = tf.contrib.layers.fully_connected(inputs = layer_7_fc2,
                                            num_outputs = 10)

layer_9_logit = tf.identity(input = layer_8_fc3,
                        name = "logistic")

我将我的成本和优化程序定义为:

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=layer_9_logit, labels=targets))
optimizer = tf.train.AdamOptimizer().minimize(cost)

当我运行它时,成本总是在一定数量左右:2.30。我试了好几次,总是收敛它。

count = 0
with tf.Session() as sess:
print(info)
sess.run(tf.global_variables_initializer())

for batch_i in range(img_shape[0] // batch_size - 1):

    feature_batch = picture[batch_i * batch_size: (batch_i + 1) * batch_size]
    label_batch =     label[batch_i * batch_size: (batch_i + 1) * batch_size]

    train_loss, _ = sess.run([cost, optimizer],
                             feed_dict={inputs: feature_batch,
                                        targets: label_batch})
    if (count % 10 == 0):
        print(str(count) + ' |  Train Loss {:.8f}'.format(train_loss))

    count += 1

输出

0 |  Train Loss 37.51004410
10 |  Train Loss 2.30226469
20 |  Train Loss 2.30263376
30 |  Train Loss 2.30258608
40 |  Train Loss 2.30258536
50 |  Train Loss 2.30265045
60 |  Train Loss 2.35271192
70 |  Train Loss 2.30241871

我可以问为什么以及如何解决这个问题?非常感谢

0 个答案:

没有答案