Ejemplo n.º 1
0
def main():
    nn = NN([2,8,8,1])

    X, Y, reds, greens = get_train(new_plot=DRAW_PLOT, name=NAME_FILE)
    Xtest = get_test()

    fig = plt.figure()
    fig.show()
    for i in range(30):

        nn.train(X, Y, iters=100, t=i)
        A = nn.predict(Xtest)
        res = get_contour(A, Y)

        plt.clf()
        plt.scatter(Xtest[0], Xtest[1], c=res)
        plt.scatter(X[0], X[1], c=[[1,0,0]]*reds + [[0,1,0]]*greens)

        props = dict(boxstyle='round', facecolor='white', alpha=0.9)
        plt.text(-0.95,0.95, f"epoch {(i+1)*100}\ncost = {round(nn.costs[-1],8)}", 
            fontsize=8,verticalalignment='top', bbox=props)
        fig.canvas.draw()

    plt.show()
    plt.plot(nn.costs)
    plt.title("Cost Function")
    plt.xlabel("epoch")
    plt.show()
Ejemplo n.º 2
0
class Controller():
    def __init__(self):
    	self.nn = None#NN()
    
    def set_file(self,file):
        #print("seteo",file)
        self.nn = NN()
        self.nn.config(tickle.load(file))
        return "Archivo seteado con éxito"
    def classify(self,images):
    	msg = False
    	if self.nn:
    		imgs = []
    		for i in images:
    			imgs.append(cv2.imread(i,cv2.IMREAD_GRAYSCALE).flatten())
    		msg = str(self.nn.classify_image(imgs))
    	return msg
    def get_input_size(self):
    	return "Input size: "+ str(self.nn.input_size)
    def get_output_size(self):
    	return "Output size: "+str(self.nn.output_size)
    def get_hidden_layers(self):
    	return "Hidden layers(Ws): "+str(self.nn.hidden_layers_size)
    def get_learning_rate(self):
    	return "Learning rate: "+str(self.nn.learning_rate)
    def get_dropout(self):
    	return "Dropout: "+str(self.nn.dropout)
Ejemplo n.º 3
0
 def load_model(self, model_location):
     '''Loads a model that was saved using save_model().
     
     [model_location example: /datefolder/file_name.h5]
     [model_location example: /2018-12-20/13h-18m.....h5]
     '''
     NN.load_model(self, model_location)
Ejemplo n.º 4
0
    def load_from_model(self, model_name):
        """
        Metoda nacita z predaneho modelu jednotlive slovniky klasifikacnich trid a spoustí GUI, ceka na stisk tlacitka a pote klasifikuje zadanou vetu.
        :param model_name: model ze ktereho se maji nacist jednotliva data.
        """

        with open(model_name, "r") as read_file:
            json_load = json.load(read_file)
            if json_load["namepriz"] == "BagOfWords":
                self.priz_metoda = BagOfWords()
            elif json_load["namepriz"] == "TfIdf":
                self.priz_metoda = TfIdf()
            elif json_load["namepriz"] == "NGram":
                self.priz_metoda = NGram()

            self.priz_metoda.words = json_load["words"]
            self.priz_metoda.klas_tridy = json_load["klas_tridy"]
            self.priz_metoda.prior = json_load["prior"]

            if json_load["nameklas"] == "NaiveBayes":
                self.klasifikator = NaiveBayes(self.priz_metoda)
            elif json_load["nameklas"] == "NN":
                self.klasifikator = NN(self.priz_metoda)

        self.top.title("Classify")
        self.top.geometry('400x300')
        buttonCommit = Button(self.top, height=1, width=10, text="Commit",
                              command=lambda: self.retrieve_input())
        self.text1.pack()
        buttonCommit.pack()
        self.label.pack()
        self.top.mainloop()
Ejemplo n.º 5
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']

        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        # self.n_W_particles = hyperparams['n_W_particles']  #S
        # self.n_W_particles = tf.placeholder(tf.int32, None)  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P
        # self.n_z_particles = tf.placeholder(tf.int32, None)  #P
        self.n_transitions = hyperparams[
            'leapfrog_steps']  #this is leapfrog steps, whereas T=1 like the paper

        # self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        # self.decoder_act_func = tf.tanh
        # self.encoder_net = hyperparams['encoder_net']
        # self.decoder_net = hyperparams['decoder_net']

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])

        # self.batch_frac = tf.placeholder(tf.float32, None)
        self.batch_size = tf.shape(self.x)[0]  #B

        #Define networks q_zlx, q_vlxz, r_vlxz, p_xlz

        # q(z|x)
        net1 = NN([self.x_size, 300, 300, self.z_size * 2], tf.nn.elu)
        # q(v|x,z)
        net2 = NN([self.x_size + self.z_size, 300, 300, self.z_size * 2],
                  tf.nn.elu)
        # r(v|x,z)
        net3 = NN([self.x_size + self.z_size, 300, 300, self.z_size * 2],
                  tf.nn.elu)
        # p(x|z)
        net4 = NN([self.z_size, 300, 300, self.x_size], tf.tanh)

        #Objective
        log_probs_list = self.log_probs(self.x, net1, net2, net3, net4)

        self.elbo = self.objective(*log_probs_list)
        self.iwae_elbo = self.iwae_objective(*log_probs_list)

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate,
            epsilon=1e-02).minimize(-self.elbo)

        # for var in tf.global_variables():
        #     print var

        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
Ejemplo n.º 6
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']

        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        self.n_W_particles = hyperparams['n_W_particles']  #S
        # self.n_W_particles = tf.placeholder(tf.int32, None)  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P
        # self.n_z_particles = tf.placeholder(tf.int32, None)  #P


        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_act_func = tf.tanh
        self.encoder_net = hyperparams['encoder_net']
        self.decoder_net = hyperparams['decoder_net']

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])

        # self.batch_frac = tf.placeholder(tf.float32, None)
        self.batch_size = tf.shape(self.x)[0]   #B

        #Define networks

        # q(z|x)
        net1 = NN(self.encoder_net, self.encoder_act_func, self.batch_size)
        # q(z|x,z)
        net2 = NN(self.decoder_net, self.decoder_act_func, self.batch_size)
        # r(z|x,z)
        net2 = NN(self.decoder_net, self.decoder_act_func, self.batch_size)
        # p(x|z)
        net2 = NN(self.decoder_net, self.decoder_act_func, self.batch_size)
        

        #Objective
        self.elbo = self.log_probs(self.x, encoder, net1, net2, net3, net4)

        # self.elbo = self.objective(*log_probs)
        # self.iwae_elbo = self.iwae_objective(*log_probs)


        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)


        # for var in tf.global_variables():
        #     print var


        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
def get_nn_predict(X, param):
    model = NN(learning_rate=0.05,
               num_of_training=100,
               input_size=X.shape[1],
               hidden_n=2,
               parameters=list(param))
    predvalue = model.predict(X)
    return np.array(predvalue)
def get_model_file():
    global NN
    model_file_name = filedialog.askopenfilename()
    if model_file_name is not "":
        NN = NeuralNetework()
        NN.load_model(model_file_name)
        model_file_label["text"] = "Model File:        " + os.path.split(
            model_file_name)[1]
Ejemplo n.º 9
0
 def generate_graphs(self,
                     training_history,
                     graph_save_folder_path,
                     show_graphs=False):
     '''Create a graph of accuracy and loss over time
     '''
     NN.generate_graphs(self, training_history, graph_save_folder_path,
                        show_graphs)
Ejemplo n.º 10
0
def epoch(eta=0.04,
          penalty=0.4,
          epochs=200,
          mini_batch_size=100,
          t0=5,
          t1=50,
          create_conf=False):
    layer1 = DenseLayer(features, 100, sigmoid())
    #layer2 = DenseLayer(100, 50, sigmoid())
    #layer3 = DenseLayer(100, 50, sigmoid())
    layer4 = DenseLayer(100, 10, softmax())

    layers = [layer1, layer4]
    network = NN(layers)
    cost_array = np.zeros((epochs, 2))

    def learning_schedule(t):
        return 0.04  #t0/(t+t1)

    for i in range(epochs):
        random.shuffle(batch)
        X_train_shuffle = X_train[batch]
        one_hot_shuffle = one_hot[batch]
        Y_train_shuffle = Y_train[batch]
        #eta = learning_schedule(i)
        network.SGD(ce, 100, X_train_shuffle, one_hot_shuffle, eta, penalty)
        Y_pred = np.argmax(network.feedforward(X_test), axis=1)
        Y_pred_train = np.argmax(network.feedforward(X_train_shuffle), axis=1)
        cost_array[i, 0] = accuracy()(Y_test.ravel(), Y_pred)
        cost_array[i, 1] = accuracy()(Y_train_shuffle.ravel(), Y_pred_train)
    print("accuracy on train data = %.3f" % cost_array[-1, 1])
    print("accuracy on test data = %.3f" % cost_array[-1, 0])
    if create_conf == True:
        #creating confusion matrix
        numbers = np.arange(0, 10)
        conf_matrix = confusion_matrix(Y_pred, Y_test, normalize="true")
        heatmap = sb.heatmap(conf_matrix,
                             cmap="viridis",
                             xticklabels=["%d" % i for i in numbers],
                             yticklabels=["%d" % i for i in numbers],
                             cbar_kws={'label': 'Accuracy'},
                             fmt=".2",
                             edgecolor="none",
                             annot=True)
        heatmap.set_xlabel("pred")
        heatmap.set_ylabel("true")

        heatmap.set_title(r"FFNN prediction accuracy with $\lambda$ = {:.1e} $\eta$ = {:.1e}"\
            .format(penalty, eta))
        fig = heatmap.get_figure()
        fig.savefig("../figures/MNIST_confusion_net.pdf",
                    bbox_inches='tight',
                    pad_inches=0.1,
                    dpi=1200)
        plt.show()
    return cost_array[-1]
Ejemplo n.º 11
0
def debug_statments(X,y):
    file_list,train_y = generateCoverage(BubbleSort,X,y,"BublleSort")
    print(train_y)
    train_X,lines = ParseCoverage("bubbleSort.py",file_list)
    print(train_X)
    statements_suspect = NN(train_X,train_y,(300),lines)
    print(lines)
    print(statements_suspect)
    print(lines[statements_suspect.tolist().index(np.max(statements_suspect))])
    print(np.max(statements_suspect))
Ejemplo n.º 12
0
def main():
    #nn = NN([1,1], learning_rate=.1)
    datos = np.genfromtxt("icgtp1datos/concentlite.csv", dtype=float, delimiter=',')
    v_datos = convert_to_one_dimension(datos) #datos en una dimensión
    particiones = particionar(v_datos, 5, .8, True)
    
    v_datos_x = v_datos[:,0]
    v_datos_y = v_datos[:,-1]
    
    for _i in range(len(particiones)):
        nn = NN([1,1], learning_rate=.1)
        v_true = []
        v_false = []
        v_false_positive = []
        v_false_negative = []
        epocas_convergencia_iteracion = nn.Train(v_datos[particiones[_i][0]], max_epochs=150, tol_error=.1, alfa=0)
        print(f"Epocas para convergencia en particion {_i+1}: {epocas_convergencia_iteracion+1}")  #max_epochs = 150    
        outputs_particiones = []
        
        for _p in particiones[_i][1]:
            outputs_particiones.append(nn.Test(np.array([v_datos_x[_p]]))) 
        
        cont_error = 0
        cont = 0
        for _k in particiones[_i][1]:
            auxLabel = -1
            if(outputs_particiones[cont]) > 0:
                auxLabel = 1
                v_true.append(_k)
            else:
                auxLabel = -1
                v_false.append(_k)

            if(auxLabel != v_datos_y[_k]):
                if(v_datos_y[_k] == -1):
                    v_false_positive.append(_k)
                else:
                    v_false_negative.append(_k)
                    
                cont_error += 1
            cont += 1
        errorPart = cont_error/len(outputs_particiones)
        print("Error en partición "+repr(_i+1)+" ->  "+repr(errorPart))
        print(f"Cantidad de errores en particion: {cont_error} de {len(outputs_particiones)} patrones.")
        print(f"Falsos positivos: {len(v_false_positive)} | Falsos negativos: {len(v_false_negative)}.")
        print("\n")


    plt.scatter(datos[v_true,0], datos[v_true,1], color=(1,0,0),label="Verdadero")
    plt.scatter(datos[v_false,0], datos[v_false,1], color=(0,0,1),label="Falso")
    plt.scatter(datos[v_false_positive,0], datos[v_false_positive,1], color=(0,1,0),label="Falso Positivo")
    plt.scatter(datos[v_false_negative,0], datos[v_false_negative,1], color=(1,1,0),label="Falso Negativo")
    plt.legend(loc="lower right", title="", frameon=False)
    plt.title("Concentlite con una dimensión")
    plt.show()
Ejemplo n.º 13
0
    def create_validation_from_training(self, num_validation_samples):
        '''Create a validation set from the training set
        '''
        NN.create_validation_from_training(self, num_validation_samples)
        n = 250

        self.log("Sample user X: {} item X: {}".format(
            self.validation_data[0][n], self.validation_data[1][n]))
        self.log("Sample rating Y: {} type: {}".format(
            self.validation_labels[n], type(self.validation_labels[n])))
        self.log("Created validation set from training set")
Ejemplo n.º 14
0
    def evaluate_model(self):
        '''Evaluate the model after it has been trained.
        '''
        NN.evaluate_model(self)

        self.log("Evaluating model...")
        self.log(
            "First Test Sample: ({}, {}) -> Expected: {} Guessed: {}".format(
                self.test_data[0][0], self.test_data[1][0],
                self.test_labels[0],
                self.model.predict(
                    ([self.test_data[0][0]], [self.test_data[1][0]]))[0][0]))
Ejemplo n.º 15
0
    def build_model(self, level=0):
        model = NN(self.cfg.n_layer[level], self.cfg.D_in, self.cfg.H,
                   self.cfg.D_out)
        criterion = torch.nn.MSELoss(reduction='sum')
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=self.cfg.learning_rate,
                                     weight_decay=self.cfg.weight_decay)
        print("-" * 20 + "Model" + "-" * 20)
        print(
            f"Model: {model}\nCriterion: {criterion}\nOptimizer: {optimizer}\n")

        print("-" * 20 + "Training" + "-" * 20)
        return model, criterion, optimizer
Ejemplo n.º 16
0
 def __init__(self, bool_display):
     print('started Sim.')
     SimAccount.initialize()
     self.loop_i = 0
     self.max_amount = 1
     self.nn = NN()
     self.nn_input_data_generator = NNInputDataGenerator()
     self.gene = Gene('./Model/best_weight.csv')
     self.pred = -1
     self.pred_log = []
     self.__bool_display = bool_display  #True=display log
     th = threading.Thread(target=self.__sim_thread)
     th.start()
Ejemplo n.º 17
0
def feature_fit(x_list,
                y_list,
                layer_sizes,
                activations,
                N_epochs=10,
                learning_rate=1.0,
                threshold=1e-3,
                lambda_reg=0.0):
    # given x,y pairs, construct the neural network, fit the data, and return the feature vector, including the normalization parameters
    N = len(x_list)
    y_norm = np.array(y_list)
    neural_net = NN(layer_sizes, activations)
    #neural_net.derivative_check(m=5, epsilon=1e-4, verbose=False)
    inputs = np.reshape(np.array(x_list), (len(x_list), 1)).T
    Js = []
    for i in range(N_epochs):
        A = neural_net.forward_prop(inputs)
        dAdZ = 1
        J = 1 / 2.0 * np.sum(np.power(A - y_norm, 2)) / N
        Js.append(J)
        dJdZ = -(A - y_norm) * dAdZ
        neural_net.back_prop(dJdZ)
        neural_net.update_weights(learning_rate=learning_rate,
                                  lambda_reg=lambda_reg)
        if J < threshold:
            break
    feature_vec = neural_net.flatten_parameters()
    return feature_vec, J
Ejemplo n.º 18
0
    def __init__(self,
                 dataset,
                 model_options=AllOptions.ModelOptions,
                 data_options=AllOptions.DataOptions):
        default_logger.log_time()

        NN.__init__(self, dataset, model_options, data_options)

        # self.num_items = dataset.get_num_items()
        # self.num_users = dataset.get_num_users()
        self.num_users = 24303
        self.num_items = 10672

        # Get the data
        self.log("Initialized UserItemRecommender")
Ejemplo n.º 19
0
def mdstr_NN(feature_map):
    db = {
        "7线": ["位于无名指下方的竖线", "高血压或者低血压"],
        "9线": ["起于食指与中指指缝之间,以弧形延伸到无名指与小指指缝之间", "过敏体质,接触电脑频繁,不孕症"],
        "10线": ["中指掌指褶纹下一弧形半月圆", "近视眼"]
    }
    illness_map = NN.NN(feature_map)
    mdstr = "<hr><h3>神经网络预测</h3>"
    mdstr += "<h5>特征提取</h5>"
    mdstr += "<table>"
    mdstr += "<tr><th>名称</th><th>描述</th><th>结果</th></tr>"
    for feature in db:
        mdstr += "<tr> <td>{0}</td><td>{1}</td><td> {2} </td></tr>  \n".format(
            feature, db[feature][0], feature in feature_map)
    mdstr += "</table>\n"

    mdstr += "<h5>诊断结果</h5>  \n"
    mdstr += "<table>"
    mdstr += "<tr><th>名称</th><th>概率</th></tr>"
    for illness in illness_map:
        mdstr += "<tr><td> {0} </td><td> {1} </td></tr> \n".format(
            illness, illness_map[illness])
    mdstr += "</table>\n"

    return mdstr
Ejemplo n.º 20
0
def prepare_predicter(y, x):
    if prediction_mode == "ESN":
        predicter = ESN(n_input=shared_input_data.shape[1],
                        n_output=1,
                        n_reservoir=n_units,
                        weight_generation="advanced",
                        leak_rate=leak_rate,
                        spectral_radius=spectral_radius,
                        random_seed=random_seed,
                        noise_level=noise_level,
                        sparseness=sparseness,
                        regression_parameters=[regression_parameter],
                        solver="lsqr",
                        input_density=input_density /
                        shared_input_data.shape[1])
    elif prediction_mode == "NN":
        predicter = NN(k=k)
    elif prediction_mode == "RBF":
        predicter = RBF(sigma=width, basisPoints=basis_points)
    else:
        raise ValueError(
            "No valid prediction_mode choosen! (Value is now: {0})".format(
                prediction_mode))

    return predicter
Ejemplo n.º 21
0
	def __init__(self, step_size=1, search_resolution=20, goal_weight=100, \
				edge_weight=-100, mob_weight=-10, turn_weight=0):

		# Agent parameters:
		self.agent_step_size = step_size
		self.agent_search_resolution = search_resolution # Smaller values make computation faster, which seems to offset any benefit from the higher resolution.
		self.agent_goal_weight = goal_weight
		self.agent_edge_weight = edge_weight
		self.agent_mob_weight = mob_weight
		self.agent_turn_weight = turn_weight # Negative values to penalise turning, positive to encourage.
		#output is the binary representation of our output index ([o,search_resolution))
		self.agent_decision_net = NN(search_resolution, \
			search_resolution, 2, math.ceil(math.log(search_resolution,2)), \
			random_seed=10, max_epoch=50000, learning_rate=0.25, momentum=0.95)
		# self.agent_decision_net = NeuralMMAgent(search_resolution, \
		# 	search_resolution, 2, math.ceil(math.log(search_resolution,2)), \
		# 	random_seed=10, max_epoch=50000, learning_rate=0.25, momentum=0.95)

		self.training_obs = []
		self.training_out = []

		self.root = tk.Tk()
		self.root.wm_title("Collect the " + MalmoSim.GOAL_TYPE + "s, dodge the " + MalmoSim.MOB_TYPE + "s!")

		self.canvas = tk.Canvas(self.root, width=MalmoSim.CANVAS_WIDTH, height=MalmoSim.CANVAS_HEIGHT, borderwidth=0, highlightthickness=0, bg="black")
		self.canvas.pack()
		self.root.update()
Ejemplo n.º 22
0
    def load(self):
        self.group = []
        for i in range(NUM_PLAYER):
            boat = Boat()
            brain = NN()
            brain.init_weights()
            player = Player(brain, boat)
            self.group.append(player)

        if LOAD_PATH is not None:
            checkpoint = torch.load(LOAD_PATH)
            self.current_generation = checkpoint['generation']
            for i, ind in enumerate(self.group):
                str_ind = 'ind_' + str(i)
                ind.brain.load_state_dict(checkpoint[str_ind])
                ind.brain.eval()
Ejemplo n.º 23
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']
        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_act_func = tf.tanh
        self.encoder_net = hyperparams['encoder_net']
        self.decoder_net = hyperparams['decoder_net']
        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        self.n_W_particles = hyperparams['n_W_particles']  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])
        self.batch_size = tf.shape(self.x)[0]   #B
        self.batch_frac = tf.placeholder(tf.float32, None)
        

        #Define endocer and decoder
        with tf.variable_scope("encoder"):
            encoder = NN(self.encoder_net, self.encoder_act_func, self.batch_size)

        with tf.variable_scope("decoder"):
            decoder = BNN(self.decoder_net, self.decoder_act_func, self.batch_size)
        

        #Objective
        log_probs = self.log_probs(self.x, encoder, decoder)

        self.elbo = self.objective(*log_probs)
        # self.iwae_elbo = self.iwae_objective(*log_probs)

        self.iwae_elbo_test = self.iwae_objective_test(*log_probs)

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)


        # for var in tf.global_variables():
        #     print var


        # FOR INSPECING MODEL

        # self.decoder_means = decoder.W_means
        # self.decoder_logvars = decoder.W_logvars

        self.recons, self.priors = self.get_x_samples(self.x, encoder, decoder)



        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
Ejemplo n.º 24
0
    def objective(self, x):
        '''
        Returns scalar to maximize
        '''

        encoder = NN(self.encoder_net, self.encoder_act_func, self.batch_size)
        decoder = BNN(self.decoder_net, self.decoder_act_func, self.batch_size)

        log_px_list = []
        log_pz_list = []
        log_qz_list = []
        log_pW_list = []
        log_qW_list = []

        for W_i in range(self.n_W_particles):

            # Sample decoder weights  __, [1], [1]
            W, log_pW, log_qW = decoder.sample_weights()

            # Sample z   [P,B,Z], [P,B], [P,B]
            z, log_pz, log_qz = self.sample_z(x, encoder, decoder, W)
            # z: [PB,Z]
            z = tf.reshape(z,
                           [self.n_z_particles * self.batch_size, self.z_size])

            # Decode [PB,X]
            y = decoder.feedforward(W, z)
            # y: [P,B,X]
            y = tf.reshape(y,
                           [self.n_z_particles, self.batch_size, self.x_size])

            # Likelihood p(x|z)  [P,B]
            log_px = log_bern(x, y)

            #Store for later
            log_px_list.append(log_px)
            log_pz_list.append(log_pz)
            log_qz_list.append(log_qz)
            log_pW_list.append(log_pW)
            log_qW_list.append(log_qW)

        log_px = tf.stack(log_px_list)  #[S,P,B]
        log_pz = tf.stack(log_pz_list)  #[S,P,B]
        log_qz = tf.stack(log_qz_list)  #[S,P,B]
        log_pW = tf.stack(log_pW_list)  #[S]
        log_qW = tf.stack(log_qW_list)  #[S]

        # Calculte log probs for printing
        self.log_px = tf.reduce_mean(log_px)
        self.log_pz = tf.reduce_mean(log_pz)
        self.log_qz = tf.reduce_mean(log_qz)
        self.log_pW = tf.reduce_mean(log_pW)
        self.log_qW = tf.reduce_mean(log_qW)
        self.z_elbo = self.log_px + self.log_pz - self.log_qz

        #Calc elbo
        elbo = self.log_px + self.log_pz - self.log_qz + self.batch_frac * (
            self.log_pW - self.log_qW)

        return elbo
Ejemplo n.º 25
0
def main():

    # download mnsit pkl from https://s3.amazonaws.com/img-datasets/mnist.pkl.gz
    f = gzip.open(
        'C:/Users/ivans_000/Desktop/MASTER/Spring2019/Deep_Learning/Data/mnist.pkl.gz',
        'rb')
    if sys.version_info < (3, ):
        data = cPickle.load(f)
    else:
        data = cPickle.load(f, encoding='bytes')
    f.close()
    (x_train, y_train), (x_test, y_test) = data
    sh = x_train.shape
    x_train = x_train.reshape(sh[0], sh[1] * sh[2])
    nn = NN(784, 50, 10)
    nn.train(x_train, y_train)
    a = 5
Ejemplo n.º 26
0
    def __init__(self, hyperparams):

    	# tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']
        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_act_func = tf.tanh
        self.encoder_net = hyperparams['encoder_net']
        self.decoder_net = hyperparams['decoder_net']
        self.z_size = hyperparams['z_size']
        self.x_size = hyperparams['x_size']
        self.rs = 0
        self.n_W_particles = hyperparams['n_W_particles']

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])
        self.batch_size = tf.placeholder(tf.int32, None)
        self.n_z_particles = tf.placeholder(tf.int32, None)
        self.batch_frac = tf.placeholder(tf.float32, None)


        # Model
        encoder = NN(self.encoder_net, self.encoder_act_func, self.batch_size)
        decoder = BNN(self.decoder_net, self.decoder_act_func, self.batch_size)


        # q(W)  # p(W)
        W, log_pW, log_qW = decoder.sample_weights()

        # q(z|x,W)   

        # p(z)

        

        # p(x|z,W)







        
        #Objective
        self.elbo = self.objective(self.x)

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)

        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
        self.sess = tf.Session()
Ejemplo n.º 27
0
    def __init__(self, session, hasShadowNet, state_size, action_size,
                 hidden_state_size):
        NN.__init__(self, session, hasShadowNet)
        # nothing special:
        inputLayer = self.buildInputLayer("inputStates",
                                          shape=[None, state_size])
        h1 = self.buildLinearReluWire(inputLayer,
                                      [state_size, hidden_state_size])
        #for i in range(numOfHiddenLayers-1): # repeat (numOfHiddenLayers-1) times
        h1 = self.buildLinearReluWire(h1,
                                      [hidden_state_size, hidden_state_size])
        out = self.buildLinearWire(h1, [hidden_state_size, action_size])
        self.setOutLayer(out)

        # a bit unique:
        Qgradient = self.buildInputLayer("Qgradients",
                                         shape=[None, action_size])
        self.addAscentOperation(Qgradient)
Ejemplo n.º 28
0
 def initNN(self, file, hlayers, hnodes, classification):
     input = file.shape[1] - 1
     if classification == 'regression':
         output = 1
     if classification == 'classification':
         self.classes = list(file['class'].unique())
         output = file['class'].nunique()
     neuralNet = NN.getNN(self, input, hlayers, hnodes, output)
     return (neuralNet)
Ejemplo n.º 29
0
 def initNNThread(self):
     from NN import NN
     # 必须导入,原始数据
     self.trainx = np.load('./NN/TGAM/TGAM_trainx.npy')
     self.trainy = np.load('./NN/TGAM/TGAM_trainy.npy') - 1  # 标签从零开始
     self.testx = np.load('./NN/TGAM/TGAM_testx.npy')
     self.testy = np.load('./NN/TGAM/TGAM_testy.npy') - 1
     # 初始模型训练 导入原始数据
     self.nn = NN(self.trainx, self.trainy, self.testx, self.testy)
Ejemplo n.º 30
0
    def __init__(self, session, hasShadowNet, state_size, action_size, hidden_state_size):
        NN.__init__(self, session, hasShadowNet)
        # nothing special:
        inputStates = self.buildInputLayer("inputStates", shape=[None, state_size])
        inputActions = self.buildInputLayer("inputActions", shape=[None, action_size])
        inputYs = self.buildInputLayer("inputYs", shape=[None])
        h1 = self.buildLinearReluWire(inputStates, [state_size, hidden_state_size])
        #for i in range(numOfHiddenLayers-1): # repeat (numOfHiddenLayers-1) times
        h1 = self.buildLinearReluWire(h1, [hidden_state_size, hidden_state_size])
        h1 = self.buildJointLinearReluWire(h1, [hidden_state_size, hidden_state_size], inputActions, [action_size, hidden_state_size])
        tmp1 = self.buildLinearWire(h1, [hidden_state_size, action_size])
        out = self.buildReduceSum(tmp1, reduction_indices=1)
        self.setOutLayer(out)

        # a bit unique:
        # action_size =1, so we do not need reduce_sum or action_choice
        self.error = tf.reduce_mean(tf.square(inputYs - out)) # all, every row has only one, let us take fast path
        self.addMinimizeOperation(tf.train.AdamOptimizer(0.001).minimize(self.error))
        self.addAnyNamedOperation("goa", tf.gradients(out, inputActions))
Ejemplo n.º 31
0
class RankFilter:
	def __init__(self, feature_vectors):
		self.feature_vectors = feature_vectors
		self.pre_calc = PreCalc(feature_vectors)
		self.NNTree = NN(feature_vectors)
		self.features_count = self.NNTree.tree.features_count

	def filter(self, k):
		weights = [0] * self.features_count

		for i, vector in enumerate(self.feature_vectors):
			print(i)
			nearest = {pr[0]:self.NNTree.nearest_by_rel(vector, k, pr[0]) for pr in self.pre_calc.probabilities.items()}
			print(str(i) + "-th nearest found.")
			for i in xrange(0, len(weights)):
				weights[i] += self.gather_misses(vector, nearest, i)
				weights[i] -= self.gather_nearest(vector, nearest, i, vector.rel)
		return weights

	def gather_nearest(self, vector, nearest, feature_id, rel):
		nn = nearest[rel]
		s = 0
		for hit in nn:
			s += self.abs_dist(vector.get_feature(feature_id), hit.feature_vector.get_feature(feature_id))
		return float(s) / len(nn)

	def gather_misses(self, vector, nearest, feature_id):
		s = 0
		for kv in nearest.items():
			rel = kv[0]
			miss = kv[1]
			if rel == vector.rel:
				continue
			n_dist = self.gather_nearest(vector, nearest, feature_id, rel)
			rel_diff = self.rel_dist(vector.rel, rel)
			p_dist = self.pre_calc.probabilities[rel] / (1 - self.pre_calc.probabilities[vector.rel])
			s += n_dist * rel_diff * p_dist
		return s

	def rel_dist(self, x, y):
		return math.log(1 + math.fabs(x - y))

	def abs_dist(self, x, y):
		return math.fabs(x - y)
Ejemplo n.º 32
0
n_redundant = 0 # number of dimensions of input which are redundant
output_dim = 5 # dimensions of output
num_passes = 3000 # number of training passes for the NN
n_samples = 2000 # number of samples
test_fraction = 0.2 # fraction of generated data to set aside as test data
hidden_layer = [3, 5, 10, 20, 40, 80] # number of hidden layer sizes to test

X, y = make_classification(n_samples=n_samples, n_classes=output_dim, n_features=input_dim, n_informative=input_dim-n_redundant, n_redundant=n_redundant, random_state=0)

features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(X, y, random_state = 0, test_size = test_fraction)

new_labels_train = []

for sample in labels_train:
	new_sample = []
	for i in range(output_dim):
		if sample == i:
			new_sample.append(1)
		else:
			new_sample.append(0)
	new_labels_train.append(new_sample)

for i in hidden_layer:
	clf = NN(i, input_dim, output_dim)
	print("Training a NN with " + repr(i) + " size hidden layer...")
	clf.train(np.array(features_train), np.array(new_labels_train), print_loss=True, num_passes=num_passes)
	print("\nPredicting...")
	pred = clf.predict(features_test)
	print("\nAccuracy Score:")
	print(accuracy_score(pred, labels_test))
	print("\n")
Ejemplo n.º 33
0
for game_num in range(0,n_games):
    print("Playing game " + repr(game_num) + "...")

    playerturn = random.randint(0,1)

    turn = 0

    while not game.won:
        boardlist = game.boardlist

        if turn%2 == playerturn:
            spot = random.randint(0, boardlist_size-1)
            while not game.addmark(spot):
                spot = random.randint(0, boardlist_size-1)
        else:
            clf = NN(num_nodes, boardlist_size, boardlist_size)

            boardlist = game.boardlist

            if turn%2:
                probs = clf.probs(-1*boardlist)
            else:
                probs = clf.probs(boardlist)

            spot = np.argmax(probs)

            while not game.addmark(spot):
                probs[0][spot] = 0
                spot = np.argmax(probs)         
    
        winner = game.winner()
Ejemplo n.º 34
0
#set up a NN to predict R + Q

#NOW
# either use dataset or train a policy using Q


#oh btw this is a discrete task, not continuos



# env_name = home + '/Documents/tmp/' + 'BreakoutNoFrameskip-v4'
# env_name = home + '/Documents/tmp/' + 'CartPole-v1'
env_name =  'CartPole-v0'
env = gym.make(env_name) 

model = NN()


MAX_EPISODES = 10000
MAX_STEPS    = 200

dataset = deque(maxlen=1000)  
episode_history = deque(maxlen=100)

for i_episode in range(MAX_EPISODES):

    s_t = env.reset()
    total_rewards = 0

    # episode = []
Ejemplo n.º 35
0
from NN import NN
import numpy as np

net = NN([4, 4, 2, 1]) # Number of neurons in each layer, so that is a three layer network, the first layer is the input, last the output.

file = open('data/input.txt', 'r')

inputList = []
targetList = []
for entry in file.readlines():                             #Train on every entry
  print(entry)
  strings = entry.strip().split(",")
  values = map(int, strings)
  inputs, targets = values[:4], values[4]
  inputList.append(inputs)
  targetList.append(targets)

training_data = zip([np.array(x) for x in inputList], [np.array(y) for y in targetList])
test_data = zip([np.array(x) for x in inputList], [np.array(y) for y in targetList])

net.SGD(training_data, 50, 1, 6.0, test_data=test_data)

print(net.feedforward(inputList[-1]))
print(net.feedforward(inputList[-2]))
print(net.feedforward(inputList[-3]))
print(net.feedforward(inputList[-4]))
Ejemplo n.º 36
0
# Helper function to plot a decision boundary.
# If you don't fully understand this function don't worry, it just generates the contour plot below.
def plot_decision_boundary(pred_func):
    # Set min and max values and give it some padding
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # Predict the function value for the whole gid
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)

plt.figure(figsize=(60, 60))

for i, nn_hdim in enumerate(hidden_layer_dimensions):
    plt.subplot(3, 3, i+1)
    clf = NN(nn_hdim, 2, n_classes)
    print("\nTraining NN with " + repr(nn_hdim) + " hidden layer nodes...")
    clf.train(X, Y, num_passes=2000, print_loss=True)
    pred = clf.predict(features_test)
    accuracy = accuracy_score(pred, labels_test)
    plt.title('Hidden Layer size ' + repr(nn_hdim) + ' , Accuracy: ' + repr(round(accuracy,3)))
    plot_decision_boundary(lambda x: clf.predict(x))

matplotlib.pyplot.tight_layout()
plt.subplots_adjust(hspace=0.5, top=0.95, bottom=0.05)
plt.show()
Ejemplo n.º 37
0
# Define how many games to play
n_games = 1000

# Build a model with a n-dimensional hidden layer
num_passes = 3000
num_nodes = 300

boardlist_size = board_rows*board_cols
game = TicTacToe(board_rows, board_cols, k_to_win)
train_X_large = []
train_Y_large = []

for game_num in range(0, n_games):
    print("Playing game " + repr(game_num) + "...")

    clf = NN(num_nodes, boardlist_size, boardlist_size)

    game.boardclear()
    turn = 0;
    winner = None;

    playerA_X = [] # X symbol
    playerB_X = [] # @ symbol

    playerA_Y = [] # X symbol
    playerB_Y = [] # @ symbol

    while not game.won:
        boardlist = game.boardlist

        if turn%2:
Ejemplo n.º 38
0
    print ('saved data')









if reinforce:

    needsoftmax_mixtureweight = torch.randn(n_components, requires_grad=True)

    #REINFORCE
    encoder = NN(input_size=1, output_size=n_components)
    # optim = torch.optim.Adam([needsoftmax_mixtureweight], lr=.004)
    # optim_net = torch.optim.Adam(net.parameters(), lr=.004)
    optim = torch.optim.Adam([needsoftmax_mixtureweight], lr=.0001)
    optim_net = torch.optim.Adam(encoder.parameters(), lr=.001)
    batch_size = 10
    n_steps = 100000
    L2_losses = []
    steps_list = []
    for step in range(n_steps):

        optim.zero_grad()

        loss = 0
        net_loss = 0
        for i in range(batch_size):
Ejemplo n.º 39
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']
        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_act_func = tf.tanh
        self.encoder_net = hyperparams['encoder_net']
        self.decoder_net = hyperparams['decoder_net']
        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        self.n_W_particles = hyperparams['n_W_particles']  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P

        self.qW_weight = hyperparams['qW_weight']
        self.lmba = hyperparams['lmba']

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])
        self.batch_size = tf.shape(self.x)[0]   #B
        self.batch_frac = tf.placeholder(tf.float32, None)

        n_transformations = 3

        #Define endocer and decoder
        with tf.variable_scope("encoder"):
            encoder = NN(self.encoder_net, self.encoder_act_func, self.batch_size)

        self.l2_sum = encoder.weight_decay()

        with tf.variable_scope("decoder"):
            decoder = BNN(self.decoder_net, self.decoder_act_func, self.batch_size)

        with tf.variable_scope("sample_z"):
            sample_z = Sample_z(self.batch_size, self.z_size, self.n_z_particles, n_transformations)

        with tf.variable_scope("log_probs"):
            log_probs = self.log_probs(self.x, encoder, decoder, sample_z)

        with tf.variable_scope("objectives"):
            self.elbo = self.objective(*log_probs)
            self.iwae_elbo_test = self.iwae_objective_test(*log_probs)


        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)


        # for var in tf.global_variables():
        #     print var

        # fasdf

        # FOR INSPECING MODEL

        self.decoder_means = decoder.W_means
        self.decoder_logvars = decoder.W_logvars

        # print 'right here'

        # with tf.variable_scope("is_it_this"):
        #     self.recons, self.priors = self.get_x_samples(self.x, encoder, decoder, sample_z)


        # print 'prob here'


        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
Ejemplo n.º 40
0
#set up a NN to predict R + Q

#NOW
# either use dataset or train a policy using Q


#oh btw this is a discrete task, not continuos



# env_name = home + '/Documents/tmp/' + 'BreakoutNoFrameskip-v4'
# env_name = home + '/Documents/tmp/' + 'CartPole-v1'
env_name =  'CartPole-v0'
env = gym.make(env_name) 

model = NN()


MAX_EPISODES = 10000
MAX_STEPS    = 200

episode_history = deque(maxlen=100)
for i_episode in range(MAX_EPISODES):

  s_t = env.reset()
  total_rewards = 0

  for t in range(MAX_STEPS):
    # env.render()
    # action = pg_reinforce.sampleAction(state[np.newaxis,:])
Ejemplo n.º 41
0
        datas = line[:-1]
        x = []
        for data in datas:
            x.append(float(data))
        X.append(x)

        label = int(line[-1])
        labels.append(label)
        if max_label < label:
            max_label = label

    data_set = SupervisedDataSet(13, max_label + 1)
    for data, label in zip(X, labels):
        label_data = np.zeros(max_label + 1).astype(np.int32)
        label_data[int(label)] = 1
        data_set.addSample(data, label_data)

    return data_set


print 'read train dataset'
train_data_set = make_data_set('doc/train.txt')

print 'read test dataset'
test_data_set = make_data_set('doc/test.txt')

print 'start train'
network = NN(13, 10, train_data_set.outdim)
network.train(train_data_set, test_data_set)
network.save('NN.xml')
Ejemplo n.º 42
0
	num_passes = int(args[4])

n_games = 0

with open(features, 'rb') as f:
    features_train = pickle.load(f)
    f.close()

with open(labels, 'rb') as f:
    labels_train = pickle.load(f)
    f.close()

features_dim = len(features_train[0])
labels_dim = len(labels_train[0])

for x in features_train:
    if np.sum(np.absolute(np.array(x))) == 0:
        n_games += 1

print("Training on dataset of " + repr(n_games) + " games...")

clf = NN(num_nodes, features_dim, labels_dim)

features_train = np.array(features_train)
labels_train = np.array(labels_train)

clf.train(features_train, labels_train, num_passes, print_loss=True)

with open('models/NN' + repr(features_dim) + repr(labels_dim), 'wb') as f:
    pickle.dump(clf,f)
    f.close()




    # logits = 0
    bern_param = torch.tensor([logits], requires_grad=True)
    val=.4


    print()
    print ('SimpLAX')
    print ('Value:', val)
    print()

    net = NN()


    # print (len(net.parameters()))

    # optim = torch.optim.Adam([bern_param] + list(net.parameters()), lr=.004)
    optim = torch.optim.Adam([bern_param], lr=.004)
    optim_NN = torch.optim.Adam(net.parameters(), lr=.0004)


    steps = []
    losses5= []
    for step in range(total_steps):

        dist = RelaxedBernoulli(torch.Tensor([1.]), logits=bern_param)
Ejemplo n.º 44
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']
        self.x_size = hyperparams['x_size']  #X
        self.z_size = hyperparams['z_size']  #Z

        self.n_W_particles = hyperparams['n_W_particles']  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P

        self.qW_weight = hyperparams['qW_weight']
        self.lmba = hyperparams['lmba']


        if hyperparams['ga'] == 'none':
            self.encoder_net = [self.x_size] +hyperparams['encoder_hidden_layers'] + [self.z_size*2]

        elif hyperparams['ga'] == 'hypo_net':
            #count size of decoder 
            if len(hyperparams['decoder_hidden_layers']) == 0:
                n_decoder_weights = (self.z_size+1) * self.x_size
            else:
                n_decoder_weights = 0
                for i in range(len(hyperparams['decoder_hidden_layers'])+1):
                    if i==0:
                        n_decoder_weights += (self.z_size+1) * hyperparams['decoder_hidden_layers'][i]
                    elif i==len(hyperparams['decoder_hidden_layers']):
                        n_decoder_weights += (hyperparams['decoder_hidden_layers'][i-1]+1) * self.x_size 
                    else:
                        n_decoder_weights += (hyperparams['decoder_hidden_layers'][i-1]+1) * hyperparams['decoder_hidden_layers'][i] 
            print 'n_decoder_weights', n_decoder_weights
            self.encoder_net = [self.x_size + n_decoder_weights] +hyperparams['encoder_hidden_layers'] +[self.z_size*2]

        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_net = [self.z_size] +hyperparams['decoder_hidden_layers'] +[self.x_size]
        self.decoder_act_func = tf.tanh



        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])
        self.batch_frac = tf.placeholder(tf.float32, None)

        #Other
        self.batch_size = tf.shape(self.x)[0]   #B
        n_transformations = hyperparams['n_qz_transformations']
        self.rs = 0


        #Define model
        with tf.variable_scope("encoder"):
            encoder = NN(self.encoder_net, self.encoder_act_func, self.batch_size)

        with tf.variable_scope("encoder_weight_decay"):
            self.l2_sum = encoder.weight_decay()

        with tf.variable_scope("decoder"):
            if hyperparams['decoder'] == 'BNN':
                decoder = BNN(self.decoder_net, self.decoder_act_func)
            elif hyperparams['decoder'] == 'MNF':
                decoder = MNF(self.decoder_net, self.decoder_act_func)


        with tf.variable_scope("sample_z"):
            sample_z = Sample_z(self.batch_size, self.z_size, self.n_z_particles, hyperparams['ga'], n_transformations)

        with tf.variable_scope("log_probs"):
            log_probs = self.log_probs(self.x, encoder, decoder, sample_z)

        with tf.variable_scope("objectives"):
            self.elbo = self.objective(*log_probs)
            self.iwae_elbo_test = self.iwae_objective_test(*log_probs)

        with tf.variable_scope("optimizer"):
            self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)





        # FOR INSPECING MODEL

        # for var in tf.global_variables():
        #     print var
        self.decoder_means = decoder.W_means
        self.decoder_logvars = decoder.W_logvars

        # with tf.variable_scope("is_it_this"):
        #     self.recons, self.priors = self.get_x_samples(self.x, encoder, decoder, sample_z)

        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
Ejemplo n.º 45
0
def train_and_test_NN(task, x_train, y_train, x_test, y_test, batch_size, numberOfLabels, vocabularySize, contextSize, d_wrd, maxit, threshold, baselineFeatures=False, pretrainedModel=None, acti="tanh", shuffle=False, l1=0, l2=0):
	""" Train and test a Neural Network """
	# compute number of minibatches for training
	n_train_batches = x_train.get_value(borrow=True).shape[0] / batch_size
	n_test_batches = x_test.get_value(borrow=True).shape[0] / batch_size
	
	print '... building the model'

	#allocate symbolic variables for the data
	index = T.lscalar()  #index to a minibatch
	x = T.matrix('x', dtype='int32')  #data is matrix, one row for each sample
	y = T.ivector('y')  # the labels are presented as vector of [int] labels 
	learningRateT = T.fscalar()

	rng = np.random.RandomState(1234)
	n_out = numberOfLabels
	#construct the NN
	if baselineFeatures:
		contextSize += 17 #17 baseline features are given

	if pretrainedModel is None:
		classifier = NN(rng, x, n_hidden, n_out, d_wrd, vocabularySize, contextSize, params=None, acti=acti)
	else:
		classifier = NN(rng, x, n_hidden, n_out, d_wrd, vocabularySize, contextSize, params=pretrainedModel, acti=acti)

	#symbolic expression for the score we maximize during training is the log likelihood of the model
	#score = (classifier.log_likelihood(y))

	#regularized score
	L1_reg = l1
	L2_reg = l2
	score = (classifier.log_likelihood(y)
		  + L1_reg * classifier.L1
		  + L2_reg * classifier.L2_sqr)
	
	cost = (classifier.errors(y))

	#compute the gradient of score with respect to theta
	gparams = [T.grad(score, param) for param in classifier.params]
	
	#SGA update for the parameters
	updates = [ (param, param + learningRateT * gparam) for param, gparam in zip(classifier.params, gparams)]

	#compiling a Theano function that returns the score and updates the parameter of the model for the given training data
	train_model = theano.function(
		inputs=[index, learningRateT],
		outputs=score,
		updates=updates,
		givens={
			x: x_train[index * batch_size:(index + 1) * batch_size],
			y: y_train[index * batch_size:(index + 1) * batch_size]
		}
	)

	#get the current cost on the test set
	eval_model_test = theano.function([], cost, givens={ x: x_test, y: y_test})
	eval_model_train = theano.function([], cost, givens={ x: x_train, y: y_train})

	#get vectors of true labels and current predictions
	predict_model_test = theano.function([], [y, classifier.outputLayer.y_pred], givens={x:x_test, y:y_test} )
	predict_model_train = theano.function([], [y, classifier.outputLayer.y_pred], givens={x:x_train, y:y_train})	

	print "... training"
	pbar = ProgressBar()	
	start_time = time.clock()
	print "...start time:", start_time
	costs = list()
	now = str(datetime.datetime.now()).replace(" ","--")

	#constant learning rate
	print "...Constant learning rate"
	learningRateT = np.float32(learningRate)
	
	#decaying learning rate, starting from 0.5, till learning rate parameter
#	print "...Decaying learning rate"
	#initialLearningRate = 0.05
	#learningRateT = np.float32(initialLearningRate)
	#learningRateLimit = np.float32(learningRate)	

	#save the current model every x iterations
	backupFrequency = 5

	#store the model with this prefix
	paramfile = "../parameters/"+now
	
	f1s_OK_train = list()
	f1s_BAD_train = list()
	f1s_sum_train = list()
	accuracies_train = list()
	
	f1s_OK_test = list()
	f1s_BAD_test = list()
	f1s_sum_test = list()
	accuracies_test = list()

	learningRates = list()

	noImprovementCounter = 0
	#parameters_best = copy.deepcopy(classifier.params)
	f1_BAD_best = -np.inf
	noImprovementLimit = 50 
	decayingFactor = 0.5

	it_times = list()
	cur_time = time.clock()

	for it in pbar(range(maxit)):
		itcosts = list()	
	
		#parameters_previous = copy.deepcopy(classifier.params)
		#learning rate: constant or 1/(t+1) or exp (1*0.85^(-1/N)) or dec (1/(1+(t/N))
		#learningRateT = np.float32(1./(1+ (float(it)/n_train_batches)))
		#learningRateT = np.float32(1./(it+1))
		print "\tIteration "+str(it)+": current learning rate", learningRateT		
		learningRates.append(learningRateT)	
	
		indices = range(n_train_batches)
		if shuffle:
			#shuffle indices of data
			np.random.shuffle(indices)
			print "...shuffling"
	
		#for minibatch_index in xrange(n_train_batches): #unshuffled
		for minibatch_index in indices:
			minibatch_avg_logli = train_model(minibatch_index, learningRateT)
			#print "\tCurrent train log likelihood:", minibatch_avg_logli
			#itcosts.append(minibatch_avg_logli)
		#costs.append(np.mean(itcosts))
		#traincost = (eval_model_train()/float(n_train_batches))
		#testcost = (eval_model_test()/float(n_test_batches))
		#print "\tCurrent avg cost on training data", traincost
		#print "\tCurrent avg cost on test data", testcost
		it_time = time.clock()
		diff = (it_time-cur_time)/60.
		it_times.append(diff)
		print "...iteration took", diff, "mins"
		cur_time = it_time 

		y_true_train, y_pred_train = predict_model_train()
		y_true_test, y_pred_test = predict_model_test()

		
		if task==2:
			print "\nF1 on train:"
			f1_OK_train, f1_BAD_train, accuracy_train = f1_ok_and_bad(y_true_train, y_pred_train)
			f1s_OK_train.append(f1_OK_train)
			f1s_BAD_train.append(f1_BAD_train)
			f1s_sum_train.append(f1_BAD_train+f1_OK_train)
			accuracies_train.append(accuracy_train)

			print "\nF1 on test:"
			f1_OK_test, f1_BAD_test, accuracy_test = f1_ok_and_bad(y_true_test, y_pred_test)
			f1s_OK_test.append(f1_OK_test)
			f1s_BAD_test.append(f1_BAD_test)
			f1s_sum_test.append(f1_BAD_test+f1_OK_test)
			accuracies_test.append(accuracy_test)
	#		if it%backupFrequency == 0:
	#			currentparamfile = paramfile+"."+str(it)+".params"
	#			print "\tSaving parameters in", currentparamfile
	#			classifier.saveParams(currentparamfile)		

			#if it>=1:
				#print "\tCurrent change in cost on training data: ", costs[-1]-costs[-2]
			#	print "\tCurrent change in sum of f1s on training data:", f1s_sum_train[-2]-f1s_sum_train[-1]
			#	print "\tCurrent change in sum of f1s on test data:", f1s_sum_test[-2]-f1s_sum_test[-1]

			#if improved (measured by BAD F1), save best parameter
			if f1_BAD_test > f1_BAD_best: 
				#parameters_best = copy.deepcopy(classifier.params)
				f1_BAD_best = f1_BAD_test
				print "\tNEW BEST!"
				noImprovementCounter = 0			
				currentparamfile = paramfile+"."+str(it)+".params"
				print "\tSaving parameters in", currentparamfile
				classifier.saveParams(currentparamfile)
		
			else:
				#count iterations where no improvement happened
				noImprovementCounter += 1
				# save parameters anyway every backupFrequency iterations
				if(it % backupFrequency == 0):
					currentparamfile = paramfile+"."+str(it)+".params"
					print "\tSaving parameters in", currentparamfile
					classifier.saveParams(currentparamfile)
					
		elif task==1: #TODO
			diff = 0
			total = 0
			zeros = 0
			error = 0
			sqerror = 0
			for i in xrange(x_train.get_value(borrow=True).shape[0]):
				total+=1
				#print y_true_train[i], y_pred_train[i]
				if y_true_train[i] != y_pred_train[i]: #y_true_test, y_pred_test
					diff+=1
					error += abs(y_true_train[i]-y_pred_train[i])
					sqerror += (y_true_train[i]-y_pred_train[i])**2
			print "...Mean Absolute Error (MAE) on training set: ",float(error)/total
			print "...Root Mean Squared Error (RMSE) on training set: ", np.sqrt(float(sqerror)/total)
		
		
		
			diff = 0
			total = 0
			zeros = 0
			error = 0
			sqerror = 0
			for i in xrange(x_test.get_value(borrow=True).shape[0]):
				total+=1
				#print y_true_test[i], y_pred_test[i]
				if y_true_test[i] != y_pred_test[i]: #y_true_test, y_pred_test
					diff+=1
					error += abs(y_true_test[i]-y_pred_test[i])
					sqerror += (y_true_test[i]-y_pred_test[i])**2
			print "...Mean Absolute Error (MAE) on test set: ",float(error)/total
			print "...Root Mean Squared Error (RMSE) on test set: ", np.sqrt(float(sqerror)/total)
		#for decaying learning rate	
		#if no improvement for k iterations, decrease learning rate and start from best parameters seen so far
	#	if noImprovementCounter >= noImprovementLimit:
	#		learningRateT *= decayingFactor	
	#		print "\tDECAY"
	#		#classifier.params = copy.deepcopy(parameters_best)
	#		noImprovementCounter = 0

		#for decaying learning rate
	#	if learningRateT < learningRateLimit:
	#		print "\tDecay limit of learning rate reached"
	#		#classifier.params = copy.deepcopy(parameters_best)
	#		break
	
		#classifier.params = copy.deepcopy(parameters_best)
		#stop if f1 on test data decreases
#		if it>1 and f1s_sum_test[-1]<f1s_sum_test[-2]:
#			print "\tSum of F1 (BAD+OK) on test data started to decrease"
#			print "\tResetting parameters to previous"
#			classifier.params = parameters_previous
#			break

		#if it>1 and (costs[-1]-costs[-2])<threshold: #improvement below threshold
		#	print "... break: after %d iterations improvement is below threshold of %f"	% (it, threshold)	
		#	break
	end_time = time.clock()

	print "... training took %.2fm" % ((end_time - start_time) / 60.)
	print "... that's on average: ", np.average(it_times), "for iterations: ", len(it_times)
	
	f1s = []
	
	if task==2:
		f1s = (f1s_OK_train, f1s_BAD_train, accuracies_train, f1s_OK_test, f1s_BAD_test, accuracies_test)


	print "...testing"
#	#theano function that returns the prediction 
	predict_model_test = theano.function([], [x, y, classifier.outputLayer.y_pred, classifier.outputLayer.p_y_given_x], givens={x:x_test, y:y_test} )
	x_test,y_test,pred,likelihood = predict_model_test()
#	diff = 0
#	total = 0
#	zeros = 0
#	error = 0
#	sqerror = 0
#	for i in xrange(len(x_test)):
#		total+=1
#		if y_test[i] != pred[i]:
#			diff+=1
#			error += abs(y_test[i]-pred[i])
#			sqerror += (y_test[i]-pred[i])**2
#		if y_test[i] == 0:
#			zeros+=1
##	if task == 2:
#		print "...accuracy on test set: ",float(total-diff)/total
#		#print "...percentage of zeros:", float(zeros)/total
#	elif task == 1:
#		print "...Mean Absolute Error (MAE) on test set: ",float(error)/total
#		print "...Root Mean Squared Error (RMSE) on test set: ", np.sqrt(float(sqerror)/total)
#		#print "...percentage of zeros:", float(zeros)/total


	return classifier, pred, likelihood, f1s, learningRates
Ejemplo n.º 46
0
	def __init__(self, feature_vectors):
		self.feature_vectors = feature_vectors
		self.pre_calc = PreCalc(feature_vectors)
		self.NNTree = NN(feature_vectors)
		self.features_count = self.NNTree.tree.features_count