Skip to content

Commit

Permalink
Added chainer_sda test data validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
ktnyt committed Sep 21, 2015
1 parent 5237056 commit f307ba7
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 6 deletions.
18 changes: 18 additions & 0 deletions python/brica1/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ def output(self, time):
assert self.last_output_time <= time, "update_output() captured a time travel"
self.last_output_time = time

def reset(self):
""" Reset the component state
This method resets the internal time of the component for reuse.
Args:
None.
Returns:
None.
"""

self.last_input_time = 0.0
self.last_output_time = 0.0
self.offset = 0.0
self.interval = 1.0

class ComponentSet(Component):
"""
`ComponentSet` groups components to fire sequentially
Expand Down
62 changes: 57 additions & 5 deletions python/examples/chainer/chainer_sda.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from chainer import Variable, FunctionSet, optimizers, cuda
import chainer.functions as F
import data
import cPickle as pickle

import brica1

Expand Down Expand Up @@ -52,6 +53,7 @@ def __init__(self, n_input, n_output, use_gpu=False):
super(SLPComponent, self).__init__()
self.model = SLP(n_input, n_output)
self.optimizer = optimizers.Adam()
self.training = True

self.make_in_port("input", n_input)
self.make_in_port("target", 1)
Expand All @@ -66,6 +68,21 @@ def __init__(self, n_input, n_output, use_gpu=False):

self.optimizer.setup(self.model)

def to_cpu(self):
if self.use_gpu:
self.model.to_cpu()
self.optimizer.setup(self.model)
self.use_gpu = False

def to_gpu(self):
if not self.use_gpu:
self.model.to_gpu()
self.optimizer.setup(self.model)
self.use_gpu = True

def set_training(self, flag):
self.training = flag;

def fire(self):
x_data = self.inputs["input"].astype(np.float32)
t_data = self.inputs["target"].astype(np.int32)
Expand All @@ -76,8 +93,9 @@ def fire(self):

self.optimizer.zero_grads()
loss, accuracy = self.model.forward(x_data, t_data)
loss.backward()
self.optimizer.update()
if self.training:
loss.backward()
self.optimizer.update()

y_data = self.model.predict(x_data)

Expand All @@ -90,6 +108,7 @@ def __init__(self, n_input, n_output, use_gpu=False):
super(AutoencoderComponent, self).__init__()
self.model = Autoencoder(n_input, n_output)
self.optimizer = optimizers.Adam()
self.training = True

self.make_in_port("input", n_input)
self.make_out_port("output", n_output)
Expand All @@ -102,16 +121,33 @@ def __init__(self, n_input, n_output, use_gpu=False):

self.optimizer.setup(self.model)

def to_cpu(self):
if self.use_gpu:
self.model.to_cpu()
self.optimizer.setup(self.model)
self.use_gpu = False

def to_gpu(self):
if not self.use_gpu:
self.model.to_gpu()
self.optimizer.setup(self.model)
self.use_gpu = True

def set_training(self, flag):
self.training = flag;

def fire(self):
x_data = self.inputs["input"].astype(np.float32)

if self.use_gpu:
x_data = cuda.to_gpu(x_data)


self.optimizer.zero_grads()
loss = self.model.forward(x_data)
loss.backward()
self.optimizer.update()
if self.training:
loss.backward()
self.optimizer.update()

y_data = self.model.encode(x_data)

Expand All @@ -125,7 +161,8 @@ def fire(self):
args = parser.parse_args()

use_gpu = False
if args.gpu >= 0:
if args.gpu >= 0:
print "Using gpu: {}".format(args.gpu)
use_gpu = True
cuda.get_device(args.gpu).use()

Expand Down Expand Up @@ -219,3 +256,18 @@ def fire(self):
mean_accuracy = sum_accuracy / N_train

print "Epoch: {}\tLoss1: {}\tLoss2: {}\tLoss3: {}\tLoss4: {}\tAccuracy: {}".format(epoch+1, mean_loss1, mean_loss2, mean_loss3, mean_loss4, mean_accuracy)

autoencoder1.reset()
autoencoder2.reset()
autoencoder3.reset()
slp.reset()
stacked_autoencoder.reset()

autoencoder1.set_training(False)
autoencoder2.set_training(False)
autoencoder3.set_training(False)
slp.set_training(False)

f = open('sda.pkl', 'wb')
pickle.dump(stacked_autoencoder, f)
f.close()
3 changes: 2 additions & 1 deletion python/examples/chainer/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
if [ ! -e data.py ]; then
wget https://raw.githubusercontent.com/pfnet/chainer/master/examples/mnist/data.py
fi
python chainer_sda.py --gpu 0
python chainer_sda.py $@
python validate_sda.py $@
81 changes: 81 additions & 0 deletions python/examples/chainer/validate_sda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
import argparse
import numpy as np
from chainer import Variable, FunctionSet, optimizers, cuda
import chainer.functions as F
import data
import cPickle as pickle

import brica1
from chainer_sda import *

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Chainer-BriCA integration")
parser.add_argument("--gpu", "-g", default=-1, type=int, help="GPU ID")

args = parser.parse_args()

use_gpu = False
if args.gpu >= 0:
print "Using gpu: {}".format(args.gpu)
use_gpu = True
cuda.get_device(args.gpu).use()

batchsize = 100

mnist = data.load_mnist_data()
mnist['data'] = mnist['data'].astype(np.float32)
mnist['data'] /= 255
mnist['target'] = mnist['target'].astype(np.int32)

N_train = 60000
x_train, x_test = np.split(mnist['data'], [N_train])
y_train, y_test = np.split(mnist['target'], [N_train])
N_test = y_test.size

f = open('sda.pkl', 'rb')
stacked_autoencoder = pickle.load(f)
f.close()

scheduler = brica1.VirtualTimeSyncScheduler()
agent = brica1.Agent(scheduler)
module = brica1.Module()
module.add_component("stacked_autoencoder", stacked_autoencoder)
agent.add_submodule("module", module)

time = 0.0

sum_loss1 = 0
sum_loss2 = 0
sum_loss3 = 0
sum_loss4 = 0
sum_accuracy = 0

for batchnum in xrange(0, N_test, batchsize):
x_batch = x_test[batchnum:batchnum+batchsize]
y_batch = y_test[batchnum:batchnum+batchsize]

stacked_autoencoder.get_in_port("input").buffer = x_batch
stacked_autoencoder.get_in_port("target").buffer = y_batch

time = agent.step()

loss1 = stacked_autoencoder.get_out_port("loss1").buffer
loss2 = stacked_autoencoder.get_out_port("loss2").buffer
loss3 = stacked_autoencoder.get_out_port("loss3").buffer
loss4 = stacked_autoencoder.get_out_port("loss4").buffer
accuracy = stacked_autoencoder.get_out_port("accuracy").buffer

sum_loss1 += loss1 * batchsize
sum_loss2 += loss2 * batchsize
sum_loss3 += loss3 * batchsize
sum_loss4 += loss4 * batchsize
sum_accuracy += accuracy * batchsize

mean_loss1 = sum_loss1 / N_test
mean_loss2 = sum_loss2 / N_test
mean_loss3 = sum_loss3 / N_test
mean_loss4 = sum_loss3 / N_test
mean_accuracy = sum_accuracy / N_test

print "Validation\tLoss1: {}\tLoss2: {}\tLoss3: {}\tLoss4: {}\tAccuracy: {}".format(mean_loss1, mean_loss2, mean_loss3, mean_loss4, mean_accuracy)

0 comments on commit f307ba7

Please sign in to comment.