GET STARTED

Let’s start with the first piece of code on the document, a simple fit

import tensorflow as tf
import numpy as np

Use NumPy to generate phony data, 100 points in total.
x_data = np.float32(np.random.rand(2, 100)) # Random input, random generation of 2*100 matrix, random samples in the matrix are located in [0, 1)
The dot method is matrix multiplication, 1 * 2 and 2 * 100
Add 0.300 to all the samples in the matrix.Y_data = np.dot([0.100, 0.200], x_data) + 0.300Construct a linear model
# bias b, length 1, sample 0
b = tf.Variable(tf.zeros([1]))
The parameter W is of the shape 1 * 2W = tf. Variable (tf) random_uniform ([1, 2], 1.0, 1.0))# Constructed linear model
y = tf.matmul(W, x_data) + b

# set variance as loss function
loss = tf.reduce_mean(tf.square(y - y_data))
Optimizer -- Gradient descentThe optimizer = tf. Train. GradientDescentOptimizer (0.5)Move in the direction of minimizing losses
train = optimizer.minimize(loss)

Initialize variables
init = tf.global_variables_initializer()

# Start graph
sess = tf.Session()
sess.run(init)

# Fit the plane
for step in range(0, 201):
    sess.run(train)
    if step % 20 == 0:
        Print the fitting results every 20 rounds
        print(step,sess.run(W),sess.run(b))
Copy the code

The above code has been modified from the introduction to Python 3 code, with three changes, replacing the outdated initialization method, changing xrange to range, and the print function. Some comments have been added. Running in my environment, other than a hint that the CPU supports the AVX2 instruction set and the version I’m currently using is not available, the output is as follows:

0 [[0.22787997 0.51918703]] [0.13709038] 20 [[0.17085488 0.2892436]] [0.21785735] 40 [[0.13355325 0.2313425]] [0.2666235] 60 [[0.11450518 0.21202013]] [0.28634468] 80 [[0.1060768 0.20480107]] [0.29439792] 100 [[0.10251605 0.2019507]] [0.2976993] 120 [[0.10103704 0.20079806]] [0.29905474] 140 [[0.10042668 0.2003274]] [0.29961157] 160 [[0.10017543 0.20013446]] [0.29984036] 180 [[0.10007212 0.20005524]] [0.2999344] 200 [[0.10002964 0.20002273]] [0.29997304]Copy the code

It can be seen that the fitting results are very close to the parameters [0.100, 0.200] and [0.300] constructed by us.

The code structure

The code as a whole is simple and divided into several parts

  1. Constructing input samples and input tags (samples, tag processing)
  2. Constructing linear model
  3. Set up loss functions, optimizers, and training methods
  4. Start diagram, in the diagram initialization, training and print the fitting results

conclusion

The document gives a simple example at the beginning. Although it is a simple example, every step in it is indispensable. We can slowly summarize the “eight strands” of neural network construction according to the code in the following document example.

About the document

TensorFly is a Chinese community of TensorFlow. There are some translated TensorFlow documents.

About TensorFlow

TensorFlow™ is an open source software library that uses Data Flow Graphs for numerical calculations. The Nodes represent mathematical operations, and the lines in the diagram represent arrays of multidimensional data related to each other between the Nodes, the tensor. Its flexible architecture allows you to scale computing across multiple platforms, such as one or more cpus (or Gpus) on desktop computers, servers, mobile devices, and more. TensorFlow was originally developed by researchers and engineers in the Google Brain Group (part of the Google Machine Intelligence Research Institute) for machine learning and deep neural networks, but the system’s versatility makes it widely usable in other computing fields. (from TensorFly)