Quick Start with Docker

We are going to use this Docker Images tensorflow/tensorflow

After installation of Docker has completed.

Start CPU only container

$ docker run -it -p 8888:8888 tensorflow/tensorflow

Going to your browser on http://localhost:8888/. This Docker Image will launch a jupyter.

图片描述

Of course,You can without Docker and start your first TensorFlow Instance.Follow steps of this tutorial

Taking a simple instance to understand

Importing modules

import tensorflow as tf
import numpy as np

Faking datas

# fake x_datas
x_data = np.random.rand(100).astype(np.float32)

# fake y_datas and build the relation between x_data and y_data
# y = x_data * K_value + shift_value
y_data = x_data*0.5 + 1.5 

Defining a training model

# define a model (the relation between x_data and y_data)
K_value = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
shift_value = tf.Variable(tf.zeros([1]))
y = K_value*x_data + shift_value

Defining the computation of loss

loss = tf.reduce_mean(tf.square(y-y_data))

Using the Gradient Descent optimization algorithm

#we use the Gradient Descent optimization algorithm 
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

Others

# variables initializer
init = tf.global_variables_initializer()  
# create a session
sess = tf.Session()
# run init 
sess.run(init)          

for step in range(201):
    # training controller
    sess.run(train)
    if step % 10 == 0:
        # print datas
        print(step, sess.run(K_value), sess.run(shift_value))
        

Result of Training

(0, array([ 0.64817411], dtype=float32), array([ 1.89406931], dtype=float32))
(10, array([ 0.47824922], dtype=float32), array([ 1.5111196], dtype=float32))
(20, array([ 0.4887996], dtype=float32), array([ 1.50572574], dtype=float32))
(30, array([ 0.49423242], dtype=float32), array([ 1.5029484], dtype=float32))
(40, array([ 0.49703008], dtype=float32), array([ 1.50151825], dtype=float32))
(50, array([ 0.49847072], dtype=float32), array([ 1.50078177], dtype=float32))
(60, array([ 0.49921253], dtype=float32), array([ 1.50040257], dtype=float32))
(70, array([ 0.49959451], dtype=float32), array([ 1.5002073], dtype=float32))
(80, array([ 0.49979115], dtype=float32), array([ 1.50010681], dtype=float32))
(90, array([ 0.49989241], dtype=float32), array([ 1.50005496], dtype=float32))
(100, array([ 0.4999446], dtype=float32), array([ 1.50002825], dtype=float32))
(110, array([ 0.49997142], dtype=float32), array([ 1.50001454], dtype=float32))
(120, array([ 0.49998531], dtype=float32), array([ 1.50000751], dtype=float32))
(130, array([ 0.49999243], dtype=float32), array([ 1.50000381], dtype=float32))
(140, array([ 0.49999613], dtype=float32), array([ 1.50000191], dtype=float32))
(150, array([ 0.49999797], dtype=float32), array([ 1.50000107], dtype=float32))
(160, array([ 0.49999899], dtype=float32), array([ 1.50000048], dtype=float32))
(170, array([ 0.49999946], dtype=float32), array([ 1.50000024], dtype=float32))
(180, array([ 0.49999961], dtype=float32), array([ 1.50000024], dtype=float32))
(190, array([ 0.49999961], dtype=float32), array([ 1.50000024], dtype=float32))
(200, array([ 0.49999961], dtype=float32), array([ 1.50000024], dtype=float32))

Finally,The training data K_value will close to 0.5 and shift_value will close to 1.5.

# y = x_data * K_value + shift_value
y_data = x_data*0.5 + 1.5 

Weny
47 声望4 粉丝

Wenyxu


引用和评论

0 条评论