def run_mlp(): # # define the model layers # layer1 = Dense(input_size=784, output_size=1000, activation='rectifier') # layer2 = Dense(inputs_hook=(1000, layer1.get_outputs()), output_size=1000, activation='rectifier') # classlayer3 = SoftmaxLayer(inputs_hook=(1000, layer2.get_outputs()), output_size=10, out_as_probs=False) # # add the layers to the prototype # mlp = Prototype(layers=[layer1, layer2, classlayer3]) # test the new way to automatically fill in inputs_hook for models mlp = Prototype() mlp.add(Dense(input_size=784, output_size=1000, activation='rectifier', noise='dropout')) mlp.add(Dense(output_size=1500, activation='tanh', noise='dropout')) mlp.add(SoftmaxLayer(output_size=10)) mnist = MNIST() optimizer = AdaDelta(model=mlp, dataset=mnist, epochs=10) optimizer.train() test_data, test_labels = mnist.test_inputs, mnist.test_targets test_data = test_data[:25] test_labels = test_labels[:25] # use the run function! yhat = mlp.run(test_data) print('-------') print('Prediction: %s' % str(yhat)) print('Actual: %s' % str(test_labels.astype('int32')))
def build_model(): # add layers one-by-one to a Prototype container to build neural net # inputs_hook created automatically by Prototype; thus, no need to specify mlp = Prototype() mlp.add(BasicLayer(input_size=28*28, output_size=512, activation='rectifier', noise='dropout')) mlp.add(BasicLayer(output_size=512, activation='rectifier', noise='dropout')) mlp.add(SoftmaxLayer(output_size=10)) return mlp
def main(): # First, let's create a simple feedforward MLP with one hidden layer as a Prototype. mlp = Prototype() mlp.add( BasicLayer(input_size=28 * 28, output_size=1000, activation='rectifier', noise='dropout')) mlp.add(SoftmaxLayer(output_size=10)) # Now, we get to choose what values we want to monitor, and what datasets we would like to monitor on! # Each Model (in our case, the Prototype), has a get_monitors method that will return a useful # dictionary of {string_name: monitor_theano_expression} for various computations of the model we might # care about. By default, this method returns an empty dictionary - it was the model creator's job to # include potential monitor values. mlp_monitors = mlp.get_monitors() mlp_channel = MonitorsChannel(name="error") for name, expression in mlp_monitors.items(): mlp_channel.add( Monitor(name=name, expression=expression, train=True, valid=True, test=True)) # create some monitors for statistics about the hidden and output weights! # let's look at the mean, variance, and standard deviation of the weights matrices. weights_channel = MonitorsChannel(name="weights") hiddens_1 = mlp[0].get_params()[0] hiddens1_mean = T.mean(hiddens_1) weights_channel.add( Monitor(name="hiddens_mean", expression=hiddens1_mean, train=True)) hiddens_2 = mlp[1].get_params()[0] hiddens2_mean = T.mean(hiddens_2) weights_channel.add( Monitor(name="out_mean", expression=hiddens2_mean, train=True)) # create our plot object to do live plotting! plot = Plot(bokeh_doc_name="Monitor Tutorial", monitor_channels=[mlp_channel, weights_channel], open_browser=True) # use SGD optimizer optimizer = SGD(model=mlp, dataset=MNIST(concat_train_valid=False), n_epoch=500, save_frequency=100, batch_size=600, learning_rate=.01, lr_decay=False, momentum=.9, nesterov_momentum=True) # train, with the plot! optimizer.train(plot=plot)
def add_list_layers(): # You can also add lists of layers at a time (or as initialization) to a Prototype! This lets you specify # more complex interactions between layers! hidden1 = BasicLayer(input_size=28 * 28, output_size=512, activation='rectifier', noise='dropout') hidden2 = BasicLayer(inputs_hook=(512, hidden1.get_outputs()), output_size=512, activation='rectifier', noise='dropout') class_layer = SoftmaxLayer(inputs_hook=(512, hidden2.get_outputs()), output_size=10) mlp = Prototype([hidden1, hidden2, class_layer]) return mlp
def sequential_add_layers(): # This method is to demonstrate adding layers one-by-one to a Prototype container. # As you can see, inputs_hook are created automatically by Prototype so we don't need to specify! mlp = Prototype() mlp.add( BasicLayer(input_size=28 * 28, output_size=1000, activation='rectifier', noise='dropout', noise_level=0.5)) mlp.add( BasicLayer(output_size=512, activation='rectifier', noise='dropout', noise_level=0.5)) mlp.add(SoftmaxLayer(output_size=10)) return mlp
def create_mlp(): # define the model layers relu_layer1 = BasicLayer(input_size=784, output_size=1000, activation='rectifier') relu_layer2 = BasicLayer(inputs_hook=(1000, relu_layer1.get_outputs()), output_size=1000, activation='rectifier') class_layer3 = SoftmaxLayer(inputs_hook=(1000, relu_layer2.get_outputs()), output_size=10, out_as_probs=False) # add the layers as a Prototype mlp = Prototype(layers=[relu_layer1, relu_layer2, class_layer3]) mnist = MNIST() optimizer = AdaDelta(model=mlp, dataset=mnist, n_epoch=20) optimizer.train() test_data, test_labels = mnist.getSubset(TEST) test_data = test_data[:25].eval() test_labels = test_labels[:25].eval() # use the run function! preds = mlp.run(test_data) log.info('-------') log.info("predicted: %s",str(preds)) log.info("actual: %s",str(test_labels.astype('int32')))
def _build_computation_graph(self): ###################### BUILD NETWORK ########################## # whether or not to mirror the input images before feeding them into the network if self.flag_datalayer: layer_1_input = mirror_images(input=self.x, image_shape=(self.batch_size, 3, 256, 256), # bc01 format cropsize=227, rand=self.rand, flag_rand=self.rand_crop) else: layer_1_input = self.x # 4D tensor (going to be in bc01 format) # Start with 5 convolutional pooling layers log.debug("convpool layer 1...") convpool_layer1 = ConvPoolLayer(inputs_hook=((self.batch_size, 3, 227, 227), layer_1_input), filter_shape=(96, 3, 11, 11), convstride=4, padsize=0, group=1, poolsize=3, poolstride=2, bias_init=0.0, local_response_normalization=True) # Add this layer's parameters! self.params += convpool_layer1.get_params() log.debug("convpool layer 2...") convpool_layer2 = ConvPoolLayer(inputs_hook=((self.batch_size, 96, 27, 27, ), convpool_layer1.get_outputs()), filter_shape=(256, 96, 5, 5), convstride=1, padsize=2, group=2, poolsize=3, poolstride=2, bias_init=0.1, local_response_normalization=True) # Add this layer's parameters! self.params += convpool_layer2.get_params() log.debug("convpool layer 3...") convpool_layer3 = ConvPoolLayer(inputs_hook=((self.batch_size, 256, 13, 13), convpool_layer2.get_outputs()), filter_shape=(384, 256, 3, 3), convstride=1, padsize=1, group=1, poolsize=1, poolstride=0, bias_init=0.0, local_response_normalization=False) # Add this layer's parameters! self.params += convpool_layer3.get_params() log.debug("convpool layer 4...") convpool_layer4 = ConvPoolLayer(inputs_hook=((self.batch_size, 384, 13, 13), convpool_layer3.get_outputs()), filter_shape=(384, 384, 3, 3), convstride=1, padsize=1, group=2, poolsize=1, poolstride=0, bias_init=0.1, local_response_normalization=False) # Add this layer's parameters! self.params += convpool_layer4.get_params() log.debug("convpool layer 5...") convpool_layer5 = ConvPoolLayer(inputs_hook=((self.batch_size, 384, 13, 13), convpool_layer4.get_outputs()), filter_shape=(256, 384, 3, 3), convstride=1, padsize=1, group=2, poolsize=3, poolstride=2, bias_init=0.0, local_response_normalization=False) # Add this layer's parameters! self.params += convpool_layer5.get_params() # Now onto the fully-connected layers! fc_config = { 'activation': 'rectifier', # type of activation function to use for output 'weights_init': 'gaussian', # either 'gaussian' or 'uniform' - how to initialize weights 'weights_mean': 0.0, # mean for gaussian weights init 'weights_std': 0.005, # standard deviation for gaussian weights init 'bias_init': 0.0 # how to initialize the bias parameter } log.debug("fully connected layer 1 (model layer 6)...") # we want to have dropout applied to the training version, but not the test version. fc_layer6_input = T.flatten(convpool_layer5.get_outputs(), 2) fc_layer6 = BasicLayer(inputs_hook=(9216, fc_layer6_input), output_size=4096, noise='dropout', noise_level=0.5, **fc_config) # Add this layer's parameters! self.params += fc_layer6.get_params() # Add the dropout noise switch self.noise_switches += fc_layer6.get_noise_switch() log.debug("fully connected layer 2 (model layer 7)...") fc_layer7 = BasicLayer(inputs_hook=(4096, fc_layer6.get_outputs()), output_size=4096, noise='dropout', noise_level=0.5, **fc_config) # Add this layer's parameters! self.params += fc_layer7.get_params() # Add the dropout noise switch self.noise_switches += fc_layer7.get_noise_switch() # last layer is a softmax prediction output layer softmax_config = { 'weights_init': 'gaussian', 'weights_mean': 0.0, 'weights_std': 0.005, 'bias_init': 0.0 } log.debug("softmax classification layer (model layer 8)...") softmax_layer8 = SoftmaxLayer(inputs_hook=(4096, fc_layer7.get_outputs()), output_size=1000, **softmax_config) # Add this layer's parameters! self.params += softmax_layer8.get_params() # finally the softmax output from the whole thing! self.output = softmax_layer8.get_outputs() self.targets = softmax_layer8.get_targets() ##################### # Cost and monitors # ##################### self.train_cost = softmax_layer8.negative_log_likelihood() cost = softmax_layer8.negative_log_likelihood() errors = softmax_layer8.errors() train_errors = softmax_layer8.errors() self.monitors = OrderedDict([('cost', cost), ('errors', errors), ('dropout_errors', train_errors)]) ######################### # Compile the functions # ######################### log.debug("Compiling functions!") t = time.time() log.debug("f_run...") # use the actual argmax from the classification self.f_run = function(inputs=[self.x], outputs=softmax_layer8.get_argmax_prediction()) log.debug("compilation took %s", make_time_units_string(time.time() - t))
from opendeep.data.standard_datasets.image.mnist import MNIST from opendeep.optimization.adadelta import AdaDelta if __name__ == '__main__': # set up the logging environment to display outputs (optional) # although this is recommended over print statements everywhere import logging from opendeep.log import config_root_logger config_root_logger() log = logging.getLogger(__name__) log.info("Creating softmax!") # grab the MNIST dataset mnist = MNIST() # create the softmax classifier s = SoftmaxLayer(input_size=28 * 28, output_size=10, out_as_probs=False) # make an optimizer to train it (AdaDelta is a good default) optimizer = AdaDelta(model=s, dataset=mnist, epochs=20) # perform training! optimizer.train() # test it on some images! test_data, test_labels = mnist.test_inputs[:25], mnist.test_targets[:25] # use the run function! preds = s.run(test_data) print('-------') print(preds) print(test_labels.astype('int32')) print() print() del mnist del s
from opendeep.optimization.adadelta import AdaDelta if __name__ == '__main__': # set up the logging environment to display outputs (optional) # although this is recommended over print statements everywhere import logging import opendeep.log.logger as logger logger.config_root_logger() log = logging.getLogger(__name__) log.info("Creating softmax!") # grab the MNIST dataset mnist = MNIST() # create the softmax classifier s = SoftmaxLayer(input_size=28 * 28, output_size=10, out_as_probs=False) # make an optimizer to train it (AdaDelta is a good default) optimizer = AdaDelta(model=s, dataset=mnist, n_epoch=20) # perform training! optimizer.train() # test it on some images! test_data = mnist.getDataByIndices(indices=range(25), subset=TEST) # use the predict function! preds = s.predict(test_data) print '-------' print preds print mnist.getLabelsByIndices(indices=range(25), subset=TEST) print print del mnist del s
from opendeep.optimization.adadelta import AdaDelta if __name__ == '__main__': # set up the logging environment to display outputs (optional) # although this is recommended over print statements everywhere import logging from opendeep.log import config_root_logger config_root_logger() log = logging.getLogger(__name__) log.info("Creating softmax!") # grab the MNIST dataset mnist = MNIST() # create the softmax classifier s = SoftmaxLayer(input_size=28 * 28, output_size=10, out_as_probs=False) # make an optimizer to train it (AdaDelta is a good default) optimizer = AdaDelta(model=s, dataset=mnist, epochs=20) # perform training! optimizer.train() # test it on some images! test_data, test_labels = mnist.test_inputs[:25], mnist.test_targets[:25] # use the run function! preds = s.run(test_data) print('-------') print(preds) print(test_labels.astype('int32')) print() print() del mnist del s
def _build_computation_graph(self): ###################### BUILD NETWORK ########################## # whether or not to mirror the input images before feeding them into the network if self.flag_datalayer: layer_1_input = mirror_images( input=self.x, image_shape=(self.batch_size, 3, 256, 256), # bc01 format cropsize=227, rand=self.rand, flag_rand=self.rand_crop) else: layer_1_input = self.x # 4D tensor (going to be in bc01 format) # Start with 5 convolutional pooling layers log.debug("convpool layer 1...") convpool_layer1 = ConvPoolLayer(inputs_hook=((self.batch_size, 3, 227, 227), layer_1_input), filter_shape=(96, 3, 11, 11), convstride=4, padsize=0, group=1, poolsize=3, poolstride=2, bias_init=0.0, local_response_normalization=True) # Add this layer's parameters! self.params += convpool_layer1.get_params() log.debug("convpool layer 2...") convpool_layer2 = ConvPoolLayer(inputs_hook=(( self.batch_size, 96, 27, 27, ), convpool_layer1.get_outputs()), filter_shape=(256, 96, 5, 5), convstride=1, padsize=2, group=2, poolsize=3, poolstride=2, bias_init=0.1, local_response_normalization=True) # Add this layer's parameters! self.params += convpool_layer2.get_params() log.debug("convpool layer 3...") convpool_layer3 = ConvPoolLayer( inputs_hook=((self.batch_size, 256, 13, 13), convpool_layer2.get_outputs()), filter_shape=(384, 256, 3, 3), convstride=1, padsize=1, group=1, poolsize=1, poolstride=0, bias_init=0.0, local_response_normalization=False) # Add this layer's parameters! self.params += convpool_layer3.get_params() log.debug("convpool layer 4...") convpool_layer4 = ConvPoolLayer( inputs_hook=((self.batch_size, 384, 13, 13), convpool_layer3.get_outputs()), filter_shape=(384, 384, 3, 3), convstride=1, padsize=1, group=2, poolsize=1, poolstride=0, bias_init=0.1, local_response_normalization=False) # Add this layer's parameters! self.params += convpool_layer4.get_params() log.debug("convpool layer 5...") convpool_layer5 = ConvPoolLayer( inputs_hook=((self.batch_size, 384, 13, 13), convpool_layer4.get_outputs()), filter_shape=(256, 384, 3, 3), convstride=1, padsize=1, group=2, poolsize=3, poolstride=2, bias_init=0.0, local_response_normalization=False) # Add this layer's parameters! self.params += convpool_layer5.get_params() # Now onto the fully-connected layers! fc_config = { 'activation': 'rectifier', # type of activation function to use for output 'weights_init': 'gaussian', # either 'gaussian' or 'uniform' - how to initialize weights 'weights_mean': 0.0, # mean for gaussian weights init 'weights_std': 0.005, # standard deviation for gaussian weights init 'bias_init': 0.0 # how to initialize the bias parameter } log.debug("fully connected layer 1 (model layer 6)...") # we want to have dropout applied to the training version, but not the test version. fc_layer6_input = T.flatten(convpool_layer5.get_outputs(), 2) fc_layer6 = BasicLayer(inputs_hook=(9216, fc_layer6_input), output_size=4096, noise='dropout', noise_level=0.5, **fc_config) # Add this layer's parameters! self.params += fc_layer6.get_params() # Add the dropout noise switch self.noise_switches += fc_layer6.get_noise_switch() log.debug("fully connected layer 2 (model layer 7)...") fc_layer7 = BasicLayer(inputs_hook=(4096, fc_layer6.get_outputs()), output_size=4096, noise='dropout', noise_level=0.5, **fc_config) # Add this layer's parameters! self.params += fc_layer7.get_params() # Add the dropout noise switch self.noise_switches += fc_layer7.get_noise_switch() # last layer is a softmax prediction output layer softmax_config = { 'weights_init': 'gaussian', 'weights_mean': 0.0, 'weights_std': 0.005, 'bias_init': 0.0 } log.debug("softmax classification layer (model layer 8)...") softmax_layer8 = SoftmaxLayer(inputs_hook=(4096, fc_layer7.get_outputs()), output_size=1000, **softmax_config) # Add this layer's parameters! self.params += softmax_layer8.get_params() # finally the softmax output from the whole thing! self.output = softmax_layer8.get_outputs() self.targets = softmax_layer8.get_targets() ##################### # Cost and monitors # ##################### self.train_cost = softmax_layer8.negative_log_likelihood() cost = softmax_layer8.negative_log_likelihood() errors = softmax_layer8.errors() train_errors = softmax_layer8.errors() self.monitors = OrderedDict([('cost', cost), ('errors', errors), ('dropout_errors', train_errors)]) ######################### # Compile the functions # ######################### log.debug("Compiling functions!") t = time.time() log.debug("f_run...") # use the actual argmax from the classification self.f_run = function(inputs=[self.x], outputs=softmax_layer8.get_argmax_prediction()) log.debug("compilation took %s", make_time_units_string(time.time() - t))
# although this is recommended over print statements everywhere import logging import opendeep.log.logger as logger logger.config_root_logger() log = logging.getLogger(__name__) log.info("Creating MLP!") # grab the MNIST dataset mnist = MNIST() # create the basic layer layer1 = BasicLayer(input_size=28 * 28, output_size=1000, activation='relu') # create the softmax classifier layer2 = SoftmaxLayer(inputs_hook=(1000, layer1.get_outputs()), output_size=10, out_as_probs=False) # create the mlp from the two layers mlp = Prototype(layers=[layer1, layer2]) # make an optimizer to train it (AdaDelta is a good default) optimizer = AdaDelta(model=mlp, dataset=mnist, n_epoch=20) # perform training! optimizer.train() # test it on some images! test_data, test_labels = mnist.getSubset(subset=TEST) test_data = test_data[:25].eval() test_labels = test_labels[:25].eval() # use the run function! preds = mlp.run(test_data) print('-------') print(preds)