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.
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.
(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.
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!
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.
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.
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).
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.
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.
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.
I was able to safely return the value of sin!
Recommended Posts