[PYTHON] [Keras] I tried to solve a donut-type region classification problem by machine learning [Study]

Hello everybody

A while ago try to make a neural network with PHP, I tried to verify what I was doing. However, it is slow and the system is not very good because there is no such thing as a sophisticated calculation method that has been verified and practiced in various places at present. So, this time, I will try Imadoki's machine learning mechanism using a framework called Keras.

Keras

Keras is a neural network library that runs on TensorFlow and is written in Python. From the feeling of using it, I thought that I could write a network by intuition. When I wrote TensorFlow raw, it was troublesome to set various parameters, but That area has also become easier to do.

Keras on Docker

Oh, there is, after all, the image with Keras https://hub.docker.com/r/gw000/keras/

Now you can try Keras without polluting your environment.

Problem setting

The problem setting is the donut type classification problem that I did before.

{f(x, y) = \left\{
\begin{array}{1}
1, (1 < x^2 + y^2 < 4)\\
0,  ( \rm{otherwise} )
\end{array}
\right.
}

double_circle.png

Implementation

Now that you've decided what you want to do, let's start implementing

Learning mechanism

First, make it from the learning mechanism.

learn.py


from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
import random
import math

def double_circle():
    x = random.uniform(-2, 2)
    y = random.uniform(-2, 2)
    sample = (x,y)
    norm = math.sqrt(x * x + y * y)
    if norm > 1 and norm < 2:
        label = 1
    else:
        label = 0

    return (sample, label)

# Model Definition
model = Sequential()
model.add(Dense(32, input_dim=2))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

data = []
labels = []
for num in range(1024):
    (sample, label) = double_circle()
    data.append(sample)
    labels.append(label)

model.fit(np.array(data), np.array(labels), nb_epoch=100, batch_size=32)

model.save('/srv/model/double_circle')

The function double_circle is used to pick random coordinates and return a label that determines if the point is inside a donut shape. Then, add the coordinates and label pairs to the list. Next, we will create a model, and Keras will form a model with the image of stacking each layer of the neural network.

model.add(Dense(32, input_dim=2))
model.add(Dense(64, activation='relu'))

For example, here we define the first and second layers. Since the input dimension of the first layer is unknown, it is clearly stated. Since the coordinate point is the input this time, enter ʻinput_dim = 2. Since the first argument of the first layer is the output dimension, the second layer does not need an input dimension. In addition, ʻactivation ='relu' is set in the activation function of the second layer (if there is no setting, it is output as it is). You can stack layers as you like like this.

$  docker run --rm -v `pwd`:/srv/ gw000/keras python learn.py

Let's run Keras's Docker container and let it learn. It took about 5 seconds on my Mac. A model file has been created in the model directory.

Use of generative model

Let's see how well the generated model works.

use.py


from keras.models import load_model
import numpy as np

model = load_model('/srv/ai/model/double_circle')

def check(x):
    data = np.array([x])
    pred = model.predict(np.array([x]))
    #print pred
    if pred > 0.5:
        return 1
    else:
        return 0

for y in range(20):
    labels = []
    for x in range(20):
        data = [(x-10.0)/5, (10.0-y)/5]
        labels.append(check(data))

    print labels

Keras just loads the model and it recreates the network you learned with learn. This script uses the loaded model to determine if the $ -2 <x <2 $, $ -2 <y <2 $ range is in the specified area in 0.1 increments. When you run this one, it looks like this.

$ docker run --rm -v `pwd`:/srv/ gw000/keras python use.py
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]

The round shape has emerged, so I don't think it's going well.

Let's take a little more detail. Now let's take data points in 0.02 increments. This time I will try to spit out the coordinate points that are judged to be included in the area in CSV format

to_csv.py


from keras.models import load_model
import numpy as np

model = load_model('/srv/model/double_circle')

def check(x):
    data = np.array([x])
    pred = model.predict(np.array([x]))
    #print pred
    if pred > 0.5:
        return 1
    else:
        return 0

for y in range(100):
    for x in range(100):
        data = [(x-50.0)/20, (50.0-y)/20]
        if check(data) == 1:
            print "%f,%f" % (data[0],data[1])

So, as usual, it will be processed by docker.

docker run --rm -v `pwd`:/srv/ gw000/keras python to_csv.py > result.csv

Let's plot the result.csv that came out. result.png Well, it might be something like this

Summary

For the time being, to get started with Keras, I tried to realize the one I made with PHP before. It's easier to understand than TensorFlow when building networks and layers.

This time it is like this.

reference

Official site

Recommended Posts

[Keras] I tried to solve a donut-type region classification problem by machine learning [Study]
I tried to solve a combination optimization problem with Qiskit
I tried to verify the yin and yang classification of Hololive members by machine learning
I tried to solve the shift scheduling problem by various methods
Want to solve a simple classification problem?
I installed Python 3.5.1 to study machine learning
I was frustrated by Kaggle, so I tried to find a good rental property by scraping & machine learning
I tried to solve the inverted pendulum problem (Cart Pole) by Q-learning.
I want to create a machine learning service without programming! Text classification
I tried to predict the presence or absence of snow by machine learning.
I tried to predict the change in snowfall for 2 years by machine learning
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to solve the problem with Python Vol.1
I tried to compress the image using machine learning
I tried to make a real-time sound source separation mock with Python machine learning
I tried to solve the virtual machine placement optimization problem (simple version) with blueqat
Uncle SE with hardened brain tried to study machine learning
I tried to implement anomaly detection by sparse structure learning
I tried using Tensorboard, a visualization tool for machine learning
I tried machine learning to convert sentences into XX style
[Machine learning] I tried to summarize the theory of Adaboost
I tried to divide with a deep learning language model
I tried to compare the accuracy of machine learning models using kaggle as a theme.
Matching app I tried to take statistics of strong people & tried to create a machine learning model
I tried to create a simple credit score by logistic regression.
I tried to make deep learning scalable with Spark × Keras × Docker
I tried machine learning with liblinear
I tried HR Tech to develop an expert search engine by machine learning in-house meeting information
I tried to implement sentence classification by Self Attention with PyTorch
I wanted to solve the ABC164 A ~ D problem with Python
I want to create a machine learning service without programming! WebAPI
[Machine learning] I tried to do something like passing an image
I tried "Implementing a genetic algorithm (GA) in python to solve the traveling salesman problem (TSP)"
(Machine learning) I tried to understand the EM algorithm in a mixed Gaussian distribution carefully with implementation.
I changed my job to a machine learning engineer at AtCoder Jobs
I tried to communicate with a remote server by Socket communication with Python.
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
I tried to classify guitar chords in real time using machine learning
(Machine learning) I tried to understand Bayesian linear regression carefully with implementation.
I tried to classify mnist numbers by unsupervised learning [PCA, t-SNE, k-means]
A beginner of machine learning tried to predict Arima Kinen with python
I tried to visualize the model with the low-code machine learning library "PyCaret"
I tried to solve the E qualification problem collection [Chapter 1, 5th question]
I tried to classify Oba Hana and Emiri Otani by deep learning
I tried to verify the result of A / B test by chi-square test
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried to let optuna solve Sudoku
I tried to make a Web API
I tried to solve TSP with QAOA
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
I tried to organize the evaluation indexes used in machine learning (regression model)
Try to draw a "weather map-like front" by machine learning based on weather data (5)
Try to draw a "weather map-like front" by machine learning based on weather data (3)
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
Machine learning beginners tried to make a horse racing prediction model with python
Try to draw a "weather map-like front" by machine learning based on weather data (1)
Try to draw a "weather map-like front" by machine learning based on weather data (4)
[Azure] I tried to create a Linux virtual machine in Azure of Microsoft Learn
I tried to extract a line art from an image with Deep Learning