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
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() rbias.randomize() obias.randomize() rec.recurrentConnection.randomize() net.addLayer(inp) net.addLayer(tgt)
from trainers.SGD import SGDTrainer from datasets.iris import * dataset, targets = load_iris_data() # Create a neural network net = NeuralNetwork() # Create layers input_layer = InputLayer(4) target_layer = InputLayer(3) hidden_layer = TanhLayer(5) output_layer = SoftmaxLayer(3) # Make the connection from input to output conn1 = FullConnection(input_layer.output, hidden_layer.input) bias1 = Bias(hidden_layer.input) conn2 = FullConnection(hidden_layer.output, output_layer.input) bias2 = Bias(output_layer.input) # Add the objective objective = CrossEntropyObjective(output_layer.output, target_layer.output) net.addLayer(input_layer) net.addLayer(target_layer) net.addConnection(conn1) net.addConnection(bias1) net.addLayer(hidden_layer) net.addConnection(conn2) net.addConnection(bias2) net.addLayer(output_layer)
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() rbias.randomize() obias.randomize() rec.recurrentConnection.randomize() net.addLayer(inp)
from nn.components.layers.RecurrentLayer import * from nn.components.layers.LinearLayer import * from nn.components.layers.InputLayer import * from nn.components.connections.FullConnection import * from nn.components.objectives.MSEObjective import * import numpy as np inp = InputLayer(1) hist = HistoryLayer(3, np.zeros((1,3))) conn = FullConnection(inp.output, hist.input) tgt = InputLayer(3) obj = MSEObjective(hist.output, tgt.output) conn.parameters[0,0] = -0.3 conn.parameters[0,1] = 0.5 conn.parameters[0,2] = -0.2 inp.setInput(np.array([[1]])) def print_state(): print " inp.output: ", inp.output.value print " conn.output: ", conn.getOutput() print " hist.output: ", hist.output.value print " obj.delta: ", obj.getDelta() print " hist.delta: ", hist.input.getDelta() print " Current History: " for i in range(len(hist.history)): print " ", hist.history[i] print
from nn.components.layers.RecurrentLayer import * from nn.components.layers.LinearLayer import * from nn.components.layers.InputLayer import * from nn.components.connections.FullConnection import * from nn.components.objectives.MSEObjective import * import numpy as np inp = InputLayer(1) hist = HistoryLayer(3, np.zeros((1, 3))) conn = FullConnection(inp.output, hist.input) tgt = InputLayer(3) obj = MSEObjective(hist.output, tgt.output) conn.parameters[0, 0] = -0.3 conn.parameters[0, 1] = 0.5 conn.parameters[0, 2] = -0.2 inp.setInput(np.array([[1]])) def print_state(): print " inp.output: ", inp.output.value print " conn.output: ", conn.getOutput() print " hist.output: ", hist.output.value print " obj.delta: ", obj.getDelta() print " hist.delta: ", hist.input.getDelta() print " Current History: " for i in range(len(hist.history)): print " ", hist.history[i] print