[Reprint + test + modification] TensorFlow installation and simple example on windows

Installation instructions: test time 2018.5.7

Platform: Currently available for installation on Ubuntu, Mac OS, Windows
Version : Provides gpu version, cpu version
Installation method: pip method, Anaconda method
Tips:

  1. Currently supports python3.5.x on Windows
      ps: After trying to install the original python3.5x directly, install tensorflow directly, but it failed. In order to facilitate the management of different python versions, Anaconda software is recommended for management.


Installation process:


Anaconda 5.1 (corresponding to python3.6) 1/1
python3.5.5 in Anaconda 1/1
Tensorflow1.8.0 1/1


Anaconda

Anaconda is the leading open data science platform powered by Python. The open source version of Anaconda is a high-performance distribution for Python and R, including over 100 of the most popular Python, R, and Scala packages for data science.
From the official Anaconda download page , see the official Anaconda tutorial
for specific usage , which is easy to understand!


Anaconda preliminary learning

0. Download the Anaconda installation package: Anaconda official download address
I downloaded is Anaconda35.1For Windows 64bit (built-in python3.6) after
downloading it, install it, and continue to the next step.

1. Check if Anaconda is successfully installed:conda --version

 
2. Detect which environments are currently installed: conda info --envs
Environmental monitoring(PS: I have modified my own and forgot to save it.)
3. Check which versions of python are currently available for installation: conda search --full-name python

(Anyway, choose python3.5x)
4. The installation is different Version of python: conda create --name tensorflow python=3.5
(guess that after entering python=3.5 version, the system will automatically select a 3.5.x version)




5. Follow the prompts to activate it: activate tensorflow

(note the left)
6. Make sure that the environment named tensorflow has been successfully added: conda info --envs

 
7. Check the python version in the new environment: python --version


8. Exit the current environment: deactivate


TensorFlow installation

Ten Guns was originally installed on the Yuansheng windows system, but the python version of Yuansheng was 3.6.5, so it failed.

So now install in Anocanda environment

installed cpu version

Under the Anaconda prompt just now


1 pip install tensorflow



2. Confirm that tensorflow is installed successfully:
under python3.5.5, typeimport tensorflow as tf



Getting started with TensorFlow routines

Installed a new thing, let's put it to use first!
After running the first applet for concepts, let’s watch it again!
Find a sense of accomplishment to keep going!
Example source: MINIST For ML Beginners

MINST dataset:

  1. 55000 training set, 10000 testing set, 5000 validation set
  2. Each picture is 28pixels*28pixels

Code:

#获得数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf

#输入图像数据占位符
x = tf.placeholder(tf.float32, [None, 784])

#权值和偏差
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

#使用softmax模型
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]))

#使用梯度下降算法优化:学习速率为0.5
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

#Session
sess = tf.InteractiveSession()

#初始化变量
tf.global_variables_initializer().run()

#训练模型,训练1000次
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}))

Running results: The output shows the accuracy of the obtained model

0.9126

(In fact, there is a lot of nonsense and Warning in front, but I haven't read it carefully. It should be that some libraries and packages are not perfect!!)


Summary of relevant information

  1. Anaconda official download address
  2. Anaconda official tutorial
  3. TensorFlow official installation tutorial
  4. The first example applet MINIST recognition

Guess you like

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