gpt4 book ai didi

python - tensorflow 模型精度没有增加

转载 作者:行者123 更新时间:2023-12-01 09:08:47 24 4
gpt4 key购买 nike

我目前正在 Udacity 上学习深度学习类(class),并正在尝试完成第四项作业,您应该创建自己的模型,并了解在 noMINST 数据集上可以实现的最佳准确度。

我尝试实现VGG 16模型,但遇到了一些问题,最初,损失直接变为nan,因此我将最后一个激活函数从relu更改为sigmoid,但现在精度不高改进并停留在 0-6% 左右,所以我猜测我的实现是错误的,但我似乎看不到错误,我将非常感谢任何帮助或建议!

贝娄是我的完整代码,除了在数据集中读取之外,因为该代码是由 提供的,所以我猜它是正确的。

pickle_file = 'notMNIST.pickle'

with open(pickle_file, 'rb') as f:
save = pickle.load(f)
train_dataset = save['train_dataset']
train_labels = save['train_labels']
valid_dataset = save['valid_dataset']
valid_labels = save['valid_labels']
test_dataset = save['test_dataset']
test_labels = save['test_labels']
del save # hint to help gc free up memory
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)

image_size = 28
num_labels = 10
num_channels = 1 # grayscale

import numpy as np


def reformat(dataset, labels):
dataset = dataset.reshape(
(-1, image_size, image_size, num_channels)).astype(np.float32)
labels = (np.arange(num_labels) == labels[:, None]).astype(np.float32)
return dataset, labels


train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)


def accuracy(predictions, labels):
return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
/ predictions.shape[0])


batch_size = 16
patch_size = 5
depth = 16
num_hidden = 64

graph = tf.Graph()

with graph.as_default():
# Input data.
tf_train_dataset = tf.placeholder(
tf.float32, shape=(batch_size, image_size, image_size, num_channels))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)

# Variables.
l1_w = tf.Variable(tf.truncated_normal([3, 3, 1, 64], stddev=0.1))
l1_b = tf.Variable(tf.zeros([64]))
l2_w = tf.Variable(tf.truncated_normal([3, 3, 64, 64], stddev=0.1))
l2_b = tf.Variable(tf.zeros([64]))

l3_w = tf.Variable(tf.truncated_normal([3, 3, 64, 128], stddev=0.1))
l3_b = tf.Variable(tf.zeros([128]))
l4_w = tf.Variable(tf.truncated_normal([3, 3, 128, 128], stddev=0.1))
l4_b = tf.Variable(tf.zeros([128]))

l5_w = tf.Variable(tf.truncated_normal([3, 3, 128, 256], stddev=0.1))
l5_b = tf.Variable(tf.zeros([256]))
l6_w = tf.Variable(tf.truncated_normal([3, 3, 256, 256], stddev=0.1))
l6_b = tf.Variable(tf.zeros([256]))
l7_w = tf.Variable(tf.truncated_normal([3, 3, 256, 256], stddev=0.1))
l7_b = tf.Variable(tf.zeros([256]))

l8_w = tf.Variable(tf.truncated_normal([3, 3, 256, 512], stddev=0.1))
l8_b = tf.Variable(tf.zeros([512]))
l9_w = tf.Variable(tf.truncated_normal([3, 3, 512, 512], stddev=0.1))
l9_b = tf.Variable(tf.zeros([512]))
l10_w = tf.Variable(tf.truncated_normal([3, 3, 512, 512], stddev=0.1))
l10_b = tf.Variable(tf.zeros([512]))

l11_w = tf.Variable(tf.truncated_normal([3, 3, 512, 512], stddev=0.1))
l11_b = tf.Variable(tf.zeros([512]))
l12_w = tf.Variable(tf.truncated_normal([3, 3, 512, 512], stddev=0.1))
l12_b = tf.Variable(tf.zeros([512]))
l13_w = tf.Variable(tf.truncated_normal([3, 3, 512, 512], stddev=0.1))
l13_b = tf.Variable(tf.zeros([512]))

l14_w = tf.Variable(tf.truncated_normal([512, num_hidden], stddev=0.1))
l14_b = tf.Variable(tf.constant(1.0, shape=[num_hidden]))

l15_w = tf.Variable(tf.truncated_normal([num_hidden, num_labels], stddev=0.1))
l15_b = tf.Variable(tf.constant(1.0, shape=[num_labels]))


# Model.
def model(data):
conv_1 = tf.nn.relu(tf.nn.conv2d(data, l1_w, [1, 1, 1, 1], padding='SAME') + l1_b)
conv_1 = tf.nn.relu(tf.nn.conv2d(conv_1, l2_w, [1, 1, 1, 1], padding='SAME') + l2_b)
max_pool_1 = tf.nn.max_pool(conv_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

conv_2 = tf.nn.relu(tf.nn.conv2d(max_pool_1, l3_w, [1, 1, 1, 1], padding='SAME') + l3_b)
conv_2 = tf.nn.relu(tf.nn.conv2d(conv_2, l4_w, [1, 1, 1, 1], padding='SAME') + l4_b)
max_pool_2 = tf.nn.max_pool(conv_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

conv_3 = tf.nn.relu(tf.nn.conv2d(max_pool_2, l5_w, [1, 1, 1, 1], padding='SAME') + l5_b)
conv_3 = tf.nn.relu(tf.nn.conv2d(conv_3, l6_w, [1, 1, 1, 1], padding='SAME') + l6_b)
conv_3 = tf.nn.relu(tf.nn.conv2d(conv_3, l7_w, [1, 1, 1, 1], padding='SAME') + l7_b)
max_pool_3 = tf.nn.max_pool(conv_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

conv_4 = tf.nn.relu(tf.nn.conv2d(max_pool_3, l8_w, [1, 1, 1, 1], padding='SAME') + l8_b)
conv_4 = tf.nn.relu(tf.nn.conv2d(conv_4, l9_w, [1, 1, 1, 1], padding='SAME') + l9_b)
conv_4 = tf.nn.relu(tf.nn.conv2d(conv_4, l10_w, [1, 1, 1, 1], padding='SAME') + l10_b)
max_pool_4 = tf.nn.max_pool(conv_4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

conv_5 = tf.nn.relu(tf.nn.conv2d(max_pool_4, l11_w, [1, 1, 1, 1], padding='SAME') + l11_b)
conv_5 = tf.nn.sigmoid(tf.nn.conv2d(conv_5, l12_w, [1, 1, 1, 1], padding='SAME') + l12_b)
conv_5 = tf.nn.sigmoid(tf.nn.conv2d(conv_5, l13_w, [1, 1, 1, 1], padding='SAME') + l13_b)
max_pool_5 = tf.nn.max_pool(conv_5, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

shape = max_pool_5.get_shape().as_list()
reshape = tf.reshape(max_pool_5, [shape[0], shape[1] * shape[2] * shape[3]])
hidden = tf.nn.sigmoid(tf.matmul(reshape, l14_w) + l14_b)
return tf.matmul(hidden, l15_w) + l15_b


logits = model(tf_train_dataset)
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))

# Optimizer.
optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)

# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(model(tf_valid_dataset))
test_prediction = tf.nn.softmax(model(tf_test_dataset))

num_steps = 1001

with tf.Session(graph=graph) as session:
tf.global_variables_initializer().run()
print('Initialized')
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels}
_, l, predictions = session.run(
[optimizer, loss, train_prediction], feed_dict=feed_dict)
if step % 50 == 0:
print('Minibatch loss at step %d: %f' % (step, l))
print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
print('Validation accuracy: %.1f%%' % accuracy(
valid_prediction.eval(), valid_labels))
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))

最佳答案

我同意@cyniikal ,您的网络对于这个数据集来说似乎太复杂了。使用单层模型,我能够在训练数据上实现 93.75% 的准确率,在测试数据上实现 86.7% 的准确率。

在我的模型中,我使用了 GradientDescentOptimizer 来最小化 cross_entropy,就像您所做的那样。我还使用了 16 批量大小。

我认为你的方法和我的方法之间的主要区别在于:

  1. OneHot 对标签进行编码
  2. 使用单层网络而不是 VGG-16

查看此notebook with my single layer model code sample .

如果您想向神经网络添加层(网络收敛会遇到更多困难),我强烈建议您阅读 this article on neural nets 。具体来说,由于您添加了 sigmoid 作为最后一个激活函数,我相信您正在遭受 vanishing gradient problem 的困扰。 。请参阅this page解决消失梯度

关于python - tensorflow 模型精度没有增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51828971/

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