Tensorflow from foundation to actual combat 01 installation and basic operation

There is a road to the mountain of books. Diligence is the path. There is no end to learning. It is hard to make a boat

What I can't create, I can't understand

1. Installation and introduction

1.1 installation

promat at anaconda

pip install tensorflow

If it cannot be installed, the external installation website

https://www.lfd.uci.edu/~gohlke/pythonlibs/#xgboost

The website can install tensorflow and other libraries. Tensorflow must be 64 bits


After downloading the cd, enter the location of the file and install pip

pip install file name

Version number

tensorflow.version

1.2 what is tensorflow

Tensorflow is a second-generation machine learning system developed by Google. It overcomes the limitations of the first-generation system DistBelief, which can only develop neural network algorithms, is difficult to configure and depends on Google's internal hardware. It is more widely used, improves flexibility and portability, and greatly improves speed and scalability. Literally, tensorflow is a framework for implementing and executing machine learning algorithms by means of tensor flow on the graph. It has the following characteristics:

  • Flexibility. TensorFlow is not a strict "neural network" library. As long as the calculation can be represented as a data flow graph, TensorFlow can be used, such as partial differential solution in scientific calculation. (in fact, the orientation of TF in the introduction of its official website is a scientific computing library based on data flow graph, not just a machine learning library)
  • Portability. The same code can be deployed to PC s, servers or mobile devices with any number of CPU s, GPU s or TPU s (Tensor Processing Unit, a processor specially developed by Google for machine learning) without modification.
  • Automatic differentiation. Like Theano, TensorFlow also supports automatic differentiation. Users do not need to solve the gradient through back propagation.
  • Multilingual support. TensorFlow officially supports python, C + +, Go and Java interfaces. Users can experiment with Python in machines with good hardware configuration and deploy with C + + in environments with tight resources or low latency.
  • Performance. Although TensorFlow only supported stand-alone when it was first released and was not excellent in performance evaluation, with Google's strong development strength, TensorFlow's performance has caught up with other frameworks

2. Variable operation

2.1 creating variables

Create variables in Python and assign values as long as a = 3

All variables in tensorflow must be in tensor format. You need to specify a standard format first

Specify variables and operations, but they have not been executed and initialized

First open a session and open up a computable area. To execute anything, you must actually run in the computable area of the session.

The initialization method of global variables is defined. Maybe run it actually

a = 3 
# Create a variable
w = tf.Variable([[0.5,1.0]])
x = tf.Variable([[2.0],[1.0]]) 

y = tf.matmul(w, x)  #The operation is also a tensorflow format


#Global variable initialization
init_op = tf.global_variables_initializer() 
with tf.Session() as sess:
    sess.run(init_op) # run initialization
    print (y.eval())

[[ 2.]]

Many operations of tensorflow are similar to numpy

  • tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

  • tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]

  • tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]

  • tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]

  • tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

  • tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] [-1. -1. -1.]]

  • tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0]

  • tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]

#The resulting values follow a normal distribution with a specified mean and standard deviation
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# shuffle the cards
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# Each execution result will be different
sess = tf.Session()
print (sess.run(norm))
print (sess.run(shuff))

[[-5.58110332 0.84881377 7.51961231]
[ 3.27404118 -7.22483826 7.70631599]]
[[5 6]
[1 2]
[3 4]]

run in the session to get the value

Each time the tensor adds 1, assign is the assignment operation. Run the initialization of the global variable with session, and then perform the update operation

state = tf.Variable(0)
new_value = tf.add(state, tf.constant(1))
update = tf.assign(state, new_value)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(state))    
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

0
1
2
3

2.2 calculation

Convert Numpy to tensor

import numpy as np
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
     print(sess.run(ta))

Arithmetic operation

a = tf.constant(5.0)
b = tf.constant(10.0)

x = tf.add(a, b, name="add")
y = tf.div(a, b, name="divide")

with tf.Session() as sess:
    print("a =", sess.run(a))
    print("b =", sess.run(b))
    print("a + b =", sess.run(x))
    print("a/b =", sess.run(y))

a = 5.0
b = 10.0
a + b = 15.0
a/b = 0.5

placeholder specifies a location, but the value in it is uncertain. A data type needs to be written in it.
During iteration, the data format is the same every time, but the value after each operation is changing. You can fix a data type and shape first and transfer data. The assignment is performed at the same time.
(similar to digging a pit and buying a radish of different sizes)

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
    print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

[array([ 14.], dtype=float32)]

3. Implement linear regression algorithm

Find the best line to fit the data in the data

3.1 generating samples

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# Randomly generate 1000 points around the line y=0.1x+0.3
num_points = 1000
vectors_set = []
for i in range(num_points):
    x1 = np.random.normal(0.0, 0.55)
    y1 = x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.03)
    vectors_set.append([x1, y1])

# Generate some samples
x_data = [v[0] for v in vectors_set]
y_data = [v[1] for v in vectors_set]

plt.scatter(x_data,y_data,c='r')
plt.show()

3.2 training model

# Generate a 1-dimensional W matrix, and the value is a random number between [- 1,1]
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')
# Generate a 1-dimensional b matrix with an initial value of 0
b = tf.Variable(tf.zeros([1]), name='b')
# The estimated value y is calculated
y = W * x_data + b

# Estimated value y and actual value y_ The mean square error between data is taken as the loss
loss = tf.reduce_mean(tf.square(y - y_data), name='loss')
# The gradient descent method is used to optimize the parameters
optimizer = tf.train.GradientDescentOptimizer(0.5)
# The training process is to minimize this error value
train = optimizer.minimize(loss, name='train')

sess = tf.Session()

init = tf.global_variables_initializer()
sess.run(init)

# What are the initialized W and b
print ("W =", sess.run(W), "b =", sess.run(b), "loss =", sess.run(loss))
# Perform 20 workouts
for step in range(20):
    sess.run(train)
    # Output trained W and b
    print ("W =", sess.run(W), "b =", sess.run(b), "loss =", sess.run(loss))
#writer = tf.train.SummaryWriter("./tmp", sess.graph)

W = [ 0.96539688] b = [ 0.] loss = 0.297884
W = [ 0.71998411] b = [ 0.28193575] loss = 0.112606
W = [ 0.54009342] b = [ 0.28695393] loss = 0.0572231
W = [ 0.41235447] b = [ 0.29063231] loss = 0.0292957
W = [ 0.32164571] b = [ 0.2932443] loss = 0.0152131
W = [ 0.25723246] b = [ 0.29509908] loss = 0.00811188
W = [ 0.21149193] b = [ 0.29641619] loss = 0.00453103
W = [ 0.17901111] b = [ 0.29735151] loss = 0.00272536
W = [ 0.15594614] b = [ 0.29801565] loss = 0.00181483
W = [ 0.13956745] b = [ 0.29848731] loss = 0.0013557
W = [ 0.12793678] b = [ 0.29882219] loss = 0.00112418
W = [ 0.11967772] b = [ 0.29906002] loss = 0.00100743
W = [ 0.11381286] b = [ 0.29922891] loss = 0.000948558
W = [ 0.10964818] b = [ 0.29934883] loss = 0.000918872
W = [ 0.10669079] b = [ 0.29943398] loss = 0.000903903
W = [ 0.10459071] b = [ 0.29949448] loss = 0.000896354
W = [ 0.10309943] b = [ 0.29953739] loss = 0.000892548
W = [ 0.10204045] b = [ 0.29956791] loss = 0.000890629
W = [ 0.10128847] b = [ 0.29958954] loss = 0.000889661
W = [ 0.10075447] b = [ 0.29960492] loss = 0.000889173
W = [ 0.10037527] b = [ 0.29961586] loss = 0.000888927

plt.scatter(x_data,y_data,c='r')
plt.plot(x_data,sess.run(W)*x_data+sess.run(b))
plt.show()

4.Mnist handwriting recognition data set

Datasets from tensorflow

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

print ("Winner Winner Chicken Dinner")

Winner Winner Chicken Dinner

Download dataset

print ("Downloading~Don't rush")
mnist = input_data.read_data_sets('data/', one_hot=True)
print
print (" Type is %s" % (type(mnist)))
print (" Training data are %d" % (mnist.train.num_examples))
print (" Test data are %d" % (mnist.test.num_examples))

The label of the dataset is defined as an onehot encoding format. Under the 10 categories of 0-9, it is only 1 under the current dataset and 0 in other locations.

When doing classification tasks, the final result is the probability of different categories. The category with the highest probability of acquisition.

Batch

n. A batch; The amount of a batch of (food, medicine, etc.); Batch;
v. Batch processing;
[other] plural: batches

Take batch data and obtain batch data in the data set for training each time

5. Tensorflow logistic regression classification task

Import data

Set parameters, set the number of categories, specify the input format, specify the number of iterations, and specify the number of batch es for each iteration

	

placeholder specifies the size of x and y, and None specifies any data format

Parameter initialization, W1 represents the weight parameter matrix, which is the matrix of [784 * 10], and the matrix of B1 bit [10,]

Construction model

Construct the predicted value y of logistic regression_ pred

Judge the accuracy of training, and judge whether the prediction is correct by whether the position is the same

Iterative calculation


test result


Tags: Python neural networks TensorFlow

Posted on Sat, 02 Oct 2021 17:16:04 -0400 by CrazeD