【发布时间】:2017-04-04 10:08:14
【问题描述】:
我刚刚创建了自己的 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:这是第二个卷积层的一些过滤器的样子(不管后面多少个 epoch)
【问题讨论】:
-
您没有在会话中运行
init来初始化变量。您不会因此而收到错误消息吗? -
我正在运行初始化,我只是忘了复制和粘贴它。我编辑了我的问题。谢谢!
-
啊,我明白了...不过,您只运行了一次优化器。通常,您需要对整个数据集进行多次训练(“epochs”),然后才能真正看到实现目标的任何进展。还是有一些隐式循环?
-
它在循环中运行。我只是尽量去掉复制这里的代码
-
而且(我知道这是基本的,但我只是在考虑常见的嫌疑人)我假设您正在确保
init操作不在循环,对吧?当然还有会话创建本身。据我所知,该模型对我来说看起来不错。当我遇到这类错误时,我发现通常是一些更简单的控制流错误。
标签: python tensorflow conv-neural-network