TensorFlow introductory tutorial (3)

tags: api  tensorflow  Artificial Intelligence-Neural Network Algorithm  python

Continue from aboveTensorFlow introductory tutorial (2)
Training (tf.train API)
tensorflow provides optimizers (optimizers) to slowly change each variable to minimize the loss function. The simplest optimizer is gradient descent, which modifies the parameter value according to the derivative of the loss function with respect to the variable. Tensorflow can use the method tf.gradients to automatically calculate derivatives for a given model.

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init)
for i in range(1000):
    sess.run(train,{x:[1,2,3,4],y:[0,-1,-2,-3]})
print(sess.run([W,b]))

The output is:

[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]

Linear regression complete code
So far, we have successfully completed a machine learning, although it is just a simple linear regression. Post the complete linear regression code here:

import tensorflow as tf

sess = tf.Session()

W = tf.Variable([.3],dtype=tf.float32)
b = tf.Variable([-.3],dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W*x+b

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

y = tf.placeholder(tf.float32) 
squared_deltas = tf.square(linear_model-y) 
loss = tf.reduce_sum(squared_deltas) 
print(sess.run(loss,{x:[1,2,3,4],y:[0,-1,-2,-3]})) 

fixW = tf.assign(W,[-1.])
fixb = tf.assign(b,[1.])
sess.run([fixW,fixb])
print(sess.run(loss,{x:[1,2,3,4],y:[0,-1,-2,-3]}))

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init)
for i in range(1000):
    sess.run(train,{x:[1,2,3,4],y:[0,-1,-2,-3]})
print(sess.run([W,b]))

writer = tf.summary.FileWriter('D:/ten', tf.get_default_graph())
writer.close()

The results are as follows:

23.66
0.0
[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]

The view in tensorboard is as follows:

So far we have been able to use TensorFlow for the simplest machine learning operation-linear regression.

Learn more here
TensorFlow newbies actually break the verification code

Intelligent Recommendation

pyqt5 introductory tutorial (3)

In fact, it can be seen from the previous code that in the main function part, only three things are actually done 1. Create an application 2. Create a graphical interface 3. The exit signal will be a...

octave introductory tutorial (3)

Matrix Operations Matrix A * B .* Means multiplying matrix by number Matrix + number means adding the number to each element in the matrix A’ represents matrix transpose log is the logarithmic f...

Tensorflow introductory tutorial (3) two-layer convolutional neural network model to filter out images of MNIST unrecognized pairs

1 Overview Yesterday I learned how to use the Softmax regression model and the two-layer convolutional neural network model to train MNIST, although using a neural network can achieve99.31% Of the cor...

TensorFlow programming introductory tutorial (3) Neural network process visualization (visual display of optimization process)

Introduction Visualization of TensorFlow neural network results (visual display of optimization process) The observation results are as follows: corresponds to the code usedTensorFlow version is 2.0.0...

Docker introductory tutorial (3)-Dockerfile

Dockerfile Dockerfile is a text file used to customize the image. The image is stored in layers, and the previous layer will be the basis of the next layer. The customization of the image is to custom...

More Recommendation

Rxjava2 introductory tutorial 3: Operators

Operators Operators: Its essence is the high-order function in functional programming, which is the product of the separation and packaging of the various processes of reactive programming. So that we...

ShaderToy introductory tutorial (3)-CSG

Review the previousShaderToy Getting Started Tutorial (2)-Lighting and Camera One of the techniques used in many presentation scenes is called ray tracing. This algorithm is used in combination with a...

TensorFlow basic principle, introductory tutorial website

TensorFlow Advanced Python is object code that can be used to construct a portion of the run should be run outside of which FIG calculation, calculation and arrangement of FIG. http://tensorfly.cn/ gi...

tensorflow introductory tutorial (XI) back propagation BP

1、Outline Fully connected neural network and using a convolutional neural network back-propagation (BackPropagation, BP), and using a convolutional neural network back-propagation time (BackPropagatio...

tensorflow v2.0 introductory tutorial --04 logistic regression

personal blog must first be understood that the present tutorialLogistic regression principle。 using MNIST handwritten digital data set, the training images 60000, 10000 test image, image size is 28 *...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top