def train_set(): global x_train global y_train global x_test global y_test print(len(x_train)) print(len(y_train)) structure = [24, 12, 1] for item in y_train: item = round(item, 3) x_train = np.asarray(x_train) y_train = np.asarray(y_train) y_test = np.asarray(y_test) x_test = np.asarray(x_test) print type(x_train.ndim) rnn = pyrenn.CreateNN(structure) #dIntern=[1] rnn = pyrenn.train_LM(np.transpose(x_train), np.transpose(y_train), rnn, verbose=True, k_max=200, E_stop=1e-7) out_test = pyrenn.NNOut(np.transpose(x_test), rnn) plt.plot(y_test, 'r', label='actual') plt.plot(out_test, 'b', label='predicted') mse = mean_squared_error(y_test, out_test) print "MSE = " + str(mse) plt.show()
def main(): # get our data as an array from read_in() res = json.loads(sys.stdin.read()) # data = [ [ 1578.0077, 0 ],[ 1581.1876, 5 ],[ 1452.4627, 33 ],[ 1449.7326, 58 ],[ 1501.0392, 80 ],[ 1460.4557, 110 ],[ 1492.824, 130 ],[ 1422.3826, 155 ],[ 1404.3431, 180 ],[ 1480.74, 210 ],[ 1410.3936, 230 ],[ 1612.336, 255 ],[ 1729.343, 280 ],[ 1735.5231, 305 ],[ 1632.595, 330 ],[ 1648.3143, 355 ],[ 1640.1972, 380 ],[ 1658.7949, 405 ],[ 1675.4953, 430 ],[ 1712.2672, 455 ],[ 1623.8666, 480 ],[ 1622.154, 505 ],[ 1630.9466, 530 ],[ 1595.8407, 555 ],[ 1548.5976, 580 ],[ 1598.6558, 605 ],[ 1624.0902, 630 ],[ 1616.8663, 655 ],[ 1661.251, 680 ],[ 2012.605, 705 ],[ 1904.3356, 730 ],[ 1760.5438, 755 ],[ 2449.3183, 780 ],[ 2417.4744, 805 ],[ 2431.7134, 830 ],[ 2391.2651, 855 ],[ 2402.8298, 885 ],[ 2417.0901, 905 ],[ 2403.8137, 930 ],[ 2407.1756, 955 ],[ 2363.049, 980 ],[ 2364.4589, 1010 ],[ 2368.4206, 1030 ],[ 2338.8434, 1055 ],[ 2369.9809, 1080 ],[ 2353.5891, 1105 ],[ 2380.8422, 1130 ],[ 2519.2731, 1155 ],[ 2557.5253, 1180 ],[ 2536.3437, 1205 ],[ 2517.6042, 1235 ],[ 2543.7378, 1255 ],[ 2355.5603, 1280 ],[ 2347.445, 1305 ],[ 2269.8631, 1335 ],[ 2307.6435, 1355 ],[ 2274.5249, 1380 ],[ 2319.0633, 1405 ],[ 2251.9456, 1430 ],[ 2273.7241, 1455 ],[ 2250.0617, 1480 ],[ 2272.8212, 1505 ],[ 2367.9611, 1530 ],[ 2351.8406, 1555 ],[ 2348.4958, 1580 ],[ 2308.7974, 1605 ],[ 2290.4632, 1630 ],[ 2303.6924, 1655 ],[ 2218.8104, 1680 ],[ 2260.9153, 1705 ],[ 2236.759, 1730 ],[ 2238.0003, 1755 ],[ 2222.3537, 1780 ],[ 2288.0802, 1805 ],[ 2240.4641, 1830 ],[ 2258.3908, 1855 ],[ 2175.4428, 1880 ],[ 2247.978, 1905 ],[ 2234.6417, 1930 ],[ 2232.0709, 1955 ],[ 2216.933, 1980 ],[ 2219.6263, 2005 ],[ 2304.114, 2030 ],[ 2230.2487, 2055 ],[ 2261.5, 2070 ] ] # #create a numpy array np_data = np.array(res['data']) # np_data = np.array(data) P = np_data[:, 1] steps = res['predict'] / res['step'] # steps = 25 Pl = np.concatenate((P, P[-1] + ((np.arange(1, steps).T) * 25))) Y = np_data[:, 0] nn = [1, 5, 5, 1] dIn = [1, 2, 3] dIntern = [] dOut = [1, 2, 3, 4] net = prn.CreateNN(nn, dIn, dIntern, dOut) net = prn.train_LM(P, Y, net, 1000, verbose=0) print('/') y_ap = prn.NNOut(Pl, net) result = np.column_stack((Pl, y_ap)) print(result.tolist())
def __init__(self, layers=None, delay=10): if layers is None: layers = [4, 20, 20, 2] self.in_nodes = layers[0] * delay self.inputs = layers[0] layers[0] *= delay self.layers = layers self.delay = delay self.past_data = [] self.net = pr.CreateNN(self.layers)
def trainGclmNN(train_data, f): fname, target_OP = generateFilenames(f) target_OP = np.array(target_OP) net = pyrenn.CreateNN([48, 20, 20, 1]) #target_OP=np.zeros((1,n)) net = pyrenn.train_LM(train_data.transpose(), np.array(target_OP), net, k_max=500, E_stop=0.5, verbose=True) y = pyrenn.NNOut(train_data.transpose(), net) for i, j in zip(final_OP(y), target_OP.transpose()): print(i, j) accuracy(final_OP(y), target_OP.transpose()) return net
def rnn(training_set_features, training_set_class, test_set_features, test_set_class): # 1D numpy arrays # rows: inputs or outputs # columns: samples P = np.array(training_set_features) P = np.transpose(P) Y = np.array(training_set_class) Y = np.reshape(Y,(-1, len(training_set_class))) Ptest = np.array(test_set_features) Ptest = np.transpose(Ptest) Ytest = np.array(test_set_class) Ytest = np.reshape(Ytest, (-1, len(test_set_class))) net = pyrenn.CreateNN([9, 18, 18, 1], dIn=[0], dIntern=[], dOut=[]) net = pyrenn.train_LM(P, Y, net, verbose=True, k_max=30, E_stop=1e-3) y = pyrenn.NNOut(P, net) ytest = pyrenn.NNOut(Ptest, net) create_predictions_file(ytest) """fig = plt.figure(figsize=(11,7))
# Ytest=Y_test.transpose()[3:4805] # # Ptest=Ptest.transpose() # Ytest= Ytest.transpose() # P0test=P0test.transpose() # Y0test= Y0test.transpose() ## # #Create and train NN # # #create recurrent neural network with 1 input, 2 hidden layers with # #2 neurons each and 1 output # #the NN has a recurrent connection with delay of 1 timestep in the hidden # # layers and a recurrent connection with delay of 1 and 2 timesteps from the output # # to the first layer net = prn.CreateNN([22, 11, 1], dIn=[0], dIntern=[], dOut=[1]) # # #Train NN with training data P=input and Y=target # #Set maximum number of iterations k_max to 100 # #Set termination condition for Error E_stop to 1e-3 # #The Training will stop after 100 iterations or when the Error <=E_stop net = prn.train_LM(P, Y, net, verbose=True, k_max=100, E_stop=1e-3) prn.saveNN(net, "RNNmodel_st.mdl") print("saved") os._exit(0) # print("loading") # net = prn.loadNN("RNNmodel.mdl") # print("loaded") ###
df = pd.ExcelFile('dataset.xlsx').parse('Sheet6') #P0 = np.array([df['s1'].values,df['s2'].values,df['s3'].values,df['s4'].values,df['s5'].values,df['s6'].values,df['s7'].values,df['s8'].values,df['s9'].values,df['s10'].values,df['s11'].values,df['s12'].values,df['s13'].values,df['s14'].values,df['s15'].values,df['s16'].values,df['s17'].values,df['s18'].values,df['s19'].values,df['s20'].values,df['s21'].values,df['s22'].values,df['s23'].values,df['s24'].values,df['s25'].values,df['s26'].values,df['s27'].values,df['s28'].values,df['s29'].values,df['s30'].values,df['s31'].values,df['s32'].values,df['s33'].values,df['s34'].values,df['s35'].values,df['s36'].values,df['s37'].values,df['s38'].values,df['s39'].values,df['s40'].values,df['s41'].values,df['s42'].values,df['s43'].values,df['s44'].values,df['s45'].values,df['s46'].values,df['s47'].values,df['s48'].values,df['s49'].values,df['s50'].values]) P = np.array([df['agitation'].values,df['apyrexial'].values,df['ascites'].values,df['asthenia'].values,df['blackout'].values,df['bradycardia'].values,df['breath sounds decreased'].values,df['chest tightness'].values,df['chill'].values,df['consciousness clear'].values,df['cough'].values,df['decreased body weight'].values,df['diarrhea'].values,df['difficulty'].values,df['distress respiratory'].values,df['drowsiness'].values,df['dyspnea'].values,df['facial paresis'].values,df['fatigue'].values,df['feeling hopeless'].values,df['feeling suicidal'].values,df['fever'].values,df['guaiac positive'].values,df['haemorrhage'].values,df['hallucinations auditory'].values,df['hallucinations visual'].values,df['headache'].values,df['hematuria'].values,df['hemodynamically stable'].values,df['homelessness'].values,df['hypokinesia'].values,df['hypotension'].values,df['intoxication'].values,df['irritable mood'].values,df['lesion'].values,df['mass of body structure'].values,df['mental status changes'].values,df['mood depressed'].values,df['nausea'].values,df['orthopnea'].values,df['pain'].values,df['pain abdominal'].values,df['pain chest'].values,df['patient non compliance'].values,df['pleuritic pain'].values,df['prostatism'].values,df['rale'].values,df['shortness of breath'].values,df['sleeplessness'].values,df['sore to touch'].values]) Y = np.array([df['acquired immuno-deficiency syndrome'].values,df['adhesion'].values,df['affect labile'].values,df['Alzheimers disease'].values,df['anemia'].values,df['aphasia'].values,df['asthma'].values,df['biliary calculus'].values,df['bipolar disorder'].values,df['carcinoma prostate'].values,df['cholecystitis'].values,df['chronic alcoholic intoxication'].values,df['chronic kidney failure'].values,df['chronic obstructive airway disease'].values,df['coronary arteriosclerosis'].values,df['decubitus ulcer'].values,df['degenerative polyarthritis'].values,df['deglutition disorder'].values,df['dehydration'].values,df['depressive disorder'].values,df['diverticulosis'].values,df['embolism pulmonary'].values,df['encephalopathy'].values,df['endocarditis'].values,df['epilepsy'].values,df['failure heart'].values,df['failure kidney'].values,df['fibroid tumor'].values,df['gastritis'].values,df['gout'].values,df['hepatitis'].values,df['hernia hiatal'].values,df['hyperbilirubinemia'].values,df['hypercholesterolemia'].values,df['hyperglycemia'].values,df['hypothyroidism'].values,df['ileus'].values,df['incontinence'].values,df['infection urinary tract'].values,df['influenza'].values,df['insufficiency renal'].values,df['lymphoma'].values,df['malignant neoplasm of breast'].values,df['malignant neoplasm of prostate'].values,df['malignant tumor of colon'].values,df['myocardial infarction'].values,df['neoplasm'].values,df['neoplasm metastasis'].values,df['obesity'].values,df['obesity morbid'].values]) #Ptest = np.array([df['CT1'].values,df['CT2'].values,df['CT3'].values,df['CT4'].values,df['CT5'].values,df['CT6'].values,df['CT7'].values,df['CT8'].values,df['CT9'].values,df['CT10'].values,df['CT11'].values,df['CT12'].values,df['CT13'].values,df['CT14'].values,df['CT15'].values,df['CT16'].values]) #Ytest = np.array([df['CT17'].values,df['CT18'].values,df['CT19'].values,df['CT20'].values,df['CT21'].values,df['CT22'].values,df['CT23'].values]) ### #Create and train NN #create feed forward neural network with 1 input, 2 hidden layers with #4 neurons each and 1 output #the NN has a recurrent connection with delay of 1 timesteps from the output # to the first layer #print len(P),len(Y) net = prn.CreateNN([50,50,50]) net = prn.loadNN('/usr/local/lib/python2.7/site-packages/examples/minor_final.csv') #Train NN with training data P=input and Y=target #Set maximum number of iterations k_max to 500 #Set termination condition for Error E_stop to 1e-5 #The Training will stop after 500 iterations or when the Error <=E_stop net = prn.train_LM(P,Y,net,verbose=True,k_max=50,E_stop=1e-5) ### #Calculate outputs of the trained NN for train and test data #j=0; #diseases = ['AIDS (acquired immuno-deficiency syndrome)','Adhesion','Affect labile','Alzheimers disease','Anemia','Aphasia','Asthma','Biliary calculus','Bipolar disorder','Carcinoma prostate','Cholecystitis','Chronic alcoholic intoxication','Chronic kidney failure','Chronic obstructive airway disease','Coronary arteriosclerosis','Decubitus ulcer','Degenerative polyarthritis','Deglutition disorder','Dehydration','Depressive disorder','Diverticulosis','Pulmonary Embolism','Encephalopathy','Endocarditis','Epilepsy','Heart Failure','Kidney Failure','Fibroid tumor','Gastritis','Gout','Hepatitis','Hernia hiatal','Hyperbilirubinemia','Hypercholesterolemia','Hyperglycemia','Hypothyroidism','Ileus','Incontinence','Infection urinary tract','Influenza','Insufficiency renal','Lymphoma','Malignant neoplasm of breast','Malignant neoplasm of prostate','Malignant tumor of colon','Myocardial infarction','Neoplasm','Neoplasm Metastasis','Obesity','Obesity Morbid'] y = prn.NNOut(P,net) print len(P);
from numpy import genfromtxt df = genfromtxt('example_data_pt2.csv', delimiter=',') P = df[1] Y = df[2] ### # Create and train NN # create recurrent neural network with 1 input, 2 hidden layers with # 2 neurons each and 1 output # the NN has a recurrent connection with delay of 1 timestep in the hidden # layers and a recurrent connection with delay of 1 and 2 timesteps from the output # to the first layer net = prn.CreateNN([1, 2, 2, 1], dIn=[0], dIntern=[1], dOut=[1, 2]) ### # Prepare input Data for gradient calculation data, net = prn.prepare_data(P, Y, net) ### # Calculate derivative vector (gradient vector) # Real Time Recurrent Learning t0_rtrl = time.time() J, E, e = prn.RTRL(net, data) g_rtrl = 2 * np.dot(J.transpose(), e) # calculate g from Jacobian and error vector t1_rtrl = time.time()
""" import pyrenn as prn import numpy as np import pandas as pd train = pd.read_csv('C:/TermProject/ClassifiedRnn/trainREC.csv', sep=',', header=None) test = pd.read_csv('C:/TermProject/ClassifiedRnn/testREC.csv', sep=',', header=None) train = np.array(train) test = np.array(test) net = prn.CreateNN([1000, 20, 1], dIn=[0], dIntern=[1], dOut=[1, 2]) net = prn.train_LM(train[:, 1:].T, train[:, 0], net, verbose=True, k_max=30, E_stop=1e-3) y = prn.NNOut(train[:, 1:].T, net) ytest = prn.NNOut(test[:, 1:].T, net) yTestPrd = np.array(ytest) yTrainPrd = np.array(y) yTestCor = test[:, 0] yTrainCor = train[:, 0]
### # Read Example Data df = genfromtxt('example_data_compressed_air.csv', delimiter=',') P = np.array([df[1], df[2], df[3]]) Y = np.array([df[4], df[5]]) Ptest = np.array([df[6], df[7], df[8]]) Ytest = np.array([df[9], df[10]]) ### # Create and train NN # create feed forward neural network with 1 input, 2 hidden layers with # 4 neurons each and 1 output # the NN has a recurrent connection with delay of 1 timesteps from the output # to the first layer net = prn.CreateNN([3, 5, 5, 2], dIn=[0], dIntern=[], dOut=[1]) # Train NN with training data P=input and Y=target # Set maximum number of iterations k_max to 500 # Set termination condition for Error E_stop to 1e-5 # The Training will stop after 500 iterations or when the Error <=E_stop net = prn.train_LM(P, Y, net, verbose=True, k_max=500, E_stop=1e-5) ### # Save outputs to certain file prn.saveNN(net, "D:/School/Masterproef/Python/pyrenn/SavedNN/compair.csv") print("savegelukt") ### # Calculate outputs of the trained NN for train and test data y = prn.NNOut(P, net) ytest = prn.NNOut(Ptest, net)
#define the first timestep t=0 of Test Data as previous (known) data P0test and Y0test P0test = Ptest_[:, 0:1] Y0test = Ytest_[:, 0:1] #Use the timesteps t = [1..99] as Test Data Ptest = Ptest_[:, 1:100] Ytest = Ytest_[:, 1:100] ### #Create and train NN #create feed forward neural network with 1 input, 2 hidden layers with #4 neurons each and 1 output #the NN has a recurrent connection with delay of 1 timesteps from the output # to the first layer net = prn.CreateNN([3, 5, 5, 2], dIn=[0], dIntern=[], dOut=[1]) #Train NN with training data P=input and Y=target #Set maximum number of iterations k_max to 500 #Set termination condition for Error E_stop to 1e-5 #The Training will stop after 500 iterations or when the Error <=E_stop prn.train_LM(P, Y, net, verbose=True, k_max=500, E_stop=1e-5) ### #Calculate outputs of the trained NN for test data with and without previous input P0 and output Y0 ytest = prn.NNOut(Ptest, net) y0test = prn.NNOut(Ptest, net, P0=P0test, Y0=Y0test) ### #Plot results fig = plt.figure(figsize=(15, 10))
# 假設70%訓練,30%要驗證 (TrainingData and TestingData) x_train, x_test, y_train, y_test = train_test_split(P.T, Y.T, test_size=0.3, random_state=None) normalization_factor = np.amax(x_train, axis=0) x_of_train = (x_train / normalization_factor).T x_of_test = (x_test / normalization_factor).T y_of_train = y_train.T / 600 y_of_test = y_test.T / 600 #------------------------------讀檔創建Dataframe--------------------------------- #----------------------------ANN 主程式--------------------------------------------- #8 input,2 hidden layer, 3 neuron (create NN) net = prn.CreateNN( [features_Num, hiddenlayer1_features, hiddenlayer2_features, 1]) # Train by NN net = prn.train_LM(x_of_train, y_of_train, net, verbose=True, k_max=iteration, E_stop=1e-10) # print out result y_prn_train = prn.NNOut(x_of_train, net) y_prn_test = prn.NNOut(x_of_test, net) # print('x train data 預測的 Predicted Y:','\n',y_prn_train*600) # print('x test data 預測的 Predicted Y:','\n',y_prn_test*600) #----------------------------ANN 主程式--------------------------------------------- #----------------------------確認執行後的結果------------------------------------------
sourcepath='source/' source=[] target=[] for i in range(10,51): if(os.path.exists(sourcepath+'arctic_a00'+str(i)+'.wav')): source.append(sourcepath+'arctic_a00'+str(i)+'.wav') targetpath='target/' for i in range(10,51): if(os.path.exists(targetpath+'arctic_a00'+str(i)+'.wav')): target.append(targetpath+'arctic_a00'+str(i)+'.wav') frameLength = 1024 overlap = 0.25 hop_length=256 subFrameLength = frameLength * overlap net=pyrenn.CreateNN([26,30,30,26]) order = 25 alpha = 0.41 gamma = -0.35 count=0 for sourcefile,targetfile in zip(source,target) : print(sourcefile,targetfile) sr, sx = wavfile.read(sourcefile) sourceframes = librosa.util.frame(sx, frame_length=frameLength, #framing the source audio hop_length=hop_length).astype(np.float64).T sourceframes *= pysptk.blackman(frameLength) #windowing sourcemcepvectors = np.apply_along_axis(pysptk.mcep, 1, sourceframes, order, alpha) #extract MCEPs of the source frames sr, tx = wavfile.read(targetfile) targetframes = librosa.util.frame(tx, frame_length=frameLength, #framing the target audio hop_length=hop_length).astype(np.float64).T targetframes *= pysptk.blackman(frameLength) . #windowing
# *********** main csv_train = pd.read_csv(dir_path + '/small_train_data.csv', delimiter=",", low_memory=False) print('Read train data') train = csv_train.drop(csv_train.columns[-1], axis=1) train_result = csv_train.drop(csv_train.columns[0:-1], axis=1) num_features = train.shape[1] num_data = train.shape[0] num_genre = len((train.iloc[:, -1]).unique()) print('Read ' + str(num_data) + ' values with ' + str(num_features) + 'features') num_features -= 1 nn = [num_features, (3 * num_features), (3 * num_features), 10] net = pyrenn.CreateNN(nn, dIn=[0], dIntern=[], dOut=[1]) train = train.drop(train.columns[0], axis=1) P1 = numpy.array(train.values) P = (P1 - P1.min(0)) / P1.ptp(0) # #joined_new_norm = (joined_new - joined_new.min(0)) / joined_new.ptp(0) P = P.T train_result = pd.get_dummies(train_result) Y = numpy.array(train_result.values) Y = Y.T print(Y.shape) print(num_features) print(P.shape) pyrenn.train_LM(P, Y, net,
# p(np.mean(TS[:,i*gr_size:(i+1)*gr_size]))#mean of group close to 0 TS_RNN[:,i] = np.var(TS[:,i*gr_size:(i+1)*gr_size], axis=1) return TS_RNN gr = 50 data_X = grouping(np.array(np.array(data_sample.loc[:,'0':])), gr) data_Y = data_sample.loc[:,'cat'] data_Y_cat = \ np.array(pd.get_dummies(data_sample.loc[:,'cat'].astype('category'))) #StratifiedKFold skf = StratifiedKFold(n_splits=2) skf.get_n_splits(data_X, data_Y) #creat ANN: 252 inputs, 3 output (prob for each category) net = prn.CreateNN([gr,20,20,20,20,3],dIn=[0],dIntern=[1],dOut=[]) #StratifiedKFold tr = np.transpose test_Y_cat = np.array([]) prod_Y_cat = np.array([]) for train_index, test_index in skf.split(data_X, data_Y): train_X, test_X = tr(data_X[train_index,:]), tr(data_X[test_index,:]) train_Y, test_Y = tr(data_Y_cat[train_index,:]), \ tr(data_Y_cat[test_index,:]) net = prn.train_LM(train_X,train_Y,net,verbose=True,k_max=500,E_stop=1e-5) prob_y = prn.NNOut(test_X,net) test_Y_cat = np.append(test_Y_cat,np.argmax(test_Y,axis=0)) prod_Y_cat = np.append(prod_Y_cat,np.argmax(prob_y, axis=0)) #confusion matrix, return accuray score
# Prints the correlation between the average of model outputs and the targets and then the correlation between the most recent model output and the targets print(np.corrcoef(nn_predictions.T, Y_test.T)[0, 1]**2) print(np.corrcoef(model.predict(X_test).T, Y_test.T)[0, 1]**2) nn_predictions = nn_predictions[:, 0] # A scatter of Tensorflow model claims predictions vs. actual claims pl.scatter(nn_predictions.T, Y_test.T) pl.show() # Creating a neural network using the Levenberg-Marquardt backpropagation training function # Used for quick descent training and possibly a more accurate prediction # Fewer hidden layers and less nodes are used due to a larger propensity to overfit # Cannont use a validation set for early stopping in pyrenn so these two lines are used to find convergence # Seems to converge around 10 epoches. Should stop early at 10 epoches to avoid overfitting on a small dataset net = pyrenn.CreateNN([8, 5, 1]) pyrenn.train_LM(X_train.T, Y_train.T, net, verbose=1, k_max=20) # The predictions are averaged across many different trained models for i in range(0, 10): print(i) net = pyrenn.CreateNN([8, 5, 1]) pyrenn.train_LM(X_train.T, Y_train.T, net, verbose=0, k_max=10) if i == 0: LM_predictions = pyrenn.NNOut(X_test.T, net) else: LM_predictions = (LM_predictions * (i) + pyrenn.NNOut(X_test.T, net)) / (i + 1) print(i)
def Tranning_by_Neural_Network(): #------------------------------讀檔創建Dataframe--------------------------------- #filepath='C:\\Users\\richard.weng\\Documents\\Python Scripts\\python_projects\\(1) NIVG Project\\ANN\\' file_data = file_name.get() + '.csv' df0 = pd.read_csv(file_data) #選擇受測人 #df = df[df.Name=='Nick'] df = df0.iloc[:, 1:] #移除first column of tester print(df.T.tail()) print('--------------------------------------------') print('df 長度為:', len(df)) print('--------------------------------------------') P = df.T.iloc[1:features_Num.get() + 1, 0:len(df)] print(P.tail()) print('input的格式:', P.shape) print('--------------------------------------------') Y = df.T.iloc[0:1, 0:len(df)] print(Y.tail()) print('output的格式:', Y.shape) print('--------------------------------------------') #轉成2d array P = np.array(P) Y = np.array(Y) # 假設70%訓練,30%要驗證 (TrainingData and TestingData) x_train, x_test, y_train, y_test = train_test_split(P.T, Y.T, test_size=0.3, random_state=None) x_of_train = (x_train / np.amax(x_train, axis=0)).T x_of_test = (x_test / np.amax(x_train, axis=0)).T y_of_train = y_train.T / 600 y_of_test = y_test.T / 600 #------------------------------讀檔創建Dataframe------------------------------------ #----------------------------ANN 主程式--------------------------------------------- #8 input,2 hidden layer, 3 neuron (create NN) net = prn.CreateNN([ features_Num.get(), hiddenlayer1_features.get(), hiddenlayer2_features.get(), 1 ]) # Train by NN net = prn.train_LM(x_of_train, y_of_train, net, verbose=True, k_max=iteration.get(), E_stop=1e-10) # print out result y_prn_train = prn.NNOut(x_of_train, net) y_prn_test = prn.NNOut(x_of_test, net) # print('x train data 預測的 Predicted Y:','\n',y_prn_train*600) # print('x test data 預測的 Predicted Y:','\n',y_prn_test*600) #----------------------------ANN 主程式--------------------------------------------- #----------------------------確認執行後的結果------------------------------------------ # visualize result plt.scatter(y_of_train * 600, y_prn_train * 600) plt.scatter(y_of_test * 600, y_prn_test * 600) plt.title('ANN Simulation Result') plt.xlabel('Input glucose (mg/dL)') plt.ylabel('Predicted glucose (mg/dL)') plt.grid() plt.show() print('測試組原本的糖值:', '\n', y_of_test * 600) print('測試組預測的糖值:', '\n', y_prn_test * 600) #----------------------------確認執行後的結果------------------------------------------ #Save ANN prn.saveNN(net, file_name.get() + '_LM_parameter' + '.csv') #Check final correlation y_all = prn.NNOut((P.T / np.amax(x_train, axis=0)).T, net) * 600 plt.scatter(Y.flatten(), y_all) Name = df0['Name'].values.tolist() df_result = pd.DataFrame({ 'Name': Name, 'total_y': Y.flatten(), 'total_pre_y': y_all }) print('相關性分析:\n', df_result.corr()) #列印出多少數據 print('總共樣本數:', len(df_result)) #Save the new result into new Excel df_result.to_csv(file_name.get() + '_LM_result' + '.csv')
def lm_NN(nn_in, nn_out, er_tar, main_win: tk.Toplevel, min_n=0, max_n=0, n_epochs=50, conf=[1, 1, 1], sect_ner=False, train_test=False, retrain=False, spl_coef=0.1, root_width=200, root_height=200, root_x=50, root_y=50): """ create and train pyrenn NN with LM optimization algorithm nn_in: list, train NN IN data nn_out: list, train OUT data er_tar: float, MSE target min_n: int, minimum neurons for selection of the number of neurons max_n: int, maximum neurons for selection of the number of neurons n_epochs: int, maximum NN train epochs conf: list, NN configuration, first element- numbers of input, last element - numbers of outputs, other elemnts - number of neuronus on hidden layers sect_ner: bool or 0/1, whether to select the number of neurons, True if yes, False if no train_test: bool or 0/1, should the input sample be separated into training and test retrain: bool or 0/1, overfitting protection return nn_obj, NN object conf: list, NN neurons configuration """ if train_test or retrain: x = tr.from_numpy(nn_in).float() y = tr.from_numpy((nn_out)).float() dataset = tr.utils.data.TensorDataset(x, y) a = int(len(dataset) * (1 - spl_coef)) data_trn, data_test = tr.utils.data.random_split( dataset, [a, int(len(dataset) - a)], generator=tr.Generator().manual_seed(42)) dataloader = tr.utils.data.DataLoader(data_trn, shuffle=False, batch_size=len(data_trn)) nn_in = (next(iter(dataloader))[0].numpy()).T nn_out = (next(iter(dataloader))[1].numpy()).T nn_in_test = data_test[:][0].numpy().T nn_out_test = data_test[:][1].numpy().T else: nn_in_test = 0 nn_out_test = 0 nn_in = nn_in.T nn_out = nn_out.T if sect_ner: if min_n > max_n: print("Error: minimum neurons>maximum neurons") for i in range(len(conf) - 2): conf[i + 1] = min_n for i in range(1, len(conf) - 1): min_loss = 20000 b = conf[i] for j in range(min_n, max_n + 1): conf[i] = j nn_obj = prn.CreateNN(conf) nn_obj = prn.train_LM(nn_in, nn_out, nn_obj, verbose=False, k_max=1, E_stop=1e-10) y_pred = prn.NNOut(nn_in, nn_obj) a = loss(y_pred, nn_out, nn_obj) print("Current configuration:", conf, ";\t Loss:", a, "%") if a < min_loss: min_loss = a b = j conf[i] = b neurons_number_info(conf, root_height + root_y, root_x) print("Best configuration:", conf) nn_obj = prn.CreateNN(conf) loss_val = [] loss_test = [] epochs_sum = 0 train = True lin_tr = None can_tr = None lin_test = None can_test = None vert_coef = None hor_coef = None while train == True: nn_obj, vert_coef, hor_coef, epochs_sum, err, lin_tr, can_tr, lin_test, can_test = train_body_LM( nn_obj, nn_in, nn_in_test, nn_out, nn_out_test, n_epochs, epochs_sum, loss_val, loss_test, er_tar, train_test, retrain, root_width, root_height, root_x, root_y, lin_tr, can_tr, lin_test, can_test, vert_coef, hor_coef) train_win = Continue_Train(err, epochs_sum) main_win.wait_window(train_win) train = train_win.answer if not train_win.answer: return nn_obj, conf, vert_coef, hor_coef #train = False return nn_obj, conf, vert_coef, hor_coef
from numpy import genfromtxt ### # Read Example Data df = genfromtxt('example_data_friction.csv', delimiter=',') P = df[1] Y = df[2] Ptest = df[3] Ytest = df[4] ### # Create and train NN # create feed forward neural network with 1 input, 2 hidden layers with # 3 neurons each and 1 output net = prn.CreateNN([1, 3, 3, 1]) # Train NN with training data P=input and Y=target # Set maximum number of iterations k_max to 100 # Set termination condition for Error E_stop to 1e-5 # The Training will stop after 100 iterations or when the Error <=E_stop net = prn.train_LM(P, Y, net, verbose=True, k_max=100, E_stop=9e-4) ### # Calculate outputs of the trained NN for train and test data y = prn.NNOut(P, net) ytest = prn.NNOut(Ptest, net) ### # Plot results fig = plt.figure(figsize=(11, 7))
sourceframes *= pysptk.blackman(frameLength) # windowing sourcemcepvectors = np.apply_along_axis( pysptk.mcep, 1, sourceframes, order, alpha) # extract MCEPs of the source frames sr, tx = wavfile.read(targetfile) targetframes = librosa.util.frame( tx, frame_length=frameLength, # framing the target audio hop_length=hop_length).astype(np.float64).T targetframes *= pysptk.blackman(frameLength) # windowing targetmcepvectors = np.apply_along_axis( pysptk.mcep, 1, targetframes, order, alpha) # extract mceps of target frames # Normalising for feeding into RNN. norm = min(len(sourcemcepvectors), len(targetmcepvectors)) transsourcemcepvectorsmod = np.transpose(sourcemcepvectors[0:norm]) transtargetmcepvectorsmod = np.transpose(targetmcepvectors[0:norm]) # Training Model. net = pyrenn.CreateNN([order + 1, order + 5, order + 5, order + 1]) net = pyrenn.train_LM(transsourcemcepvectorsmod, transtargetmcepvectorsmod, net, k_max=100, verbose=True, E_stop=5) # Saving Model. pyrenn.saveNN(net, 'pyrennweights_2.csv')
xpred2.append(xpred[len(xpred) - 2]) xpred2.append(xpred[len(xpred) - 1]) #xpred = xpred2 #xpred = np.asarray(xpred,dtype=float) print(xpred) print(xpred.shape) xpred = xpred.reshape(30, len(xpred)) print(xpred.shape) print(x.shape) print(xpred) #print(ny_y.shape) # Train the Linear Regression Object #mlpr= MLPRegressor().fit(x,y) net = pyrenn.CreateNN([30, 10, 1]) #print(net) net = pyrenn.train_LM(x, ny_y, net, verbose=True, k_max=200, E_stop=1e-2) y2 = pyrenn.NNOut(xpred, net) print(y2) """ ytest = pyrenn.NNOut(Ptest,net) fig = plt.figure(figsize=(11,7)) ax0 = fig.add_subplot(211) ax1 = fig.add_subplot(212) fs=18 #Train Data ax0.set_title('Train Data',fontsize=fs)
dc2_p_temp = [] for i in range(0,len(dc2_p[:])): temp = (dc2_p[i] - min(dc2_p)) / (max(dc2_p) - min(dc2_p)) dc2_p_temp.append(temp) dc2_p = dc2_p_temp row_temp = np.array(row_temp) dc1_p = np.array(dc1_p) dc2_p = np.array(dc2_p) G_t = np.array(G_t) print dc1_p net = prn.CreateNN([1,13,20,1]) net = prn.train_LM(dc1_p,G_t,net,verbose=True,k_max=1000,dampfac=.2,dampconst=5,E_stop=1e-5) csv_file_training = r"training_data_unnormalized.csv" row_temp_training = [] dc1_p_training = [] dc2_p_training = [] G_t_training = [] with open(csv_file_training, 'rb') as f:
x = pd.read_csv('train_input.txt', index_col=False) t1 = np.array(x['time'].values) P = np.array([x['pitch'].values, x['roll'].values]) print P y = pd.read_csv('train_output.txt', index_col=False) Y = np.array([y['pitch'].values, y['roll'].values]) x = pd.read_csv('test_output_100ms.txt', index_col=False) Ptest = np.array([x['pitch'].values, x['roll'].values]) print P y = pd.read_csv('test_input_100ms.txt', index_col=False) Ytest = np.array([y['pitch'].values, y['roll'].values]) t2 = np.array(y['time'].values) print 'Creating' net = prn.CreateNN([2, 7, 7, 2]) print 'Start training...' prn.train_LM(P, Y, net, verbose=True, k_max=40, E_stop=1e-3) ### ##Calculate outputs of the trained NN for train and test data y = prn.NNOut(P, net) ytest = prn.NNOut(Ptest, net) print ytest ### #Plot results fig = plt.figure(figsize=(15, 10)) ax0 = fig.add_subplot(221) ax1 = fig.add_subplot(222, sharey=ax0) ax2 = fig.add_subplot(223) ax3 = fig.add_subplot(224, sharey=ax2) fs = 18
print(np.corrcoef(nn_predictions[:, 0].T, Y_test.T)[0, 1]**2) print(np.corrcoef(model.predict(X_test)[:, 0].T, Y_test.T)[0, 1]**2) nn_predictions = nn_predictions[:, 0] # A scatter of Tensorflow model claims predictions vs. actual claims pl.scatter(nn_predictions.T, Y_test.T) pl.show() # Creating a neural network using the Levenberg-Marquardt backpropagation training function # Used for quick descent training and possibly a more accurate prediction # Fewer hidden layers and less nodes are used due to a larger propensity to overfit # Cannont use a validation set for early stopping in pyrenn so these two lines are used to find convergence # Seems to converge around 10 epoches. Should stop early at 10 epoches to avoid overfitting on a small dataset Y_train = Y_train[:, 0] net = pyrenn.CreateNN([30, 5, 1]) pyrenn.train_LM(X_train.T, Y_train.T, net, verbose=1, k_max=20) # The predictions are averaged across many different trained models for i in range(0, 10): print(i) net = pyrenn.CreateNN([30, 5, 1]) pyrenn.train_LM(X_train.T, Y_train.T, net, verbose=0, k_max=10) if i == 0: LM_predictions = pyrenn.NNOut(X_test.T, net) else: LM_predictions = (LM_predictions * (i) + pyrenn.NNOut(X_test.T, net)) / (i + 1) print(i)
import numpy as np import pyrenn as prn from numpy import genfromtxt from keras.datasets import mnist ### # Read Example Data (P, Y), (Ptest, Ytest) = mnist.load_data() ### # Create and train NN # create recurrent neural network with 28*28 inputs, # 1 hidden layers with 10 neurons # and 10 outputs (one for each possible class/number) # the NN uses no delayed or recurrent inputs/connections net = prn.CreateNN([28 * 28, 10, 10]) batch_size = 1000 number_of_batches = 20 for i in range(number_of_batches): r = np.random.randint(0, 25000 - batch_size) Ptrain = P[:, r:r + batch_size] Ytrain = Y[:, r:r + batch_size] # Train NN with training data Ptrain=input and Ytrain=target # Set maximum number of iterations k_max # Set termination condition for Error E_stop # The Training will stop after k_max iterations or when the Error <=E_stop net = prn.train_LM(Ptrain, Ytrain, net, verbose=True, k_max=1, E_stop=1e-5) print('Batch No. ', i, ' of ', number_of_batches)
import pandas as pd import numpy as np import matplotlib.pyplot as plt import pyrenn as prn datosIn = pd.read_csv("pacientestrain.csv", sep=',') datosOut = pd.read_csv("pacientestarg.csv", sep=',') P = np.array(datosIn) Y = np.array(datosOut) inputs = 7 outputs = 1 net = prn.CreateNN([inputs, 10, 14, outputs], dIn=[0], dIntern=[100], dOut=[10, 20]) net = prn.train_LM(P, Y, net, verbose=True, k_max=20000, E_stop=1e-10) y = prn.NNOut(P, net) ytest = prn.NNOut(P, net) ytest = np.array(ytest) print(ytest) filename = "nnBP.csv" prn.saveNN(net, filename)
#define the first 3 timesteps t=[0,1,2] of Test Data as previous (known) data P0test and Y0test P0test = Ptest_[0:3] Y0test = Ytest_[0:3] #Use the timesteps t = [3..99] as Test Data Ptest = Ptest_[3:100] Ytest = Ytest_[3:100] ### #Create and train NN #create recurrent neural network with 1 input, 2 hidden layers with #3 neurons each and 1 output #the NN uses the input data at timestep t-1 and t-2 #The NN has a recurrent connection with delay of 1,2 and 3 timesteps from the output # to the first layer (and no recurrent connection of the hidden layers) net = prn.CreateNN([1,3,3,1],dIn=[1,2],dIntern=[],dOut=[1,2,3]) #Train NN with training data P=input and Y=target #Set maximum number of iterations k_max to 200 #Set termination condition for Error E_stop to 1e-3 #The Training will stop after 200 iterations or when the Error <=E_stop net = prn.train_LM(P,Y,net,verbose=True,k_max=200,E_stop=1e-3) ### #Calculate outputs of the trained NN for test data with and without previous input P0 and output Y0 ytest = prn.NNOut(Ptest,net) y0test = prn.NNOut(Ptest,net,P0=P0test,Y0=Y0test) ### #Plot results
#轉成2d array P = np.array(P) Y = np.array(Y) # 假設70%訓練,30%要驗證 (TrainingData and TestingData) x_train, x_test, y_train, y_test = train_test_split(P.T, Y.T, test_size=0.3, random_state=None) x_of_train = (x_train / np.amax(x_train, axis=0)).T x_of_test = (x_test / np.amax(x_train, axis=0)).T y_of_train = y_train.T / 600 y_of_test = y_test.T / 600 #8 input,2 hidden layer, 3 neuron (create NN) net = prn.CreateNN([8, 3, 3, 1]) # Train by NN net = prn.train_LM(x_of_train, y_of_train, net, verbose=True, k_max=100, E_stop=1e-10) # print out result y_prn_train = prn.NNOut(x_of_train, net) y_prn_test = prn.NNOut(x_of_test, net) # print('x train data 預測的 Predicted Y:','\n',y_prn_train*600) # print('x test data 預測的 Predicted Y:','\n',y_prn_test*600) # visualize result plt.scatter(y_of_train * 600, y_prn_train * 600) plt.scatter(y_of_test * 600, y_prn_test * 600)