def test_train_loopClass(Xtrain,Ytrain,Xtest,Ytest,iterations,ridge_penalty,hiddens,plot_in_train=True): """or loops to iterate through training iterations, ridge penalty, and hidden layer list """ results = {} global nnet,random_network_seed for niter in iterations: for penalty in ridge_penalty: for hidden in hiddens: ### Check / use random seed if random_network_seed == None: np.random.seed(None) random_network_seed = int(np.random.randint(1, 100000)) np.random.seed(random_network_seed) random.seed(random_network_seed) tf.set_random_seed(0) ### Standardize the data Xtrain,Xtest,stdVals = dSS.standardize_data(Xtrain,Xtest) Xmean,Xstd = stdVals ### Define the model model = defineNN(hidden, input_shape=np.shape(Xtrain)[1], output_shape=np.shape(Ytrain)[1], ridgePenalty=penalty) ### Train the net model, history = trainNN(model,Xtrain, Ytrain,niter=niter,verbose=0) ### After training, use the network with training data to ### check that we don't have any errors and output RMSE rmse_train = dSS.rmse(Ytrain,model.predict(Xtrain)) if type(Ytest) != bool: rmse_test = 0. rmse_test = dSS.rmse(Ytest,model.predict(Xtest)) else: rmse_test = False this_result = {'iters': niter, 'hiddens' : hidden, 'RMSE Train' : rmse_train, 'RMSE Test' : rmse_test, 'ridge penalty': penalty, 'zero mean' : rm_annual_mean, 'zero merid mean' : rm_merid_mean, 'land only?' : land_only, 'ocean only?' : ocean_only, 'Segment Seed' : random_segment_seed, 'Network Seed' : random_network_seed } results.update(this_result) global experiment_result experiment_result = experiment_result.append(results, ignore_index=True) #'unlock' the random seed np.random.seed(None) random.seed(None) tf.set_random_seed(None) return experiment_result, model
# ### Loop over folds for loop in np.arange(0,foldsN): K.clear_session() #--------------------------- #random_segment_seed = 34515 random_segment_seed = int(np.genfromtxt('/Users/zlabe/Documents/Research/ExtremeEvents/Data/SelectedSegmentSeed.txt',unpack=True)) #--------------------------- Xtrain,Ytrain,Xtest,Ytest,Xtest_shape,Xtrain_shape,data_train_shape,data_test_shape,testIndices = segment_data(data,classesl,segment_data_factor) YtrainClassMulti = Ytrain YtestClassMulti = Ytest # For use later XtrainS,XtestS,stdVals = dSS.standardize_data(Xtrain,Xtest) Xmean, Xstd = stdVals #--------------------------- random_network_seed = 87750 #--------------------------- # Create and train network exp_result,model = test_train_loopClass(Xtrain, YtrainClassMulti, Xtest, YtestClassMulti, iterations=iterations, ridge_penalty=ridge_penalty, hiddens=hiddensList, plot_in_train = True)