簡體   English   中英

Tensorflow:CNN完全沒有學習

[英]Tensorflow: CNN is not learning at all

我剛創建了自己的CNN,它從磁盤讀取數據並嘗試學習。 但是重量似乎根本沒有學習,它們都是隨機的。

偏見只會改變一點。 我已經嘗試使用灰度圖像,但沒有成功。 我也厭倦了將我的數據集減少到只有2個類,在我看來應該有用。 但測得的准確度低於50%(也許我正在計算准確度假)

這是一些代碼:

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, classes])
weights = {
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
    'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
    'wd1': tf.Variable(tf.random_normal([12*12*64, 1024])),
    'out': tf.Variable(tf.random_normal([1024, classes]))
}
biases = {
    'bc1': tf.Variable(tf.random_normal([32])),
    'bc2': tf.Variable(tf.random_normal([64])),
    'bd1': tf.Variable(tf.random_normal([1024])),
    'out': tf.Variable(tf.random_normal([classes]))
}

pred = model.conv_net(x, weights, biases, keep_prob, imgSize)

with tf.name_scope("cost"):
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
with tf.name_scope("optimizer"):
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
with tf.name_scope("accuracy"):
    correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    while(step < epochs):
         batch_x, batch_y = batch_creator(batch_size, train_x.shape[0], 'train')
         sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
          if(step % display_step == 0):
                 batchv_x, batchv_y = batch_creator(batch_size, val_x.shape[0], 'val')
                 summary, loss, acc = sess.run([merged, cost,  accuracy], feed_dict={x: batchv_x, y: batchv_y})
                 train_writer.add_summary(summary, step)

我查看了創建的批次,看起來很好。 batch_x是一個長度為2304個浮點值的數組,表示48x48圖像batch_y是一個帶有one_hot標簽的數組:[0 0 ... 0 1 0 ... 0 0]

這是我的模特:

def conv2d(x, W, b, strides=1):
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)

def maxpool2d(x, k=2):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
                          padding='SAME')

def conv_net(x, weights, biases, dropout, imgSize):
    with tf.name_scope("Reshaping_data") as scope:
        x = tf.reshape(x, shape=[-1, imgSize, imgSize, 1], name="inp") #(?, 48, 48, 1)

    with tf.name_scope("Conv1") as scope:
        conv1 = conv2d(x, weights['wc1'], biases['bc1'])
        conv1 = maxpool2d(conv1, k=2) #(?, 24, 24, 32)

    with tf.name_scope("Conv2") as scope:
        conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
        conv2 = maxpool2d(conv2, k=2) #(?, 12, 12, 64)

    with tf.name_scope("FC1") as scope:
        fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) #(?, 9216)
        fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) #(?, 1024)
        fc1 = tf.nn.relu(fc1) #(?, 1024)

    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'], name="out") #(?, 43)
    return out

謝謝您的幫助!

PS:這是第二個卷積層的一些過濾器看起來像(后面有多少個時代)

conv2filter

我用cifar-10數據庫嘗試過你的網絡。

我擔心問題是由巨大的參數引起的,特別是在fc1層。 您可以嘗試在卷積層中減少內核數(例如除以2),並在池中使用4或6作為k以減少空間維度。 然后你可以減少很多fc1層的權重。

當參數很多時,請注意重量初始化。 使用tf.contrib.layers.xavier_initializer()tf.random_normal_initializer(stddev=np.sqrt(2.0 / n))可以更好地啟動。

在減少參數並具有更好的權重初始化之后,cifar-10上的損失開始收斂。 您可以使用自己的數據庫進行嘗試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM