[PYTHON] Let's create a tic-tac-toe AI with Pylearn 2-Save and load models-

Introduction

This article is a sequel to "Tic-tac-toe AI with Pylearn 2". It contains information such as the MLP model you are using and the format of the game record, so we recommend that you read the previous article before reading this article.

In the previous article

In the future, based on the results of one training, I will try to implement it in a tic-tac-toe game so that I can easily obtain the next move for any input. First of all, I have to make a game program of tic-tac-toe.

Since it was tied with, I will realize this. To do this, I had to save and load the trained model. I'll show you how to do that and actually create a tic-tac-toe with wxPython. Necessary files such as source code are posted on GitHub, so please get them from there.

How to save and load a model in Pylearn 2

Once I knew it, it was easy.

from pylearn2.utils import serial
...
...
ann = mlp.MLP([h0,out], nvis=9)
path = "./hoge.pkl"

# save model
serial.save(path, ann, on_overwrite='backup')

# load model
ann = serial.load(path)

You can select'ignore' or'backup'for'on_overwrite'.

Unless you have a specific reason, I think'backup' is fine.

Tic-tac-toe with wxPython

Operation check environment

I think wxPython will work with 2.8 as well. Click here for wxPython installation instructions (http://qiita.com/kanlkan/items/5e6f2e63de406f46b3b1#wxpython%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3 Please see% 83% 88% E3% 83% BC% E3% 83% AB) and so on. Installing Pylearn 2 on Windows is a difficult task, so run it on Linux. On a Mac ... I'm sorry, I don't know ...

How to use

tic-tac-toe.png

  1. Set the model parameters and save the learned model Set the model parameters in the right half panel.
  1. Select Game Mode
    1. Specify the model file to load and Game Start
  1. Left-click the mouse to place 〇 ×. Computer automatically puts it from the learning result.

Once you save a model, you can simply specify it and play games with that model.

Points to enjoy

Please enjoy watching the behavior of AI by changing the parameters such as. It is also recommended to play the game while watching the confidence level (I call it) of the hand that Computer outputs to the console as shown below.

[ 0.1223754   0.07839377  0.1005455   0.09967972  0.0958171   0.05355689
  0.13877278  0.08772236  0.22313648]
[  1.69255291e-01   1.79474672e-01   6.59611187e-02   8.35728072e-02
   1.76704145e-01   5.69182580e-05   1.74977445e-01   1.48576416e-01
   1.42118607e-03]
[  3.94020768e-02   3.56583963e-03   9.39233627e-05   1.20089713e-01
   4.85647829e-01   2.05857441e-04   2.00150417e-01   1.50013023e-01
   8.31320404e-04]
[  3.55036488e-01   8.74969597e-03   2.24572898e-04   7.35919590e-04
   1.89100732e-02   3.48102279e-04   2.63566398e-01   3.48737495e-01
   3.69125555e-03]

The nine numbers enclosed in [] add up to 1. Nine numbers correspond to nine squares. AI hits the square with the highest value among them. If you think of this number as a probability,

When you hit a strange square with full confidence, it's a little adorable.

Please try various things and play with them.

reference

https://github.com/lisa-lab/pylearn2/blob/master/pylearn2/train.py http://fastml.com/how-to-get-predictions-from-pylearn2/ http://deeplearning.net/software/pylearn2/library/utils.html

Recommended Posts

Let's create a tic-tac-toe AI with Pylearn 2-Save and load models-
Let's make a tic-tac-toe AI with Pylearn 2
Let's create a PRML diagram with Python, Numpy and matplotlib.
Let's create a free group with Python
Create a native GUI app with Py2app and Tkinter
Let's make a simple game with Python 3 and iPhone
Create a batch of images and inflate with ImageDataGenerator
Let's make a Mac app with Tkinter and py2app
Create a 3D model viewer with PyQt5 and PyQtGraph
[Linux] Create a self-signed certificate with Docker and apache
Create a web surveillance camera with Raspberry Pi and OpenCV
Let's create a script that registers with Ideone.com in Python.
Create applications, register data, and share with a single email
Create a deploy script with fabric and cuisine and reuse it
Create a homepage with django
Create a heatmap with pyqtgraph
Create and list Django models
Create a directory with python
Create a video player with PySimpleGUI + OpenCV 2 Add ROI setting and save function (DIVX, MJPG, GIF)
[AWS Hands-on] Let's create a celebrity identification service with a serverless architecture!
Create a temporary file with django as a zip file and return it
Create a striped illusion with gamma correction for Python3 and openCV3
Specify or create a python folder and then save the screenshot.
Create a private DMP with zero initial cost and zero development with BigQuery
I tried to create Bulls and Cows with a shell program
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Get OCTA simulation conditions from a file and save with pandas
Create and return a CP932 CSV file for Excel with Chalice
Save & load data with joblib, pickle
Let's make a GUI with python.
HTTPS with Django and Let's Encrypt
Let's make a breakout with wxPython
Create a virtual environment with Python!
Let's make Othello AI with Chainer-Part 1-
Save and retrieve files with Pepper
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Let's make Othello AI with Chainer-Part 2-
Create a poisson stepper with numpy.random
Create a file uploader with Django
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Let's create a chat function with Vue.js + AWS Lambda + dynamo DB [AWS settings]
Create a stack with a queue and a queue with a stack (from LetCode / Implement Stack using Queues, Implement Queue using Stacks)
Create a Todo app with Django ④ Implement folder and task creation functions
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Create a decision tree from 0 with Python and understand it (5. Information Entropy)