def Core_train(in_nodes, hid_nodes, out_nodes, x, y, epochs): Core = net.Sequential() Core.add(net.DenseLayer(in_nodes, hid_nodes, lr=0.2)) Core.add(net.ActivationLayer(net.Sigmoid)) Core.add(net.DenseLayer(hid_nodes, hid_nodes * 2, lr=0.2)) Core.add(net.ActivationLayer(net.Sigmoid)) #Core.add(net.DenseLayer(hid_nodes*2,hid_nodes*2,lr = 0.2)) #Core.add(net.ActivationLayer(net.Sigmoid)) Core.add(net.DenseLayer(hid_nodes * 2, hid_nodes, lr=0.2)) Core.add(net.ActivationLayer(net.Sigmoid)) #Below is the same structure/ similar structure an ocsvm used Core.add(net.DenseLayer(hid_nodes, out_nodes * 10, lr=0.3)) Core.add(net.ActivationLayer(net.Sigmoid)) Core.add(net.DenseLayer(out_nodes * 10, out_nodes, lr=0.2)) #To 'pretrain', the specific random data class is extracted for training for kll in range( 10): #Maximum 20 in this dataset, in fact can set as len(x) z = 300 temp_x = [] temp_y = [] label_chosen = y[ 0] #input("Input the label you want to choose to initiate: ") #print("Label chosen: ",label_chosen) temp_x, temp_y = Get_Certain_label(x, y, label_chosen, int(z * (kll + 1))) ################# Core.fit(temp_x, temp_y, epochs) prediction = Core.predict(x) #print('',y[0]) Current_Score = Score_Metrics.Score(prediction, label_chosen, y) print("") print("The following is current score") print(Current_Score) print("_____________________________________________") pass return Core pass
def Single_OC_SVM_Train(real_x, y, x, answer_list, CK_T): temp = net.Sequential() temp.add(net.DenseLayer(hid_nodes, out_nodes * 10, lr=0.3)) temp.add(net.ActivationLayer(net.Sigmoid)) temp.add(net.DenseLayer(out_nodes * 10, out_nodes, lr=0.2)) temp_x = [] temp_y = [] while True: z = random.randint(0, len(answer_list) - 1) if answer_list[z] <= 0: temp_x.append(x[z]) temp_y.append(1) break while True: #data extraction, for partial training z = random.randint(0, len(answer_list) - 1) if answer_list[z] > 0: temp_x.append(x[z]) temp_y.append(-1) break temp_y = np.asarray(temp_y).reshape(len(temp_y), 1) temp.fit(temp_x, temp_y, 50) print("") Improve_train.Improve_train(x, temp, 100, CK_T) prediction = temp.predict(x) print(len(prediction)) #Following is used to supervise the unsupervised network --> how it is doing now --> and have no interference with the code if len(prediction) > 0: current_label = Score_Metrics.Get_Sample(prediction, real_x, y) Current_Score = Score_Metrics.Score(prediction, current_label, y) print("") print("Current defined answer", Score_Metrics.Answer_defined(prediction)) print("") ''' print("The following is current score after pretrained") #It seems that it has some problem here. print(Current_Score, len(y)) ''' print("_____________________________________________") #print(prediction) return temp else: print("Error: ", temp.predict(x))