My name is Cho. I usually develop web applications using CakePHP3.
Recently I'm interested in Python and have been touching it personally.
This time I would like to write a story about making simple machine learning using TensorFlow.
・ Beginner of TensorFlow
-Develop using docker on mac.
・ Expressiveness using data flow graphs ・ Works in CPU / GPU mode without code modification ・ Available from idea test to service · ** Support Python ** / C ++
First, let's install from the docker environment. Please set the environment while referring to the following URl. https://docs.docker.com/engine/installation/mac/#/docker-for-mac
Hit the following command to execute container from docker image
docker run -it b.gcr.io/tensorflow/tensorflow-full
Check the operation
$ python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a+b)
42
>>>
At this point, the development environment has been set.
Now that the environment is set Learn the terms used in TensorFlow. Operation The nodes on the graph are called Operation (op). Operation can receive one or more Tensors. Operation returns the result of executing the calculation as one or more Tensors.
Tensor Internally all data is represented through Tensor. Tensor is like a multidimensional array, and only Tensor exchanges between Operations in the graph.
Session You need a Session object to run the graph. Session is an encapsulation of the execution environment of Operation.
Variables Variables are used to save and update parameters after running the graph. It is the role of a buffer that stores the Tensor in memory.
Now, let's use TensorFlow personally.
Please refer to the following URL for details on the steepest descent method. https://ja.wikipedia.org/wiki/%E6%9C%80%E6%80%A5%E9%99%8D%E4%B8%8B%E6%B3%95
Weighted matrix W
and bias b
import tensorflow as tf
import numpy as np
#Put 100 data with Numpy random.
x_data = np.float32(np.random.rand(2, 100))
#The learning goal is calculated by the following formula. (W = [0.1, 0.2], b = 0.3)
y_data = np.dot([0.100, 0.200], x_data) + 0.300
Define a model using the input data and W, b
#b is 0,
b = tf.Variable(tf.zeros([1]))
#W is a 1x2 weight variable(Initialize with an even random value)
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b
#Loss function definition
loss = tf.reduce_mean(tf.square(y - y_data))
#Initialize loss function with steepest descent(0.5 is the learning ratio)
optimizer = tf.train.GradientDescentOptimizer(0.5)
#Learning Operation definition
train = optimizer.minimize(loss)
Learning Session start
#Initialize all variables.
init = tf.initialize_all_variables()
# Session start
sess = tf.Session()
sess.run(init)
#Learn 200 times.
for step in xrange(0, 201):
sess.run(train)
if step % 20 == 0:
print step, sess.run(W), sess.run(b)
result
0 [[ 0.8228116 0.25179306]] [-0.21591029]
20 [[ 0.31065419 0.22789511]] [ 0.17903577]
40 [[ 0.15808699 0.20829338]] [ 0.26633102]
60 [[ 0.11601268 0.20247138]] [ 0.29062203]
80 [[ 0.10441278 0.20073807]] [ 0.2973859]
100 [[ 0.10121564 0.20022091]] [ 0.29927069]
120 [[ 0.10033476 0.20006624]] [ 0.29979634]
140 [[ 0.10009213 0.20001991]] [ 0.29994306]
160 [[ 0.10002533 0.20000601]] [ 0.29998407]
180 [[ 0.10000696 0.20000178]] [ 0.29999554]
200 [[ 0.10000192 0.20000054]] [ 0.29999873]
・ For the time being, Python is amazing and TensorFlow is amazing. ・ This time, I felt that I had to study more mathematics while trying to solve the problem I gave as an Example task. ・ This time, I tried to touch machine learning a little with Python, so I would like to touch other places as well.
Recommended Posts