def exportCatDogRNN(net, fileName=root.path() + "/res/cat_dog_nm_params"): # arr = net.params # np.save(fileName, arr) # fileObject = open(fileName+'.pickle', 'w') # pickle.dump(net, fileObject) # fileObject.close() NetworkWriter.writeToFile(net, fileName + '.xml')
def exportCatDogRFCNN(net, fileName = root.path()+"/res/cat_dog_fc_params"): # arr = net.params # np.save(fileName, arr) # fileObject = open(fileName+'.pickle', 'w') # pickle.dump(net, fileObject) # fileObject.close() NetworkWriter.writeToFile(net, fileName+'.xml')
def xmlInvariance(n, forwardpasses = 1): """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare if the result looks the same (compare string representation, and forward processing of some random inputs) """ tmpfile = tempfile.NamedTemporaryFile() f = tmpfile.name NetworkWriter.writeToFile(n, f) tmpnet = NetworkReader.readFrom(f) NetworkWriter.writeToFile(tmpnet, f) endnet = NetworkReader.readFrom(f) netCompare(tmpnet, endnet, forwardpasses, True)
def xmlInvariance(n, forwardpasses=1): """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare if the result looks the same (compare string representation, and forward processing of some random inputs) """ tmpfile = tempfile.NamedTemporaryFile() f = tmpfile.name NetworkWriter.writeToFile(n, f) tmpnet = NetworkReader.readFrom(f) NetworkWriter.writeToFile(tmpnet, f) endnet = NetworkReader.readFrom(f) netCompare(tmpnet, endnet, forwardpasses, True)
def build_net(self): if os.path.exists(self.NET_FILE): return NetworkReader.readFrom(self.NET_FILE) ds = ClassificationDataSet(len(feats), nb_classes=len(classes)) for c in classes: print c with codecs.open(os.path.join(self.data_root, c+".txt"), 'r', 'utf8') as f: for line in f: r = Record("11", line, c, "") ds.appendLinked(r.features(), [r.class_idx()]) ds._convertToOneOfMany([0, 1]) net = buildNetwork(ds.indim, int((ds.indim + ds.outdim)/2), ds.outdim, bias=True, hiddenclass=TanhLayer, outclass=SoftmaxLayer) trainer = BackpropTrainer(net, ds, momentum=0.75, verbose=True) trainer.trainUntilConvergence(maxEpochs=300) NetworkWriter.writeToFile(net, self.NET_FILE) return net
def train(): f = open('train.csv', 'r') csv_reader = csv.reader(f) dataset = SupervisedDataSet(64, 1) for d in csv_reader: dataset.addSample(d[0:64], d[64]) network = buildNetwork(64, 19, 1) trainer = BackpropTrainer(network, dataset) for i in range(100): trainer.train() NetworkWriter.writeToFile(network, "model.xml") f.close()
def xmlInvariance(n, forwardpasses = 1): """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare if the result looks the same (compare string representation, and forward processing of some random inputs) """ # We only use this for file creation. tmpfile = tempfile.NamedTemporaryFile(dir=".") f = tmpfile.name tmpfile.close() NetworkWriter.writeToFile(n, f) tmpnet = NetworkReader.readFrom(f) NetworkWriter.writeToFile(tmpnet, f) endnet = NetworkReader.readFrom(f) # Unlink temporary file. os.unlink(f) netCompare(tmpnet, endnet, forwardpasses, True)
def train(): f = open('train_tower.csv', 'r') csvreader = csv.reader(f) dataset = SupervisedDataSet(64, 2) for d in csvreader: if d[64] == '0': dataset.addSample(d[0:64], [1, 0]) else: dataset.addSample(d[0:64], [0, 1]) network = buildNetwork(64, 19, 2) trainer = BackpropTrainer(network, dataset) for i in range(100): trainer.train() trainer.testOnData(dataset, verbose=True) NetworkWriter.writeToFile(network, "tower.xml") f.close()
def xmlInvariance(n, forwardpasses = 1): """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare if the result looks the same (compare string representation, and forward processing of some random inputs) """ import os.path f = 'temp/xmlInvarianceTest.xml' if os.path.split(os.path.abspath(os.path.curdir))[1] == 'unittests': f = '../'+f NetworkWriter.writeToFile(n, f) tmpnet = NetworkReader.readFrom(f) NetworkWriter.writeToFile(tmpnet, f) endnet = NetworkReader.readFrom(f) if str(n) == str(endnet): print 'Same representation' else: print n print "-" * 80 print endnet outN = zeros(n.outdim) outEnd = zeros(endnet.outdim) n.reset() endnet.reset() for dummy in range(forwardpasses): inp = randn(n.indim) outN += n.activate(inp) outEnd += endnet.activate(inp) if sum(map(abs, outN - outEnd)) < 1e-9: print 'Same function' else: print outN print outEnd if n.__class__ == endnet.__class__: print 'Same class' else: print n.__class__ print endnet.__class__
def xmlInvariance(n, forwardpasses=1): """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare if the result looks the same (compare string representation, and forward processing of some random inputs) """ import os.path f = 'temp/xmlInvarianceTest.xml' if os.path.split(os.path.abspath(os.path.curdir))[1] == 'unittests': f = '../' + f NetworkWriter.writeToFile(n, f) tmpnet = NetworkReader.readFrom(f) NetworkWriter.writeToFile(tmpnet, f) endnet = NetworkReader.readFrom(f) if str(n) == str(endnet): print 'Same representation' else: print n print "-" * 80 print endnet outN = zeros(n.outdim) outEnd = zeros(endnet.outdim) n.reset() endnet.reset() for dummy in range(forwardpasses): inp = randn(n.indim) outN += n.activate(inp) outEnd += endnet.activate(inp) if sum(map(abs, outN - outEnd)) < 1e-9: print 'Same function' else: print outN print outEnd if n.__class__ == endnet.__class__: print 'Same class' else: print n.__class__ print endnet.__class__
def saveNetwork(self, fname): """ save the trained network to a file """ NetworkWriter.writeToFile(self.Trainer.module, fname) logging.info("Network saved to: "+fname)
def save(self, file_path): NetworkWriter.writeToFile(self.network, file_path)
def export_network(network, file_path): NetworkWriter.writeToFile(network, file_path)
outputTest = net.activateOnDataset(test_data) outputTest = outputTest.argmax(axis=1) testResult = percentError(outputTest, real_test) finalTrainResult = 100 - trainResult finalTestResult = 100 - testResult print "Epoch: " + str(i + 1) + "\tTraining set accuracy: " + str(finalTrainResult) + "\tTest set accuracy: " + str( finalTestResult) # getStatistics( ) trainResultArr.append(finalTestResult) testResultArr.append(finalTrainResult) epochs.append(i) prediction = net.activate(test_input) # returns the index of the highest value down the columns p = argmax(prediction, axis=0) NetworkWriter.writeToFile(net, 'extra_layers.xml') plt.plot(epochs, trainResultArr) plt.plot(epochs, testResultArr) plt.title('Training Result (Orange) vs Test Result of ANN (Blue)') plt.xlabel('Epochs') plt.ylabel('Accuracy %') plt.show()
trainResultArr.append((finalTestRes)) testResultArr.append((finalTrainRes)) trainResultArrHB.append((finalTestResHB)) testResultArrHB.append((finalTrainResHB)) epochs.append((i)) X1 = im3.reshape((X.shape[1])) prediction = net.activate(X1) predictionHB = netHB.activate(X1) # returns the index of the highest value down the columns p = argmax(prediction, axis=0) pHB = argmax(predictionHB, axis=0) NetworkWriter.writeToFile(net, 'dig.xml') print("predicted output after training is", p) NetworkWriter.writeToFile(netHB, 'digHB.xml') print( "predicted output after training net with hyperbolic activation function is", pHB) plt.plot(epochs, trainResultArr) plt.plot(epochs, testResultArr) plt.plot(epochs, trainResultArrHB) plt.plot(epochs, testResultArrHB) plt.title('Training Result (Orange) vs Test Result of ANN (Blue)') plt.xlabel('Epochs') plt.ylabel('Accuracy %')
outputTest = net.activateOnDataset(test_data) outputTest = outputTest.argmax(axis=1) testResult = percentError(outputTest, real_test) finalTrainRes = 100 - trainResult finalTestRes = 100 - testResult print "Epoch: " + str(i) + "\tTraining set accuracy: " + str(finalTrainRes) + "\tTest set accuracy: " + str(finalTestRes) trainResultArr.append((finalTestRes)) testResultArr.append((finalTrainRes)) epochs.append((i)) X1 = im3.reshape((X.shape[1])) prediction = net.activate(X1) # returns the index of the highest value down the columns p = argmax(prediction, axis=0) NetworkWriter.writeToFile(net, 'dig_img_from_dir.xml') print("predicted output after training is", p) plt.plot(epochs,trainResultArr) plt.plot(epochs,testResultArr) plt.title('Training Result (Orange) vs Test Result of ANN (Blue)') plt.xlabel('Epochs') plt.ylabel('Accuracy %') plt.show()
def write_to_file(self, fnn): path_to_file = self.get_path() NetworkWriter.writeToFile(fnn, path_to_file)
# Predefine iterations: epochs & cycles EPOCHS_PER_CYCLE = 5 CYCLES = 100 EPOCHS = EPOCHS_PER_CYCLE * CYCLES # Training loop for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) error = trainer.testOnData() epoch = (i + 1) * EPOCHS_PER_CYCLE print("\r Epoch: {}/{} Error: {}".format(epoch, EPOCHS, error), end="") stdout.flush() # Save model NetworkWriter.writeToFile(rnn, 'rnn3.xml') # Ad hoc test for test in test_data: for i in xrange(0, len(test) - 6, 5): # Get 5 obs, 6th we wish to predict obs, nxt = test[i:i + 5], test[i + 6] # Predict all prds = map(rnn.activate, obs) # Get 6th prediction prd = prds.pop()[0] # Test if prd is anomalous anm = prd > (1 + np.mean(obs) + 2 * np.std(obs)) # Get previous 5 obs,prd error rate
def save_net(): from pybrain.tools.xml import NetworkWriter save_filename = tkFileDialog.asksaveasfilename() NetworkWriter.writeToFile(net,save_filename)
for single_npz in training_data: with np.load(single_npz) as data: print data.files train_temp = data['train'] train_labels_temp = data['train_labels'] print (train_temp.shape) print (train_labels_temp.shape) image_array = np.vstack((image_array, train_temp)) label_array = np.vstack((label_array, train_labels_temp)) t1 = time.time() for i in range(train_temp.shape[0]): target.addSample(image_array[i],label_array[i]) print 'loading time : ' , time.time() - t1 # train while True: try: while True: errors = trainer.train() if (j % 5) == 0: print ("epoch%d error : %f" % (j, errors)) elif(errors < 2e-2) : print ("epoch%d error : %f" % (j, errors)) break j += 1 finally: NetworkWriter.writeToFile(network, XML) break raw_input('>')
def savenet(net): fileObject = open('C:\\Users\\hardy_000\\Documents\\datasci\\net', 'w') pickle.dump(net, fileObject) NetworkWriter.writeToFile(net, 'C:\\Users\\hardy_000\\Documents\\datasci\\net.xml') fileObject.close()