gpt4 book ai didi

python - 使用 cifar 100 的图像分类器,训练精度没有增加

转载 作者:行者123 更新时间:2023-11-30 08:48:28 29 4
gpt4 key购买 nike

我试图在 tensorflow 中使用 cifar100 数据集训练图像分类器模型,但准确率没有增加超过 1.2%。我用谷歌搜索了这个问题并找到了几种解决方案,但我的模型仍然表现不佳。

我实现了一些步骤,例如:

  1. 增加 CNN 层和池化以及 dropout 和标准化
  2. 更改编号致密层
  3. 更改批量大小和纪元
  4. 更改优化器

我注意到的一个常见现象是,epoch=10、batch size=256 & epoch=500、batch size=512 时,训练损失和准确度以同样的方式变化。

为了防止过度拟合,我还尝试了 dropout 正则化,这显示了一些变化(训练加速度在 0.5 到 1.2% 之间变化),当我增加 epoch 时参数相同,没有任何变化(训练和模型加速度)。

我想知道这是数据集的问题还是模型定义的问题。

分类器模型:

def classifierModel(inp):
layer1=tf.nn.relu(tf.nn.conv2d(inp, filter=tf.Variable(tf.truncated_normal([5,5,3,16])),
strides=[1,2,2,1], padding='SAME'))
layer1=tf.nn.bias_add(layer1, tf.Variable(tf.truncated_normal([16])))
layer1=tf.nn.relu(tf.nn.max_pool(layer1, ksize=[1,1,1,1], strides=[1,2,2,1], padding='SAME'))

layer2=tf.nn.relu(tf.nn.conv2d(layer1, filter=tf.Variable(tf.truncated_normal([5,5,16,32])),
strides=[1,2,2,1], padding='SAME'))
layer2=tf.nn.bias_add(layer2, tf.Variable(tf.truncated_normal([32])))
layer2=tf.nn.relu(tf.nn.max_pool(layer2, ksize=[1,1,1,1], strides=[1,2,2,1], padding='SAME'))

layer3=tf.nn.relu(tf.nn.conv2d(layer2, filter=tf.Variable(tf.truncated_normal([5,5,32, 64])),
strides=[1,2,2,1], padding='SAME'))
layer3=tf.nn.bias_add(layer3, tf.Variable(tf.truncated_normal([64])))

layer3=tf.nn.relu(tf.nn.max_pool(layer3, ksize=[1,1,1,1], strides=[1,2,2,1], padding='SAME'))
layer3=tf.nn.dropout(layer3, keep_prob=0.7)
print(layer3.shape)


fclayer1=tf.reshape(layer3, [-1, weights['fc1'].get_shape().as_list()[0]])
fclayer1=tf.add(tf.matmul(fclayer1, weights['fc1']), biases['fc1'])
fclayer1= tf.nn.dropout(fclayer1, keep_prob=0.5)
fclayer2=tf.add(tf.matmul(fclayer1, weights['fc2']), biases['fc2'])
fclayer2=tf.nn.dropout(fclayer2, keep_prob=0.5)
fclayer3=tf.add(tf.matmul(fclayer2, weights['fc3']), biases['fc3'])
fclayer3=tf.nn.dropout(fclayer3, keep_prob=0.7)
outLayer=tf.nn.softmax(tf.add(tf.matmul(fclayer3, weights['out']), biases['out']))
return outLayer

优化器、成本、准确性:

cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=model, labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred=tf.equal(tf.argmax(model, 1), tf.argmax(y, 1))
accuracy=tf.reduce_mean(tf.cast(correct_pred, tf.float32))

培训:

with tf.Session() as sess:
sess.run(init)
for i in range(epochs):
#shuffle(idx)
#train_features=train_features[idx, :, :, :]
#train_labels=train_labels[idx, ]
for batch_features, batch_labels in get_batches(batch_size, train_features, train_labels):
sess.run(optimizer, feed_dict={x:batch_features, y:batch_labels})
if (i%display_step==0):

epoch_stats(sess, i, batch_features, batch_labels)

model_acc=sess.run(accuracy, feed_dict={x:test_features, y:test_labels})
saver.save(sess, save_file)

writer.add_graph(sess.graph)

结果:

  1. 纪元:0 - 成本:4.62 - acc:0.01
  2. 纪元:1 - 成本:4.62 - acc:0.01
  3. 纪元:2 - 成本:4.62 - acc:0.008
  4. 纪元:3 - 成本:4.61 - acc:0.012
  5. 纪元:4 - 成本:4.61 - acc:0.005
  6. 纪元:5 - 成本:4.62 - acc:0.006
  7. 纪元:6 - 成本:4.62 - acc:0.016
  8. 纪元:7 - 成本:4.62 - acc:0.012
  9. 纪元:8 - 成本:4.61 - acc:0.014
  10. 纪元:9 - 成本:4.62 - acc:0.009
  11. 模型准确度 - 0.010499999858438969

最佳答案

您传递给 softmax_cross_entropy_with_logits_v2 的第一个参数不正确。您必须传递“先前”值才能应用 softmax。那是因为 softmax_cross_entropy_with_logits_v2 实际上是 cross_entropy (softmax (x))。理由是导数可以简化。

在模型中,您应该执行以下操作:

def classifierModel(inp):
layer1=tf.nn.relu(tf.nn.conv2d(inp, filter=tf.Variable(tf.truncated_normal([5,5,3,16])),
strides=[1,2,2,1], padding='SAME'))
layer1=tf.nn.bias_add(layer1, tf.Variable(tf.truncated_normal([16])))
layer1=tf.nn.relu(tf.nn.max_pool(layer1, ksize=[1,1,1,1], strides=[1,2,2,1], padding='SAME'))

layer2=tf.nn.relu(tf.nn.conv2d(layer1, filter=tf.Variable(tf.truncated_normal([5,5,16,32])),
strides=[1,2,2,1], padding='SAME'))
layer2=tf.nn.bias_add(layer2, tf.Variable(tf.truncated_normal([32])))
layer2=tf.nn.relu(tf.nn.max_pool(layer2, ksize=[1,1,1,1], strides=[1,2,2,1], padding='SAME'))

layer3=tf.nn.relu(tf.nn.conv2d(layer2, filter=tf.Variable(tf.truncated_normal([5,5,32, 64])),
strides=[1,2,2,1], padding='SAME'))
layer3=tf.nn.bias_add(layer3, tf.Variable(tf.truncated_normal([64])))

layer3=tf.nn.relu(tf.nn.max_pool(layer3, ksize=[1,1,1,1], strides=[1,2,2,1], padding='SAME'))
layer3=tf.nn.dropout(layer3, keep_prob=0.7)
print(layer3.shape)


fclayer1=tf.reshape(layer3, [-1, weights['fc1'].get_shape().as_list()[0]])
fclayer1=tf.add(tf.matmul(fclayer1, weights['fc1']), biases['fc1'])
fclayer1= tf.nn.dropout(fclayer1, keep_prob=0.5)
fclayer2=tf.add(tf.matmul(fclayer1, weights['fc2']), biases['fc2'])
fclayer2=tf.nn.dropout(fclayer2, keep_prob=0.5)
fclayer3=tf.add(tf.matmul(fclayer2, weights['fc3']), biases['fc3'])
fclayer3=tf.nn.dropout(fclayer3, keep_prob=0.7)
logits = tf.add(tf.matmul(fclayer3, weights['out']), biases['out'])
outLayer=tf.nn.softmax(logits)
return outLayer, logits

在损失函数中:

model, logits = classifierModel(inp)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred=tf.equal(tf.argmax(model, 1), tf.argmax(y, 1))
accuracy=tf.reduce_mean(tf.cast(correct_pred, tf.float32))

关于python - 使用 cifar 100 的图像分类器,训练精度没有增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54259807/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com