[PYTHON] Validate the learning model with Pylearn2

We will explain how to perform verification using the model and test data trained in Pylearn2.

Read test data

Test data can be read from the pylearn2.datasets module or a pkl file. After reading, you can refer to the input value in data.X and the output value in data.y.

When reading MNIST datasets from the pylearn2.datasets module:

from pylearn2.datasets import mnist

data = mnist.MNIST(which_set="train")

When reading a pkl file:

import pickle

data = pickle.load(open("path/to/test_dataset.pkl"))

Load the model

The model after training is a pkl file, and you can read it.

import pickle

model= pickle.load(open("path/to/model.pkl"))

Calculate the predicted value

Create a function to calculate the predicted value. You can get the predicted value by generating theano function using the model's fprop method and passing the input value to that function. However, the space may be different between the fprop input and the dataset input, so use Space # format_as to perform the conversion. The generated function can calculate the predicted value for multiple input values at once. The predicted value is calculated in multiple times, because if you calculate it all at once, you may run out of memory. In the following function, input is X of the dataset and model is the model.

import theano
from pylearn2.space import VectorSpace

...

def simulate(inputs, model):
    space = VectorSpace(inputs.shape[1])
    X = space.get_theano_batch()
    Y = model.fprop(space.format_as(X, model.get_input_space()))
    f = theano.function([X], Y)
    result = []
    batch_size = 100
    for x in xrange(0, len(inputs), batch_size):
      result.extend(f(inputs[x:x + batch_size]))
    return result

Aggregate the results

In the case of statistical classification, it can be aggregated as follows. Pass the return value of simulate in the outputs of the following function, and the y of the dataset in labels. Use numpy's argmax to find the element that maximizes the output and compare it to y in the dataset.

import numpy as np

...

def count_correct(outputs, labels):
    correct = 0;
    for output, label in zip(outputs, labels):
        if np.argmax(output) == label:
            correct += 1
    return correct

Example of use

Here is an example when using the MNIST dataset.

import pickle
from pylearn2.datasets import mnist

model = pickle.load(open("path/to/model.pkl"))
data = mnist.MNIST(which_set="test")

predicts = simulate(data.X, model)
correct = count_correct(predicts, data.y)
print "{} / {}".format(correct, len(data.X))

References

http://fastml.com/how-to-get-predictions-from-pylearn2/

Recommended Posts

Validate the learning model with Pylearn2
Calibrate the model with PyCaret
Explore the maze with reinforcement learning
Machine learning model management to avoid quarreling with the business side
Let's tune the model hyperparameters with scikit-learn!
Run the interaction model with Attention Seq2 Seq
I tried to visualize the model with the low-code machine learning library "PyCaret"
A model that identifies the guitar with fast.ai
The story of doing deep learning with TPU
Exposing the DCGAN model for Cifar 10 with keras
Solving the Lorenz 96 model with Julia and Python
Challenge block breaking with Actor-Critic model reinforcement learning
Load the TensorFlow model file .pb with readNetFromTensorflow ().
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Learning Python with ChemTHEATER 05-1
Model fitting with lmfit
Learning Python with ChemTHEATER 01
Machine Learning with Caffe -1-Category images using reference model
Regression with linear model
Summary of the basic flow of machine learning with Python
Record of the first machine learning challenge with Keras
I made an API with Docker that returns the predicted value of the machine learning model
Try to evaluate the performance of machine learning / regression model
PyTorch learning memo (I made the same model as Karas)
Reinforcement learning in the shortest time with Keras with OpenAI Gym
Implement the mathematical model "SIR model" of infectious diseases with OpenModelica
Enjoy the Gray-Scott model with short code using matrix math
Try to evaluate the performance of machine learning / classification model
Recognize your boss and hide the screen with Deep Learning
Real-time image recognition on mobile devices with TensorFlow learning model
Create a python machine learning model relearning mechanism with mlflow
I captured the Touhou Project with Deep Learning ... I wanted to.
Analyze the topic model of becoming a novelist with GensimPy3
I tried to divide with a deep learning language model
Let's move word2vec with Chainer and see the learning progress
Machine learning model considering maintainability
Machine learning learned with Pokemon
Try deep learning with TensorFlow
Play with reinforcement learning with MuZero
Insert the debugger with nose
Ensemble learning summary! !! (With implementation)
Reinforcement learning starting with Python
About learning with google colab
Machine learning with Python! Preparation
Deep Kernel Learning with Pyro
Try Deep Learning with FPGA
Guess the password with klee
Linux fastest learning with AWS
gethostbyaddr () communicates with the outside
Machine learning Minesweeper with PyTorch
Use Maxout + CNN with Pylearn2
scraping the Nikkei 225 with playwright-python
Check the code with flake8
Call the API with python3.
Python Iteration Learning with Cheminformatics
Try machine learning with Kaggle
Generate Pokemon with Deep Learning
A story stuck with the installation of the machine learning library JAX
[Machine learning] Check the performance of the classifier with handwritten character data
[In-Database Python Analysis Tutorial with SQL Server 2017] Step 6: Using the model