def test_update_layer(self): cOutput = random.randint(5, 10) cInput = random.randint(5, 10) listDblOutputs = randlist(-1.0, 1.0, cOutput) listDblError = [] for dblOut in listDblOutputs: listDblError.append(1.0 / (1.0 - dblOut)) listDblInput = randlist(-1.0, 1.0, cInput) listPcpt = [] listListDblWOrig = [] for ixPcpt in xrange(cOutput): listDblW = randlist(-1.0, 1.0, cInput) listListDblWOrig.append(listDblW) listPcpt.append(nn.Perceptron(list(listDblW), 0.0, ixPcpt)) listDblDelta = nn.layer_deltas(listDblOutputs, listDblError) layer = nn.NeuralNetLayer(cInput, listPcpt) dblLearningRate = random.random() nn.update_layer(layer, listDblInput, listDblDelta, dblLearningRate) listZip = zip(layer.listPcpt, listListDblWOrig, listDblOutputs) for pcpt, listDblWOrig, dblOut in listZip: listZipInner = zip(pcpt.listDblW, listDblWOrig, listDblInput) for dblW, dblWOrig, dblIn in listZipInner: dblExpectedDiff = dblLearningRate * dblIn * dblOut self.assertAlmostEqual(dblExpectedDiff, dblW - dblWOrig, 4)
def test_update_layer(self): cOutput = random.randint(5,10) cInput = random.randint(5,10) listDblOutputs = randlist(-1.0,1.0,cOutput) listDblError = [] for dblOut in listDblOutputs: listDblError.append(1.0/(1.0 - dblOut)) listDblInput = randlist(-1.0,1.0,cInput) listPcpt = [] listListDblWOrig = [] for ixPcpt in xrange(cOutput): listDblW = randlist(-1.0,1.0,cInput) listListDblWOrig.append(listDblW) listPcpt.append(nn.Perceptron(list(listDblW),0.0,ixPcpt)) listDblDelta = nn.layer_deltas(listDblOutputs,listDblError) layer = nn.NeuralNetLayer(cInput,listPcpt) dblLearningRate = random.random() nn.update_layer(layer, listDblInput, listDblDelta, dblLearningRate) listZip = zip(layer.listPcpt,listListDblWOrig,listDblOutputs) for pcpt,listDblWOrig,dblOut in listZip: listZipInner = zip(pcpt.listDblW,listDblWOrig,listDblInput) for dblW,dblWOrig,dblIn in listZipInner: dblExpectedDiff = dblLearningRate*dblIn*dblOut self.assertAlmostEqual(dblExpectedDiff, dblW - dblWOrig, 4)
def test_layer_deltas(self): cInput = random.randint(10, 20) listDblTarget = randlist(-3.0, 3.0, cInput) listDblActivation, listDblError = pairwise_factors(listDblTarget) listDblDelta = nn.layer_deltas(listDblActivation, listDblError) listZip = zip(listDblDelta, listDblTarget, listDblActivation) for dblDelta, dblTarget, dblAct in listZip: self.assertAlmostEqual(dblTarget * (1.0 - dblAct), dblDelta, 5)
def test_layer_deltas(self): cInput = random.randint(10,20) listDblTarget = randlist(-3.0,3.0,cInput) listDblActivation,listDblError = pairwise_factors(listDblTarget) listDblDelta = nn.layer_deltas(listDblActivation,listDblError) listZip = zip(listDblDelta,listDblTarget,listDblActivation) for dblDelta,dblTarget,dblAct in listZip: self.assertAlmostEqual(dblTarget*(1.0 - dblAct),dblDelta, 5)