Beispiel #1
0
def ffnetwork(inputs=1, layers=1, outputs=1):
    """
    Define a feedforward neural network

    Parameters
    ----------
    inputs : integer
        default = 1
        defines the length of input vector
    layers : integer
        default = 1
        defines the number of layers
    outputs : integer
        default = 1
        defines the length of output vector

    Returns
    -------
    net : Feed-forward neural network
    """
    conec = ffnet.mlgraph((inputs, layers, outputs), biases=False)
    net = ffnet.ffnet(conec)
    '''NX.draw_graphviz(net.graph, prog='dot')
    plt.show()'''
    return net
Beispiel #2
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
Beispiel #3
0
def learn(inputD,outputData):
    conec = mlgraph((2,22,12,1))
    net = ffnet(conec)
    print("READING DATA")
    inputData = inputD
    target = numpy.array(outputData)#data[:, -1] #last column

    print ("TRAINING NETWORK...")
    sys.stdout.flush()
    net.train_tnc(inputData, target, maxfun = 5000, messages=1)
    print ("TESTING NETWORK...")
    output, regression = net.test(inputData, target, iprint = 0)
    print(regression)
    Rsquared = regression[0][2]
    maxerr = abs( numpy.array(output).reshape( len(output) ) - numpy.array(target) ).max()
    print ("R-squared:           %s  (should be >= 0.999999)" %str(Rsquared))
    print ("max. absolute error: %s  (should be <= 0.05)" %str(maxerr))
    print ("Is ffnet ready for a stock?")
    try:
        plot( target, 'b--' )
        plot( output, 'k-' )
        legend(('target', 'output'))
        xlabel('pattern'); ylabel('price')
        title('Outputs vs. target of trained network.')
        grid(True)
        show()
        return net
    except ImportError:
        print ("Cannot make plots. For plotting install matplotlib.\n%s" % e)
        return net
Beispiel #4
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)
Beispiel #5
0
def build_ffnet(sorted_data,network_config):

	#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]] # the first col
	for i in col_training_set[1:]: # and the rest of the cols
		new_data_in = numpy.column_stack((new_data_in, sorted_data[:training_set_size,i]))
	data_in_training2 = new_data_in.astype(float).tolist()

	print 'defining network'

	# Define net (large one)
	conec = mlgraph(network_config, biases=False)

	net = ffnet(conec)

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

	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)
	net.train_tnc(data_in_training2, data_target_training2, maxfun = max_functions, messages=1)
	#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)

	return net
    def train(self, X, Y):
        """ Trains the neural network based on the given set of inputs
        and outputs. """

        inp = array(X)
        targ = array(Y)
        n_inputs = len(inp[0])

        # 1 Output node because Surrogate Model has only 1 output
        self._nn_surr = ffnet(mlgraph((n_inputs, self.n_hidden_nodes, 1)))

        # Start the training
        if self.method == 'cg':
            self._nn_surr.train_cg(inp, targ, disp=False)
        elif self.method == 'genetic':
            self._nn_surr.train_genetic(inp, targ, individuals=10*n_inputs,
                                        generations=500)
        elif self.method == 'tnc':
            self._nn_surr.train_tnc(inp, targ, maxfun=5000)
        elif self.method == 'momentum':
            self._nn_surr.train_momentum(inp, targ, momentum=1)
        elif self.method == 'rprop':
            self._nn_surr.train_rprop(inp, targ)
        elif self.method == 'bfgs':
            self._nn_surr.train_bfgs(inp, targ)
        else:
            self.raise_exception('Unknown training method %r' % self.method)
 def __init__(self, arch, verbose = False, biases = True):
     """
     Initialises the neural network
     """
     self.verbose = verbose
     self.arch = arch
     self.conec = mlgraph(self.arch, biases = biases)
     self.net = ffnet(self.conec)
def deserialize(data, used_activation=None):
    arguments = data['arguments']
    if used_activation is None:
        used_activation = getattr(ActivationFunctions, data['activation_function'])
    nn = ffnet(*arguments)
    for index, layer in enumerate(data['layers']):
        for p_index, perceptron in enumerate(nn.layers[index]):
            perceptron.weights = layer[p_index]
    return nn
def create_bp(input, output):
    it = len(input[0])
    ot = len(output[0])

    connection = mlgraph((it, 10, ot))
    net = ffnet(connection)

    print("Created new network...")
    return net
Beispiel #10
0
def do(inp):
	conec = mlgraph((1,2,2,1))
	net = ffnet(conec)
	input=inp[0]
	target=inp[1]
	#net.train_genetic(input, target, individuals=20, generations=500)
	print "TRAINING NETWORK..."
	net.train_tnc(input, target, maxfun = 5000)
	x=mgrid[0:1:n*1j]
	y=map(lambda x: net([x])[0],x)
	return vstack((x,y))
Beispiel #11
0
def create_net(inno, layer_definitions, outno, network_function):
    from ffnet import ffnet
    pts = [
        inno,
    ] + layer_definitions + [
        outno,
    ]
    conec = network_function(pts, biases=True)
    net = ffnet(conec)
    print 'created new network with ', pts, 'layers and', len(
        net.conec), 'connections'
    return net
Beispiel #12
0
    def train(self, X, Y):
        """ Trains the neural network based on the given set of inputs
        and outputs. """

        inp = array(X)
        targ = array(Y)
        n_inputs = len(inp[0])
        
        # 1 Output node because Surrogate Model has only 1 output
        self._nn_surr = ffnet(mlgraph((n_inputs, self.n_hidden_nodes, 1)))
                        
        # Start the training
        self._nn_surr.train_cg(inp, targ, disp=False)
Beispiel #13
0
 def fit(self, descs, target_values, train_alg='tnc',**kwargs):
     # setup neural network
     if self.full_conn:
         conec = tmlgraph(self.shape, self.biases)
     else:
         conec = mlgraph(self.shape, self.biases)
     self.model = ffnet(conec)
     if self.random_weights:
         if not self.random_state is None:
             random_seed(self.random_state)
         self.model.randomweights()
     # train
     getattr(self.model, 'train_'+train_alg)(descs, target_values, nproc='ncpu' if self.n_jobs < 1 else self.n_jobs, **kwargs)
     return self
def get_nn(init_and_save_weights):
    # Create a new NN and save it to a json
    if init_and_save_weights:
        arguments = NN_ARGUMENTS
        arguments.append(USED_ACTIVATION)
        autoencoder = ffnet(*arguments)
        with open(NN_FILE, 'w+') as file:
            json.dump(ffnet_utils.serialize(autoencoder), file)
    # Load the NN weights from the json
    else:
        with open(NN_FILE, 'r') as file:
            data = json.load(file)
        autoencoder = ffnet_utils.deserialize(data, USED_ACTIVATION)
    return autoencoder
Beispiel #15
0
def train(input, target):
    print input[0]
    print len(input)
    conec = mlgraph((16, 16, 10))
    net = ffnet(conec)
    input = input.tolist()
    target = target.tolist()

    print "FINDING STARTING WEIGHTS WITH GENETIC ALGORITHM..."
    # net.train_genetic(input, target, individuals=20, generations=30)
    #then train with scipy tnc optimizer
    print "TRAINING NETWORK..."
    net.train_tnc(input, target, maxfun=1000, messages=1)
    return net
Beispiel #16
0
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.field = Field(20, 20)

        self.outputs = []

        self.input = []
        self.target = []

        b = QtGui.QPushButton("Learn!")
        self.connect(b, QtCore.SIGNAL("clicked()"), self.learn)

        self.outcomes_list = QtGui.QComboBox()
        self._add_output("Square")
        self._add_output("Triangle")
        self._add_output("Line")

        hpanel = QtGui.QHBoxLayout()
        hpanel.addWidget(self.outcomes_list)
        hpanel.addWidget(b)

        btn_classify = QtGui.QPushButton("Classify")
        self.connect(btn_classify, QtCore.SIGNAL("clicked()"), self.classify)

        btn_clear = QtGui.QPushButton("Clear")
        self.connect(btn_clear, QtCore.SIGNAL("clicked()"), self.clear)

        self.label_output = QtGui.QLabel()
        self.label_output.setMaximumHeight(20)

        self.label_epoch = QtGui.QLabel()
        self.label_epoch.setMaximumHeight(20)

        vpanel = QtGui.QVBoxLayout()
        vpanel.addWidget(self.field)
        vpanel.addLayout(hpanel)
        vpanel.addWidget(self.label_output)
        vpanel.addWidget(self.label_epoch)
        vpanel.addWidget(btn_classify)
        vpanel.addWidget(btn_clear)

        self.setLayout(vpanel)

        try:
            self.net, self.epoch = loadnet("netdata.dat")
        except IOError:
            conec = mlgraph((self.field.x * self.field.y, 10, 10, 3))
            self.net = ffnet(conec)
            self.epoch = 0
Beispiel #17
0
 def __init__(self):
     super(NeuralNetwork, self).__init__()
     self.field = Field(20, 20)
     
     self.outputs = []
     
     self.input = []
     self.target = []
     
     b = QtGui.QPushButton("Learn!")
     self.connect(b, QtCore.SIGNAL("clicked()"), self.learn)
     
     self.outcomes_list = QtGui.QComboBox()
     self._add_output("Square")
     self._add_output("Triangle")
     self._add_output("Line")
     
     hpanel = QtGui.QHBoxLayout()
     hpanel.addWidget(self.outcomes_list)
     hpanel.addWidget(b)
     
     btn_classify = QtGui.QPushButton("Classify")
     self.connect(btn_classify, QtCore.SIGNAL("clicked()"), self.classify)
     
     btn_clear = QtGui.QPushButton("Clear")
     self.connect(btn_clear, QtCore.SIGNAL("clicked()"), self.clear)
     
     self.label_output = QtGui.QLabel()
     self.label_output.setMaximumHeight(20)
     
     self.label_epoch = QtGui.QLabel()
     self.label_epoch.setMaximumHeight(20)
     
     vpanel = QtGui.QVBoxLayout()
     vpanel.addWidget(self.field)
     vpanel.addLayout(hpanel)
     vpanel.addWidget(self.label_output)
     vpanel.addWidget(self.label_epoch)
     vpanel.addWidget(btn_classify)
     vpanel.addWidget(btn_clear)
     
     self.setLayout(vpanel)
     
     try:
         self.net, self.epoch = loadnet("netdata.dat")
     except IOError:
         conec = mlgraph((self.field.x*self.field.y, 10, 10, 3))
         self.net = ffnet(conec)
         self.epoch = 0
Beispiel #18
0
    def fit(self, X, y):
        dim = X.shape
        nFeatures = int(dim[1])

        #input = [ [0.,0.], [0.,1.], [1.,0.], [1.,1.] ]
        #target  = [ [1.], [0.], [0.], [1.] ]
        input = list(X)
        target = list(y)

        conec = mlgraph((nFeatures, self.nNodes, 1))
        self.net = ffnet(conec)
        if self.maxfun == '':
            self.net.train_tnc(input, target)
        else:
            self.net.train_tnc(input, target, maxfun=self.maxfun)
Beispiel #19
0
    def add_mlp(self, n_hidden):
        """
        Dodajem novu neuralnu mrežu s n_hidden neurona u skrivenom sloju.
        n_hidden može biti broj ili tuple
        """
        ind = len(self.nns)
        nin = len(self._invars)
        nout = len(self._outvars)
        if isinstance(n_hidden, int): n_hidden = (n_hidden, )
        arch = (nin, ) + n_hidden + (nout, )
        net = ffnet(mlgraph(arch))

        self.nns.append(net)

        return ind
def main():
    """ load training data"""
    inputs = np.loadtxt("../handwriting/X2_100samples.dat")
    targets = np.loadtxt("../handwriting/y2_100samples.dat")
    """ define network topology """
    conec = mlgraph((inputs.shape[1], 10, 1))

    #     reg = 0.1
    reg = False
    net = ffnet(conec)
    system = NNSystem(net, inputs, targets, reg=reg)

    database = system.create_database(
        #                     db="/home/ab2111/machine_learning_landscapes/neural_net/db_ffnet_100samples_reg"+str(reg) +".sqlite"
        db="../db/db_ffnet_100samples.sqlite")
    run_gui(system, database)
def train(trainData):
    print('Starting Training')
    inLength = 64

    inData = []
    for i in range(len(trainData)):
        row = []
        for j in range(inLength):
            row.append(trainData[i][j])
        inData.append(row)

    conec = mlgraph((inLength, 10, 10, inLength))
    net = ffnet(conec)
    input = numpy.array(inData)
    target = numpy.array(inData)
    net.train_tnc(input, target, maxfun=2000, messages=1)
    print('Training completed')
Beispiel #22
0
    def __init__(self,
                 inputDims,
                 actionSpace,
                 hiddenUnits=5,
                 bias=True,
                 independentOutputs=False,
                 **kwargs):
        assert not actionSpace.hasContinuousDimensions() \
                or actionSpace.getNumberOfDimensions() == 1, \
                "MLP policy can currently not deal with continuous action "\
                "spaces with more than one dimension!"

        try:
            import ffnet
        except:
            import warnings
            warnings.warn("The MLP policy module requires the ffnet package.")

        self.continuousActions = actionSpace.hasContinuousDimensions()

        if self.continuousActions:
            actionDimension = actionSpace.getDimensions()[
                0]  # there is per assert only 1
            actionRanges = actionDimension.getValueRanges()
            assert len(actionRanges) == 1, "MLP policy cannot deal with "\
                                           "non-contiguous action ranges."
            self.actionRange = actionRanges[0]
            self.numActions = 1  # TODO
        else:
            self.actions = actionSpace.getActionList()
            self.numActions = len(self.actions)

        self.inputDims = inputDims
        self.hiddenUnits = hiddenUnits
        self.bias = bias

        # Determine network topology
        if independentOutputs:
            conec = ffnet.imlgraph((inputDims, hiddenUnits, self.numActions),
                                   biases=self.bias)
        else:
            conec = ffnet.mlgraph((inputDims, hiddenUnits, self.numActions),
                                  biases=self.bias)

        # Create net based on connectivity
        self.net = ffnet.ffnet(conec)
def load_speaker_recognition_newtork(filename, create_new=False):
    """
    Load or create (if you  want) network for speker recognition form file

    returns tuple: (network, people_names, people_number)
    """
    people = voice_sample.get_names(); 
    people_num = len(people)
    network = None

    try:
        network = loadnet(filename)
    except IOError, ex:
        if create_new:
            
            network = ffnet(mlgraph((LPC_COE_NUM, people_num + LPC_COE_NUM,
            #network = ffnet(mlgraph((LPC_COE_NUM, 10,
                people_num)) )
def main():
    """ load training data"""
    inputs  = np.loadtxt("../handwriting/X2_100samples.dat")
    targets = np.loadtxt("../handwriting/y2_100samples.dat")
    
    """ define network topology """
    conec = mlgraph((inputs.shape[1],10,1))

#     reg = 0.1
    reg=False
    net = ffnet(conec)
    system = NNSystem(net, inputs, targets, reg=reg)
    
    database = system.create_database(
#                     db="/home/ab2111/machine_learning_landscapes/neural_net/db_ffnet_100samples_reg"+str(reg) +".sqlite"
                    db="../db/db_ffnet_100samples.sqlite"
                )
    run_gui(system, database)
Beispiel #25
0
 def fit(self, descs, target_values, train_alg='tnc', **kwargs):
     # setup neural network
     if self.full_conn:
         conec = tmlgraph(self.shape, self.biases)
     else:
         conec = mlgraph(self.shape, self.biases)
     self.model = ffnet(conec)
     if self.random_weights:
         if not self.random_state is None:
             random_seed(self.random_state)
         self.model.randomweights()
     # train
     getattr(self.model, 'train_' + train_alg)(
         descs,
         target_values,
         nproc='ncpu' if self.n_jobs < 1 else self.n_jobs,
         **kwargs)
     return self
Beispiel #26
0
    def __init__(self, stateSpace, actions, **kwargs):
        super(MLP, self).__init__(stateSpace)
        try:
            import ffnet
        except ImportError:
            raise Exception(
                "Error: MLP function approximator cannot be used without the ffnet module!"
            )

        self.numberOfInputs = len(stateSpace.keys())
        self.numberOfOutputs = len(actions)
        self.actions = actions
        self.stateSpace = stateSpace

        conec = ffnet.mlgraph((self.numberOfInputs, 8, 1))
        self.nets = dict([(action, ffnet.ffnet(conec)) for action in actions])
        for action in self.actions:
            for index in range(len(self.nets[action].weights)):
                self.nets[action].weights[index] = 0.0
def run():

    def formatPrediction(u):
        if (u>0.5):
            return 1
        else:
            return 0
    dat = json.loads(dict(request.form).keys()[0])
    dat["pgood"]["values"] = []
    dat["pbad"]["values"] = []
    good = set(dat["good"]["values"])
    none = set(dat["none"]["values"])
    bad = set(dat["bad"]["values"])
    data = get_all_fruits()
    train_input  = []
    train_target = []
    test_input = []
    for f in data:
        if f[0] in good:
            train_input.append(f[1:])
            train_target.append(1)
        elif f[0] in bad:
            train_input.append(f[1:])
            train_target.append(0)
        elif f[0] in none:
            pass
        else:
            test_input.append(f)            
    
    print len(test_input)
    # Run algorithm
    l = len(data[0])-1
    conec = mlgraph( (l,2,1)) 
    net = ffnet(conec)
    net.train_tnc(train_input, train_target, maxfun = 1000)
    ## Print the name of the fruits used for test 
    
    o = net.test([u[1:] for u in test_input], [0]*len(test_input),iprint=0)
    res = [formatPrediction(u[0]) for u in o[0]]

    dat["pgood"]["values"] = [ k[0] for i,k in enumerate(test_input) if res[i] == 1 ]
    dat["pbad"]["values"] = [ k[0] for i,k in enumerate(test_input) if res[i] == 0 ]
    return json.dumps(dat)
    def train(self, X, Y):
        """ Trains the nerual network based on the given set of inputs
        and outputs. """

        inp = array(X)
        targ = array(Y)
        n_inputs = len(inp[0])
        
        # 1 Output node because Surrogate Model has only 1 output
        self._nn_surr = ffnet(mlgraph((n_inputs, self.n_hidden_nodes, 1)))
                        
        # Start the training
        #self._nn_surr.train_genetic(inp, targ, individuals=10*n_inputs, generations=500)

        #self._nn_surr.train_tnc(inp, targ,maxfun=5000)
        
        #self._nn_surr.train_momentum(inp,targ,momentum=1)
        #self._nn_surr.train_rprop(inp,targ)
        self._nn_surr.train_cg(inp,targ,disp=False)
def main():
    """ load training data"""
    inputs = np.loadtxt("../handwriting/X2_100samples.dat")
    targets = np.loadtxt("../handwriting/y2_100samples.dat")

    ValInputs, ValTargets = get_validation_data()
    """ define network topology """
    conec = mlgraph((inputs.shape[1], 10, 1))

    net = ffnet(conec)
    system = NNSystem(net, inputs, targets)

    pot = system.get_potential()

    database = system.create_database(
        db=
        "/home/ab2111/machine_learning_landscapes/neural_net/db_ffnet_100samples.sqlite"
    )
    # database = system.create_database(db="/home/ab2111/machine_learning_landscapes/neural_net/db_ffnet_me3.sqlite")
    # run_gui(system, database)

    #     check_its_a_minimum(system, database)

    energies = np.array([])
    for m in database.minima():
        coords = m.coords
        testenergy = pot.getValidationEnergy(coords, ValInputs,
                                             ValTargets) / len(ValTargets)
        energies = np.append(energies, testenergy)


#         plt.plot(m.coords,'o')
#         np.max(m.coords)

#     plt.plot([m._id for m in database.minima()], np.array([m.energy for m in database.minima()])/100., 'o')
    plt.plot(np.array([m.energy for m in database.minima()]) / 100)
    plt.plot(energies)
    plt.plot(
        np.array([np.max(m.coords) for m in database.minima()]) / 1000, 'x')

    plt.legend(["Etrain", "Evalidation", "max(params)"])
    plt.show()
def creatingNeuralNetwork(num_hidden,data_s,data_l):
  dataset_data = data_s
  dataset_labels = data_l
  #create the gab of 6-grams vocabulary
  tfidf_vectorizer = TfidfVectorizer(ngram_range=(1, 6),token_pattern=ur'\b\w+\b',min_df=0.05)
  tfidf_vectorizer.fit(dataset_data)
  feature_names = tfidf_vectorizer.get_feature_names()
  number_of_input_features = len(feature_names)
  # Create the feature dataset
  indata = np.zeros((len(dataset_data),number_of_input_features))
  for j,sentence in enumerate(dataset_data):
    indata[j,:] =  tfidf_vectorizer.transform([sentence]).toarray()[0]
  #create the neural network
  number_of_hidden_nodes = num_hidden
  conec = tmlgraph((number_of_input_features,number_of_hidden_nodes, 1))
  net = ffnet(conec)
  #please print nothing
  net.train_tnc(indata, dataset_labels, maxfun = 5000, messages=0) 
  output, regression = net.test(indata, dataset_labels, iprint = 0)
  return net, number_of_input_features, tfidf_vectorizer
Beispiel #31
0
def load_speaker_recognition_newtork(filename, create_new=False):
    """
    Load or create (if you  want) network for speker recognition form file

    returns tuple: (network, people_names, people_number)
    """
    people = voice_sample.get_names()
    people_num = len(people)
    network = None

    try:
        network = loadnet(filename)
    except IOError, ex:
        if create_new:

            network = ffnet(
                mlgraph((
                    LPC_COE_NUM,
                    people_num + LPC_COE_NUM,
                    #network = ffnet(mlgraph((LPC_COE_NUM, 10,
                    people_num)))
def main():
    def formatPrediction(u):
        if (u>0.5):
            return 1
        else:
            return 0
    res = []
    for k in range(100):
        header, tests, train = read_data()
        inputLength = len(header) - 2
        # 2 here is the middle layer, you can remove it and try, it does not 
        # seem to have much impact in that very case
        conec = mlgraph( (inputLength,1) )
        net = ffnet(conec)
        train_input = [ u[1:-1] for u in train ]
        target_input  = [ u[-1] for u in train ]
        test_input = [ u[1:-1] for u in tests ]
        test_target  = [ u[-1] for u in tests ]
        net.train_tnc(train_input, target_input, maxfun = 1000)
        # Print the name of the fruits used for test 
        o = net.test(test_input, test_target,iprint=0)#), iprint = 2)
        res.append(float(sum([formatPrediction(u[0]) ^ int(test_target[i]) for i,u in enumerate(o[0])]))/len(test_target))
    print sum(res)/len(res)
Beispiel #33
0
 def __init__(
     self, shape=None, full_conn=True, biases=True, random_weights=True, normalize=True, reduce_empty_dims=True
 ):
     """
     shape: shape of a NN given as a tuple
     """
     self.shape = shape
     self.full_conn = full_conn
     self.biases = biases
     self.random_weights = random_weights
     self.normalize = normalize
     self.reduce_empty_dims = reduce_empty_dims
     if self.normalize:
         self.norm = StandardScaler()
     self.shape = shape
     if shape:
         if self.full_conn:
             conec = tmlgraph(self.shape, self.biases)
         else:
             conec = mlgraph(self.shape, self.biases)
         self.model = ffnet(conec)
         if random_weights:
             self.model.randomweights()
Beispiel #34
0
def main():
	conec = mlgraph((1, 4 ,1))
	net = ffnet(conec)
	
	patterns = 16
	input = [[ k * 2 * pi / patterns] for k in xrange(patterns + 1)]
	target = [[sin(x[0])] for x in input]
	
	print "training"
	net.train_genetic(input, target, individuals=20, generations=500)
	print "simple trainig"
	net.train_tnc(input, target, maxfun = 5000, messages = 1)
	
	print "test"
	output, regression = net.test(input, target, iprint = 2)
	
	# draw it
	points = 128
	xaxis = [[ k * 2 * pi / patterns] for k in xrange(patterns + 1)]
	sine = [sin(x) for x in xaxis]
	cosine = [cos(x) for x in xaxis]
	netsine = [net([x])[0] for x in xaxis]
	netcosine = [net.derivative([x])[0][0] for x in xaxis]
	
	subplot(211)
	plot(xaxis, sine, 'b--', xaxis, netsine, 'k-')
	legend(('sine', 'network output'))
	grid(True)
	title('Outputs of trained network.')

	subplot(212)
	plot(xaxis, cosine, 'b--', xaxis, netcosine, 'k-')
	legend(('cosine', 'network derivative'))
	grid(True)
	show()
	
	return 0
def main():
    """ load training data"""
    inputs  = np.loadtxt("../handwriting/X2_100samples.dat")
    targets = np.loadtxt("../handwriting/y2_100samples.dat")    
    
    ValInputs, ValTargets = get_validation_data()

    """ define network topology """
    conec = mlgraph((inputs.shape[1],10,1))
    
    net = ffnet(conec)
    system = NNSystem(net, inputs, targets)
    
    pot = system.get_potential()
            
    database = system.create_database(db="/home/ab2111/machine_learning_landscapes/neural_net/db_ffnet_100samples.sqlite")
    # database = system.create_database(db="/home/ab2111/machine_learning_landscapes/neural_net/db_ffnet_me3.sqlite")
    # run_gui(system, database)
    
#     check_its_a_minimum(system, database)

    energies = np.array([])
    for m in database.minima():
        coords = m.coords
        testenergy = pot.getValidationEnergy(coords,ValInputs,ValTargets)/len(ValTargets)
        energies = np.append(energies,testenergy)
#         plt.plot(m.coords,'o')
#         np.max(m.coords)
         

#     plt.plot([m._id for m in database.minima()], np.array([m.energy for m in database.minima()])/100., 'o')
    plt.plot(np.array([m.energy for m in database.minima()])/100)
    plt.plot(energies)
    plt.plot(np.array([np.max(m.coords) for m in database.minima()])/1000, 'x')
    
    plt.legend(["Etrain","Evalidation","max(params)"])
    plt.show()
Beispiel #36
0
def _train2(X, Y, filename, epochs=50):
    global nn
    conec = mlgraph((INPUT_SIZE, HIDDEN_LAYERS, OUTPUT_LAYER))
    nn = ffnet(conec)

    nn.train_momentum(X, Y, eta=0.001, momentum=0.8, maxiter=epochs, disp=True)
Beispiel #37
0
def ANN_gapfill_func(myBaseforResults,New_combined,Site_ID,list_in,list_out,iterations,index_str,is_this_all,ANN_label_all,ANN_label,frequency,Use_Fc_Storage):

    ###########################################################################################################
    ##                 START MAIN CODE
    ###########################################################################################################
    if 'Fc' in list_out:
        units="umol.m-2.s-1"
    elif ('Fe' or 'Fh' or 'Fg') in list_out:
        units="W.m-2"
    else:
        units=" "
    
    ###### User-set IO file locations ######
    
    print "Starting ANN gap filling"
    #Check for place to put results - does it exist? If not create
    if not os.path.isdir(myBaseforResults):
        os.mkdir(myBaseforResults)
    #Then subdirectories
    if not os.path.isdir(myBaseforResults+"/ANN"):
        os.mkdir(myBaseforResults+"/ANN")
    mypathforResults=myBaseforResults+"/ANN"  
      
    #We need to update VPD for input here so also need e and es
    # Calculate vapour pressure from absolute humidity and temperature
    #  Ah - absolute humidity, g/m3
    #  Ta - air temperature, C
    New_combined['VPD_Con']=(metfuncs.es(New_combined['Ta_Con']))-(metfuncs.vapourpressure(New_combined['Ah_Con'],New_combined['Ta_Con']))
    
    number_of_inputs=len(list_in)
    number_of_outputs=len(list_out)
    #startdate=dt.date(2008,7,1)
    #enddate=dt.date(2008,8,1)
    alllist=list_in + list_out
    xnow=New_combined[alllist]                         #[startdate:enddate]
    xnow=xnow.dropna(how='any')
    #Drop nans and missing values so that Good data only is used in the training
    xarray=np.array(xnow.dropna().reset_index(drop=True))
    #Define inputs and targets for NN from DF
    inputs =  xarray[:, :number_of_inputs] #first 2 columns
    lastcolums=(-1*number_of_outputs)
    targets = xarray[:, lastcolums:] #last column
    
    # Generate standard layered network architecture and create network
    #different network architectures avaiable
    #conec = mlgraph((number_of_inputs,24,16,number_of_outputs))  # Creates standard multilayer network architecture
    conec = tmlgraph((number_of_inputs,24,16,number_of_outputs))  # Creates multilayer network full connectivity list
    #conec = imlgraph((number_of_inputs,24,16,number_of_outputs))  # Creates multilayer architecture with independent outputs
    
    net = ffnet(conec)
    
    print "TRAINING NETWORK..."
    net.train_tnc(inputs, targets, maxfun = iterations, messages=1)
    #net.train_rprop(inputs, targets, maxiter=iterations)
    #net.train_momentum(inputs, targets, maxfun = iterations, messages=1)
    #net.train_genetic(inputs, targets, maxfun = iterations, messages=1)
    #net.train_cg(inputs, targets, maxfun = iterations, messages=1)
    #net.train_bfgs(inputs, targets, maxfun = iterations, messages=1)
    
    
    # Test network
    print "TESTING NETWORK..."
    output, regression = net.test(inputs, targets, iprint = 0)
    
    print "R-squared:           %s  " %str(regression[0][2])
    #print "max. absolute error: %s  " %str(abs( array(output).reshape( len(output) ) - array(targets) ).max())
    output, regress = net.test(inputs, targets)
    
    #Create array for results. Then loop through elements on the original data to predict the ANN value
    predicted=np.empty((len(xarray),number_of_outputs))
    observed=np.empty((len(xarray),number_of_outputs))
    for index,rowdata in enumerate(xarray):
        predicted[index]=net([rowdata[0:number_of_inputs]])
        observed[index]=np.array(rowdata[-1.0*number_of_outputs : ])
	#observed[index]=np.array(rowdata[(-1.0*number_of_outputs)])
	
    ############################################    
    # Generate output and return new variables
    ############################################
    #Create a new variable called '_NN'
    for index, item in enumerate(list_out):
	ANN_label=str(item+"_NN")
	ANN_label_all=str(item+"_NN_all")
	if is_this_all == True:
	    New_combined[ANN_label_all]=net.call(New_combined[list_in])[:,index] 
	else:
	    New_combined[ANN_label]=net.call(New_combined[list_in])[:,index]    
    
    for index, item in enumerate(list_out):   
        #####################################################
        #  Plots 
        #####################################################
        #Plot time series of all 30 minute data
        mintimeseries_plot(mypathforResults,predicted,observed,regress,item, Site_ID,units,targets,output,list_out,index_str)
        #Plot regression of Tower versus ANN
        regressionANN2(mypathforResults,predicted,observed,regress,item, Site_ID,units,list_out,index_str)
        #Plot diurnals for every second month 6 graphs - only when enough months so all or annual
        if frequency=="all" or frequency=="annual" or is_this_all==True:
	    Doplots_diurnal_monthly(mypathforResults,New_combined,item, Site_ID,units,list_out,index_str,is_this_all)
        #Plot diurnals for every second month 6 graphs
        Doplots_diurnal(mypathforResults,New_combined,item, Site_ID,units,list_out,index_str,frequency)	
        #Plot timeseries of monthly over all periods
        Doplots_monthly(mypathforResults,New_combined,item, Site_ID,units,list_out,index_str,frequency)
                        
    return New_combined
Beispiel #38
0
from ffnet import ffnet
from math import pi, sin, cos

# Let's define network connectivity by hand:
conec = [(1, 2), (1, 3), (1, 4), (1, 5), (2, 6), (3, 6), (4, 6), (5, 6), \
         (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
# Note 1: Biases in ffnet are handled as the connections
#         from special node numbered 0. Input nodes cannot be biased.
# Note 2: Node numbering and order of links in conec is meaningless,
#         but the connections have to be from source to target.
# Note 3: The same connectivity can be obtained using mlgraph function
#         provided with ffnet (layered architecture (1,4,1)).

# Network creation
net = ffnet(conec)

# Generation of training data (sine values for x from 0 to 2*pi)
patterns = 16
input = [[0.]] + [[k * 2 * pi / patterns] for k in xrange(1, patterns + 1)]
target = [[sin(x[0])] for x in input]

# Training 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=5000, messages=1)

# Testing network
    # a = np.concatenate((np.ones(1).T, np.array(x)), axis=1)
    a = np.concatenate((np.ones(1).T, x), axis=1)
    for l in xrange(0, len(theta) - 1):
        a = sigmoid(np.dot(a, theta[l]))
        # print sum(a[a == 1])
        # if sum(a[a == 1]) > 0: raise ValueError('predict warning 1')
    a = np.dot(a, theta[l + 1])
    # if a[0] == 1.0: raise ValueError('predict warning 2')
    # print a[0]
    # if a[0] > 10**11 or a[0] < -10**11:
    #     print('warning!')
    return a[0]


conec = fn.mlgraph((64, 40, 20, 1))
net = fn.ffnet(conec)


def predict3(weights, x):
    net.weights = weights
    s = net.call(x)
    # print(s)
    # assert s != 1
    return s


if __name__ == '__main__':

    # a = list(np.matrix('[1,2;3,4]').T)
    # b = list(np.matrix('[1,3;4,5]').T)
Beispiel #40
0
 def __init__(self, con=(2,2,1)):
     self.network = ffnet(mlgraph(con))
Beispiel #41
0
from ffnet import ffnet, mlgraph
from math import pi, sin, cos

# Let's define network connectivity by hand:
# conec = [(1, 2), (1, 3), (1, 4), (1, 5), (2, 6), (3, 6), (4, 6), (5, 6), \
#         (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
# Note 1: Biases in ffnet are handled as the connections
#         from special node numbered 0. Input nodes cannot be biased.
# Note 2: Node numbering and order of links in conec is meaningless,
#         but the connections have to be from source to target.
# Note 3: The same connectivity can be obtained using mlgraph function
#         provided with ffnet (layered architecture (1,4,1)).

# Network creation
net = ffnet(mlgraph((1, 4, 1)))

# Generation of training data (sine values for x from 0 to 2*pi)
patterns = 16
input = [ [ 0. ] ] + [ [ k * 2 * pi / patterns ] for k in xrange(1, patterns + 1) ]
target = [ [ sin(x[0]) ] for x in input ]

# Training 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=5000, messages=1)

# Testing network
Beispiel #42
0
def createNNetwork(design):
    conec = mlgraph(design)
    return ffnet(conec)
Beispiel #43
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
 def __init__(self, nnStructure):
     self.nnStructure = nnStructure
     conec = mlgraph(nnStructure)
     self.network = ffnet(conec)
Beispiel #45
0

def calculate_network(input_, target_, test_input, test_output, net1):
    network, output, regression, error = single_network(input_, target_, test_input, test_output, net1)
    for i in range(4):
        network1, output1, regression1, error1 = single_network(input_, target_, test_input, test_output, net1)
        if (error1 < error):
            network = network
            output = output1
            regression = regression1
            error = error1
    return network, output, regression, error


CONEC = tmlgraph((54, 10, 24))  # model of connections
NET = ffnet(CONEC)


# NX.draw_graphviz(net.graph, prog='dot') show the network that's nice!
# pl.show()
#
# calculate special days
summ_error = 0.0
for i in range(4):
    net = ffnet(CONEC)  # year have 52 weeks so, we have 52 special days in year
    network, output, regression, error = calculate_network(
        LEARNING_SPECIALIST[i][:-52],
        LEARNING_SPECIALIST_OUTPUT[i][:-52],
        LEARNING_SPECIALIST[i][-52:],
        LEARNING_SPECIALIST_OUTPUT[i][-52:], net)
    summ_error += error
Beispiel #46
0
 def fit(self, training_set, training_target):
     self.feature_size = training_set.shape[1]
     hidden_layer_size = self.hidden_node
     connection_tuple = (self.feature_size, hidden_layer_size, 1)
     self.nn = ffnet(mlgraph(connection_tuple))
     self.nn.train_momentum(training_set, training_target)
Beispiel #47
0
             result.append(float(l[i]))
         train_x.append(result)
 print "FINISH READING TRAIN FILE"
 with open("para_test.txt") as f:
     for l in f:
         l = l.strip().split()
         result = []
         for i in range(len(l)):
             result.append(float(l[i]))
         test_x.append(result)
 print "FINISH READING TEST FILE"
 #train_x = train_x[:5]
 #test_x = test_x[:5]
 #train_y = train_y[:5]
 #test_y = test_y[:5]
 c = ffnet.ffnet(ffnet.mlgraph((len(train_x[0]), 50, 1)))
 print "TRAINING....",
 c.train_tnc(train_x, train_y, messages=1, nproc='ncpu', maxfun=1000)
 print "OK"
 print "TESTING....",
 wrong = 0
 for i in range(len(test_y)):
     result = c.call(test_x[i]).tolist()[0]
     if result >= 0.5:
         result = 1.0
     else:
         result = 0.0
     if result != test_y[i]:
         wrong += 1
 print "OK"
 print float(wrong) / float(len(test_y))
Beispiel #48
0
        if (headerskipped):
            line = line.split(',')
            last = len(line) - 1
            instr = line[0:last]
            inline = []
            for j in range(len(instr)):
                x = float(instr[j])
                input[i, j] = x
            target[i, 0] = float(line[last].strip())
            i += 1
        else:
            headerskipped = True

    f.close()
    return input, target


input, target = readin(infile, inrownum)

testin, testtarget = readin(testfile, testrownum)

connections = mlgraph((inputnum, 100, 1))
net = ffnet(connections)

print('training net...')
#net.train_momentum(input, target, eta=0.5, momentum=.1)
net.train_tnc(input, target)
print('testing net...')
output, regression = net.test(testin, testtarget, iprint=2)
print(output)
print(regression)
        nx.draw_networkx_nodes(G, pos, cmap=color_map, node_color=node_colorss, vmin=0, vmax=1.0)

def deserialize(data, used_activation=None):
    arguments = data['arguments']
    if used_activation is None:
        used_activation = getattr(ActivationFunctions, data['activation_function'])
    nn = ffnet(*arguments)
    for index, layer in enumerate(data['layers']):
        for p_index, perceptron in enumerate(nn.layers[index]):
            perceptron.weights = layer[p_index]
    return nn


def serialize(nn):
    serialized_nn = {'arguments': nn.arguments}
    weights = []
    for layer in nn.layers:
        layer_weights = []
        for perceptron in layer:
            layer_weights.append(perceptron.weights)
        weights.append(layer_weights)
    serialized_nn['layers'] = weights
    serialized_nn['activation_function'] = nn.activation_function.__name__
    return serialized_nn

if __name__ == '__main__':
    args = [[10, 3, 10], ActivationFunctions.tanh]
    nn = ffnet(*args)
    draw_ffnet(nn)
    plt.show()
Beispiel #50
0
from ffnet import ffnet, mlgraph, readdata
import numpy as np
import sys

def readfile( fname ):
    lines = open( fname ).readlines()[1:]
    vec = [ map( float, x.split(',')[0:-1] ) for x in lines ]
    clas = [ x.split(',')[-1] for x in lines ]
    output = [ [1,0,0,0,0,0,0,0,0],
               [0,1,0,0,0,0,0,0,0],
               [0,0,1,0,0,0,0,0,0],
               [0,0,0,1,0,0,0,0,0],
               [0,0,0,0,1,0,0,0,0],
               [0,0,0,0,0,1,0,0,0],
               [0,0,0,0,0,0,1,0,0],
               [0,0,0,0,0,0,0,1,0],
               [0,0,0,0,0,0,0,0,1] ]
    return vec, [ output[int(x.split('_')[-1])-1] for x in\
                       clas ]

if __name__ == '__main__':
    conec = mlgraph((94,30,30,9))
    net = ffnet(conec)
    tra, out = readfile( sys.argv[1] )
    tra = np.array(tra)
    out = np.array(out)
    net.train_tnc( tra, out, maxfun=2000, messages=1 )
Beispiel #51
0
def Fre_ANN_gapfill_func(myBaseforResults,New_combined,Site_ID,list_in,list_out,iterations,latitude,longitude,index_str,ANN_label,frequency,evening,min_threshold,max_threshold,Ustar_filter_type):     
    
    if 'Fc' in list_out:
        units="umol.m-2.s-1"
    elif ('Fe' or 'Fh' or 'Fg') in list_out:
        units="W.m-2"
    else:
        units=" "
    
    ###### User-set IO file locations ######
    
    print "Starting ANN gap filling"
    
    #Check for place to put results - does it exist? If not create
    if not os.path.isdir(myBaseforResults):
        os.mkdir(myBaseforResults)
    #Then subdirectories
    if not os.path.isdir(myBaseforResults+"/ANN"):
        os.mkdir(myBaseforResults+"/ANN")
    mypathforResults=myBaseforResults+"/ANN"  
    if not os.path.isdir(myBaseforResults+"/ANN/Fre"):
        os.mkdir(myBaseforResults+"/ANN/Fre")
    mypathforResults=myBaseforResults+"/ANN/Fre" 
    
    #We need to update VPD for input here so also need e and es
    # Calculate vapour pressure from absolute humidity and temperature
    #  Ah - absolute humidity, g/m3
    #  Ta - air temperature, C
  
    number_of_inputs=len(list_in)
    number_of_outputs=len(list_out)
    #startdate=dt.date(2008,7,1)
    #enddate=dt.date(2008,8,1)
    alllist=list_in + list_out
    #Here now for Re we can further restrict the traing data to be noctural and ustar filtered
    #So first create a series with day_night based on solar geometry
    
    #Get timezone info, call routines from external code
    global timezone
    currentdate="2013-06-01"
    timezone,InDstNow=TimeZone.get_timezone_info(latitude,longitude,currentdate)
    print "AskGEO TimZone offset (hrs):  ", timezone
 
    #Start with blank series
    New_combined['day_night']=np.nan
     
    def day_night(x):
	#Get date from data group
	currentdate= x.index[0]
	currentyear= x.index[0].year
	currentmonth= x.index[0].month
	currentday= x.index[0].day
	basedate=dt.datetime(1900, 1, 1,0,0,0,0)    
	delta=(currentdate-basedate).days  
	#Calculate Approximate Solar noon, call routines from external code
	#Call Solar_Calcs.solar_calculations(Date_input_excel,latitude,longitude,timezone)
	solar_sunrise,solar_noon_for_date,solar_sunset  =Solar_Calcs.solar_calculations(delta,latitude,longitude,timezone)
	#return a fraction.  Convert to decimal hours
	solar_sunrise=solar_sunrise*24
	solar_sunset=solar_sunset*24
	daystart_hour= int(solar_sunrise)
	daystart_minute=int((solar_sunrise-daystart_hour)*60)
	daystart_dt=dt.datetime(currentyear,currentmonth,currentday,daystart_hour,daystart_minute,0)
	dayend_hour= int(solar_sunset)
	dayend_minute=int((solar_sunset-dayend_hour)*60)
	dayend_dt=dt.datetime(currentyear,currentmonth,currentday,dayend_hour,dayend_minute,0)	

	x['day_night'][daystart_dt:dayend_dt]=1
	#Define evening as 3 hours after sunset.  Needed for van Gorsel approach
	d=dt.timedelta(hours=3)
	eveningend_dt=dayend_dt+d
	x['day_night'][dayend_dt:eveningend_dt]=2
	#Else fill remainder with night
	x['day_night'][x['day_night'].isnull()]=3	
	#print x
	#print x[dayend_dt:eveningend_dt]
	#print solar_sunset
	#print daystart_dt
	#print dayend_dt
	#print eveningend_dt	
	return x
    
    #For each day of each year run the function to calculate day or night
    New_combined=New_combined.groupby([lambda x: x.year,lambda x: x.month,lambda x: x.day]).apply(day_night)
    #New_combined.to_csv(myBaseforResults+'/'+'Final_combined_'+Site_ID+'.csv', sep=',')
    
    ###############################
    #Define the data to select
    ###############################
    #Can select period of 'night' (2) and or 'evening' (3)
    if evening==True:
	xnow=New_combined[(New_combined['day_night']==3)]
    else:
	xnow=New_combined[(New_combined['day_night']==2)]
    # Use the actual ustar_used column which is defined from the type of ustar thershold approach chosen in the config
    xnow=xnow[xnow['ustar']>xnow['ustar_used']]

	
    #Remove -ve and +ve spikes which are uptake and inconsistent with Fre.
    xnow=xnow[xnow['Fc']>min_threshold][xnow['Fc']<max_threshold]
    xnow=xnow[alllist]
    #Drop nans and missing values so that Good data only is used in the training
    xnow=xnow.dropna(how='any')	

    xarray=np.array(xnow.dropna().reset_index(drop=True))
    #Define inputs and targets for NN from DF
    inputs =  xarray[:, :number_of_inputs] #first 2 columns
    lastcolums=(-1*number_of_outputs)
    targets = xarray[:, lastcolums:] #last column
    
    # Generate standard layered network architecture and create network
    #different network architectures avaiable
    #conec = mlgraph((number_of_inputs,24,16,number_of_outputs))  # Creates standard multilayer network architecture
    conec = tmlgraph((number_of_inputs,6,4,number_of_outputs))  # Creates multilayer network full connectivity list
    #conec = imlgraph((number_of_inputs,24,16,number_of_outputs))  # Creates multilayer architecture with independent outputs
    
    net = ffnet(conec)
    
    print "TRAINING NETWORK..."
    #net.train_tnc(inputs, targets, maxfun = iterations, messages=1)
    try:
	net.train_rprop(inputs, targets, maxiter=iterations)
    except:
	net.train_tnc(inputs, targets, maxfun = iterations, messages=1)
    #net.train_momentum(inputs, targets, maxfun = iterations, messages=1)
    #net.train_genetic(inputs, targets, maxfun = iterations, messages=1)
    #net.train_cg(inputs, targets, maxfun = iterations, messages=1)
    #net.train_bfgs(inputs, targets, maxfun = iterations, messages=1)
    
    
    # Test network
    print "TESTING NETWORK..."
    output, regression = net.test(inputs, targets, iprint = 0)
    
    print "R-squared:           %s  " %str(regression[0][2])
    #print "max. absolute error: %s  " %str(abs( array(output).reshape( len(output) ) - array(targets) ).max())
    output, regress = net.test(inputs, targets)
    
    #Create array for results. Then loop through elements on the original data to predict the ANN value
    predicted=np.empty((len(xarray),number_of_outputs))
    observed=np.empty((len(xarray),number_of_outputs))
    for index,rowdata in enumerate(xarray):
        predicted[index]=net([rowdata[0:number_of_inputs]])
        observed[index]=np.array(rowdata[(-1.0*number_of_outputs)])
    
    ############################################    
    # Generate output and return new variables
    ############################################
 
    for index, item in enumerate(list_out):
        New_combined[ANN_label]=net.call(New_combined[list_in])[:,index]
    
    #TEST
    #New_combined.to_csv("E:/My Dropbox/Dropbox/Data_flux_data/Site data processing/HowardSprings/Advanced/test_assertion.csv") 
    

    #####################################################
    #  Plots 
    #####################################################
    #Plot time series of all 30 minute data
    mintimeseries_plot(mypathforResults,predicted,observed,regress,item, Site_ID,units,targets,output,ANN_label,index_str)
    #Plot regression of Tower versus ANN
    regressionANN2(mypathforResults,predicted,observed,regress,item, Site_ID,units,ANN_label,index_str)
    #Plot diurnals for every second month 6 graphs
    Doplots_diurnal(mypathforResults,New_combined,item, Site_ID,units,ANN_label,index_str)
    #Plot diurnals for every second month 6 graphs
    if frequency=="all" or frequency=="annual":
	Doplots_diurnal_monthly(mypathforResults,New_combined,item, Site_ID,units,ANN_label,index_str)    
    #Plot timeseries of monthly over all periods
    Doplots_monthly(mypathforResults,New_combined,item, Site_ID,units,ANN_label,index_str)
		    
    ###################################################
    #  File stuff
    ###################################################
    
 
    return (New_combined)