错误训练Cifar-10模型张量流量 - 准确度为0,不会报告优化和损失

时间:2017-07-18 21:16:26

标签: python tensorflow

我目前正在尝试训练我的模型来对cifar-10数据集进行分类。我读了这样的数据:

def convert_images(raw):
raw_float = np.array(raw, dtype = float)
images = raw_float.reshape([-1,3,32,32])
images = images.transpose([0,2,3,1])
return images

def load_data(filename):
data = unpickle(filename)
raw_images = data[b'data']
labels = np.array(data[b'labels'])
images = convert_images(raw_images)
return images, labels

def load_training_data():
images = np.zeros(shape=[50000,32,32,3], dtype = float)
labels = np.zeros(shape = [50000], dtype = int)
begin = 0 
for i in range(5):
    filename = "data_batch_" + str(i+1)
    images_batch, labels_batch = load_data(filename)
    num_images = len(images_batch)
    end = begin + num_images
    images[begin:end, :] = images_batch
    labels[begin:end] = labels_batch
    begin = end
    return images, labels, OneHotEncoder(categorical_features=labels, n_values=10)

这样做会重塑数据,使其成为4d数组,像素和rgb颜色的值为32x32x3。我像这样定义我的模型(我首先将X重塑为行向量,因为4d数组会产生错误):

X = tf.placeholder(tf.float32, [None,32,32,3])
Y_labeled = tf.placeholder(tf.int32, [None])
data = load_training_data()

with tf.name_scope('dnn'):
    XX = tf.reshape(X, [-1,3072])
    hidden1 = tf.layers.dense(XX, 300, name = 'hidden1', activation = tf.nn.relu)
    hidden2 = tf.layers.dense(hidden1, 200, name = 'hidden2', activation = tf.nn.relu)
    hidden3 = tf.layers.dense(hidden2, 200, name = 'hidden3', activation = tf.nn.relu)
    hidden4 = tf.layers.dense(hidden3, 100, name = 'hidden4', activation = tf.nn.relu)
    logits = tf.layers.dense(hidden4, 10, name = 'outputs')

with tf.name_scope('loss'):
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels = (Y_labeled), logits = logits)
    loss = tf.reduce_mean(cross_entropy, name = 'loss')

learning_rate = 0.01

with tf.name_scope('train'):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    training_op = optimizer.minimize(loss)

with tf.name_scope('eval'):
    correct = tf.nn.in_top_k(logits,Y_labeled, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

init = tf.global_variables_initializer()

batch_size = 100
n_epochs = 50

with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
    for iteration in range(50000 // batch_size):
        X_batch = data[0][iteration*batch_size:(iteration+1)*batch_size]
        y_batch = data[1][iteration*batch_size:(iteration+1)*batch_size]
        #X_batch, y_batch = data.train.next_batch(batch_size)
        sess.run(training_op, feed_dict = {X: X_batch,Y_labeled: y_batch})
    acc_train = accuracy.eval(feed_dict = {X: X_batch,Y_labeled: y_batch})
    print(epoch, "train accuracy:", acc_train, "loss", loss)

我想定义一个有4个隐藏层的简单模型。当我运行它时,它编译没有错误并开始“训练”,但精度为0.0并且它不会打印任何损失。我不确定错误是在我的计算精度和损失中,还是在我对模型的定义中。

1 个答案:

答案 0 :(得分:0)

您提供标签的方式似乎有问题。当您创建placholder Y_labeled = tf.placeholder(tf.int32, [None, 10])时,它似乎是维度10的向量,但是稍后当您创建标签numpy tensor labels = np.zeros(shape = [50000], dtype = int)时,它似乎是一个标量。

这就是为什么你有这个错误,占位符需要提供一个维度(batch_size, 10)的张量,但你用(batch_size, 0)

提供它