An introduction to machine learning for bot developers

Introduction

2016 was a year when bots got a lot of attention. It was also a year in which new possibilities were revealed one after another due to remarkable technological progress related to machine learning.

If you read this article, you probably want to develop a bot that uses machine learning. "But I don't know what to do" This article is intended for such readers.

How to use and use machine learning

I think there are two main uses for machine learning. One is the "interaction between users and bots" and the other is the "services provided to users".

The former allows you to interact with the user by natural writing. It would be realistic to use an existing service such as Microsoft LUIS here. Below is a good summary of what services are available other than LUIS.

Summary of conversation APIs, libraries, and services that can be used with BOT

Next, about the "services provided to users" part. I think this can be further divided into the following two.

Part 1. Use services and APIs that are generally provided

(Mainly) API collection using the results of deep learning (for myself)

By using the API etc. introduced in the above article, the latest machine learning results can be used very easily. The point to note is that if you just use the API as it is, you will end up with similar bots.

Part 2. Use a machine learning framework

The other is to write code using a machine learning framework. Furthermore, it is an approach of learning using your own data and resources (such as GPU). The hurdles will rise at once, but I think it is an unavoidable part if we provide highly original services.

Now let's actually develop a bot that offers a unique service using machine learning!

Framework selection

Before getting into the implementation, select the machine learning framework to use this time.

Deep Learning Framework Hitchhiking Guide

The most popular is Tensorflow, but its low-level API (doesn't it mean poor performance?) May be a bit of a challenge for beginners. This time I would like to select Keras. Since this is a high-level API, it has the advantage that the description is simple and easy to understand.

In addition, choose a framework that implements your bot.

As you can see from the above article, most of the languages used are Python. On the other hand, is Node.js often used to implement bots? Probably the general method to eliminate this mismatch is to "implement each separately and use it via API", but this time I would like to write the bot in Python to simplify the implementation. The framework used is python-rtmbot.

What should I do?

If you want to develop a bot that offers your own services using machine learning, you need to do the following three things very roughly.

  1. Build a learning model
  2. Prepare the data and train the model
  3. Process using the trained model

Most of the machine learning framework tutorials do just one or two above.

Basically, it is enough to build and train the model once. Of course, it is a prerequisite that the prediction is sufficiently accurate. After that, the general flow is to write the model structure and training results (parameters) to a file and read them at runtime (= 3).

Model building, learning

As mentioned above, there are abundant tutorials and samples for building and learning the model, so this time we will use the existing ones.

Try Keras callback (model save, restore / TensorBoard export / early stopping) Use this to create a bot that "returns the value of sin when spoken to". It's not practical, but it should be a good sample.

In addition, this code set has been uploaded to Github (fullkawa / ml-bot-sample). After that, I will explain only the necessary parts from here.

First, "train.py", but this is just a summary of the source of the reference article. It will be.

The point is the 73rd line

train.py


cp_cb = ModelCheckpoint(filepath = fpath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')

And lines 78-80

train.py


json_string = model.to_json()
#open(os.path.join(f_model,'./tensorlog/rnn_model.json'), 'w').write(json_string)
open(os.path.join('./tensorlog/rnn_model.json'), 'w').write(json_string)

is. The former writes the learning results (parameters) and the latter writes the model structure to a file. See the source article for what you're doing elsewhere.

Run python train.py once before running the bot.

112-233:ml-bot-sample y.furukawa$ python train.py
Using TensorFlow backend.
____________________________________________________________________________________________________
Layer (type) Output ShapeParam # Connected to
====================================================================================================
lstm_1 (LSTM)(None, 300) 362400lstm_input_1[0][0]
____________________________________________________________________________________________________

dense_1 (Dense)(None, 1) 301 lstm_1[0][0]
____________________________________________________________________________________________________
activation_1 (Activation)(None, 1) 0 dense_1[0][0]
====================================================================================================
Total params: 362701
____________________________________________________________________________________________________
Train on 3325 samples, validate on 176 samples
Epoch 1/10
3000/3325 [==========================>...] - ETA: 4s - loss: 0.2627 - acc: 0.0000e+00 Epoch 00000: val_loss improved from inf to 0.19250, saving model to ./tensorlog/weights.00-0.24-0.19.hdf5
3325/3325 [==============================] - 49s - loss: 0.2408 - acc: 3.0075e-04 - val_loss: 0.1925 - val_acc: 0.0000e+00
Epoch 2/10
3000/3325 [==========================>...] - ETA: 4s - loss: 0.0456 - acc: 3.3333e-04 Epoch 00001: val_loss improved from 0.19250 to 0.00085, saving model to ./tensorlog/weights.01-0.04-0.00.hdf5
3325/3325 [==============================] - 48s - loss: 0.0412 - acc: 3.0075e-04 - val_loss: 8.4748e-04 - val_acc: 0.0000e+00
Epoch 3/10
3000/3325 [==========================>...] - ETA: 4s - loss: 0.0015 - acc: 3.3333e-04 Epoch 00002: val_loss did not improve
3325/3325 [==============================] - 47s - loss: 0.0024 - acc: 3.0075e-04 - val_loss: 0.0228 - val_acc: 0.0000e+00
Epoch 4/10
3000/3325 [==========================>...] - ETA: 4s - loss: 0.0189 - acc: 3.3333e-04 Epoch 00003: val_loss did not improve
3325/3325 [==============================] - 46s - loss: 0.0177 - acc: 3.0075e-04 - val_loss: 0.0055 - val_acc: 0.0000e+00
Epoch 5/10
3000/3325 [==========================>...] - ETA: 4s - loss: 0.0089 - acc: 3.3333e-04 Epoch 00004: val_loss did not improve
3325/3325 [==============================] - 47s - loss: 0.0095 - acc: 3.0075e-04 - val_loss: 0.0163 - val_acc: 0.0000e+00
Epoch 00004: early stopping

In this case, "weights.01-0.04-0.00.hdf5" output by Epock 2/10 is the most accurate (val_loss: 8.4748e-04 --val_acc: 0.0000e + 00), and it is rather worse after that. I will.

Bot implementation

Next is the bot program. This (plugins / sin.py) is the main dish this time.

plugins/sin.py


model_json = open('tensorlog/rnn_model.json', 'r').read()
model = model_from_json(model_json)

Load the model structure,

plugins/sin.py


files = glob.glob('tensorlog/weights.*.hdf5')
model.load_weights(files[-1])

Read the learning result (parameter). The file with the largest number = the most accurate learned parameter is being read.

With this alone

plugins/sin.py


predicted = self.model.predict(X, batch_size=1)

You can get the sin value like this. Before that

plugins/sin.py


X = np.zeros((1,100,1))
for i in range(0, 100):
  X[0, i, 0] = i #Fixed in this sample

Is not a particularly important part, just preparing the data for calculating the sin value. However, the need to match the size of the array is tedious.

Execution result

112-233:ml-bot-sample y.furukawa$ rtmbot
Using TensorFlow backend.
Model loaded.
Weights loaded from tensorlog/weights.01-0.04-0.00.hdf5
READY!

This time, I named the bot "stag.feec". I'll talk to you.

Slackキャプチャ

I was able to safely return the value of sin!

Recommended Posts

An introduction to machine learning for bot developers
An introduction to OpenCV for machine learning
An introduction to Python for machine learning
An introduction to machine learning
Introduction to machine learning
[For beginners] Introduction to vectorization in machine learning
Super introduction to machine learning
An introduction to machine learning from a simple perceptron
Introduction to machine learning Note writing
An introduction to Mercurial for non-engineers
Introduction to Machine Learning Library SHOGUN
An introduction to Python for non-engineers
Introduction to Machine Learning: How Models Work
Introduction to ClearML-Easy to manage machine learning experiments-
An introduction to Python for C programmers
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Before the introduction to machine learning. ~ Technology required for machine learning other than machine learning ~
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
[Python] Easy introduction to machine learning with python (SVM)
[Super Introduction to Machine Learning] Learn Pytorch tutorials
An introduction to object-oriented programming for beginners by beginners
An introduction to statistical modeling for data analysis
Take the free "Introduction to Python for Machine Learning" online until 4/27 application
[Super Introduction to Machine Learning] Learn Pytorch tutorials
An introduction to voice analysis for music apps
Introduction to Deep Learning (1) --Chainer is explained in an easy-to-understand manner for beginners-
How to Introduce IPython (Python2) to Mac OS X-Preparation for Introduction to Machine Learning Theory-
Python learning notes for machine learning with Chainer Chapters 11 and 12 Introduction to Pandas Matplotlib
Build an interactive environment for machine learning in Python
Preparing to start "Python machine learning programming" (for macOS)
Everything for beginners to be able to do machine learning
Data set for machine learning
[Learning memorandum] Introduction to vim
Japanese preprocessing for machine learning
An introduction to private TensorFlow
Introduction to Deep Learning ~ Learning Rules ~
An introduction to Python Programming
An introduction to Bayesian optimization
Deep Reinforcement Learning 1 Introduction to Reinforcement Learning
Introduction to Python For, While
Introduction to Deep Learning ~ Backpropagation ~
I tried to build an environment for machine learning with Python (Mac OS X)
Rebuilding an environment for machine learning with Miniconda (Windows version)
Build an environment for machine learning using Python on MacOSX
Introduction to Machine Learning with scikit-learn-From data acquisition to parameter optimization
Made icrawler easier to use for machine learning data collection
For those who want to start machine learning with TensorFlow2
How to use machine learning for work? 03_Python coding procedure
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
[Introduction to Reinforcement Learning] Reinforcement learning to try moving for the time being
Introduction to Deep Learning ~ Function Approximation ~
Introduction to Deep Learning ~ Coding Preparation ~
<For beginners> python library <For machine learning>
Machine learning meeting information for HRTech
[Recommended tagging for machine learning # 4] Machine learning script ...?
Introduction to Deep Learning ~ Dropout Edition ~
Introduction to Deep Learning ~ Forward Propagation ~
Introduction to Deep Learning ~ CNN Experiment ~
How to collect machine learning data
[Python Tutorial] An Easy Introduction to Python