本文整理汇总了Python中pyfann.libfann.training_data函数的典型用法代码示例。如果您正苦于以下问题:Python training_data函数的具体用法?Python training_data怎么用?Python training_data使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了training_data函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test
def test(self, ann_file, test_file):
"""Test an artificial neural network."""
if not os.path.isfile(ann_file):
raise IOError("Cannot open %s (no such file)" % ann_file)
if not os.path.isfile(test_file):
raise IOError("Cannot open %s (no such file)" % test_file)
# Get the prefix for the classification columns.
try:
dependent_prefix = self.config.data.dependent_prefix
except:
dependent_prefix = OUTPUT_PREFIX
self.ann = libfann.neural_net()
self.ann.create_from_file(ann_file)
self.test_data = TrainData()
try:
self.test_data.read_from_file(test_file, dependent_prefix)
except IOError as e:
logging.error("Failed to process the test data: %s" % e)
exit(1)
logging.info("Testing the neural network...")
fann_test_data = libfann.training_data()
fann_test_data.set_train_data(self.test_data.get_input(),
self.test_data.get_output())
self.ann.test_data(fann_test_data)
mse = self.ann.get_MSE()
logging.info("Mean Square Error on test data: %f" % mse)
示例2: main
def main():
# setting the prediction parameters
known_days = 7
predict_days = 1
verify_days = 30
# setting up the parameters of the network
connection_rate = 1
learning_rate = 0.1
num_input = known_days * 2
num_hidden = 60
num_output = predict_days
# setting up the parameters of the network, continued
desired_error = 0.000040
max_iterations = 10000
iteration_between_reports = 100
# setting up the network
net = libfann.neural_net()
net.create_sparse_array(connection_rate, (num_input, num_hidden, num_output))
net.set_learning_rate(learning_rate)
net.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE)
# read the input file and format data
fin = open("cw3.in")
lines = fin.readlines()
fin.close()
rawdata = list(map(float, lines))[-1000:]
datain0 = rawdata[0::2]
datain1 = rawdata[1::2]
n0 = max(datain0) * 1.4
n1 = max(datain1) * 1.4
datain0 = list(map(lambda x: x / n0, datain0))
datain1 = list(map(lambda x: x / n1, datain1))
# train the network
data = libfann.training_data()
drange = range(len(datain0) - known_days - verify_days)
data.set_train_data(
map(lambda x: datain0[x:][:known_days] + datain1[x:][:known_days], drange),
map(lambda x: datain0[x + known_days:][:predict_days], drange)
)
net.train_on_data(data, max_iterations, iteration_between_reports, desired_error)
#
result = []
for i in range(verify_days):
dtest = datain0[-known_days - verify_days + i:][:known_days] + datain1[-known_days - verify_days + i:][:known_days]
result += [net.run(dtest)[0] * n0]
plot.plot(list(map(lambda x: x * n0, datain0[-verify_days: -verify_days])) + result, "r")
plot.plot(map(lambda x: x * n0, datain0[-verify_days:]), "b")
#plot.plot(list(map(lambda x: x * n0, datain0[-verify_days * 2: -verify_days])) + result, "r")
#plot.plot(map(lambda x: x * n0, datain0[-verify_days * 2:]), "b")
plot.show()
# net.train_on_file("cw3.in", max_iterations, iteration_between_reports, desired_error)
#print(net.run([1,1]))
print("hehe")
return
示例3: train
def train(self, inputs, outputs, params):
self.p = inputs.shape[1] #number of input features
self.n_r = outputs.shape[1] #size of output grid in rows
self.n_c = outputs.shape[2] #size of output grid in cols
self.out_min = outputs.min()
self.out_max = outputs.max()
d = self.out_max - self.out_min
self.out_min -= d / 98
self.out_max -= d / 98
outputs = (outputs - self.out_min) / (self.out_max - self.out_min)
assert inputs.shape[0] == outputs.shape[0]
nn = libfann.neural_net()
#nn.create_standard_array((self.p, 50, 50, self.n_r*self.n_c))
nn.create_shortcut_array((self.p, self.n_r*self.n_c))
nn.set_learning_rate(.7)
nn.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC)
nn.set_activation_function_output(libfann.SIGMOID)
data = libfann.training_data()
data.set_train_data(inputs, outputs.reshape((-1, self.n_r*self.n_c)))
#nn.train_on_data(data, 500, 10, .001)
nn.cascadetrain_on_data(data, 15, 1, .001)
nn.save('nn.net')
nn.destroy()
示例4: __init__
def __init__(self,
datafile,
desired_error = 0.0000000001,
iterations_between_reports = 1000):
self.datafile = datafile
self.desired_error = desired_error
self.iterations_between_reports = iterations_between_reports
f = open(datafile+".train", 'r')
firstline = f.readline()
f.close
l = string.split(firstline)
self.num_input = int(l[1])
self.num_output = int(l[2])
self.breeding = False
self.stage = 0
self.netsTried = 0
self.maxMutations = 18
self.populationSize = 12
self.trainingData = libfann.training_data()
self.trainingData.read_train_from_file(datafile+".train")
self.testData = libfann.training_data()
self.testData.read_train_from_file(datafile+".test")
self.flist = [libfann.FANN_LINEAR,libfann.FANN_SIGMOID,libfann.FANN_SIGMOID_STEPWISE,libfann.FANN_SIGMOID_SYMMETRIC,libfann.FANN_SIGMOID_SYMMETRIC_STEPWISE,
libfann.FANN_GAUSSIAN,libfann.FANN_GAUSSIAN_SYMMETRIC,libfann.FANN_ELLIOT,libfann.FANN_ELLIOT_SYMMETRIC,libfann.FANN_LINEAR_PIECE,
libfann.FANN_LINEAR_PIECE_SYMMETRIC,libfann.FANN_SIN_SYMMETRIC,libfann.FANN_COS_SYMMETRIC]
self.mutationlist = ["change_connection_rate",
"change_learning_rate",
"change_num_neurons_hidden",
"change_num_layers_hidden",
"change_max_iterations",
"change_training_algorithm",
"change_activation_function_hidden",
"change_activation_function_output",
"change_learning_momentum",
"change_activation_steepness_hidden",
"change_activation_steepness_output",
"change_training_param"]
self.trmutlist = ["change_connection_type",
"change_quickprop_decay",
"change_quickprop_mu",
"change_rprop_increase_factor",
"change_rprop_decrease_factor",
"change_rprop_delta_min",
"change_rprop_delta_max",
# "change_rprop_delta_zero"
]
示例5: testNet
def testNet():
data = libfann.training_data()
data.read_train_from_file(test_file);
ann = libfann.neural_net()
ann.create_from_file(nn_file)
ann.reset_MSE()
ann.test_data(data)
print("Mean square error: {0}".format(ann.get_MSE()));
示例6: load_data_prefix
def load_data_prefix(prefix):
inp = numpy.loadtxt(prefix + "_i.txt")
inp = check_matrix(inp)
out = numpy.loadtxt(prefix + "_o.txt")
out = check_matrix(out)
data = fann.training_data()
data.set_train_data(inp,out)
return data
示例7: load_data
def load_data(filename, in_outs):
a = numpy.loadtxt(filename)
inp = numpy.compress(numpy.ones(in_outs[0]), a, axis=1)
inp = check_matrix(inp)
out = numpy.compress(numpy.concatenate([numpy.zeros(in_outs[0]), numpy.ones(in_outs[1])]), a, axis=1)
out = check_matrix(out)
data = fann.training_data()
data.set_train_data(inp,out)
return data
示例8: test
def test(self):
print "Creating network."
train_data = libfann.training_data()
train_data.read_train_from_file(tfile)
ann = libfann.neural_net()
ann.create_sparse_array(
connection_rate, (len(train_data.get_input()[0]), num_neurons_hidden, len(train_data.get_output()[0]))
)
ann.set_learning_rate(learning_rate)
ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE)
ann.set_activation_function_output(libfann.SIGMOID_STEPWISE)
ann.set_training_algorithm(libfann.TRAIN_INCREMENTAL)
ann.train_on_data(train_data, max_iterations, iterations_between_reports, desired_error)
print "Testing network"
test_data = libfann.training_data()
test_data.read_train_from_file(test_file)
ann.reset_MSE()
ann.test_data(test_data)
print "MSE error on test data: %f" % ann.get_MSE()
示例9: load_data
def load_data(self, data_file,val_file=None):
# create training data, and ann object
print "Loading data"
self.train_data = libfann.training_data()
self.train_data.read_train_from_file(data_file)
self.dim_input=self.train_data.num_input_train_data()
self.dim_output=self.train_data.num_output_train_data()
input=self.train_data.get_input()
target=self.train_data.get_output()
data_lo_hi=[0,0]
for i in range(len(input)):
if target[i][0]<0.5:
data_lo_hi[0]+=1
elif target[i][0]>0.5:
data_lo_hi[1]+=1
print "\t Train data is %d low and %d high" % tuple(data_lo_hi)
if (val_file and os.path.exists(val_file)):
print "Loading validation data"
self.do_validation=True
self.val_data=libfann.training_data()
self.val_data.read_train_from_file(val_file)
input=self.val_data.get_input()
target=self.val_data.get_output()
data_lo_hi=[0,0]
for i in range(len(input)):
if target[i][0]<0.5:
data_lo_hi[0]+=1
elif target[i][0]>0.5:
data_lo_hi[1]+=1
print "\t Validation data is %d low and %d high" % tuple(data_lo_hi)
else:
self.val_data=self.train_data
self.do_validation=False
示例10: mainLoop
def mainLoop():
n_iter = 0
last_save = 0
min_test_MSE = 1.0
max_iters_after_save = 50
try:
while True:
n_iter += 1
print "Iteration: %5d " % (n_iter),
seg_copy = map(lambda (c, seg): (c, cv.CloneImage(seg)), segments)
seg_copy = map(lambda (c, seg): (c, spoil(seg)), seg_copy)
shuffle(seg_copy)
f = open(train_file, "w")
f.write("%d %d %d\n" % (len(segments), num_input, num_output))
for c, image in seg_copy:
image = adjustSize(image, (segW, segH))
for y in range(image.height):
for x in range(image.width):
n = image[y, x] / 159.375 - 0.8
f.write("%f " % n)
f.write("\n")
n = charset.index(c)
f.write("-1 " * n + "1" + " -1" * (num_output - n - 1) + "\n")
f.close()
train = libfann.training_data()
train.read_train_from_file(train_file)
ann.train_epoch(train)
train.destroy_train()
print "Train MSE: %f " % (ann.get_MSE()),
print "Train bit fail: %5d " % (ann.get_bit_fail()),
ann.test_data(test)
mse = ann.get_MSE()
print "Test MSE: %f " % (mse),
print "Test bit fail: %5d " % (ann.get_bit_fail()),
if mse < min_test_MSE:
min_test_MSE = mse
ann.save(ann_file)
last_save = n_iter
print "saved",
if n_iter - last_save > max_iters_after_save: break
print
except KeyboardInterrupt: print "Interrupted by user."
示例11: train_my_net
def train_my_net(data_file, net=None):
desired_error = 0.01
max_iter = 100000
report_time = 100
if net is None:
network = new_net()
else:
network = net
data = libfann.training_data()
data.read_train_from_file(data_file)
network.train_on_data(data, max_iter, report_time, desired_error)
return network
示例12: initNet
def initNet():
learning_rate = 0.3
num_neurons_hidden = num_input / 3
#desired_error = 0.015
#max_iterations = 10000
#iterations_between_reports = 10
global ann
ann = libfann.neural_net()
ann.create_standard_array((num_input, num_neurons_hidden, num_output))
ann.set_learning_rate(learning_rate)
ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE)
ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE)
train = libfann.training_data()
train.read_train_from_file(train_file)
ann.init_weights(train)
train.destroy_train()
示例13: TestOnData
def TestOnData(nn, testdata):
ann = libfann.neural_net()
ann.create_from_file(nn)
testData = libfann.training_data()
testData.read_train_from_file(testdata)
ann.reset_MSE()
if args.full:
inputs = testData.get_input()
outputs = testData.get_output()
missed_goodbuys = 0
missed_badbuys = 0
correct_goodbuys = 0
correct_badbuys = 0
print "#Row\tCorrect\tCalc\tFail"
for i in xrange(len(inputs)):
nn_out = ann.run(inputs[i])[0]
c_out = outputs[i][0]
s = ' '
if c_out == 1.0 and nn_out < 0.8:
s = 'B'
missed_badbuys += 1
if c_out == 0.0 and nn_out >= 0.8:
s = 'G'
missed_goodbuys += 1
if c_out == 1.0 and nn_out >= 0.8:
correct_badbuys += 1
if c_out == 0.0 and nn_out < 0.8:
correct_goodbuys += 1
print "%5u\t%1.3f\t%1.3f\t%s" % (i+1, outputs[i][0], ann.run(inputs[i])[0], s)
print "Missed %u bad buys of %u (%2.1f%%)" % (missed_badbuys, correct_badbuys+missed_badbuys,
float(missed_badbuys)/(correct_badbuys+missed_badbuys)*100)
print "Missed %u good buys of %u (%2.1f%%)" % (missed_goodbuys, correct_goodbuys+missed_goodbuys,
float(missed_goodbuys)/(correct_goodbuys+missed_goodbuys)*100)
else:
ann.test_data(testData)
print "Bit Fail: " + str(ann.get_bit_fail())
print "Mean Squared Error: " + str(ann.get_MSE())
示例14: XY_to_fann_train_data
def XY_to_fann_train_data(X, Y):
if len(X) != len(Y):
raise ValueError("X and Y must have the same number of lines.")
train_data = libfann.training_data()
if len(X):
dim_X, dim_Y = len(X[0]), len(Y[0])
tmp = tempfile.NamedTemporaryFile(delete=False)
with tmp:
tmp.write("%d %d %d\n"%(len(X), dim_X, dim_Y))
for i in xrange(len(X)):
for line in [ X[i], Y[i] ]:
tmp.write("%s\n"% ' '.join( str(float(val)) for val in line ))
train_data.read_train_from_file(tmp.name)
tmp.unlink(tmp.name)
return train_data
示例15: __init__
def __init__(self,xdat,ydat,idxs):
if shape(xdat)[0] != shape(ydat)[0]:
raise Exception('dimension mismatch b/w x, y')
nt = len(xdat)
ny = shape(ydat)[1]
nx = shape(xdat)[1]
num_input = nx;
num_output = ny;
num_layers = 3;
num_neurons_hidden = 3;
desired_error = 0.2;
max_epochs =2000;
epochs_between_reports = 1000;
net = fann.neural_net()
net.create_standard_array([num_layers, num_input, num_neurons_hidden, num_output]);
net.set_activation_function_hidden( fann.SIGMOID_SYMMETRIC);
net.set_activation_function_output( fann.SIGMOID_SYMMETRIC);
t = fann.training_data()
t.set_train_data(xdat,ydat)
nt = net.train_on_data(t,max_epochs,epochs_between_reports,desired_error)
out = net.save( "xor_float.net");
print net.get_training_algorithm()
raise Exception()
fann.train_on_file( "xor.data", max_epochs, epochs_between_reports, desired_error);
out = net.save( "xor_float.net");
net.destroy();