from nn.components.layers.RecurrentLayer import * from nn.components.layers.SigmoidLayer import * from nn.components.layers.InputLayer import * from nn.components.connections.FullConnection import * from nn.components.connections.Bias import * from nn.components.objectives.MSEObjective import * from nn.models.RecurrentNeuralNetwork import * from trainers.SGD import SGDTrainer import numpy as np # Build a network net = RecurrentNeuralNetwork() inp = InputLayer(1) rec = RecurrentLayer(5) out = SigmoidLayer(1) rec.zeroInitialHistoryBatch(2) i2r = FullConnection(inp.output, rec.input) r2o = FullConnection(rec.output, out.input) rbias = Bias(rec.input) obias = Bias(out.input) tgt = InputLayer(1) obj = MSEObjective(out.output, tgt.output) i2r.randomize()
from nn.components.layers.RecurrentLayer import * from nn.components.layers.SigmoidLayer import * from nn.components.layers.InputLayer import * from nn.components.connections.FullConnection import * from nn.components.connections.Bias import * from nn.components.objectives.MSEObjective import * from nn.models.RecurrentNeuralNetwork import * from trainers.SGD import SGDTrainer import numpy as np # Build a network net = RecurrentNeuralNetwork() inp = InputLayer(1) rec = RecurrentLayer(5) out = SigmoidLayer(1) rec.zeroInitialHistoryBatch(2) i2r = FullConnection(inp.output, rec.input) r2o = FullConnection(rec.output, out.input) rbias = Bias(rec.input) obias = Bias(out.input) tgt = InputLayer(1) obj = MSEObjective(out.output, tgt.output) i2r.randomize() r2o.randomize()
def buildNetwork(rnn_size): """ Build an RNN with softmax output """ net = RecurrentNeuralNetwork() input_layer = InputLayer(27) # Input is 27 unit vector recurrent_layer = RecurrentLayer(rnn_size) output_layer = SoftmaxLayer(34) input_to_recurrent = FullConnection(input_layer.output, recurrent_layer.input) recurrent_to_output = FullConnection(recurrent_layer.output, output_layer.input) recurrent_bias = Bias(recurrent_layer.input) output_bias = Bias(output_layer.input) target_layer = InputLayer(34) # 34 classes! objective_layer = CrossEntropyObjective(output_layer.output, target_layer.output) net.addLayer(input_layer) net.addLayer(target_layer) net.addConnection(input_to_recurrent) net.addConnection(recurrent_bias) net.addRecurrentLayer(recurrent_layer) net.addConnection(recurrent_to_output) net.addConnection(output_bias) net.addLayer(output_layer) net.addObjective(objective_layer) net.setInputLayer(input_layer) net.setTargetLayer(target_layer) net.setOutputLayer(output_layer) net.setObjective(objective_layer) net.randomize() return net