Tensorflow learning two _mnist entry

Two days ago just installed my Tensorflow, then by tensorflow today's Chinese website (http://www.tensorfly.cn/tfdoc/get_started/introduction.html),

Getting ready to start learning about handwriting recognition tensorflow of --mnist entry letter.

The major record what small error when running my first code I, arise.

A simple example

There are some in the introduction TensorFlow use the sample code written in Python API,

Directly used to run: An error occurred

  File "D:/tensorflow/python文件/tensorflow1.py", line 37
    print step, sess.run(W), sess.run(b)
             ^
SyntaxError: invalid syntax

Later, through an online search I found that because the code used on the official website is python2.x, and I'm using python3,

1, to change the range xrange

2, modified print format

 

Successful operation

Import tensorflow TF AS
 Import numpy NP AS 

# use NumPy generates dummy data (phony data), a total of 100 points. 
x_data = np.float32 (np.random.rand (2, 100)) # stochastic input 
y_data = np.dot ( [0.100, 0.200], x_data) + 0.300 # configured a linear model 
#  
B = tf.Variable (tf.zeros ([. 1 ])) 
W is = tf.Variable (tf.random_uniform ([. 1, 2], -1.0, 1.0 )) 
Y = tf.matmul (W is, x_data) + B # minimize variance 
Loss = tf.reduce_mean (tf.square (Y - y_data)) 
Optimizer = tf.train.GradientDescentOptimizer (0.5 ) 
Train = optimizer.minimize ( loss)





# Initialize variables 
the init = tf.initialize_all_variables () 

# start FIG (Graph) 
Sess = tf.Session () 
sess.run (the init) 

# -fit plane 
for STEP in Range (0, 201 ): 
    sess.run (Train) 
    IF 20 is ==% STEP 0:
         Print (STEP, sess.run (W is), sess.run (B))
View Code

Two, mnist entry

Download the data set in the time, the official website provides two ways: First, download the code and imported into the project, the second is the direct use of python source code to automatically download and install.

Here, I was directly in python source code download and install.

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

#实现回归模型
import tensorflow as tf
x = tf.placeholder("float", [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(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}))

 Error:

ImportError: No module named 'input_data'

将import input_data 代码换成  from tensorflow.examples.tutorials.mnist import input_data

 

Run successfully:

The accuracy of operating results of 92%

Guess you like

Origin www.cnblogs.com/smile321/p/11205527.html