Machine learning Tensorflow中cifar10数据集的CNN

Machine learning Tensorflow中cifar10数据集的CNN,machine-learning,tensorflow,deep-learning,Machine Learning,Tensorflow,Deep Learning,我正在尝试使用Tensorflow复制通过卷积神经网络为CIFAR10获得的结果,但是经过一些时期(~60个时期)后,我的性能(准确度)约为10%,因此如果CNN经过良好训练,我不知道 此代码基于面向专家的Deep mnist,但在Cifar10中不起作用 import numpy as np import tensorflow as tf def unpickle(file): import cPickle fo = open(file, 'rb') dict = c

我正在尝试使用Tensorflow复制通过卷积神经网络为CIFAR10获得的结果,但是经过一些时期(~60个时期)后,我的性能(准确度)约为10%,因此如果CNN经过良好训练,我不知道

此代码基于面向专家的Deep mnist,但在Cifar10中不起作用

import numpy as np
import tensorflow as tf

def unpickle(file):
    import cPickle
    fo = open(file, 'rb')
    dict = cPickle.load(fo)
    fo.close()
    return dict

#unpacking training and test data
b1 = unpickle("~/cifar-10-batches-py/data_batch_1")
b2 = unpickle("~/cifar-10-batches-py/data_batch_2")
b3 = unpickle("~/cifar-10-batches-py/data_batch_3")
b4 = unpickle("~/cifar-10-batches-py/data_batch_4")
b5 = unpickle("~/cifar-10-batches-py/data_batch_5")

test = unpickle("~/cifar-10-batches-py/test_batch")

#Preparing test data
test_data = test['data']
test_label = test['labels']

#Preparing training data
train_data = np.concatenate([b1['data'],b2['data'],b3['data'],b4['data'],b5['data']],axis=0)
train_label = np.concatenate([b1['labels'],b2['labels'],b3['labels'],b4['labels'],b5['labels']],axis=0)

#Reshaping data
train_data = np.reshape(train_data,[50000,32,32,3])
test_data = np.reshape(test_data,[10000,32,32,3])

batch_size = 100
image_width = 32
image_height = 32
channels = 3

#Constructing Graph
x = tf.placeholder(tf.float32, [None, image_width, image_height, channels])#Training Data
y = tf.placeholder(tf.int32, [None])
one_hot = tf.one_hot(y,depth=10)#Converting in one hot vectors

#Constructing CNN Layers
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

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

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


#Given an input tensor of shape [batch, in_height, in_width, in_channels] and a filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels], taken from: http://textminingonline.com/dive-into-tensorflow-part-v-deep-mnist
W_conv1 = weight_variable([7, 7, 3, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([5, 5, 32, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

W_conv3 = weight_variable([5, 5, 32, 64])
b_conv3 = bias_variable([64])
h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)

#Constructing MLP layers
W_fc1 = weight_variable([8 * 8 * 64, 64])
b_fc1 = bias_variable([64])
h_pool3_flat = tf.reshape(h_conv3, [-1, 8*8*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1)

W_fc2 = weight_variable([64, 10])
b_fc2 = bias_variable([10])

y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)


#Computing Cost function
cross_entropy = -tf.reduce_sum(one_hot*tf.log(tf.clip_by_value(y_conv,1e-10,1e20)))
train_step = tf.train.MomentumOptimizer(learning_rate = 0.0001, momentum = 0.9).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(one_hot,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

init = tf.initialize_all_variables()
sess = tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=16))
sess.run(init)

epochs = 100
b_per = 0
row = []
for e in range(epochs):

    print( "epoch", e)
    avg_cost = 0
    #foreach batch
    for j in range(int(train_data.shape[0]/batch_size)):

        subset=range((j*batch_size),((j+1)*batch_size))
        data = train_data[subset,:,:,:]
        label = train_label[subset]
        _,c = sess.run([train_step,cross_entropy], feed_dict={x: data, y: label})

        avg_cost += c / data.shape[0]
        #print(avg_cost)

        b_per = b_per + 1

        if b_per%10==0 :

            row.append(sess.run(accuracy, feed_dict={x: test_data, y: test_label }))



    print(row[-1])

数据整形部分出错!应该是,

# Reshaping data
train_data = train_data.reshape(50000, 3, 32, 32).transpose(
    0, 2, 3, 1).astype("uint8")
test_data = test_data.reshape(10000, 3, 32, 32).transpose(
    0, 2, 3, 1).astype("uint8")

数据整形部分出错!应该是,

# Reshaping data
train_data = train_data.reshape(50000, 3, 32, 32).transpose(
    0, 2, 3, 1).astype("uint8")
test_data = test_data.reshape(10000, 3, 32, 32).transpose(
    0, 2, 3, 1).astype("uint8")

我需要知道我是否有一个概念错误,你试图重现的结果是否报告了他们使用了多少个时代?是的,在教程中,他们报告62个时代后的错误为31%Mnist模型非常简单。也许你想尝试更高级的模型,比如《盗梦空间》。我需要知道我是否有概念错误。你试图重现的结果是否报告了他们使用了多少个纪元?是的,在教程中,他们报告62个纪元后的错误为31%。列表模型非常简单。也许你想尝试更高级的模式,比如《盗梦空间》。