示例#1
0
def run_network():
    # Generate standard layered network architecture and create network
    conec = mlgraph((14,28,1))
    net = ffnet(conec)

    df = pd.read_csv('data/copacabana.csv', sep=';')
    variables = [
        'Posicao', 'Quartos', 'Vagas', 'DistIpanema', 'DistPraia',
        'DistFavela', 'RendaMedia', 'RendaMovel', 'RendaMovelRua',
        'Vu2009', 'Mes', 'Idade', 'Tipologia', 'AreaConstruida'
    ]
    input = df[variables]
    target = df[['VAL_UNIT']]

    # Train network
    #first find good starting point with genetic algorithm (not necessary, but may be helpful)
    print "FINDING STARTING WEIGHTS WITH GENETIC ALGORITHM..."
    net.train_genetic(input, target, individuals=20, generations=500)
    #then train with scipy tnc optimizer
    print "TRAINING NETWORK..."
    net.train_tnc(input, target, maxfun = 1000, messages=1)

    print "TESTING NETWORK..."
    output, regression = net.test(input, target, iprint=0)

    # Save/load/export network
    print "Network is saved..."
    savenet(net, "data/capacabana.net")

    return output, regression
示例#2
0
def train(data):
  #trains neural network based on passed training data.
  #training data is a list of [input,output] lists
   
  print "amount of training data:" + str(len(data))
  inputsize = 30 * 30
  outsize = 10
  nodes = 350 #((inputsize + outsize) * 2) / 3

  inp = [i for i,t in data]
  trg = [t for i,t in data]
    
  print "creating neural network, hidden nodes:" + str(nodes)
  conec = mlgraph((inputsize,nodes,outsize))
  
  print "initializing ffnet"
  net = ffnet(conec)
  
  #print "loading ffnet"
  #net = loadnet("ffnet.net")

  # print "assigning random weights"
  # net.randomweights()

  # Train process
  print "training network"
  net.train_tnc(inp, trg, messages=1,nproc=4)

  print "saving trained network"
  savenet(net, "ffnet.net")

  print "testing network"
  net.test(inp, trg, iprint = 1)
示例#3
0
def create_then_save_network_trained_on(name, pattern, train_method):
    input, output = load_snns(pattern)
    net = create_bp(input, output)

    print("Training the net with {0} algorithm...".format(train_method))
    training = getattr(net, "train_" + train_method)
    training(input, output)

    filename = "{0}_{1}.net".format(name, train_method)
    savenet(net, filename)
    print("saved as: {0}".format(filename))
示例#4
0
def export_network(net, ANNname, Nlayers, Nneurons, pspace):
    from ffnet import savenet
    import os
    try:
        f = '{0:}_{1:}_{2:}'.format(ANNname, Nlayers, Nneurons)
        savenet(net, f)
        export_ANN_to_nc(net, ANNname + '.nc', Nlayers, Nneurons, pspace)

    except Exception, e:
        print 'Error when writing ffnet network file', e
        raise (e)
示例#5
0
def learn_net(filename, word, **args):
    (network, people,
     people_num) = load_speaker_recognition_newtork(filename, True)

    print "Loading voice samples..."
    samples = [voice_sample.VoicePersonSamples(s) for s in people]

    rs = args.get("rs", None)
    re = args.get("re", None)

    if rs is None or re is None:
        ans = raw_input("Select sample range (1-10):  ")
        (rs, re) = map(lambda x: int(x), ans.strip().split("-"))

    (inp, out) = get_inputs_and_outputs(samples, word, rs, re)

    train_method = args.get("method", "momentum")
    training = getattr(network, "train_" + train_method)

    training(inp, out)

    _save = args.get("save", True)

    _save and savenet(network, filename)

    return (network, samples)
def learn_net(filename, word, **args):
    (network, people, people_num) = load_speaker_recognition_newtork(filename, True);

    print "Loading voice samples..."
    samples = [ voice_sample.VoicePersonSamples(s) for s in people]

    rs = args.get("rs",None)
    re = args.get("re", None)

    if rs is None or re is None:
        ans = raw_input("Select sample range (1-10):  ")
        (rs, re) = map(lambda x: int(x), ans.strip().split("-"))
    
    (inp, out) = get_inputs_and_outputs(samples, word, rs, re);

    train_method = args.get("method", "momentum")
    training = getattr(network, "train_" + train_method)

    training(inp, out)

    _save = args.get("save", True)

    _save and savenet(network, filename)

    return (network, samples)
    def save_network(self):
        # Save/load/export network
        from ffnet import savenet, loadnet, exportnet

        print "Network is saved..."
        savenet(self.net, "xor.net")
        print "Network is reloaded..."
        net = loadnet("xor.net")
        print "Network is tested again, but nothing is printed..."
        output, regression = net.test(input, target, iprint=0)
        print
        print "Exporting trained network to the fortran source..."
        exportnet(net, "xor.f")
        print "Done..."
        print "Look at the generated xor.f file."
        print "Note: you must compile xor.f along with the ffnet.f"
        print "file which can be found in ffnet sources."
示例#8
0
    def write(self, desc, stats):

        self.lock.acquire()
        self.counter += 1
        #print "write: "+repr(stats)
        fid = open(self.filename, 'a')

        r2 = np.array([t[2]**2 for t in stats.regression_ve])
        print "%3d: hh=%8s, iters=%7d, r2_ve=%0.3f" % (
            self.counter, repr(desc.hh), stats.iters, r2.mean()),

        if 'regression_te' in stats:
            r2 = np.array([t[2]**2 for t in stats.regression_te])
            print ", r2_te=%0.3f" % r2.mean(),

        print

        #print desc,stats
        #print "write: 1"
        nname = "%s_%03d_%s.ffnet" % (self.project, self.counter, repr(
            desc.hh))
        #print "write: 2: "+os.path.join(self.outfolder,nname)
        savenet(stats.net, os.path.join(self.outfolder, nname))
        #print "write: 3"

        fid.write(
            '%d\t%s\t%d' %
            (self.counter, '_'.join([str(h)
                                     for h in desc.hh]), len(stats.net.conec)))
        #print "write: 4"

        #tt=net.test(ens.input_data(2),ens.target_data(2),iprint=0)
        #print "1"
        fid.write('\t' + '\t'.join(['%0.3f' % rr for rr in r2]))
        #print "2"
        fid.write('\t' + '%0.3f' % r2.mean())
        fid.write('\n')
        #print "3"
        #fid.flush()
        #print "4"
        fid.close()
        #print "Gotovo zapisivanje. ..."
        self.num_in_qeue -= 1
        self.lock.release()
示例#9
0
def train_ANN(inputs_array, target_array, iterations, node_architecture,
              **configs_dict):

    # Same first dimension?
    if not inputs_array.shape[0] == target_array.shape[0]:
        raise Exception('Input and target arrays must have same first ' \
                        'dimension!')

    # Specified number of input nodes matches second dim of input array?
    n_input_nodes = node_architecture[0]
    if len(inputs_array.shape) == 1:
        sec_dim_inputs = 1
    else:
        sec_dim_inputs = inputs_array.shape[1]
    if not n_input_nodes == sec_dim_inputs:
        raise Exception('Specified input node architecture (n = %s) ' \
                        'incompatible with passed input arrays... Returning!'
                        %str(n_input_nodes))

    # Specified number of target nodes matches second dim of target array?
    n_target_nodes = node_architecture[-1]
    if len(target_array.shape) == 1:
        sec_dim_target = 1
    else:
        sec_dim_target = target_array.shape[1]
    if not n_target_nodes == sec_dim_target:
        raise Exception('Specified target node architecture (n = %s) ' \
                        'incompatible with passed input arrays... Returning!'
                        %str(n_target_nodes))

    # Missing data in inputs array? (Warning only)
    if np.isnan(inputs_array).any():
        missing_inputs_flag = True
        warnings.warn('Specified ANN training input variables contain missing ' \
                      'data. NaNs will be inserted into prediction series!')
    else:
        missing_inputs_flag = False

    # Missing data in target array? (Warning only)
    if np.isnan(target_array).any():
        missing_target_flag = True
        warnings.warn('Specified ANN training target variables contain missing ' \
                      'data. These will be removed for training!')
    else:
        missing_target_flag = False

    # Check if saving trained network
    save_flag = False
    if 'save_network' in configs_dict.keys():
        if configs_dict['save_network']:
            save_flag = True
        if not 'network_filepath' in configs_dict.keys():
            raise Exception('You must specify a file path if you wish to ' \
                            'save a new network!')
        else:
            split_pathname_list = os.path.split(
                configs_dict['network_filepath'])
            if not os.path.isdir(split_pathname_list[0]):
                raise Exception('The specified file path is not valid!')
            if split_pathname_list[1] == '':
                print 'Filename not supplied - using this_net.ann!'
                configs_dict['network_filepath'] = os.path.join(
                    split_pathname_list[0], 'this_net.ann')

    # Check if doing testing
    test_flag = False
    if 'test' in configs_dict:
        if configs_dict['test']:
            test_flag = True

    # Create a second series with nans dropped
    if missing_inputs_flag or missing_target_flag:
        new_array = np.empty(
            [inputs_array.shape[0], sec_dim_inputs + sec_dim_target])
        new_array[:, :sec_dim_target] = target_array
        new_array[:, sec_dim_target:] = inputs_array
        new_array = new_array[~np.isnan(new_array).any(axis=1)]
        clean_target_array = new_array[:, :sec_dim_target]
        clean_inputs_array = new_array[:, sec_dim_target:]

    # Generate network and train
    conec = tmlgraph(node_architecture)
    net = ffnet(conec)
    net.train_tnc(clean_inputs_array,
                  clean_target_array,
                  maxfun=iterations,
                  messages=1)

    # Save network if requested
    if save_flag:
        ffnet_class.savenet(net, configs_dict['network_filepath'])

    # Generate full series from inputs
    predict_array = net.call(inputs_array)

    # Do testing if requested
    if test_flag:
        vars_list = [
            'slope', 'intercept', 'r-value', 'p-value', 'slope stderr',
            'estim. stderr'
        ]
        valid_predict_array, stats_list = net.test(clean_inputs_array,
                                                   clean_target_array)
        stats_dict = {var: stats_list[0][i] for i, var in enumerate(vars_list)}
        return predict_array, stats_dict
    else:
        return predict_array
        binary_solution = m.get_binary_from_code(m.get_morse_code_from_text())

        m.read_wav_file("%s" % filename)

        input_data, certainty = get_nnet_input_data(m)
        output_data = np.array(binary_solution, dtype=np.float32)

        if X_train is None:
            X_train = input_data
            Y_train = output_data
        else:
            X_train = np.concatenate((X_train, input_data), axis=0)
            Y_train = np.concatenate((Y_train, output_data), axis=0)

    net.train_tnc(X_train, Y_train, nproc=4, maxfun=200000, messages=2)
    savenet(net, "morse.net")

else:
    net = loadnet("morse.net")

## ===============================
## GENERATE SUBMISSION
## ===============================
with open('sampleSubmission.csv') as f:
    trainingset = f.read().split("\n")

files = [(('000' + x.split(",")[0])[-3:], x.split(",")[1])
         for x in trainingset[1:] if "," in x]

f = open('submission.csv', 'w')
f.write("ID,Prediction\n")
示例#11
0
def train_ANN(inputs_array, target_array, iterations, node_architecture, 
              **configs_dict):

    # Same first dimension?
    if not inputs_array.shape[0] == target_array.shape[0]:
        raise Exception('Input and target arrays must have same first ' \
                        'dimension!')

    # Specified number of input nodes matches second dim of input array?
    n_input_nodes = node_architecture[0]
    if len(inputs_array.shape) == 1:
        sec_dim_inputs = 1
    else: 
        sec_dim_inputs = inputs_array.shape[1]
    if not n_input_nodes == sec_dim_inputs:
        raise Exception('Specified input node architecture (n = %s) ' \
                        'incompatible with passed input arrays... Returning!'
                        %str(n_input_nodes))

    # Specified number of target nodes matches second dim of target array?
    n_target_nodes = node_architecture[-1]
    if len(target_array.shape) == 1:
        sec_dim_target = 1
    else: 
        sec_dim_target = target_array.shape[1]
    if not n_target_nodes == sec_dim_target:
        raise Exception('Specified target node architecture (n = %s) ' \
                        'incompatible with passed input arrays... Returning!'
                        %str(n_target_nodes))        

    # Missing data in inputs array? (Warning only)
    if np.isnan(inputs_array).any():
        missing_inputs_flag = True
        warnings.warn('Specified ANN training input variables contain missing ' \
                      'data. NaNs will be inserted into prediction series!')
    else:
        missing_inputs_flag = False

    # Missing data in target array? (Warning only)
    if np.isnan(target_array).any():
        missing_target_flag = True
        warnings.warn('Specified ANN training target variables contain missing ' \
                      'data. These will be removed for training!')
    else:
        missing_target_flag = False

    # Check if saving trained network
    save_flag = False
    if 'save_network' in configs_dict.keys():
        if configs_dict['save_network']: 
            save_flag = True
        if not 'network_filepath' in configs_dict.keys():
            raise Exception('You must specify a file path if you wish to ' \
                            'save a new network!')
        else:
            split_pathname_list = os.path.split(configs_dict['network_filepath'])
            if not os.path.isdir(split_pathname_list[0]):
                raise Exception('The specified file path is not valid!')
            if split_pathname_list[1] == '':
                print 'Filename not supplied - using this_net.ann!'
                configs_dict['network_filepath'] = os.path.join(split_pathname_list[0],
                                                                'this_net.ann')
                      
    # Check if doing testing
    test_flag = False
    if 'test' in configs_dict:
        if configs_dict['test']:
            test_flag = True

    # Create a second series with nans dropped
    if missing_inputs_flag or missing_target_flag:
        new_array = np.empty([inputs_array.shape[0], 
                              sec_dim_inputs + sec_dim_target])
        new_array[:, :sec_dim_target] = target_array
        new_array[:, sec_dim_target:] = inputs_array
        new_array = new_array[~np.isnan(new_array).any(axis = 1)]
        clean_target_array = new_array[:, :sec_dim_target]
        clean_inputs_array = new_array[:, sec_dim_target:]

    # Generate network and train
    conec = tmlgraph(node_architecture)
    net = ffnet(conec)
    net.train_tnc(clean_inputs_array, clean_target_array, 
                  maxfun = iterations, messages = 1)

    # Save network if requested
    if save_flag:
        ffnet_class.savenet(net, configs_dict['network_filepath'])

    # Generate full series from inputs
    predict_array = net.call(inputs_array)

    # Do testing if requested
    if test_flag:
        vars_list = ['slope', 'intercept', 'r-value', 'p-value', 
                     'slope stderr', 'estim. stderr']
        valid_predict_array, stats_list = net.test(clean_inputs_array, 
                                                   clean_target_array)
        stats_dict = {var: stats_list[0][i] for i, var in enumerate(vars_list)}
        return predict_array, stats_dict
    else:
        return predict_array
示例#12
0
 def save_net(self):
     savenet((self.net, self.epoch), "netdata.dat")
示例#13
0
 def save(self, filename):
     savenet(self.network, filename)
示例#14
0
# Train network
#first find good starting point with genetic algorithm (not necessary, but may be helpful)
print "FINDING STARTING WEIGHTS WITH GENETIC ALGORITHM..."
net.train_genetic(input, target, individuals=20, generations=500)
#then train with scipy tnc optimizer
print "TRAINING NETWORK..."
net.train_tnc(input, target, maxfun=1000, messages=1)

# Test network
print
print "TESTING NETWORK..."
output, regression = net.test(input, target, iprint=2)

# Save/load/export network
from ffnet import savenet, loadnet, exportnet

print "Network is saved..."
savenet(net, "xor.net")
print "Network is reloaded..."
net = loadnet("xor.net")
print "Network is tested again, but nothing is printed..."
output, regression = net.test(input, target, iprint=0)
print
print "Exporting trained network to the fortran source..."
exportnet(net, "xor.f")
print "Done..."
print "Look at the generated xor.f file."
print "Note: you must compile xor.f along with the ffnet.f"
print "file which can be found in ffnet sources."
示例#15
0
             coeff_type=args.coeff_type,
             ANN_setup=setup,
             test_percentage=args.test_perc,
             num_of_proc=args.nproc,
             basename=args.basename,
             netcdf_output=netcdf_output,
             err_increase=args.err_inc)

# train the network for the first time
net.train_tnc(src_train, trg_train, nproc=nproc)
err = Test_ANN(net, [src, trg], [src_train, trg_train], [src_test, trg_test])
print 'err:\terr_train:\terr_test:\trel_err [%]:\trel_err_train [%]:\trel_err_test [%]:'
print '{}\t{}\t{}\t{}\t{}\t{}'.format(*err)

# save network as ffnet and in netCDF4-format:
ff.savenet(net, args.basename + '_1_.net')
train_num = 2
if args.coeff_type == 'diffuse' or args.coeff_type == 'diff2diff':
    ANN_to_NetCDF(net,
                  netcdf_output,
                  iprint=False,
                  dz=LUT[0]['dz'],
                  kabs=LUT[0]['kabs'],
                  ksca=LUT[0]['ksca'],
                  g=LUT[0]['g'])
else:
    ANN_to_NetCDF(net,
                  netcdf_output,
                  iprint=False,
                  dz=LUT[0]['dz'],
                  kabs=LUT[0]['kabs'],
示例#16
0
pa_data = pa_data[:len(pa_data)//N*N]

limit = len(hr_data)

hr_input = [sum(hr_data[x:x+N])/N for x in range(0, limit-N, N)]
hr_target = [sum(hr_data[x:x+N])/N for x in range(N, limit, N)]

pa_diffs = [abs(pa_data[x]-pa_data[x+1]) for x in range(limit-N)]
#acc_diffs = [abs(fst-snd) for fst, snd in zip(pa_data[:-1], pa_data[1:])]
pa_input = [sum(pa_diffs[x:x+N])/N for x in range(0, limit-N, N)]


input = map(list, zip(hr_input, pa_input))
target = [[n] for n in hr_target]
print input[:10]
print target[:10]


net.train_tnc(input, target)


#output, regression = net.test(input, hr_target, iprint=3)
output, regression = net.test(input, target, iprint=2)
#print
#print output
#print regression


savenet(net, 'hrnet.net')

        binary_solution = m.get_binary_from_code(m.get_morse_code_from_text())

        m.read_wav_file("%s" % filename)

        input_data, certainty = get_nnet_input_data(m)
        output_data = np.array(binary_solution, dtype=np.float32)

        if X_train is None:
            X_train = input_data
            Y_train = output_data
        else:
            X_train = np.concatenate((X_train, input_data), axis=0)
            Y_train = np.concatenate((Y_train, output_data), axis=0)

    net.train_tnc(X_train, Y_train, nproc=4, maxfun=200000, messages=2)
    savenet(net, "morse.net")

else:
    net = loadnet("morse.net")

## ===============================
## GENERATE SUBMISSION
## ===============================
with open('sampleSubmission.csv') as f:
    trainingset = f.read().split("\n")

files = [(('000' + x.split(",")[0])[-3:], x.split(",")[1]) for x in trainingset[1:] if "," in x]

f = open('submission.csv','w')
f.write("ID,Prediction\n")
示例#18
0
文件: xor.py 项目: cephdon/meta-core
target  = [[1.], [0.], [0.], [1.]]

# Train network
#first find good starting point with genetic algorithm (not necessary, but may be helpful)
print "FINDING STARTING WEIGHTS WITH GENETIC ALGORITHM..."
net.train_genetic(input, target, individuals=20, generations=500)
#then train with scipy tnc optimizer
print "TRAINING NETWORK..."
net.train_tnc(input, target, maxfun = 1000, messages=1)

# Test network
print
print "TESTING NETWORK..."
output, regression = net.test(input, target, iprint = 2)

# Save/load/export network
from ffnet import savenet, loadnet, exportnet
print "Network is saved..."
savenet(net, "xor.net")
print "Network is reloaded..."
net = loadnet("xor.net")
print "Network is tested again, but nothing is printed..."
output, regression = net.test(input, target, iprint = 0)
print
print "Exporting trained network to the fortran source..."
exportnet(net, "xor.f")
print "Done..."
print "Look at the generated xor.f file."
print "Note: you must compile xor.f along with the ffnet.f" 
print "file which can be found in ffnet sources."
示例#19
0
 def save(self, name):
     savenet(self.net, name)
示例#20
0
 def save_net(self):
     savenet((self.net, self.epoch), "netdata.dat")
示例#21
0
文件: run_me.py 项目: ryanmp/dlnn
def build_ffnet(sorted_data,training_set_size):

	logging.info('starting new run! -----------------------------')
	print 'defining network'

	from ffnet import ffnet, imlgraph, mlgraph, loadnet, savenet
	from time import time
	from multiprocessing import cpu_count
	import networkx
	import pylab

	#data_in_training2 = sorted_data[:training_set_size,10:-2].astype(float).tolist()
	data_target_training2 = [[i] for i in sorted_data[:training_set_size,0].astype(float)]

	new_data_in = sorted_data[:training_set_size,col_training_set[0]]
	for i in col_training_set[1:]:
		new_data_in = numpy.column_stack((new_data_in, sorted_data[:training_set_size,i]))
	data_in_training2 = new_data_in.astype(float).tolist()

	# Define net (large one)
	conec = mlgraph(network_config, biases=False) #skipping first 11 cols
	net = ffnet(conec)
	print 'saving initialized net'
	savenet(net, 'starting_net.n')
	#net = loadnet('starting_net.n') # this way we can init a complex net just once

	#print 'draw network'
	#networkx.draw_graphviz(net.graph, prog='dot')
	#pylab.show()

	graph_weekly(net, sorted_data,training_set_size) # just saving a pic

	logging.info('network built as: ' + str(network_config) )

	print "TRAINING NETWORK..."
	# that are many different training algos

	#net.train_rprop(data_in_training2, data_target_training2, a=1.9, b=0.1, mimin=1e-06, mimax=15.0, xmi=0.5, maxiter=max_functions, disp=1)
	###net.train_momentum(data_in_training2, data_target_training2, eta=0.2, momentum=0.1, maxiter=max_functions, disp=1)
	stats = []
	smallest_error = 1000
	total = 0
	try:
		for i in xrange(min_loops,max_loops):
			total += max_functions+i
			if total>max_total:
				break
			print 'training for:',max_functions+i, "total is:", total

			net.train_tnc(data_in_training2, data_target_training2, maxfun = max_functions+i, messages=1)
			#net.train_rprop(data_in_training2, data_target_training2, a=1.2, b=0.5, mimin=1e-06, mimax=50.0, xmi=0.1, maxiter=max_functions*20, disp=1)

			graph_weekly(net, sorted_data,training_set_size) # just saving a pic

			in0, out0, s1, s2, mape_weekly_all = calc_stats(net,sorted_data,training_set_size)
			stats.append((in0, out0,total, s1, s2, mape_weekly_all))
			#if out0<=(biggest/1.4) and in0>.7:
			#if out0<=(smallest_error/4) and in0>overfitting_threshold:
			#	print 'we hit overfitting threshold - breaking out early'
			#	break
			if mape_weekly_all < smallest_error: # found a new best
				smallest_error = mape_weekly_all
				savenet(net, 'best_net.n')
	except KeyboardInterrupt: # this way command-c just breaks out of this loop
		pass


	#net.train_cg(data_in_training2, data_target_training2, maxiter=max_functions, disp=1)
	#net.train_genetic(data_in_training2, data_target_training2, individuals=max_population, generations=max_functions)
	#net.train_bfgs(data_in_training2, data_target_training2, maxfun = max_functions, disp=1)
	stats = sorted(stats, reverse=True, key=lambda x: x[1])
	for i in stats:
		temp_string = ''
		for x in i:
			temp_string += str(x) + ','
		print temp_string

	net = loadnet('best_net.n')
	return net