1. 程式人生 > >Tensorflow實現BP神經網路

Tensorflow實現BP神經網路

Tensorflow實現BP神經網路

摘要:深度學習中基本模型為BP深度神經網路,其包括輸入層、隱含層和輸出層。輸入層的神經元個數取決於資料集屬性特徵的個數,輸出層神經元個數取決於劃分類標的個數。BP神經網路通過梯度下降法不斷調整權重矩陣和偏向進行調參,實現神經網路的訓練。
  本人為初學者,自己嘗試編寫了tensorflow實現BP神經網路,輸入層為三個神經元。隱含層為四個神經元、輸出層為兩個神經元,權重矩陣和偏向均為正態分佈隨機數。
  本人對神經網路進行的剖析,深度神經網路的詳細解讀:深度神經網路
1、main函式:

#code by WangJianing
#email:
[email protected]
or [email protected] #time:2018.11.24 import tensorflow as tf import numpy as np from neural_network import NN #從檔案中讀取資料 def readFile(filename): """ read file from txt """ input_x = [] input_y = [] with open(filename,'r') as f: while True: line = f.readline() if line == '': break else: line = line.replace('\n','') sample = line.split(' ') x = sample[0:3] x = list(map(np.float32, x)) y = sample[3] y = list(map(np.int32, y)) input_x.append(x) input_y.append(y) return input_x,input_y #main函式 if __name__ == '__main__': config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.2 # need ~700MB GPU memory train_x,train_y = readFile('./data.txt') test_x,test_y = readFile('./data_test.txt') sample_size = [len(train_y),len(test_y)] print(sample_size) train_x = np.transpose(train_x) input_y = np.zeros([2,sample_size[0]]) test_x = np.transpose(test_x) test_y = np.transpose(test_y) for ei,i in enumerate(train_y): input_y[i[0]][ei]=1 # print(ei,i) #build neural network n = NN(train_x, input_y, test_x, test_y, 'GradientDescentOptimizer', sample_size, config, learning_rate=0.05) #train n.train1() #test n.test()

2、神經網路類:

#code by WangJianing
#email:[email protected] or [email protected]
#time:2018.11.24

import tensorflow as tf
import numpy as np

class NN(object):
    """docstring for NN"""
    def __init__(self, train_x, train_y, test_x, test_y, optimize, sample_size, config, learning_rate=0.05):
        super(NN, self).__init__()
        self.train_x = tf.to_float(train_x, name='ToFloat1')
        self.train_y = tf.to_float(train_y, name='ToFloat2')
        self.test_x = tf.to_float(test_x, name='ToFloat3')
        self.test_y = tf.to_float(test_y, name='ToFloat4')
        self.learning_rate = learning_rate
        self.optimize = optimize
        self.sess = tf.Session()        
        self.sample_size = sample_size
        self.config = config
        self.para = [[],[],[],[],0]
        self.bildGraph()
        # self.train()

    #建立計算圖(訓練時)
    def bildGraph(self):
        self.parameter_op()
        self.towards_op()
        self.loss_op()
        self.backwords_op()
        # self.test_towords()
        self.init_op()
    #建立評估測試計算圖
    def testBuildGraph(self):
        self.parameter_op()
        self.towards_op()
    
    #建立引數初始化結點
    def parameter_op(self):
        self.weight1 = tf.Variable(tf.random_normal([4, 3], stddev=0.03), dtype=tf.float32, name='weight1')
        self.bias1 = tf.Variable(tf.random_normal([4, 1]), dtype=tf.float32, name='bias1')
        self.weight2 = tf.Variable(tf.random_normal([2, 4], stddev=0.03), dtype=tf.float32, name='weight2')
        self.bias2 = tf.Variable(tf.random_normal([2, 1]), dtype=tf.float32, name='bias2')
        self.input_xx = tf.Variable(self.train_x,name='xx1')
        self.input_xx_test = tf.Variable(self.test_x,name='xx3')
        self.input_yy = tf.Variable(self.train_y,name='xx2')

    #該方法是將一個一維向量v複製size次並拼起來
    def appendVector(self, v, size, kind):
        _v = tf.transpose(v)[0]
        # print('_v=',_v)
        new_v = []
        if kind == 0:
            for i in range(size):
                new_v.append(_v)
            self.bias1_train = tf.Variable(new_v, dtype=tf.float32, name='bias1_train')
            self.bias1_train = tf.transpose(self.bias1_train)
        elif kind == 1:
            for i in range(size):
                new_v.append(_v)
            self.bias2_train = tf.Variable(new_v, dtype=tf.float32, name='bias2_train')
            self.bias2_train = tf.transpose(self.bias2_train) 
        elif kind == 2:
            for i in range(size):
                new_v.append(_v)
            self.bias1_test = tf.Variable(new_v, dtype=tf.float32, name='bias1_test')
            self.bias1_test = tf.transpose(self.bias1_test) 
        elif kind == 3:
            for i in range(size):
                new_v.append(_v)
            self.bias2_test = tf.Variable(new_v, dtype=tf.float32, name='bias2_test')
            self.bias2_test = tf.transpose(self.bias2_test)  

    #前向傳播
    def towards_op(self):
        self.m1 = tf.matmul(self.weight1, self.input_xx, name='matmul1')
        # print('m1=',self.m1)
        self.appendVector(self.bias1, self.sample_size[0], 0)
        # print('self.bias1_train=',self.bias1_train)
        self.z1 = tf.add(self.m1 ,self.bias1_train, name='z1')
        self.a1 = tf.nn.sigmoid(self.z1,name='a1')
        self.appendVector(self.bias2, self.sample_size[0], 1)
        self.z2 = tf.add(tf.matmul(self.weight2, self.a1, name='matmul2'),self.bias2_train, name='z2')
        self.a2 = tf.transpose(tf.nn.softmax(tf.transpose(self.z2,[1,0]),name='a2'),[1,0])

    #測試時前向傳播
    def test_towords(self):
        self.t_m1 = tf.matmul(self.para[0], self.input_xx_test, name='matmul3')
        self.appendVector(self.para[2], self.sample_size[1], 2)
        self.t_z1 = tf.add(self.t_m1 ,self.bias1_test, name='z1')
        self.t_a1 = tf.nn.sigmoid(self.t_z1,name='a1')
        self.appendVector(self.para[3], self.sample_size[1], 3)
        self.t_z2 = tf.add(tf.matmul(self.para[1], self.t_a1, name='matmul4'),self.bias2_test, name='z2')
        self.t_a2 = tf.transpose(tf.nn.softmax(tf.transpose(self.t_z2,[1,0]),name='a2'),[1,0])

    #損失函式
    def loss_op(self):
        self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.train_y, logits=self.a2))
        self.optimizer = tf.train.GradientDescentOptimizer(self.learning_rate)

    #反向傳播
    def backwords_op(self):
        self.train = self.optimizer.minimize(self.loss)

    #初始化所有全域性變數
    def init_op(self):
        self.init_op = tf.global_variables_initializer()

    #訓練
    def train1(self):
        with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333))) as sess:
            sess.run(self.init_op)
            for i in range(10):
                sess.run(self.train)
                self.para = [sess.run(self.weight1),sess.run(self.weight2),sess.run(self.bias1),sess.run(self.bias2),sess.run(self.loss)]
                print("==========step",i,"==========")
                print("weight1:\n",self.para[0],"\nb1:\n",self.para[2])
                print("\nweight2:\n",self.para[1],"\nb2:\n",self.para[3])
                print("\nloss=",self.para[4])

    #測試
    def test(self):
        self.test_towords()
        with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333))) as sess:
            sess.run(tf.global_variables_initializer())
            sess.run([self.bias1_test,self.bias2_test])
            #每個樣本的每個類標取值的概率
            predict_proba = sess.run(self.t_a2)        	
            #預測每個樣本的類標(0或1)
            predict_proba = np.transpose(predict_proba)
            print('\npredict_proba=',predict_proba)
            predict_value = np.argmax(predict_proba,axis=1)
            print('\npredic_value=',predict_value)
            #計算準確率:
            # accuracy = 0
            # # print(test_y[0][0])
            # for ei,i in enumerate(predict_value):
            #     if i == self.test_y[0][ei]:
            #         accuracy += 1
            # accuracy  /= sample_size
            # print('\naccuracy=',accuracy)   

  可以詳細閱讀程式,並嘗試在自己的PC上執行。若使用自己的資料集,可修改程式中的相應超引數(學習率、神經網路各層的神經網路個數、train1函式中迭代次數、引數初始化方式、最優化策略、損失函式等)。

部落格記錄著學習的腳步,分享著最新的技術,非常感謝您的閱讀,本部落格將不斷進行更新,希望能夠給您在技術上帶來幫助。