def task(self): net = build_xor_net() pts = [] listTrue = [] listFalse = [] listSMarker = ["triangle-down", "triangle"] listSColor = ["#FF0000", "#000000"] for inst in XOR_INSTANCES: dblOut = nn.feed_forward(net, inst.listDblFeatures)[0] fLabel = inst.iLabel > 0.5 iGuess = int(dblOut > 0.5) dblX,dblY = inst.listDblFeatures dictPoint = {"x": dblX, "y": dblY, "marker": {"symbol": listSMarker[iGuess], "fillColor": listSColor[int(fLabel)]}} if fLabel: listTrue.append(dictPoint) else: listFalse.append(dictPoint) return {"chart": {"defaultSeriesType":"scatter"}, "plotOptions": {"scatter": { "marker": {"radius": 20, "states": { "hover":{"enabled":False}}}}}, "title": {"text": "XOR Classification"}, "xAxis": {"title": {"text": "Input 0"}, "min": -1.05, "max": 1.05, "tickInterval": 1, "gridLineWidth": 1}, "yAxis": {"title": {"text": "Input 1"}, "min": -1.2, "max": 1.2, "tickInterval": 1, "startOnTick": False, "endOnTick": False}, "series": [{"name":"True", "data": listTrue}, {"name": "False", "data": listFalse}], "legend": {"enabled": False}, "tooltip": {"enabled": False}}
def evaluate_net(net,listInst,fxnDecode): cCorrect = 0 for inst in listInst: iResult = fxnDecode(nn.feed_forward(net,inst.listDblFeatures)) cCorrect += int(iResult == inst.iLabel) res = float(cCorrect)/float(len(listInst)) print >>sys.stderr, 'evaluation:', res return res
def learn_xor(): """Build a neural network which solves XOR.""" net = nn.init_net([2, 2, 1]) for _ in xrange(5000): for inst in XOR_INSTANCES: nn.update_net(net, inst, 0.5, [inst.iLabel]) for inst in XOR_INSTANCES: print inst.iLabel, nn.feed_forward(net, inst.listDblFeatures) nn.print_net(net)
def classify(net, boostInst, fxnDecode): """Using the neural net, return the predicted label for the boostInst. net = nn.init_net([2,2,1]) net = learn_nn_classifier(net, XOR_INSTANCES, nn.binary_encode_label, nn.binary_decode_net_output) classify(net, BoostInstance([-1.0,-1.0], 0), nn.binary_decode_net_output) Out[108]: 0 classify(net, BoostInstance([1.0,-1.0], 1), nn.binary_decode_net_output) Out[109]: 1""" listDblOut = nn.feed_forward(net, boostInst.listAttrs) return fxnDecode(listDblOut)
def evaluate_net(net, listInst, fxnDecode): """ listInstTrain = load_training_9k(9000) listInstTest = load_test_1k(1000) net = nn.init_net([14*14,15,10]) evaluate_net(net,listInstTest,nn.distributed_decode_net_output) Out: 0.105""" cCorrect = 0 for inst in listInst: iResult = fxnDecode(nn.feed_forward(net, inst.listDblFeatures)) cCorrect += int(iResult == inst.iLabel) return float(cCorrect) / float(len(listInst))
def num_correct(net, listInst, fxnDecode): """ listInstTrain = load_training_9k(9000) listInstTest = load_test_1k(1000) net = nn.init_net([14*14,40,10]) num_correct(net, listInstTest,nn.distributed_decode_net_output) Out: 105 num_correct(net, listInstTrain,nn.distributed_decode_net_output) Out: 893""" cCorrect = 0 for inst in listInst: listDblOut = nn.feed_forward(net, inst.listDblFeatures) iGuess = fxnDecode(listDblOut) cCorrect += int(inst.iLabel == iGuess) return cCorrect
def is_nutritious_by_NN(plant_image): return nn.feed_forward(net, plant_image)[0] > .5
trained_weights_flat = res.x trained_weights = nn.unflatten_array(res.x, weight_shapes) # compute cost of training sample J_train = nn.compute_cost_and_grad( trained_weights_flat, train['Xnorm'][0:im,:], train['y1hot'][0:im,:], weight_shapes, lam=lam, cost_only=True) train_Js.append(J_train) # compute accuracy on validation sample aa, zz = nn.feed_forward(train['Xnorm'][0:im,:], trained_weights) h = aa[-1] y_predict = numpy.argmax(h, axis=1) accuracy = (train['y'][0:im] == y_predict).astype(numpy.float).mean() train_accs.append(accuracy) # compute cost on validation sample J_valid = nn.compute_cost_and_grad( trained_weights_flat, valid['Xnorm'], valid['y1hot'], weight_shapes, lam=lam, cost_only=True) valid_Js.append(J_valid)
def experiment(opts): """Run a neural net experiment.""" dictSeen = {} writer = csv.writer(open("500epochs_80Nodes_4.csv", "wb")) def load(sAttrFilename, sClassFilename): if sAttrFilename in dictSeen: return dictSeen[sAttrFilename] sys.stderr.write("Loading %s and %s..." % (sAttrFilename, sClassFilename)) listInst = load_separate_data(sAttrFilename, sClassFilename, opts.max_inst) sys.stderr.write("done.\n") dictSeen[sAttrFilename] = listInst return listInst listInstTrain = load(opts.attrtrain, opts.classtrain) listInstVal = load(opts.attrvalidation, opts.classvalidation) listInstTest = load(opts.attrtest, opts.classtest) config = [opts.num_inputs] if opts.hidden_units: print 'Adding a hidden layer with %d units' % opts.hidden_units config.append(opts.hidden_units) config.append(7) #Covertype data set #config.append(10) #config.append(4) dblPrevAccuracy = 0.0 dblCurAccuracy = 0.0 dblStopDeltaAccuracy = 0.05 iEpochInterval = 5 net = nn.init_net(config) for ixRound in xrange(opts.rounds): dblAlpha = 2.0 * opts.rounds / (ixRound + opts.rounds) print 'Learning rate: %f' % dblAlpha # Count error errors = 0 for inst in listInstTrain: listDblOut = nn.update_net( net, inst, dblAlpha, nn.distributed_encode_label(inst.iLabel)) iGuess = nn.distributed_decode_net_output(listDblOut) if iGuess != inst.iLabel: errors += 1 # Compute validation error validation_correct = num_correct(net, listInstVal, nn.distributed_decode_net_output) sys.stderr.write( "Round %d complete. Training Accuracy: %f, Validation Accuracy: %f\n" % (ixRound + 1, 1 - errors * 1.0 / len(listInstTrain), validation_correct * 1.0 / len(listInstVal))) # RECORDING writer.writerow([ ixRound + 1, dblAlpha, 1 - errors * 1.0 / len(listInstTrain), validation_correct * 1.0 / len(listInstVal) ]) if opts.stopping_condition: if (ixRound + 1) % iEpochInterval is 0: dblCurAccuracy = validation_correct * 1.0 / len(listInstVal) if (dblCurAccuracy - dblPrevAccuracy) < dblStopDeltaAccuracy: break else: dblPrevAccuracy = dblCurAccuracy cCorrect = 0 for inst in listInstTest: listDblOut = nn.feed_forward(net, inst.listDblFeatures) iGuess = nn.distributed_decode_net_output(listDblOut) cCorrect += int(inst.iLabel == iGuess) print "correct:", cCorrect, "out of", len(listInstTest), print "(%.1f%%)" % (100.0 * cCorrect / len(listInstTest))
#!/usr/bin/python import nn n = nn.read_from_file('net.pic') d = nn.load_data('test_img') f0 = [nn.feed_forward(n, q.listDblFeatures)[0] for q in d if q.iLabel == 0] f1 = [nn.feed_forward(n, q.listDblFeatures)[0] for q in d if q.iLabel == 1] print >>open('d0', 'w'), '\n'.join(map(str, sorted(f0))) print >>open('d1', 'w'), '\n'.join(map(str, sorted(f1)))
def evaluate_net(net,listInst,fxnDecode): cCorrect = 0 for inst in listInst: iResult = fxnDecode(nn.feed_forward(net,inst.listDblFeatures)) cCorrect += int(iResult == inst.iLabel) return float(cCorrect)/float(len(listInst))
#!/usr/bin/python import nn n = nn.read_from_file('net.pic') d = nn.load_data('test_img') f0 = [nn.feed_forward(n, q.listDblFeatures)[0] for q in d if q.iLabel == 0] f1 = [nn.feed_forward(n, q.listDblFeatures)[0] for q in d if q.iLabel == 1] print >> open('d0', 'w'), '\n'.join(map(str, sorted(f0))) print >> open('d1', 'w'), '\n'.join(map(str, sorted(f1)))