sjwhitworth / golearn

Machine Learning for Go
MIT License
9.26k stars 1.19k forks source link

How to read the multi-layer perceptrons model in Golang written using python #155

Open palaiya opened 7 years ago

palaiya commented 7 years ago

I am using the wrapper of scikit-learn Multilayer Perceptron in Python https://github.com/aigamedev/scikit-neuralnetwork to train the neural network and save it to a file. Now, I want to expose it on production to predict in real time. So, I was thinking to use Golang for better concurrency than Python. Hence, my question is whether can we read the model using this library written using Python or above wrapper? The code below I am using for training the model and last three lines I want to port to GOLang to expose it on production

import pickle
import numpy as np
import pandas as pd
from sknn.mlp import Classifier, Layer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

f = open("TrainLSDataset.csv")
data = np.loadtxt(f,delimiter = ',')

x = data[:, 1:]
y = data[:, 0]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

nn = Classifier(
    layers=[                    
        Layer("Rectifier", units=5),
        Layer("Softmax")],
    learning_rate=0.001,
    n_iter=100)

nn.fit(X_train, y_train)
filename = 'finalized_model.txt'
pickle.dump(nn, open(filename, 'wb'))

**#Below code i want to write in GoLang for exposing it on Production** :
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_test, y_test)
y_pred = loaded_model.predict(X_test)
Sentimentron commented 7 years ago

I would say that, at present, Golearn's neural API is not currently sufficient for your use-case because

  1. It's not vectorized, meaning that it will likely be slower than doing it in Python.
  2. It doesn't implement the concept of feed-forward layers efficiently.
  3. It doesn't offer the requisite per-node control over the activation function.

Basically, I wrote this bit back in 2014, when I knew significantly less about neural networks that I do now. I'd also say that provided you're deploying your Python server-side application behind something like gunicorn, you'll have most of the concurrency benefits anyway.

SCKelemen commented 7 years ago

@Sentimentron is it foreseeable that we will soon have a vectorized ML lib in Golang?

lastzero commented 5 years ago

@Sentimentron That means this is (and will be) just a simple implementation for experimenting with ML and there is no way to "port" existing models from other libs like Inception v3? Would be really nice to have a native Go lib for inference.