def test(): DS = loadPybrainData() train, test = DS.splitWithProportion(0.1) fnn = joblib.load(PKL) # 预测test情况 output = fnn.activateOnDataset(test) # ann.activate(onedata)可以只对一个数据进行预测 outputs = [] target = [] count = 0 for out in output: outs = out.argmax() outputs.append(outs) for tar in test['target']: ta = tar.argmax() target.append(ta) for i in range(0, len(target)): if outputs[i] == target[i]: count += 1 right = count / len(target) #单个字符正确率 rate = (right**4) print("分类正确率是:%.4f%%" % (rate * 100)) v = Validator() print(u'均方和差为:', v.MSE(output, test['target'])) #计算test的原始值和预测值的均方差和,两者格式必须相等
def dataset_eval(dataset): """Return dataset hit rate and MSE""" # Transform output values to bit vectors, similar to the targets predicted = bit_array_transform(ff_network.activate(x) for x in dataset['input']) target = dataset['target'] # Lists of positions holding predicted and target classes to compare predicted_pos = [list(x).index(1) for x in predicted] target_pos = [list(x).index(1) for x in target] hits = Validator.classificationPerformance(predicted_pos, target_pos) mse = Validator.MSE(predicted, target) return hits, mse
def __init__(self, evolino_network, dataset, **kwargs): """ @param evolino_network: an instance of NetworkWrapper() @param dataset: The evaluation dataset @param evalfunc: Compares output to target values and returns a scalar, denoting the fitness. Defaults to -mse(output, target). @param wtRatio: Float array of two values denoting the ratio between washout and training length. Defaults to [1,2] @param verbosity: Verbosity level. Defaults to 0 """ Filter.__init__(self) ap = KWArgsProcessor(self, kwargs) ap.add( 'verbosity', default=0 ) ap.add( 'evalfunc', default=lambda output, target: -Validator.MSE(output, target) ) ap.add( 'wtRatio', default=array([1,2], float) ) self.network = evolino_network self.dataset = dataset self.max_fitness = -Infinity
plotname = os.path.join(plotdir, ('jpq2layers_plot' + str(iter))) pylab.savefig(plotname) # set-up the neural network nneuron = 5 mom = 0.98 netname = "LSL-" + str(nneuron) + "-" + str(mom) mv = ModuleValidator() v = Validator() #create the test DataSet x = numpy.arange(0.0, 1.0 + 0.01, 0.01) s = 0.5 + 0.4 * numpy.sin(2 * numpy.pi * x) tsts = SupervisedDataSet(1, 1) tsts.setField('input', x.reshape(len(x), 1)) tsts.setField('target', s.reshape(len(s), 1)) #read the train DataSet from file trndata = SupervisedDataSet.loadFromFile(os.path.join(os.getcwd(), 'trndata')) myneuralnet = os.path.join(os.getcwd(), 'myneuralnet.xml') if os.path.isfile(myneuralnet): n = NetworkReader.readFrom(myneuralnet, name=netname) #calculate the test DataSet based on the trained Neural Network ctsts = mv.calculateModuleOutput(n, tsts) tserr = v.MSE(ctsts, tsts['target']) print 'MSE error on TSTS:', tserr myplot(trndata, tsts=tsts, ctsts=ctsts) pylab.show()
tsts.setField('target', s.reshape(len(s), 1)) #read the train DataSet from file trndata = SupervisedDataSet.loadFromFile(os.path.join(os.getcwd(), 'trndata')) #create the trainer t = BackpropTrainer(n, learningrate=0.01, momentum=mom) #train the neural network from the train DataSet cterrori = 1.0 print("trainer momentum:" + str(mom)) for iter in range(25): t.trainOnDataset(trndata, 1000) ctrndata = mv.calculateModuleOutput(n, trndata) cterr = v.MSE(ctrndata, trndata['target']) relerr = abs(cterr - cterrori) cterrori = cterr print('iteration:', iter + 1, 'MSE error:', cterr) myplot(trndata, ctrndata, iter=iter + 1) if cterr < 1.e-5 or relerr < 1.e-7: break #write the network using xml file myneuralnet = os.path.join(os.getcwd(), 'myneuralnet.xml') if os.path.isfile(myneuralnet): NetworkWriter.appendToFile(n, myneuralnet) else: NetworkWriter.writeToFile(n, myneuralnet) #calculate the test DataSet based on the trained Neural Network ctsts = mv.calculateModuleOutput(n, tsts)