如何提高Tensorflow预测精度

时间:2017-11-02 06:39:02

标签: python-3.x tensorflow deep-learning

我正在使用tensorflow来创建一个模型,该模型可以使用google提供的SVHN数据集对数字进行分类。我的识别率非常低(约25%),但我看到笔记本具有88%的准确率。{{3} }

我想知道是否有人可以给我一些提示,我应该如何提高我的准确性,以使我的模型更好。

这是我的型号代码。

filename='extra.pickle'

with open(filename,'rb') as f:
    other=pickle.load(f)
    train_data=other['train_dataset']
    test_data=other['test_dataset']

    del other

train_dataset=train_data['X']
test_dataset=test_data['X']
train_labels=train_data['y']
test_labels=test_data['y']

print(len(test_dataset))
print(len(test_labels))
print(test_dataset.shape)
#print(train_dataset.length())

classes=10

batch_size=32

num_steps = 200000

graph=tf.Graph()

#Placeholder for the data
with graph.as_default():

    data_placeholder = tf.placeholder(tf.float32, shape=(batch_size,32,32,3))
    label_placeolder = tf.placeholder(tf.int64, shape=(batch_size, classes))

    tf_test_dataset = tf.placeholder(tf.float32, shape=(batch_size,32,32,3))

    tf_label_dataset = tf.placeholder(tf.float32, shape=(batch_size, classes))

    layer1_weights=tf.Variable(tf.truncated_normal([3,3,3,16]))
    layer1_biases=tf.Variable(tf.zeros([16]))

    layer2_weights=tf.Variable(tf.truncated_normal([3,3,16,32]))
    layer2_biases=tf.Variable(tf.zeros([32]))

    layer3_weights=tf.Variable(tf.truncated_normal([2,2,32,64]))
    layer3_biases=tf.Variable(tf.zeros([64]))

    layer4_weights=tf.Variable(tf.truncated_normal([1024,10]))
    layer4_biases=tf.Variable(tf.zeros([10]))

    layer5_weights=tf.Variable(tf.truncated_normal([10,classes]))
    layer5_biases=tf.Variable(tf.zeros([classes]))


    def layer_multiplication(data_input_given,dropping=False):

        #Convolutional Layer 1

        CNN1=tf.nn.relu(tf.nn.conv2d(data_input_given,layer1_weights,strides=[1,1,1,1],padding='SAME')+layer1_biases)

        print('CNN1 Done!!')

        #Pooling Layer

        Pool1=tf.nn.max_pool(CNN1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
        print('Pool1 DOne')

        #second Convolution layer

        CNN2=tf.nn.relu(tf.nn.conv2d(Pool1,layer2_weights,strides=[1,1,1,1],padding='SAME'))+layer2_biases
        print('CNN2 Done')
        #Second Pooling

        Pool2 = tf.nn.max_pool(CNN2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        print('pool2 Done')
        #Third Convolutional Layer

        print(Pool2.shape)

        CNN3 = tf.nn.relu(tf.nn.conv2d(Pool2, layer3_weights, strides=[1, 1, 1, 1], padding='SAME')) + layer3_biases
        print('CNN3 Done')
        #Third Pooling Layer

        Pool3 = tf.nn.max_pool(CNN3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        print('Pool3 DOne')
        #Fully Connected Layer

        #print(Pool3.shape)

        shape = Pool3.get_shape().as_list()

        # print(shape)

        reshape = tf.reshape(Pool3, [shape[0], shape[1] * shape[2] * shape[3]])

        #print(reshape.shape)

        FullyCon = tf.nn.relu(tf.matmul(reshape, layer4_weights) + layer4_biases)

        #print(FullyCon.shape)

        if dropping==False:
            print('Training')
            dropout = tf.nn.dropout(FullyCon, 0.6)
            z=tf.matmul(dropout,layer5_weights)+layer5_biases
            return z

        else:
            print('Testing')
            z = tf.matmul(FullyCon, layer5_weights) + layer5_biases
            return z


    gloabl_step = tf.Variable(0, trainable=False)

    decay_rate=tf.train.exponential_decay(1e-6,gloabl_step,4000,0.96,staircase=False,)

    train_input=layer_multiplication(data_placeholder,False)

    test_prediction = tf.nn.softmax(layer_multiplication(tf_test_dataset,True))

    loss=(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label_placeolder,logits=train_input))
                                   + 0.01 * tf.nn.l2_loss(layer1_weights)
                                   + 0.01 * tf.nn.l2_loss(layer2_weights)
                                   + 0.01 * tf.nn.l2_loss(layer3_weights)
                                   + 0.01 * tf.nn.l2_loss(layer4_weights)
                                   + 0.01 * tf.nn.l2_loss(layer5_weights)
                                   )

    optimizer = tf.train.GradientDescentOptimizer(name='Stochastic', learning_rate=decay_rate).minimize(loss,global_step=gloabl_step)


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

    config=tf.ConfigProto()
    config.gpu_options.allocator_type ='BFC'

    saver = tf.train.Saver()

    test_accuracy=[]

    with tf.Session(config=config) as session:
        tf.global_variables_initializer().run()
        print('Initialized')
        tf.train.write_graph(session.graph_def, '.', './SVHN.pbtxt')

        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), :]

            batch_test_data =  test_dataset[offset:(offset + batch_size), :, :]
            batch_test_labels = test_labels[offset:(offset + batch_size),:]
            #print(batch_data)
            #print(batch_test.shape)

            feed_dict = {data_placeholder:batch_data, label_placeolder:batch_labels}
            _, l, predictions = session.run(
                [optimizer, loss, train_input], feed_dict=feed_dict)
            if (step % 500 == 0):
                #print(session.run(decay_rate))
                print('Minibatch loss at step %d: %f' % (step, l))
                print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
                if(batch_test_data.shape!=(32,32,32,3)):
                    print('Skip')
                else:
                    correct_prediction = tf.equal(tf.argmax(test_prediction, 1), tf.argmax(tf_label_dataset, 1))
                    accuracy_for_test = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
                    print("Test Accuracy")

                    test_accuracy.append(accuracy_for_test.eval(feed_dict={tf_test_dataset:batch_test_data,
                                                tf_label_dataset:batch_test_labels}))

                    print(accuracy_for_test.eval(feed_dict={tf_test_dataset:batch_test_data,
                                                tf_label_dataset:batch_test_labels}))

        print(np.mean(test_accuracy))


        saver.save(sess=session, save_path='./SVHN.ckpt')

Ps-代码在我的系统中工作。问题似乎与我相信的架构有关。

2 个答案:

答案 0 :(得分:1)

使用不同的权重初始化,您的结果应该更好。在你的参考笔记本中,他们使用stddev = 0.1,但你也可以看一下glorot或者他初始化,这应该更好。

此外,您的学习率非常低,而且衰减使得学习率更低,因此网络不会以这种方式学到很多东西。通过更好地初始化网络,您可以提高学习速度并从数据中学习有用的东西。

答案 1 :(得分:0)

首先,没有单个参数可以提高您的准确性。张量流的训练取决于许多参数,如初始重量,学习率,训练模型的步数。用不同的组合尝试。就像学习率不应该低也不是很多其他明智的你会错过最小的。它就像我们的大脑,有时我们在有关于那个东西的清晰数据时非常快地学习东西。有些时候我们需要更多的数据以更好的方式找到东西。

对所有这些参数的不同混淆进行培训,然后检查您的准确性