Ejemplo n.º 1
0
model = ChainList(L.Linear(784, 200), L.Linear(200, 100), L.Linear(100, 10))

# or load a serialized model
#f = open("./ae_2015-12-01_11-26-45.model")
#model = pickle.load(f)
#f.close()
#model.add_link(L.Linear(100,10))


def forward(self, x):
    h = F.relu(self.model[0](x))
    h = F.relu(self.model[1](h))
    return F.relu(self.model[2](h))


def output(self, y):
    y_trimed = y.data.argmax(axis=1)
    return np.array(y_trimed, dtype=np.int32)


NNManager.forward = forward
nn = NNManager(model,
               optimizers.Adam(),
               F.softmax_cross_entropy,
               epoch=100,
               batch_size=100,
               log_path="./log_training_" + utility.now() + ".csv")

nn.fit(X_train, y_train, X_test, y_test, is_classification=True)
nn.predict(X_test)
Ejemplo n.º 2
0
import chainer.functions as F
import chainer.links as L
from chainer import ChainList, optimizers
from sklearn.base import ClassifierMixin
from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_mldata

import sys,os.path
sys.path.append('../')
from zChainer import NNAutoEncoder, utility

mnist = fetch_mldata('MNIST original', data_home=".")
data = mnist.data.astype(np.float32)

encoder = ChainList(
    L.Linear(784, 200),
    L.Linear(200, 100))
decoder =ChainList(
    L.Linear(200, 784),
    L.Linear(100, 200))

# You can set your own forward function. Default is as below.
#def forward(self, x):
#    h = F.dropout(F.relu(self.model[0](x)))
#    return F.dropout(F.relu(self.model[1](h)))
#
#NNAutoEncoder.forward = forward
ae = NNAutoEncoder(encoder, decoder, optimizers.Adam(), epoch=100, batch_size=100,
    log_path="./ae_"+utility.now()+"_log.csv", export_path="./ae_"+utility.now()+".model")

ae.fit(data)
Ejemplo n.º 3
0
import pickle

import sys, os.path

sys.path.append('../')
from zChainer import NNManager, NNAutoEncoder, utility

data_size = 1000
X = np.linspace(-1, 1, data_size).reshape(data_size, 1).astype(np.float32)
y = np.array([2 * x * x - 1 for x in X], np.float32)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)

model = ChainList(L.Linear(1, 1024), L.Linear(1024, 1))


def forward(self, x):
    h = F.relu(self.model[0](x))
    return self.model[1](h)


NNManager.forward = forward
nn = NNManager(model,
               optimizers.Adam(),
               F.mean_squared_error,
               epoch=200,
               batch_size=100,
               log_path="./log_regression_" + utility.now() + "_log.csv")

nn.fit(X_train, y_train, X_test, y_test)
nn.predict(X_test)
Ejemplo n.º 4
0
import numpy as np
import chainer.functions as F
import chainer.links as L
from chainer import ChainList, optimizers
from sklearn.cross_validation import train_test_split
import pickle

import sys,os.path
sys.path.append('../')
from zChainer import NNManager, NNAutoEncoder, utility

data_size = 1000
X = np.linspace(-1,1,data_size).reshape(data_size, 1).astype(np.float32)
y = np.array([2*x*x-1 for x in X], np.float32)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)

model = ChainList(L.Linear(1, 1024), L.Linear(1024, 1))

def forward(self, x):
    h = F.relu(self.model[0](x))
    return self.model[1](h)

NNManager.forward = forward
nn = NNManager(model, optimizers.Adam(),
  F.mean_squared_error, epoch=200, batch_size=100,
  log_path="./log_regression_"+utility.now()+"_log.csv")

nn.fit(X_train, y_train, X_test, y_test)
nn.predict(X_test)