def Test_dbn_bp_MNIST(): import input_data import matplotlib.pyplot as plt 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 trX = trX[:5000] trY = trY[:5000] teX = teX[:1000] teY = teY[:1000] dbn = DBN(layer_sizes=[784, 400, 100]) # pre-training (TrainUnsupervisedDBN) errs = dbn.pretrain(input=trX, lr=0.1, k=1, epochs=100, batch_size=100) #, gaus=True) print("dbn :", errs) network = BP([784, 400, 100, 10]) network.setActivation([sigmoid, sigmoid, sigmoid]) #softmax wList, bList = dbn.getHyperParameter() network.setHyperParameter(wList, bList) network.train(trX, trY, lr=0.1, epochs=1, batch_size=100) # for item in test_data: res = network.predict(teX) print(np.mean(np.argmax(teY, axis=1) == np.argmax(res, 1))) '''0.098'''
def __init__(self, layer_sizes=[3, 3], bp_layer=[], rng=None): ''' :param input: :param label: :param n_ins: :param hidden_layer_sizes: :param n_outs: :param rng: 随机数发生器 ''' '''说明:layer_sizes的最后一个参数,是bp_layer的第一个输入参数 例:DBN(layer_sizes[10,10,20], bp_layer=[5,1]) bp 是[20, 5, 1] ''' self.rbm_layers = [] self.n_layers = len(layer_sizes) - 1 # = len(self.rbm_layers) if rng is None: rng = np.random.RandomState(1234) '''最少为一层''' assert self.n_layers > 0 # construct multi-layer for i in range(self.n_layers): # layer_size # construct rbm_layer rbm_layer = RBM(n_visible=layer_sizes[i], n_hidden=layer_sizes[i + 1], rng=rng) self.rbm_layers.append(rbm_layer) self.bp_layers = None self._n_bp_layers = len(bp_layer) if self._n_bp_layers > 0: para = [layer_sizes[-1]] + bp_layer self.bp_layers = BP(para)
def DBN_BP_Test(seq_len, win, lr): data_src = LD.loadCsvData_Np("../data/co2-ppm-mauna-loa-19651980.csv", skiprows=1, ColumnList=[1]) # Australia/US British/US Canadian/US Dutch/US French/US German/US Japanese/US Swiss/US data_t =data_src[:,0:1].copy() # 数据还源时使用 t_mean = np.mean(data_t) t_min = np.min(data_t) t_max = np.max(data_t) # 数据预处理 result,x_result,y_result = LD.dataRecombine_Single(data_t, seq_len) # print(x_result, y_result) result_len = len(result) row = round(0.8 * result.shape[0]) row = result_len - 87 windowSize = row windowSize = win # 数据归一化 # data_normalization = EvaluationIndex.归一化.normalization_max_min_负1_1(data_src) x_result_GY = ((x_result - t_min) / (t_max - t_min)).copy() y_result_GY = ((y_result - t_min) / (t_max - t_min)).copy() # x_result = (x_result - t_mean) / np.std(x_result) # y_result = (y_result - t_mean) / np.std(x_result) y_rbf_all = [] y_test_all = [] rng = np.random.RandomState(1233) for y_i in range(row, result_len): if y_i < windowSize: continue x_train = x_result_GY[y_i - windowSize:y_i] y_train = y_result_GY[y_i - windowSize:y_i] x_test = x_result_GY[y_i:y_i + 1] y_test = y_result_GY[y_i:y_i + 1] # print(x_train, y_train) # assert False net = DBN(layer_sizes=[seq_len,20,40], bp_layer=[1]) net.pretrain(x_train,lr=lr, epochs=200) # net.fineTune(x_train, y_train,lr=lr, epochs=10000) bp = BP([seq_len,20,40, 1]) w_list, b_list = net.getHyperParameter() bp.setHyperParameter(w_list, b_list) bp.train(x_test, y_test, lr=lr, epochs=10000) y_rbf = bp.predict(x_train) y_rbf_all.append(y_rbf) y_test_all.append(y_test) # print(np.array(y_rbf_all).ravel()) # print(np.array(y_test_all).ravel())#, np.array(y_test_all)) # print("全部预测RMSE") # y_rbf_all = np.array(y_rbf_all).ravel() # y_test_all = np.array(y_test_all).ravel() # ei = EI.evalueationIndex(y_rbf_all, y_test_all) # ei.show() import MDLearn.utils.Draw as draw # draw.plot_results_point(y_rbf_all, y_test_all) '''还原数据''' print("DBN_BP_Test还原预测RMSE") y_rbf_all = np.array(y_rbf_all) y_test_all = np.array(y_test_all) y_rbf_haunYuan = y_rbf_all * (t_max - t_min) + t_min y_test_haunYuan = y_test_all * (t_max - t_min) + t_min ei = EI.evalueationIndex(y_rbf_haunYuan, y_test_haunYuan) ei.show()