[PYTHON] Creating artificial intelligence by machine learning using TensorFlow from zero knowledge-Introduction 1

TensorFlow

TensorFlow is a machine learning library released by Google as open source. www.tensorflow.org

This is the conclusion I made after reading a machine learning book for a while and touching TensorFlow.

** TensorFlow is not something that machine learning beginners can easily use **

With TensorFlow, you can intuitively incorporate the concept of machine learning into your code. However, for that purpose, it is necessary to know the theory of machine learning in advance. If you don't understand the terms such as "neural network" and "structural pattern mining", you need to stop and investigate.

However, if you like mathematics, the world where everything is expressed by mathematical formulas is strangely fun. If you know the algorithm and how to use it, you can certainly implement interesting things at a considerable level.

Installation of TensorFlow (MacOS)

Well, it's the same even if you look here and install it, but I will write it for the time being. It will be completed in about 5 minutes. https://www.tensorflow.org/versions/master/get_started/os_setup.html

Check the version of python. Ok if 2.7 or higher

$ python -V
Python 2.7.10

install pip

$ sudo easy_install pip

Install virtualenv

$ sudo pip install --upgrade virtualenv

Create a virtualenv environment in ~ / tensorflow (change the folder specification if you want to put it in another location)

$ virtualenv --system-site-packages ~/tensorflow

Activate tensorflow. After execution, the command prompt display will be (tensorflow) $.

$ source ~/tensorflow/bin/activate

Install TensorFlow

(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

end.

Easy Python program creation

In response to the feeling of "I want to move anything because it doesn't matter!" Before a difficult theory, I will start with Python code that even inexperienced Python users can easily understand.

x = 1
y = x + 2
print(y)

Write this code and save it as test.py. And when you run it

(tensorflow)$ python test.py
3

Naturally, the result is 3.

Rewrite a simple Python program with TensorFlow

Next, let's rewrite this a little like TensorFlow.

import tensorflow as tf

x = tf.constant(1, name='x')
y = tf.Variable(x + 2, name='y')

print(y)

This is the result of saving and executing with test2.py.

(tensorflow) $ python test2.py
<tensorflow.python.ops.variables.Variable object at 0x10f6e5110>

Something is obviously different. The reason is that it is performing a different process than the previous code.

Explain the processing contents line by line in the code of test2.py

  1. Import the tensorflow module and make it tf
  2. Make a constant value and let it be x. Put the number 1 in the x
  3. Create a variable and let it be y. Let the definition of y be the same as x + 2.
  4. Output y

In other words, the subtle difference is that y is only defined, and the numerical value is not yet entered like the previous code.

If you modify this to a proper code, it will be like this.

import tensorflow as tf

x = tf.constant(1, name='x')
y = tf.Variable(x + 2, name='y')

model = tf.initialize_all_variables()

with tf.Session() as session:
    session.run(model)
    print(session.run(y))

Explanation for each line

  1. Import the tensorflow module and make it tf
  2. Make a constant value and let it be x. Put the number 1 in the x
  3. Create a variable and let it be y. Let the definition of y be the same as x + 2.
  4. Initialize variables with initialize_all_variables
  5. Create a session to run 6.4 Run the model created in 4
  6. Execute y to output

Then, the result is 3 as shown.

(tensorflow) hellow $ python test2.py
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
3

Summary of Introductory 1

You may think that the above example is "troublesome", but this is just a simple example. And this is where the normal code and TensorFlow are a little different. If you think, "It's not that easy, but explain" natural language processing "or" future prediction "," if you don't understand this, you'll end up coming back here later. I wrote it from here first because it would end up. If you're excited about the news that AlphaGo has defeated the world champion of Go, and you're excited by the Amazon Echo, which responds nicely just by talking, you're sure to be immersed in the world of machine learning with TensorFlow. No, machine learning Omoroi! hot!

This introductory book will be interesting in earnest from the next time onwards.

To all engineers

"Puzzle that can be solved by most engineers, but not only by the bottom 10% of bad engineers?" I made some series. If you are interested, please try to solve it. By the way, this puzzle has nothing to do with TensorFlow. http://tango-ruby.hatenablog.com/entry/2015/11/30/122814

Recommended Posts

Creating artificial intelligence by machine learning using TensorFlow from zero knowledge-Introduction 1
A story about simple machine learning using TensorFlow
Creating a position estimation model for the Werewolf Intelligence Tournament using machine learning
Python learning memo for machine learning by Chainer from Chapter 2
Installation of TensorFlow, a machine learning library from Google
Machine learning (TensorFlow) + Lotto 6
Artificial intelligence, machine learning, deep learning to implement and understand
Collect machine learning data by scraping from bio-based public databases
Learning from the basics Artificial intelligence textbook Chapter 5 Chapter end problems
[For beginners] Re: Genetic algorithm starting from zero [Artificial intelligence]
4 [/] Four Arithmetic by Machine Learning
Using open data from Data City Sabae to predict water level gauge values by machine learning Part 2
Summary of recommended APIs for artificial intelligence, machine learning, and AI
Deep Learning / Deep Learning from Zero 2 Chapter 4 Memo
Deep Learning / Deep Learning from Zero Chapter 3 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
[Machine learning] Understanding uncorrelatedness from mathematics
Machine learning summary by Python beginners
Deep Learning / Deep Learning from Zero 2 Chapter 7 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 8 Memo
Deep Learning / Deep Learning from Zero Chapter 5 Memo
Deep Learning / Deep Learning from Zero Chapter 4 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 3 Memo
Creating a learning model using MNIST
Deep Learning / Deep Learning from Zero 2 Chapter 6 Memo
Application development using Azure Machine Learning
Aiming to become a machine learning engineer from sales positions using MOOCs
[ML-Aents] I tried machine learning using Unity and Python TensorFlow (v0.11β compatible)
[Memo / Creating] "Hands-On Machine Learning with Scikit-Learn & TensorFlow" English translation into Japanese