def _createNet(self, type): """Instantiate the correct network type attribute from the XML file. Complain if there is no support for the specified type.""" if ('bpn' == type): self._network = ANN.BackPropNet() self._nettype = 'bpn' else: raise Exception('Unsupported network type : ' + type)
def predict(self, trainDir, testDir, dirOut, HiddenNum, itNum, learningRate): ''' trainDir : the directory of train data; testDir : the directory of test data; dirOut : the directory for the result file HiddenNum : the number of the nodes in the hidden layer itNum : the times for iterate learningRate: the learn Rate for each iteration ''' names = util.readNames(testDir + "namefile") if (0 == len(names)): names = util.readDir(testDir) outfile = dirOut + "Result_ANN_" + str(time.ctime()) fout = open(outfile, "w") for name in names: self.trainfile = trainDir + name self.testfile = testDir + name examplars = self.getExamplars() inputs = self.getInputs() outNum = len(self.ClassOrderList) inNum = len(self.VocabOrderList) # hiddenNum =int(inNum/20) bpNet = ANN.BackPropNet() bpNet.addinput(inNum) bpNet.addhidden(HiddenNum) bpNet.addouput(outNum) print "Learning", name bpNet.learn(examplars, itNum, learningRate) print "Predict", name results = bpNet.run(inputs) print "Writting result..." for i, e in enumerate(self.testName): string = name + " " + e index = results[i].index(max(results[i])) string = string + " " + self.ClassOrderList[index] + "\n" fout.write(string) fout.flush() fout.close() print "Finished Predicting" return outfile
def predict(self, trainDir, testDir, dirOut): names = util.readNames(testDir + "namefile") if (0 == len(names)): names = util.readDir(testDir) outfile = dirOut + "Result_ANN_" + str(time.ctime()) fout = open(outfile, "w") for name in names: self.trainfile = trainDir + name self.testfile = testDir + name examplars = self.getExamplars() inputs = self.getInputs() outNum = len(self.ClassOrderList) inNum = len(self.VocabOrderList) hiddenNum = int(inNum / 20) bpNet = ANN.BackPropNet() bpNet.addinput(inNum) bpNet.addhidden(5) bpNet.addouput(outNum) print "Learning", name bpNet.learn(examplars, 40) print "Predict", name results = bpNet.run(inputs) print "Writting result..." for i, e in enumerate(self.testName): string = name + " " + e index = results[i].index(max(results[i])) string = string + " " + self.ClassOrderList[index] + "\n" fout.write(string) fout.flush() fout.close() print "Finished Predicting" return outfile