机器学习初探

来源:互联网 发布:深圳勘察研究院知乎 编辑:程序博客网 时间:2024/05/16 13:44

深度学习挖坑

tesorflow初学 mnist数字识别
tensorflow教程

开发环境:python 3.5+windows 10

使用softmax模型的代码:(已包含下载数据集部分,先贴代码,原理缓更)

#http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html"""Functions for downloading and reading MNIST data."""from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport gzipimport osimport tempfileimport numpyfrom six.moves import urllibfrom six.moves import xrange  # pylint: disable=redefined-builtinimport tensorflow as tffrom tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_setsimport tensorflow.examples.tutorials.mnist.input_data as input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)x = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784,10]))b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x,W) + b)y_ = tf.placeholder("float", [None,10])cross_entropy = -tf.reduce_sum(y_*tf.log(y))train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)for i in range(100):  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, "float"))print (sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
0 0
原创粉丝点击