[PYTHON] A story about simple machine learning using TensorFlow

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.

Target person

・ Beginner of TensorFlow

environment

-Develop using docker on mac.

Features of TensorFlow

・ Expressiveness using data flow graphs ・ Works in CPU / GPU mode without code modification ・ Available from idea test to service · ** Support Python ** / C ++

Installation

Take image of docker and execute container

  1. 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

  2. Hit the following command to execute container from docker image docker run -it b.gcr.io/tensorflow/tensorflow-full

  3. 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.

the term

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.

Try to find a factor by the steepest descent method

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]    

something felt

・ 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

A story about simple machine learning using TensorFlow
A story about machine learning with Kyasuket
A story about data analysis by machine learning
A story about using Python's reduce
(Note) A story about creating a question answering system using Spring Boot and machine learning (SVM)
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
A addictive story when using tensorflow on Android
About machine learning overfitting
A story about automating online mahjong (Mahjong Soul) with OpenCV and machine learning
Machine learning (TensorFlow) + Lotto 6
A story about achieving a horse racing recovery rate of over 100% through machine learning
What I learned about AI / machine learning using Python (1)
A story about using Resona's software token with 1Password
A story about predicting exchange rates with Deep Learning
Installation of TensorFlow, a machine learning library from Google
What I learned about AI / machine learning using Python (3)
Memo for building a machine learning environment using Python
What I learned about AI / machine learning using Python (2)
An introduction to machine learning from a simple perceptron
About machine learning mixed matrices
Build a machine learning environment
A note about TensorFlow Introduction
Machine learning algorithm (simple perceptron)
Build a machine learning environment using PyCharm on Ubuntu environment (TensorFlow will also be introduced!)
[Linux] A story about mounting a NAS through a firewall using NFS
What I learned about AI and machine learning using Python (4)
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
Launching a machine learning environment using Google Compute Engine (GCE)
A story about installing matplotlib using pip with an error
I tried using Tensorboard, a visualization tool for machine learning
A story about a 40-year-old engineer manager passing "Deep Learning for ENGINEER"
A story about developing a machine learning model while managing experiments and models with Azure Machine Learning + MLflow
A refreshing story about Python's Slice
Learn Zundokokiyoshi using a simple RNN
Make a face recognizer using TensorFlow
A sloppy story about Python's Slice
Creating a simple table using prettytable
Machine learning algorithm (simple regression analysis)
Inversely analyze a machine learning model
Creating a learning model using MNIST
Application development using Azure Machine Learning
A story stuck with the installation of the machine learning library JAX
Creating artificial intelligence by machine learning using TensorFlow from zero knowledge-Introduction 1
[Tutorial] Make a named entity extractor in 30 minutes using machine learning
How about Anaconda for building a machine learning environment in Python?
A story about a student who does not know the machine learning machine learned machine learning (deep learning) for half a year
Stock price forecast using machine learning (scikit-learn)
A story about remodeling Lubuntu into a Chromebook
Aiming to become a machine learning engineer from sales positions using MOOCs
[Machine learning] LDA topic classification using scikit-learn
Solve a simple traveling salesman problem using a Boltzmann machine with simulated annealing
Machine learning with python (2) Simple regression analysis
I tried playing a ○ ✕ game using TensorFlow
TensorFlow Machine Learning Cookbook Chapter 2 Personally Clogged
[Machine learning] FX prediction using decision trees
Stock price forecast using deep learning (TensorFlow)
[ML-Aents] I tried machine learning using Unity and Python TensorFlow (v0.11β compatible)
I made a Dir en gray face classifier using TensorFlow --⑥ Learning program
I made a Dir en gray face classifier using TensorFlow --- ⑧ Learning execution
Machine learning
[Machine learning] Supervised learning using kernel density estimation