没有Keras的情况下如何学习Fashion-MNIST模型?

时间:2019-05-11 09:41:48

标签: neural-network deep-learning

我想学习不用Keras进行深度学习的fashion-mnist模型。

我必须获得超过87%的准确度〜

所以我尝试了几次,但准确率达到了82%。

我该怎么办?

我认为我必须再添加一个隐藏层。但是我不知道怎么办。

请让我知道:) 提前非常感谢您。 (对于大量代码感到抱歉)

    import numpy as np 
    import random 
    import matplotlib.pyplot as plt 
    %matplotlib inline
    from sklearn.metrics import classification_report 
    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('MNIST_data/', 
         source_url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/',
         one_hot=True)

    print("Training set (images): {shape}".format(shape=mnist.train.images.shape)) 
    print("Training set (labels): {shape}".format(shape=mnist.train.labels.shape))
    print("Test set (images): {shape}".format(shape=mnist.test.images.shape)) 
    print("Test set (labels): {shape}".format(shape=mnist.test.labels.shape))

    label_names = [ 
        'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 
        'Sneaker','Bag', 'Ankle boot' ]
    # get 28 by 28 image
    sample_1 = mnist.train.images[47].reshape(28, 28)
    # get corresponding integer label from one-hot encoded data 
    sample_label_1 = np.where(mnist.train.labels[47] == 1)[0][0]
    # plot sample 
    print("y = {label_index} ({label})".format(label_index=sample_label_1, label=label_names[sample_label_1]))
    plt.imshow(sample_1, cmap='Greys')

    #input place holders
    X = tf.placeholder(dtype=tf.float32, shape=[None, 784])
    Y = tf.placeholder(dtype=tf.float32, shape=[None, 10])
    # weights and biases 
    W = tf.Variable(tf.random_normal([784, 10]))
    b = tf.Variable(tf.random_normal([10])) 
    S = tf.matmul(X, W) + b
    hypothesis = S 
    loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=Y))
    corrects = tf.equal(tf.argmax(hypothesis, axis=1), tf.argmax(Y, axis=1)) 
    accuracy = tf.reduce_mean(tf.cast(corrects, tf.float32))

    LEARNING_RATE = 0.001
    EPOCHS =50
    BATCH_SIZE=100

    optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for epoch in range(EPOCHS):
            for _ in range(int(mnist.train.num_examples / BATCH_SIZE)):
                X_batch, Y_batch = mnist.train.next_batch(BATCH_SIZE)
                sess.run(optimizer, feed_dict={X: X_batch, Y: Y_batch})

            if (epoch+1) % 1 == 0:
                acc_val = sess.run(accuracy, feed_dict={X: mnist.test.images, Y: 
                mnist.test.labels})
                print("epoch: {:04d}, accuracy: {:.2f}".format(epoch+1, acc_val))
        print("Learning finished.")

0 个答案:

没有答案