Skip to content

Commit

Permalink
Compile
Browse files Browse the repository at this point in the history
  • Loading branch information
pchavanne committed Mar 1, 2017
1 parent 6409990 commit d171709
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def convpool(input_var=None):
net.add(l_in)
net.add(l_rs)
net.add(l_cp)
net.add(l_fl)
net.add(l_out)

return net, hp
Expand Down
2 changes: 1 addition & 1 deletion examples/normalization_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@
model_BN.updates = yadll.updates.adagrad

# train the model and save it to file at each best
model_BN_report = model.train()
model_BN_report = model_BN.train()
120 changes: 120 additions & 0 deletions examples/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
"""
import logging

import numpy as np
import yadll
import examples.networks as networks

logging.basicConfig(level=logging.DEBUG, format='%(message)s')

_networks = [# 'logistic_regression',
# 'mlp',
# 'dropout',
# 'dropconnect',
'convpool',
'lenet5',
'autoencoder',
'denoising_autoencoder',
'gaussian_denoising_autoencoder',
'contractive_denoising_autoencoder',
'stacked_denoising_autoencoder',
'rbm',
'dbn',
'batch_normalization',
]

# load the data
data = yadll.data.Data(yadll.data.mnist_loader())


def build_network(network='Logistic_regression', input_var=None):
network_builder = getattr(networks, network)
return network_builder(input_var=input_var)


for network in _networks:
################################################
# construct the model
model = yadll.model.Model(name=network, data=data, file='best_model.ym')
# construct the network
net, hp = build_network(network)
# add the network to the model
model.network = net
# add the hyperparameters to the model
model.hp = hp
# updates method
model.updates = yadll.updates.momentum
# Compile model
model.compile('all')
# Saving configuration of the model. Model doesn't have to be trained
conf = model.to_conf() # get the configuration
model.to_conf('conf.yc') # or save it to file .yc by convention
# train the model
model.train(unsupervised_training=True)
# Saving network parameters after training
net.save_params('net_params.yp')

# We can test it on some examples from test
test_set_x = data.test_set_x.get_value()
test_set_y = data.test_set_y.get_value()

predicted_values = [np.argmax(prediction) for prediction in model.predict(test_set_x[:30])]
true_values = [np.argmax(true_value) for true_value in test_set_y[:30]]

print ("Model 1 Predicted & True values for the first 30 examples in test set:")
print predicted_values
print true_values

##########################################################################
# Loading model from file
model_2 = yadll.model.load_model('best_model.ym')
# model is ready to use we can make prediction directly.
# Watch out this not the proper way of saving models.
predicted_values_2 = [np.argmax(prediction) for prediction in model_2.predict(test_set_x[:30])]

print ("Model 2 Predicted & True values for the first 30 examples in test set:")
print predicted_values_2
print true_values
##########################################################################
# Recreate model and load parameters
model_3 = yadll.model.Model(data=data)
model_3.network = build_network(network)[0]
# Network as been re-created so parameters has just been initialized
# Let's try prediction with this network.
predicted_values_3 = [np.argmax(prediction) for prediction in model_3.predict(test_set_x[:30])]
print ("Model 3 without loading parameters values for the first 30 examples in test set:")
print predicted_values_3
print true_values
# Now let's load parameters
model_3.network.load_params('net_params.yp')
# And try predicting again
predicted_values_3 = [np.argmax(prediction) for prediction in model_3.predict(test_set_x[:30])]
print ("Model 3 after loading parameters values for the first 30 examples in test set:")
print predicted_values_3
print true_values

##########################################################################
# Reconstruction the model from configuration and load parameters
model_4 = yadll.model.Model()
model_4.from_conf(conf) # load from conf obj
model_5 = yadll.model.Model()
model_5.from_conf('conf.yc') # load from conf file

model_4.network.load_params('net_params.yp')
model_5.network.load_params('net_params.yp')

predicted_values_4 = [np.argmax(prediction) for prediction in model_4.predict(test_set_x[:30])]
print ("Model 4 after loading parameters values for the first 30 examples in test set:")
print predicted_values_4
print true_values

predicted_values_5 = [np.argmax(prediction) for prediction in model_5.predict(test_set_x[:30])]
print ("Model 5 after loading parameters values for the first 30 examples in test set:")
print predicted_values_5
print true_values



2 changes: 0 additions & 2 deletions yadll/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from .utils import shared_variable, np_rng
from .activations import *



# init_obj = glorot_uniform or init_obj = (glorot_uniform, {'gain':tanh, 'borrow':False})


Expand Down
19 changes: 13 additions & 6 deletions yadll/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def to_conf(self):
conf['input_layer'] = self.input_layer.name
return conf

@classmethod
def from_conf(cls, incoming_conf):
return cls(**incoming_conf)


class InputLayer(Layer):
"""
Expand Down Expand Up @@ -179,12 +183,14 @@ def get_output(self, **kwargs):
if self.reshape_shape[0] is None:
lst = list(self.reshape_shape)
lst[0] = T.shape(X)[0]
self.reshape_shape = tuple(lst)
return X.reshape(self.reshape_shape)
reshape_shape = tuple(lst)
return X.reshape(reshape_shape)
else:
return X.reshape(self.reshape_shape)

def to_conf(self):
conf = super(ReshapeLayer, self).to_conf()
conf['output_shape'] = self.output_shape
conf['output_shape'] = self.reshape_shape
return conf


Expand Down Expand Up @@ -448,7 +454,7 @@ def get_output(self, stochastic=False, **kwargs):
def to_conf(self):
conf = super(PoolLayer, self).to_conf()
conf['pool_size'] = self.pool_size
conf['pool_size'] = self.stride
conf['stride'] = self.stride
conf['ignore_border'] = self.ignore_border
conf['pad'] = self.pad
conf['mode'] = self.mode
Expand All @@ -464,7 +470,7 @@ class ConvLayer(Layer):
def __init__(self, incoming, image_shape=None, filter_shape=None, W=glorot_uniform,
border_mode='valid', subsample=(1, 1), l1=None, l2=None, pool_scale=None, **kwargs):
super(ConvLayer, self).__init__(incoming, **kwargs)
assert image_shape[1] == filter_shape[1]
# assert image_shape[1] == filter_shape[1]
self.image_shape = image_shape # (batch size, num input feature maps, image height, image width)
self.filter_shape = filter_shape # (number of filters, num input feature maps, filter height, filter width)
self.border_mode = border_mode # {'valid', 'full'}
Expand Down Expand Up @@ -544,7 +550,8 @@ def get_output(self, stochastic=False, **kwargs):
return self.activation(pool_X + self.b.dimshuffle('x', 0, 'x', 'x'))

def to_conf(self):
conf = super(ConvLayer, self).to_conf()
conf = super(ConvPoolLayer, self).to_conf()
conf.pop('pool_scale', None)
conf['activation'] = activation_to_conf(self.activation)
return conf

Expand Down
4 changes: 2 additions & 2 deletions yadll/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def from_conf(self, conf):
for layer_conf in layers_conf.items():
layer_class = getattr(yadll.layers, layer_conf[1]['type'])
if layer_class is yadll.layers.InputLayer:
layer = layer_class(**layer_conf[1])
layer = layer_class.from_conf(layer_conf[1])
else:
layer = layer_class(**dict({'incoming': self.layers[-1]}, **layer_conf[1])) # ony work for sequential nets
layer = layer_class.from_conf(dict({'incoming': self.layers[-1]}, **layer_conf[1])) # ony work for sequential nets
self.add(layer)

0 comments on commit d171709

Please sign in to comment.