The first tensorflow program

Keywords: Docker sudo Python Ubuntu

Run a tensorflow Hello World project using docker images.

After installing ubuntu 18.04, installing tensorflow through pip is always a mysterious mistake and can only offer docker. With docker, only one mirror can run without any other dependencies.

docker installs tensorflow

1. Install docker

$ sudo apt install docker.io

2. Add users to the docker group so that you can run the docker image without sudo

$ sudo usermod -a -G docker $HOME
$ sudo systemctl restart docker

3. Successful test installation

$ docker run hello-world

4. docker uses proxy (optional)

If the image cannot be retrieved through docker pull due to network problems, you can try using proxy. See how docker uses proxy Official documents

  • Create an embedded system D directory for docker services
$ mkdir -p /etc/systemd/system/docker.service.d
  • Create / etc / system / docker. service. D / http-proxy. conf file and add HTTP_PROXY environment variable. Where [proxy-addr] and [proxy-port] are changed to the actual proxy address and port respectively:
[Service]
Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/"
  • If there are internal Docker registries that do not require proxy access, you need to specify NO_PROXY environment variables:
[Service]
Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
  • Update configuration:
$ systemctl daemon-reload
  • Restart the Docker service:
$ systemctl restart docker

5. Pull tensorflow image

$ docker pull tensorflow/tensorflow

It may take a long time to retrieve the mirror here.

Recognition of Handwritten Pictures Using MNIST Data Set

When we start learning to program, the first thing we usually do is to learn to print "Hello World". It's like Hello World for beginners of programming and MNIST for beginners of machine learning. MNIST is an entry-level computer vision data set, which contains a variety of handwritten digital pictures. Here we will train a machine learning model to predict the number in the picture. This example comes from Introduction to MNIST Machine Learning.

  • Pull up MNIST data sets, including training model data and test data sets. The inout_data module has been installed in the mirror. Calling read_data_sets will automatically download the imported data sets.
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
  • Import tensorflow package
import tensorflow as tf
  • Define X and y_placeholders, and tensorflow then enters the correct number through these two placeholders. X stands for picture information, because the picture is 28x28 pixels (28x28=784), and None stands for any number. Y_denotes the number represented by the picture, with a total of 10 digits ranging from 0 to 9.
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder("float", [None,10])
  • Define the soft Max regression model, W and b are two variables
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
  • The model is trained by gradient descent algorithm. Cross_entropy represents cross-entropy, which is used to measure the error of the model. The smaller the cross-entropy is, the smaller the error between the model and the training data is. y_is the actual distribution of input and y is the predicted probability distribution. Gradient Descent Optimizer should be familiar with machine learning algorithm if he understands it. It means training with gradient descent algorithm. The learning rate is 0.01. The aim is to minimize cross_entropy.
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
  • Start the model, each time through mnist.train.next_batch from the data set to take 100 data model training, 1000 cycles
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  • Accuracy of Model Recognition Based on MNIST Test Set
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})

Running in docker

  • Create a new folder on this machine, named tensorflow, and write the above code to test.py under the folder
$ mkdir tensorflow
$ vim tensorflow/test.py
  • Start docker and mount tensorflow folder into docker instance as a virtual volume
$ docker run -it -v $PWD/tensorflow:/tensorflow tensorflow/tensorflow /bin/bash
  • At this point, the current terminal runs through python test.py in the docker instance
# cd /tensorflow
# python test.py

The recognition accuracy is about 91%. We can try to reduce or increase the number of cycles of model training, and then compare the recognition accuracy. It can be found that after more than 100 cycles, the recognition accuracy can not be increased by increasing the number of cycles.

Posted by waskelton4 on Sat, 18 May 2019 09:01:29 -0700