[PYTHON] Dare to learn with Ruby "Deep Learning from scratch" Importing pickle files from forbidden PyCall

In 72p "3.6.2 Neural network inference processing" of "Deep Learning from scratch", the pickle file of python is called. This pickle file is a raw binary, so it's not easy to call. So, try importing the pickle file from the forbidden PyCall.

Environment

#Install pycall
$ gem install pycall
#Incorporation of pyenv
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# .bash_Reuptake profile
$ source ~/.bash_profile
#python version check
$ python3 --version
3.7.3
#pyenv shared library installation
$ CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.3
#numpy installation
$ pip install numpy

Now ready

How to import

require 'pycall/import'
include PyCall::Import

pyimport :numpy
pyimport :pickle
pkl = open("sample_weight.pkl", "rb")
network = pickle.load(pkl) 

Remarks: What I was addicted to

I was angry that I just called pycall normally.

> require 'pycall/import'
true
> include PyCall::Import
Object
> hoge = PyCall.eval('0')
Traceback (most recent call last):
        9: from /usr/local/bin/irb:23:in `<main>'
        8: from /usr/local/bin/irb:23:in `load'
        7: from /usr/local/lib/ruby/gems/2.7.0/gems/irb-1.2.1/exe/irb:11:in `<top (required)>'
        6: from (irb):3
        5: from /usr/local/bundle/gems/pycall-1.3.0/lib/pycall.rb:39:in `eval'
        4: from /usr/local/bundle/gems/pycall-1.3.0/lib/pycall.rb:62:in `import_module'
        3: from /usr/local/bundle/gems/pycall-1.3.0/lib/pycall/init.rb:16:in `const_missing'
        2: from /usr/local/bundle/gems/pycall-1.3.0/lib/pycall/init.rb:35:in `init'
        1: from /usr/local/bundle/gems/pycall-1.3.0/lib/pycall/libpython/finder.rb:95:in `find_libpython'
PyCall::PythonNotFound (PyCall::PythonNotFound)

I was able to solve it by installing the shared library of pyenv referring to the following article

Reference article

Install pyenv and pyenv-virtualenv https://qiita.com/shigechioyo/items/198211e84f8e0e9a5c18 [Ruby] Machine learning ①: Introduction to Ruby https://qiita.com/chamao/items/cd62715c6be2fad2f8e7

Recommended Posts

Dare to learn with Ruby "Deep Learning from scratch" Importing pickle files from forbidden PyCall
Deep Learning from scratch ① Chapter 6 "Techniques related to learning"
Python vs Ruby "Deep Learning from scratch" Summary
Deep Learning from scratch
[Deep Learning from scratch] I tried to explain Dropout
"Deep Learning from scratch" Self-study memo (No. 16) I tried to build SimpleConvNet with Keras
"Deep Learning from scratch" Self-study memo (No. 17) I tried to build DeepConvNet with Keras
Deep Learning from scratch 1-3 chapters
Try to build a deep learning / neural network with scratch
Create an environment for "Deep Learning from scratch" with Docker
I tried to implement Perceptron Part 1 [Deep Learning from scratch]
Deep learning from scratch (cost calculation)
Deep Learning memos made from scratch
Realize environment construction for "Deep Learning from scratch" with docker and Vagrant
[Deep Learning from scratch] I tried to implement sigmoid layer and Relu layer.
Python vs Ruby "Deep Learning from scratch" Chapter 2 Logic circuit by Perceptron
[Deep Learning from scratch] Layer implementation from softmax function to cross entropy error
Python vs Ruby "Deep Learning from scratch" Chapter 4 Implementation of loss function
[Learning memo] Deep Learning made from scratch [Chapter 7]
Deep learning from scratch (forward propagation edition)
Deep learning / Deep learning made from scratch Chapter 6 Memo
[Learning memo] Deep Learning made from scratch [Chapter 5]
[Learning memo] Deep Learning made from scratch [Chapter 6]
Image alignment: from SIFT to deep learning
"Deep Learning from scratch" in Haskell (unfinished)
Deep learning / Deep learning made from scratch Chapter 7 Memo
[Windows 10] "Deep Learning from scratch" environment construction
Learning record of reading "Deep Learning from scratch"
[Deep Learning from scratch] About hyperparameter optimization
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Python vs Ruby "Deep Learning from scratch" Chapter 3 Implementation of 3-layer neural network
Prepare the environment for O'Reilly's book "Deep Learning from scratch" with apt-get (Debian 8)
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
I tried to extract a line art from an image with Deep Learning
Lua version Deep Learning from scratch Part 5.5 [Making pkl files available in Lua Torch]
"Deep Learning from scratch" self-study memo (unreadable glossary)
"Deep Learning from scratch" Self-study memo (9) MultiLayerNet class
Good book "Deep Learning from scratch" on GitHub
Deep Learning from scratch Chapter 2 Perceptron (reading memo)
[Learning memo] Deep Learning from scratch ~ Implementation of Dropout ~
Machine learning starting from scratch (machine learning learned with Kaggle)
Countermeasures for "Unable to get upper directory" error when using Deep Learning ② created from scratch with spyder of ANACONDA
"Deep Learning from scratch" Self-study memo (10) MultiLayerNet class
"Deep Learning from scratch" Self-study memo (No. 11) CNN
Python vs Ruby "Deep Learning from scratch" Chapter 3 Graph of step function, sigmoid function, ReLU function
Python vs Ruby "Deep Learning from scratch" Chapter 1 Graph of sin and cos functions
[Deep Learning from scratch] I implemented the Affine layer
"Deep Learning from scratch" Self-study memo (No. 19) Data Augmentation
"Deep Learning from scratch 2" Self-study memo (No. 21) Chapters 3 and 4
Application of Deep Learning 2 made from scratch Spam filter
Site summary to learn machine learning with English video
MVC --Model edition to learn from 0 with prejudice only
Create a machine learning environment from scratch with Winsows 10
A learning roadmap that allows you to develop and publish services from scratch with Python
[Deep Learning from scratch] I tried to explain the gradient confirmation in an easy-to-understand manner.
A memo when executing the deep learning sample code created from scratch with Google Colaboratory
"Deep Learning from scratch" Self-study memo (Part 8) I drew the graph in Chapter 6 with matplotlib