[关闭]
@NumberFairy 2017-09-23T05:54:56.000000Z 字数 1186 阅读 748

总结(第三周)

对mnist手写图片集合(2017/9/16 -- 2017/9/23)


根据tensorflow官网的学习步骤,机器学习的入门首先不得不先了解一下mnist这个手写数字集合。

我的工作:
1、从tensorflow官网提供的方法获取mnist集合数据,获取到的数据包含三部分,55000个样本数据,10000个测试数据,5000个验证数据;
2、构建多分类模型Softmax Regression;
3、建立损失函数,损失函数的好坏是评价一个模型的标准,这里用到了交叉熵损失;
4、对我们的损失函数求最小值(偏导数等于零),这里用到了梯度下降的方法。


上述提到的构建分类模型、建立损失函数以及对损失函数进行优化的方法梯度下降,tensorflow中都有对应的方法进行了实现。下面给出代码:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", ont_hot = True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Vairable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict = {x: batch_xs, y_:batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注