Tensorflow1.0 第一集续:MNIST数据集(没带卷积)

Tensorflow 1.0 第一集续:MNIST数据集

用了批次(Batch)的概念。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 输入数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

# 每个批次大小,即每次训练的时候用的样本大小
batch_size = 50
# 计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32, [None, 784]) #每张图像有784个像素块 28*28的
y = tf.placeholder(tf.float32, [None, 10])

# 一个简单的神经网络
W = tf.Variable(tf.zeros([784, 100]))
b = tf.Variable(tf.zeros([1, 100]))
L = tf.nn.tanh(tf.matmul(x, W) + b)

W1 = tf.Variable(tf.random_normal([100, 10]))
b1 = tf.Variable(tf.zeros([1, 10]))
prediction = tf.nn.softmax(tf.nn.tanh(tf.matmul(L, W1) + b1))

loss = tf.reduce_mean(tf.square(prediction - y))
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.2).minimize(loss)

init = tf.global_variables_initializer()
# 求准确率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) #布尔型列表
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #tf.cast 把布尔型转换为浮点型

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(30):
        for batch in range(n_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})

        acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print("Iter: " + str(epoch) + ", Testing Accuracy:" + str(acc))

就这样的准确度还挺好,不愧是MNIST。要是我的论文准确度也能有这么高就好了🌚

结果:

Iter: 0, Testing Accuracy:0.9072
Iter: 1, Testing Accuracy:0.9156
Iter: 2, Testing Accuracy:0.9149
Iter: 3, Testing Accuracy:0.9165
Iter: 4, Testing Accuracy:0.9206
Iter: 5, Testing Accuracy:0.9227
Iter: 6, Testing Accuracy:0.9257
Iter: 7, Testing Accuracy:0.9282
Iter: 8, Testing Accuracy:0.9317
Iter: 9, Testing Accuracy:0.9328
Iter: 10, Testing Accuracy:0.9335
Iter: 11, Testing Accuracy:0.9349
Iter: 12, Testing Accuracy:0.9357
Iter: 13, Testing Accuracy:0.9395
Iter: 14, Testing Accuracy:0.9402
Iter: 15, Testing Accuracy:0.9424
Iter: 16, Testing Accuracy:0.9438
Iter: 17, Testing Accuracy:0.9434
Iter: 18, Testing Accuracy:0.9448
Iter: 19, Testing Accuracy:0.9471
Iter: 20, Testing Accuracy:0.9481
Iter: 21, Testing Accuracy:0.9481
Iter: 22, Testing Accuracy:0.9489
Iter: 23, Testing Accuracy:0.9484
Iter: 24, Testing Accuracy:0.95
Iter: 25, Testing Accuracy:0.9504
Iter: 26, Testing Accuracy:0.9521
Iter: 27, Testing Accuracy:0.9514
Iter: 28, Testing Accuracy:0.9524
Iter: 29, Testing Accuracy:0.9529

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值