def load_separate_data(sAttrFilename, sClassFilename, cInstances):
    """Load cInstances attribute instances from sAttrFilename and 
    class instances from sClassFilename (two separate files), 
    and combine them into Instances"""
    listClass = parse_input(open(sClassFilename, "r"), int, cInstances)
    listAttr = parse_input(open(sAttrFilename, "r"), float, cInstances)
    return map(lambda cl, attr: nn.Instance(cl[-1], attr), listClass, listAttr)
Esempio n. 2
0
    def test_update_net(self):
        # Test a simple network to make sure that forward and backprop are
        # working properly.
        # 2 inputs, 2 hidden nodes, 1 output node
        net = nn.init_net([2, 2, 1])
        # Set the weights on the first hidden node to 0.1, the weights on the
        # second hidden node to -0.1.
        def init_weights(p, input_weight, w0):
          for i in xrange(len(p.listDblW)):
            p.listDblW[i] = input_weight
          p.dblW0 = w0
        # Weights for the first hidden node to be 0.1
        init_weights(net.listLayer[0].listPcpt[0], 0.1, 0.0)
        # Weights for the second hidden node to be -0.1
        init_weights(net.listLayer[0].listPcpt[1], -0.1, 0.0)
        # Weights for the output layer to be 0.1
        init_weights(net.listLayer[1].listPcpt[0], 1.0, 0.0)
        # Inputs are 1 and -1
        inst = nn.Instance(0, [ 1.0, -0.9 ])
        # Target output is 0.5
        targets = [ 0.5 ]
        # The output of hidden unit 1 will be 1 / (1 + e^(-0.01))
        # The output of hidden unit 2 will be 1 / (1 + e^(0.01))
        # The inputs to the output unit will be 1.0, leading to output 0.731
        # The error at the output will be -0.231
        nn.update_net(net, inst, 1.0, targets)
        def get_weight(layer_id, perceptron_id, input_id):
          if input_id == -1:
            return net.listLayer[layer_id].listPcpt[perceptron_id].dblW0
          return net.listLayer[layer_id].listPcpt[perceptron_id].listDblW[input_id]

        output = 1.0 / (1.0 + math.exp(-1.0))
        delta_out = (0.5 - output) * output * (1 - output)
        h1_out = 1.0 / (1.0 + math.exp(-.01))
        h2_out = 1.0 / (1.0 + math.exp(.01))
        self.assertAlmostEqual(1.0 + 1.0 * h1_out * delta_out, get_weight(1, 0, 0))
        self.assertAlmostEqual(1.0 + 1.0 * h2_out * delta_out, get_weight(1, 0, 1))
        self.assertAlmostEqual(1.0 * 1.0 * delta_out, get_weight(1, 0, -1))

        in1 = 1.0 / (1.0 + math.exp(-0.01))
        delta_hidden1 = in1 * (1 - in1) * delta_out
        # For the hidden units, in is 0, so no weight updates should occur.
        self.assertAlmostEqual(0.1 + 1.0 * delta_hidden1, get_weight(0, 0, 0))
        self.assertAlmostEqual(0.1 - 0.9 * delta_hidden1, get_weight(0, 0, 1))
        self.assertAlmostEqual(delta_hidden1, get_weight(0, 0, -1))

        in2 = 1.0 / (1.0 + math.exp(0.01))
        delta_hidden2 = in2 * (1 - in2) * delta_out
        self.assertAlmostEqual(-0.1 + 1.0 * delta_hidden2,
                               get_weight(0, 1, 0))
        self.assertAlmostEqual(-0.1 - 0.9 * delta_hidden2,
                               get_weight(0, 1, 1))
        self.assertAlmostEqual(delta_hidden2, get_weight(0, 0, -1))
Esempio n. 3
0
def learn_nn_classifier(listCLayerSize, lstTrainBoostInst, fxnEncode, fxnDecode):
    """Given the test data set of BoostInstances, learns and 
    returns the learned neural net.
    net = nn.init_net([2,2,1])
    learn_nn_classifier(net, XOR_INSTANCES, nn.binary_encode_label, 
                        nn.binary_decode_net_output)
    Learning rate: 1.923447Round 200 complete.  Training Accuracy: 0.250000
    Learning rate: 1.905125Round 250 complete.  Training Accuracy: 0.750000
    Learning rate: 1.887149Round 300 complete.  Training Accuracy: 0.750000
    Learning rate: 1.869508Round 350 complete.  Training Accuracy: 0.750000
    Learning rate: 1.852195Round 400 complete.  Training Accuracy: 1.000000
    Out[99]: <nn.NeuralNet at 0x9504950>
    """
    # learn through rounds number of epochs
    rounds = 500
    interval = 50
    stopRound = 350
    iInst = len(lstTrainBoostInst)
    net = nn.init_net(listCLayerSize)
    
    for ixRound in xrange(rounds):
        dblAlpha = 2.0*rounds/(ixRound + rounds)
        
        # learn through one epoch and compute the error
        errors = 0
        for boostInst in lstTrainBoostInst:
            inst = nn.Instance(boostInst.iLabel, boostInst.listAttrs)
            listDblOut = nn.update_net(net, inst, dblAlpha, fxnEncode(inst.iLabel))
            iGuess = fxnDecode(listDblOut)
            if iGuess != inst.iLabel:
              errors += 1
          
        # print result after an interval of rounds
        if not((ixRound+1) % interval):
            sys.stderr.write('Learning rate: %f ' % dblAlpha)
            sys.stderr.write("Epoch: %d Training Accuracy: %f \n" % (ixRound + 1,
            1 - errors * 1.0 / iInst))
            
        # implement a stopping condition.
        if (ixRound+1) == stopRound:
            return net
    return net
    Out: (0.705, 0.7373333333333333)"""
    for _ in xrange(cRounds):
        for inst in listInstTrain:
            listDblTarget = fxnEncode(inst.iLabel)
            nn.update_net(net, inst, dblLearningRate, listDblTarget)
        dblTestError = evaluate_net(net, listInstTest, fxnDecode)
        dblTrainingError = evaluate_net(net, listInstTrain, fxnDecode)
        return dblTestError, dblTrainingError


#++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++ XOR example/testing +++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++

XOR_INSTANCES = [
    nn.Instance(0.1, [-1.0, -1.0]),
    nn.Instance(0.9, [-1.0, 1.0]),
    nn.Instance(0.9, [1.0, -1.0]),
    nn.Instance(0.1, [1.0, 1.0])
]


def build_xor_net():
    HIDDEN_NODES = 2
    ROUNDS = 5000
    assert XOR_INSTANCES
    net = nn.init_net([2, HIDDEN_NODES, 1], 0.001)
    for ixRound in xrange(ROUNDS):
        dblAlpha = 2.0 * ROUNDS / (ixRound + ROUNDS)
        for inst in XOR_INSTANCES:
            nn.update_net(net, inst, dblAlpha, [inst.iLabel])
Esempio n. 5
0
#!/usr/bin/env python

"""
tasknn.py -- Visualizations for neural networks.
"""

from os import path
import random

from tfutils import tftask

import nn

XOR_INSTANCES = [nn.Instance(0.1, [-1.0,-1.0]), nn.Instance(0.9, [-1.0,1.0]),
                 nn.Instance(0.9, [1.0,-1.0]), nn.Instance(0.1, [1.0,1.0])]

def build_xor_net():
    HIDDEN_NODES = 2
    ROUNDS = 5000
    LEARNING_RATE = 0.35
    assert XOR_INSTANCES
    net = nn.init_net([2, HIDDEN_NODES, 1], 0.001)
    for ixRound in xrange(ROUNDS):
        dblAlpha = 2.0*ROUNDS/(ixRound + ROUNDS)
        for inst in XOR_INSTANCES:
            nn.update_net(net, inst, dblAlpha, [inst.iLabel])
    return net

def serialize_net(net):
    def build_edge(sLabelIn, sLabelOut, dblWeight):
        return (sLabelIn, sLabelOut,