TensorFLow Learning (1) - Native Windows installs TensorFlow for MNIST machine learning

On November 9, 2015, Google released the artificial intelligence system TensorFlow and announced it as open source.
[TensorFlow] is Google's internal machine learning system for many years. Today, Google is making this system open source and publishing its parameters to industry engineers, academics, and technologists with extensive programming capabilities.

Recently, I began to try to learn TensorFlow, because I am used to the windows system and the python development environment of anaconda and spyder. The following tutorial is based on the above. The main material comes from: TensorFlow official documentation.

Step 1: Install anaconda3

Download and install the Python scientific computing distribution anaconda3 on the official website
https://www.continuum.io/downloads/
Anaconda integrates many third-party libraries for python scientific computing, and it is easy to install.

Step 2: Build a tensorflow virtual environment

[1] Open the cmd command window
[2] Build a tensorflow virtual environment: conda create –name tensorflow python=3.5
[3] Install spyder in the virtual environment: conda install -n tensorflow spyder
[4] Start the virtual environment: activate tensorflow
[5 ] ] Install the tensorflow package in the virtual environment:

pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl

[6] Close the virtual environment: deactivate tensorflow

Step 3: Test TensorFlow

[1] Open the cmd command window
[2] activate tensorflow
[3] Start the python compiler: python
[4] Enter the following code and check the result, it is normal if it is equal to 32

import tensorflow as tf
sess = tf.Session()
a = tf.constant(10)
b = tf.constant(22)
print(sess.run(a + b))
32

the fourth step:

[1] The official website of the MNIST dataset is Yann LeCun's website. A python source code is provided below to automatically download and install this dataset. Put the py file downloaded from the following address in the working address wd
https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py
[2] Execute the following code to view the result:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
#读取mnist文件

#设定参数
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])

#定义cost
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

#设定训练算法
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#初始化变量
init = tf.global_variables_initializer()

#启动模型
sess = tf.Session()
sess.run(init)

#施行训练
for i 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, "float"))

#计算所学习到的模型在测试数据集上面的正确率
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325603258&siteId=291194637