Quantcast
Channel: Active questions tagged feed-forward+python+neural-network - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Network weights no longer updating

$
0
0

`Hi,I have to run some tensorflow v.1.8 code in a v.2 environment. I have made all the necessary changes (I believe). I have tried everything I can think of, but after I start the training the weights only update after the first epoch and then stop. I have gone through all of my old libraries to see if there is something that has potentially been changed, however I can find nothing. Can anyone help with this? I have to turn a thesis in soon and it has been quite problematic not being able to re-calculate my original results :( Any help is greatly appreciated!!!

from __future__ import print_function# Import MNIST dataimport randomimport numpy as npimport tensorflow as tfimport accl_functions as afimport matplotlib.pyplot as pltimport os.pathimport ostf.compat.v1.reset_default_graph()tf.compat.v1.disable_eager_execution()tf.compat.v1.disable_v2_behavior()random.seed(23)# Parameterslearning_rate = 0.001training_epochs = 600batch_size = 32display_step = 1# Network Parametersn_hidden_1 = 300 # 1st layer number of neuronsn_hidden_2 = 300 # 2nd layer number of neuronsn_hidden_3 = 300 # 2nd layer number of neuronsn_input = 900 # MNIST data input (img shape: 28*28)n_classes = 2 # MNIST total classes (0-9 digits)################################"""The proceeding booleans must be set correctly prior torunning the algo"""################################plot_progress = Trueuse_training = Falsesingle_trial = False# tf Graph inputX = tf.compat.v1.placeholder("float", [None, n_input])Y = tf.compat.v1.placeholder("float", [None, n_classes])pred=tf.compat.v1.placeholder("float",)# initializer = tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform")initializer = tf.compat.v1.keras.initializers.glorot_normal()##XAVIER INITIALIZATION##weights = {'h1': tf.Variable(initializer([n_input, n_hidden_1])),'h2': tf.Variable(initializer([n_hidden_1, n_hidden_2])),'h3': tf.Variable(initializer([n_hidden_2, n_hidden_3])),'weight_out': tf.Variable(initializer([n_hidden_3, n_classes]))}biases = {'b1': tf.Variable(initializer([n_hidden_1])),'b2': tf.Variable(initializer([n_hidden_2])),'b3': tf.Variable(initializer([n_hidden_3])),'bias_out': tf.Variable(initializer([n_classes]))}# Create modeldef multilayer_perceptron(x):    # Hidden fully connected layer with 256 neurons    layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['h1']), biases['b1']))    # Hidden fully connected layer with 256 neurons    layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']))    layer_3 = tf.nn.relu(tf.add(tf.matmul(layer_2, weights['h3']), biases['b3']))    # Output fully connected layer with a neuron for each class    out_layer = tf.matmul(layer_3, weights['weight_out']) + biases['bias_out']    return out_layerlogits = multilayer_perceptron(X)################################################################weighted_lossloss_weights = tf.constant(([[.05, 0.95]]))loss_op = tf.reduce_mean(input_tensor=tf.nn.weighted_cross_entropy_with_logits(        logits=logits,labels=Y,pos_weight=loss_weights))################################################################optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate)train_op = optimizer.minimize(loss_op)######################################### EVALUATE MODEL# evaluates accuracy of softmax of logit output#pred = tf.nn.softmax(logits)correct_prediction = tf.equal(tf.argmax(input=pred, axis=1), tf.argmax(input=Y, axis=1))### Calculate accuracyaccuracy = tf.reduce_mean(input_tensor=tf.cast(correct_prediction, "float"))############################################ def tsne_layer(x):#         # Hidden fully connected layer with 256 neurons#     layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['h1']), biases['b1']))#     # Hidden fully connected layer with 256 neurons#     layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']))#     layer_3 = tf.nn.relu(tf.add(tf.matmul(layer_2, weights['h3']), biases['b3']))#     return layer_3# layer =tsne_layer(X)##########################################################################################################################"""The below code loads all of the preprocessed data. Make sureyou import the data from the correct files. Additionally, adjustmax_to_keep if you would like to save weight from more than 1000 epochs.Default save is every 10 epochs"""#let's save some weightssaver = tf.compat.v1.train.Saver(max_to_keep=101)# Initializing the variablesinit = tf.compat.v1.global_variables_initializer()# if use_training:# #load data from the preprocessed algos#     outfile='Train_Test_Data.npz'#     npzfile = np.load('Train_Test_Data.npz')#     test_set = npzfile['test_set']#     test_labels = npzfile['test_labels']# else:outfile='Train_Test_Data.npz'npzfile = np.load('Train_Test_Data_intromission.npz')training_data = npzfile['training_data']training_labels = npzfile['training_labels']test_set = npzfile['test_set']test_labels = npzfile['test_labels']#file path for model saverfile_name = '/home/baylor/Documents/Neural_Network_Training/Model_Weights/model.ckpt'#file that is created once the network has finished trainingoutfile2 = 'Train_Test_Data_Plotting_Intromissions.npz'#############################################################################################################################if use_training == False:        #randomly shuffle all the data for training the network    shuffle_accl, shuffle_labels = af.shuffle_data(training_data, training_labels)    #set percentage of data to be used for validating    percentage=0.2    #Generate test set and training set    train_x,train_y,validation_x,validation_y = af.generate_test(shuffle_accl,shuffle_labels,percentage,n_classes,n_input)    total_batch = int(train_y.shape[0]/batch_size)    avg_cost_array = np.zeros(training_epochs)    avg_cost_validation_array = np.zeros(training_epochs)##########################################################################################################################"""The following arrays are what will be saved after thetraining has finished"""soft_max_prediction=np.zeros((test_labels.shape[0],n_classes),dtype='float32')#soft_max_prediction_single=np.zeros((test_labels_single.shape[0],2),dtype='float32')#used for plotting the difference between logits, alternative#to using softmaxlogit_diff=np.zeros((test_labels.shape[0],n_classes),dtype='float32')#logit_diff_single=np.zeros((test_labels_single.shape[0],2),dtype='float32')##Start traning ###############################################################with tf.compat.v1.Session() as sess:    sess.run(init)    if use_training:        #reloads previous training data        saver.restore(sess, "/home/baylor/Documents/Neural_Network_Training/old_weights/model290.ckpt")#        for filename in os.listdir('dirname'):#            callthecommandhere(blablahbla, filename, foo)    else:        # Training cycle        for epoch in range(training_epochs):            avg_cost = 0.            avg_cost_test = 0.            for i in range(total_batch):                #get batches for training                batch_x,batch_y = af.batch_dat_data(train_x,train_y,i,batch_size)                #calculate loss on test set, should be calculated before first training set                # Run optimization op (backprop) and cost op (to get loss value)                _, c = sess.run([train_op, loss_op], feed_dict={X: batch_x, Y: batch_y})                # Compute average loss                avg_cost += c / total_batch            #Display logs per epoch step            if epoch % display_step == 0:                tc = sess.run([loss_op], feed_dict = {X: validation_x,                                  Y: validation_y})                tc=float(tc[0])                avg_cost_validation = tc                avg_cost_array[epoch] = avg_cost                avg_cost_validation_array[epoch] = avg_cost_validation                print("Epoch:", '%04d' % (epoch+1), "cost={:.15f}".format(avg_cost),"cost={:.15f}".format(avg_cost_validation))                if plot_progress:                    plt.plot((epoch+1),avg_cost,'.r')                    plt.plot((epoch+1),avg_cost_validation,'xg')                    plt.xlabel('Epoch')                    plt.ylabel('Loss')                    plt.pause(1)            if epoch%10 == 0 or epoch==training_epochs:                #save weights every 10 epochs                save_path = saver.save(sess, "/home/baylor/Documents/Neural_Network_Training/Model_Weights/model" + str(epoch) +".ckpt")        print("Optimization Finished!")

Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>