def __init__(self): self.game = carmunk.GameState() self.episodes_length = 10000 self.nn = NN(7, 3) self.gamma = 0.9 # Generate the necessary tensorflow ops self.inputs1, self.nextQ = self.nn.placeholder_inputs(None) self.Qout = self.nn.inference(self.inputs1, 128, 32) self.loss = self.nn.loss_val(self.Qout, self.nextQ) self.train_op = self.nn.training(self.loss, learning_rate=0.01) self.time_per_epoch = tf.placeholder(tf.float32, shape=()) self.init = tf.initialize_all_variables() self.saver = tf.train.Saver() # Generate the requisite buffer self.experience_memory = 10000 self.replay = [] self.minibatch_size = 128 self.epsilon = 0.9 # self.saver.restore(self.sess, "newmodel1.ckpt") self.logs_path = '/tmp/tensorflow_logs/example21' # Create a summary to monitor loss tensor tf.scalar_summary("timeperepoch", self.time_per_epoch) # Merge all summaries into a single op self.merged_summary_op = tf.merge_all_summaries()
def Test_nn(): mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images,\ mnist.test.labels '''def __init__(self, epoches, learning_rate, batchsize, momentum, penaltyL2, dropoutProb):''' opts = DLOption(10, 1., 100, 0.0, 0., 0.) nn = NN([500, 300], opts, trX, trY) nn.train() print(np.mean(np.argmax(teY, axis=1) == nn.predict(teX)))
def Test_dbn(): mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images,\ mnist.test.labels opts = DLOption(10, 1., 100, 0.0, 0., 0.) dbn = DBN([400, 100], opts, trX) errs = dbn.train() print(errs) nn = NN([100], opts, trX, trY) nn = NN([400, 100], opts, trX, trY) nn.load_from_dbn(dbn) nn.train() print(np.mean(np.argmax(teY, axis=1) == nn.predict(teX)))
def test_bp2(): print("test neural network") x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0, 1], [1, 0], [1, 0], [0, 1]]) np.set_printoptions(precision=3, suppress=True) rng = np.random.RandomState(123) # network = BP(n_ins=2, hidden_layer_sizes=[3], n_outs=2, rng=rng) # network.fitBatch(x, y, learning_rate=0.1, epochs=500) opts = DLOption(10, 1., 100, 0.0, 0., 0.) nn = NN([3], opts, x, y) nn.train() print(nn.predict(x)) print(np.mean(np.argmax(y, axis=1) == nn.predict(x))) return
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2016 Peng Liu <*****@*****.**> # # Distributed under terms of the GNU GPL3 license. """ Test some function. """ import input_data from opts import DLOption from dbn_tf import DBN from nn_tf import NN import numpy as np mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images,\ mnist.test.labels opts = DLOption(10, 1., 100, 0.0, 0., 0.) dbn = DBN([400, 100], opts, trX) dbn.train() nn = NN([100], opts, trX, trY) nn = NN([400, 100], opts, trX, trY) nn.load_from_dbn(dbn) nn.train() print(np.mean(np.argmax(teY, axis=1) == nn.predict(teX)))
# -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2016 Peng Liu <*****@*****.**> # # Distributed under terms of the GNU GPL3 license. """ Test some function. """ import input_data from opts import DLOption from dbn_tf import DBN from nn_tf import NN import numpy as np mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images,\ mnist.test.labels opts = DLOption(10, 1., 100, 0.0, 0., 0.) dbn = DBN([400, 100], opts, trX) dbn.train() nn = NN([100], opts, trX, trY) nn = NN([400, 100], opts, trX, trY) nn.load_from_dbn(dbn) nn.train() print np.mean(np.argmax(teY, axis=1) == nn.predict(teX))
with open('E:/数据集/2020年2月nyiso数据集/2月2日数据/x_2month2_all.csv', 'r', encoding="utf-8") as file: # (标准IEEE14的测试数据) #with open('E:/数据集/2020年2月nyiso数据集/2月1日数据/all_有名值_数据/x_2month1_all.csv', 'r', encoding="utf-8") as file: # (标准IEEE14的测试数据) reader = csv.reader(file) a = [] for item in reader: a.append(item) a = [[float(x) for x in item] for item in a] #将矩阵数据转化为浮点型 data = np.array(a) x_data_migration_test = autoNorm(data[:, 0:54]) y_data_migration_test = data[:, [54, 55]] x_data_migration_test = x_data_migration_test.astype(np.float32) y_data_migration_test = y_data_migration_test.astype(np.float32) opts = DLOption(18, 450, 0.1, 0.2, 1000, 0, 0., 0., 0.01, 7000, 300, 0.001, 50000) dbn = DBN([40, 20, 12], opts, x_data_pretrain) dbn.train() nn = NN([40, 20, 12], [10, 6], opts, x_data_train, y_data_train, x_data_test, y_data_test, x_migration_train, y_migration_train, x_migration_test, y_migration_test, x_data_migration_test, y_data_migration_test, [10]) nn.load_from_dbn(dbn) nn.train() #print( np.mean(np.argmax(y_data_test, axis=1) == nn.predict(x_data_test))) nn.train_migration() nn.train_migration_all()
def svmBagging(SamplingMethode): # 读取数据 train = pd.read_csv("../data/data.csv", header=0) # 将数据都变为int型 for col in train.columns: for i in range(1000): train[col][i] = int(train[col][i]) # 归一化处理 min_max_scaler = preprocessing.MinMaxScaler() train = min_max_scaler.fit_transform(train) # 分割为train和test两个数据集 train, test = train_test_split(train, test_size=0.2) #À travers Upsampling and LowSampling, Équilibrer les données, # C'est à dire, le nombre de bons clients soit le même que le nombre de mauvais clients # Le différence entre eux, c'est UpSampling élève le nombre de mauvais clients just qu'aux égal bon clients if SamplingMethode == 'upSampling': # 这里做上采样 X_train, y_train = upSampling(train) y_train = y_train.reshape(len(y_train), 1) train = np.append(y_train, X_train, axis=1) print("Apres UpSampling, la quantité des données équal: ", len(train)) # LowSampling réduit le nombre de bon clients just qu'aux égal mauvais clients elif SamplingMethode == 'lowSamoling': train = pd.DataFrame(train) train = np.array(lowSampling(train)) print("Apres LowSampling, la quantité des données équal: ", len(train)) # 切割出EI数据集 # EI = np.array(RepetitionRandomSampling(train, len(train), 0.5)) EI_train = EI[:, 1:] EI_test = EI[:, 0] clf_svm = [svm.SVC(kernel='rbf', gamma='scale', C=1.75) for _ in range(40)] #clf_svm = [fsvmClass.HYP_SVM(kernel='polynomial', C=1.5, P=1.5) for _ in range(40)] # clf_svm = [HYP_SVM(C=1.5) for _ in range(2)] bag = Bagging(40, clf_svm, 0.5) svms = bag.MutModel_clf(np.array(train), np.array(test)) result = list() for each in svms: result.append(each.predict(EI_train)) #chaque colonne est le résultat de chaque svms result = np.array(result) trX = np.transpose(result) #transpose pour chaque ligne est le résultat trX = trX.astype(np.float32) trY = to_categorical(EI_test) # devenir une binary class trY = trY.astype(np.float32) # DLOption为一个用于存储模型超参数的类。 # 按顺序来是: epoches, learning_rate, batchsize, momentum, penaltyL2,dropoutProb opts = DLOption(300, 0.01, 64, 0.01, 0., 0.2) # DBN类代表DBN网络类,参数分别为sizes, opts, X # 这里的[400,200,100]表示有三层RBM,每一层输出为400,200和100 dbn = DBN([400, 100], opts, trX) # DBN训练 dbn.train() # 这里初始化三层全联接层,前两层全联接层使用已训练好的RBM参数填入进去,最后一层进行fine-turn训练 # 输入参数分别为sizes, opts, X, Y # nn = NN([100], opts, trX, trY) # 这里创建的三层,前两层的输出与上面保持一致 nn = NN([400, 100], opts, trX, trY) # 这里加载已经训练好的dbn参数 nn.load_from_dbn(dbn) # 训练最后一层输出层,达到分类效果 nn.train() testX = test[:, 1:] testY = test[:, 0] test_result = list() for each in svms: test_result.append(each.predict(testX)) test_result = np.array(test_result) teX = np.transpose(test_result) teX = teX.astype(np.float32) teY = testY.astype(np.float32) # score = Voting(result) cm = confusion_matrix(teY, nn.predict(teX)) sns.heatmap(cm, annot=True, fmt='') plt.title('DBN') plt.ylabel('True label') plt.xlabel('Predicted label') plt.show() acc = len(teY[teY == nn.predict(teX)]) / len(teY) sp = cm[0, 0] / (cm[0, 0] + cm[0, 1]) se = cm[1, 1] / (cm[1, 0] + cm[1, 1]) print("accuracy for DBN: ", round(acc, 3)) print("specifity for DBN: ", round(sp, 3)) print("Sensitivity for DBN: ", round(se, 3)) score = Voting(test_result) cm = confusion_matrix(teY, score) sns.heatmap(cm, annot=True, fmt='') plt.title('Voting') plt.ylabel('True label') plt.xlabel('Predicted label') plt.show() acc = len(teY[teY == score]) / len(teY) sp = cm[0, 0] / (cm[0, 0] + cm[0, 1]) se = cm[1, 1] / (cm[1, 0] + cm[1, 1]) print("accuracy for Voting: ", round(acc, 3)) print("specifity for Voting: ", round(sp, 3)) print("Sensitivity for Voting: ", round(se, 3))
def fsvmBagging(SamplingMethode): # 读取数据 train = pd.read_csv("../data/data.csv", header=0) # 将数据都变为int型 for col in train.columns: for i in range(1000): train[col][i] = int(train[col][i]) features = train.columns[1:21] X = train[features] y = train['Creditability'] min_max_scaler = preprocessing.MinMaxScaler() X = min_max_scaler.fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) y_train = np.array(y_train) y_train = y_train.reshape(len(y_train), 1) train = np.append(y_train, np.array(X_train), axis=1) if SamplingMethode == 'upSampling': X_train, y_train = upSampling(train) elif SamplingMethode == 'lowSampling': train = pd.DataFrame(train) train = np.array(lowSampling(train)) X_train = train[:, 1:] y_train = train[:, 0] X_train = np.asarray(X_train) y_train = np.asarray(y_train) for i in range(len(y_train)): if y_train[i] == 0: y_train[i] = -1 y_test = np.array(y_test) for i in range(len(y_test)): if y_test[i] == 0: y_test[i] = -1 y_train = np.array(y_train) y_train = y_train.reshape(len(y_train), 1) train = np.append(y_train, np.array(X_train), axis=1) y_test = np.array(y_test) y_test = y_test.reshape(len(y_test), 1) test = np.append(y_test, np.array(X_test), axis=1) # 切割出EI数据集 # le numbre de sous ensembles de donnée est 0.5*len(train) EI = np.array(RepetitionRandomSampling(train, len(train), 0.5)) EI_train = EI[:, 1:] EI_test = EI[:, 0] clf_svm = [HYP_SVM(kernel='polynomial', C=1.75, P=0.1) for _ in range(20)] # 20 sous ensembles de données, estimator est fsvm, rate est 0.5 bag = Bagging(20, clf_svm, 0.5, 'fsvm') svms = bag.MutModel_clf(np.array(train), np.array(test)) result = list() for each in svms: result.append(each.predict(EI_train)) result = np.array(result) trX = np.transpose(result) trX = trX.astype(np.float32) for i in range(len(EI_test)): if EI_test[i] == -1: EI_test[i] = 0 trY = to_categorical(EI_test) trY = trY.astype(np.float32) # DLOption为一个用于存储模型超参数的类。 # 按顺序来是: epoches, learning_rate, batchsize, momentum, penaltyL2,dropoutProb opts = DLOption(10, 1, 64, 0.01, 0., 0.2) # DBN类代表DBN网络类,参数分别为sizes, opts, X # 这里的[400,100]表示有两层RBM,每一层输出为400和100 [100,50,10] dbn = DBN([100, 50, 10], opts, trX) # DBN训练 dbn.train() # 这里初始化三层全联接层,前两层全联接层使用已训练好的RBM参数填入进去,最后一层进行fine-turn训练 # 输入参数分别为sizes, opts, X, Y # nn = NN([100], opts, trX, trY) # 这里创建的三层,前两层的输出与上面保持一致 nn = NN([100, 50, 10], opts, trX, trY) # 这里加载已经训练好的dbn参数 nn.load_from_dbn(dbn) # 训练最后一层输出层,达到分类效果 nn.train() testX = test[:, 1:] testY = test[:, 0] test_result = list() for each in svms: test_result.append(each.predict(testX)) test_result = np.array(test_result) teX = np.transpose(test_result) teX = teX.astype(np.float32) for i in range(len(testY)): if testY[i] == -1: testY[i] = 0 teY = testY.astype(np.float32) # print('Fianl acc: ',teY == nn.predict(teX)) # score = Voting(result) cm = confusion_matrix(teY, nn.predict(teX)) sns.heatmap(cm, annot=True, fmt='') plt.title('DBN') plt.ylabel('True label') plt.xlabel('Predicted label') plt.show() acc = len(teY[teY == nn.predict(teX)]) / len(teY) sp = cm[0, 0] / (cm[0, 0] + cm[0, 1]) se = cm[1, 1] / (cm[1, 0] + cm[1, 1]) print("accuracy for DBN: ", round(acc, 3)) print("specifity for DBN: ", round(sp, 3)) print("Sensitivity for DBN: ", round(se, 3)) score = Voting(test_result) cm = confusion_matrix(teY, score) sns.heatmap(cm, annot=True, fmt='') plt.title('Voting') plt.ylabel('True label') plt.xlabel('Predicted label') plt.show() acc = len(teY[teY == score]) / len(teY) sp = cm[0, 0] / (cm[0, 0] + cm[0, 1]) se = cm[1, 1] / (cm[1, 0] + cm[1, 1]) print("accuracy for Voting: ", round(acc, 3)) print("specifity for Voting: ", round(sp, 3)) print("Sensitivity for Voting: ", round(se, 3))
class Agent(): def __init__(self): self.game = carmunk.GameState() self.episodes_length = 10000 self.nn = NN(7, 3) self.gamma = 0.9 # Generate the necessary tensorflow ops self.inputs1, self.nextQ = self.nn.placeholder_inputs(None) self.Qout = self.nn.inference(self.inputs1, 128, 32) self.loss = self.nn.loss_val(self.Qout, self.nextQ) self.train_op = self.nn.training(self.loss, learning_rate=0.01) self.time_per_epoch = tf.placeholder(tf.float32, shape=()) self.init = tf.initialize_all_variables() self.saver = tf.train.Saver() # Generate the requisite buffer self.experience_memory = 10000 self.replay = [] self.minibatch_size = 128 self.epsilon = 0.9 # self.saver.restore(self.sess, "newmodel1.ckpt") self.logs_path = '/tmp/tensorflow_logs/example21' # Create a summary to monitor loss tensor tf.scalar_summary("timeperepoch", self.time_per_epoch) # Merge all summaries into a single op self.merged_summary_op = tf.merge_all_summaries() # Maps state to Q values def StoQ_FApprox(self, state): return self.sess.run(self.Qout, feed_dict={self.inputs1: state}) def StoQ_FApprox_train(self, current_input, target_output): self.sess.run([self.train_op], feed_dict={self.inputs1: current_input, self.nextQ: target_output}) def epsilon_greedy(self, qval): ran = random.random() if (ran < self.epsilon): return random.randint(0, qval.shape[1] - 1) else: return np.argmax(qval) def experience_replay(self, current_state, current_action, current_reward, next_state): self.replay.append((current_state, current_action, current_reward, next_state)) states_history=[] targetQ_history=[] if(len(self.replay) > self.minibatch_size): if(len(self.replay) > self.experience_memory): self.replay.pop(0) minibatch = random.sample(self.replay, self.minibatch_size) for experience in minibatch: hState = experience[0] hAction = experience[1] hReward = experience[2] hNextState = experience[3] oldq = self.StoQ_FApprox(hState) newq = self.StoQ_FApprox(hNextState) maxq = np.max(newq) target = oldq.copy() if(hReward == 500): # Terminal stage target[0][hAction] = hReward else: # Non terminal stage target[0][hAction] = hReward + self.gamma * maxq states_history.append(hState.copy()) targetQ_history.append(target.copy()) states_history = np.array(states_history).reshape(self.minibatch_size, -1) targetQ_history = np.array(targetQ_history).reshape(self.minibatch_size, -1) return states_history, targetQ_history def learn(self): self.sess = tf.Session() self.sess.run(self.init) # op to write logs to Tensorboard summary_writer = tf.train.SummaryWriter(self.logs_path, graph=tf.get_default_graph()) total_time = 0 for episode_number in range(1, self.episodes_length+1): reward, state = self.game.frame_step(2) epoch_time = 0 while True : orgstate = state.copy() # Choose a from s using policy derived from Q (epsilon-greedy) Qs = self.StoQ_FApprox(state) action = self.epsilon_greedy(Qs) # Take action action, observe reward and snext. reward, state = self.game.frame_step(action) states_history, targetQ_history = self.experience_replay(orgstate.copy(), action, reward, state.copy()) if(len(self.replay) > self.minibatch_size): self.StoQ_FApprox_train(states_history, targetQ_history) if(len(self.replay) >= 10000 and self.epsilon > 0.1): self.epsilon = self.epsilon - 1. / total_time epoch_time += 1 total_time += 1 if(reward == 500): save_path = self.saver.save(self.sess, "/tmp/model.ckpt") print "episode: ", episode_number, " and new epsilon ", self.epsilon, "len_experience buffer", len(self.replay), "total time", total_time, "epoch time", epoch_time # This game ends now. break; summary = self.sess.run(self.merged_summary_op, feed_dict={self.time_per_epoch: epoch_time}) summary_writer.add_summary(summary, episode_number)