Ternsorflow learning: 001 through routine preliminary understanding Tensorflow

Foreword

The purpose of this chapter is to understand and run TensorFlow, before you start, let's look at some sample code using TensorFlow written by Python API, allowing you to have a preliminary impression of the content to be learned.

Below this short Python program will some data into two-dimensional space, and then a line to fit these data:

import tensorflow as tf
import numpy as np
 # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3

x_data = np.random.rand(100).astype("float32")
y_data = x_data * 0.1 + 0.3

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but Tensorflow will
# figure that out for us.)
w = tf.Variable(tf.random_uniform([1],-1.0,1.0))
b = tf.Variable(tf.zeros([1]))
y = w * x_data +b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
 # Before starting, initialize the variables. We will 'run' this first
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(201):
    sess.run(train)
    if step % 20 ==0:
        print(step, sess.run(w), sess.run(b))
# Learns best fit is W: [0.1], b: [0.3]
(tensorflow-dev)

The first part of the code constructed above flow diagram (fl ow graph) data. Is established and run in a session () before the function is run, TensorFlow does not make any substantial calculations. Running in a virtual environment, python3 tf_001.pythe results for the future:

0 [0.5336875] [0.07610922]
20 [0.22660719] [0.2293212]
40 [0.13763347] [0.278991]
60 [0.1111864] [0.29375517]
80 [0.10332511] [0.29814377]
100 [0.10098837] [0.29944825]
120 [0.10029378] [0.299836]
140 [0.10008732] [0.29995126]
160 [0.10002597] [0.2999855]
180 [0.10000773] [0.2999957]
200 [0.10000231] [0.29999873]
(tensorflow-dev)

To further stimulate your desire to learn, we want you to look at how to solve TensorFlow is a classic machine learning problems.
In the field of neural networks, the most classic problem than MNIST handwritten digital classification.
To this end, we prepared two different tutorials, respectively, for beginners and experts. If you are already using other software trained many MNIST model, see Advanced tutorial (red pill). If you've never heard of MNIST, read the primary curriculum (blue pills). If your level is between these two groups, we suggest you take a quick initial tutorial, and then read the advanced tutorials.

Guess you like

Origin www.cnblogs.com/schips/p/12147218.html