天天看點

theano學習搭建自己的神經網絡之類層

定義一個類層,然後套用

from __future__ import print_function
import theano
import theano.tensor as T
import numpy as np

#-----------------------------定義神經網絡層類,然後可以套用------------------------------#
class Layer(object):
    def __init__(self, inputs, in_size, out_size, activation_function=None):#類的構造函數
        self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))#權重為實時變的值,是以設定為shared類型,這裡設初始值為0到1之間的值,矩陣規格為insize-outsize
        self.b = theano.shared(np.zeros((out_size, )) + 0.1)#權重為實時變的值,是以設定為shared類型,偏置是一個向量,規格為outsize-1
        self.Wx_plus_b = T.dot(inputs, self.W) + self.b
        self.activation_function = activation_function
        if activation_function is None:
            self.outputs = self.Wx_plus_b
        else:
            self.outputs = self.activation_function(self.Wx_plus_b)

"""
to define the layer like this:
l1 = Layer(inputs, 1, 10, T.nnet.relu)
l2 = Layer(l1.outputs, 10, 1, None)
"""
           

邏輯回歸:

import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt

#---------------------------------定義類層-----------------------------------#
class Layer(object):
    def __init__(self, inputs, in_size, out_size, activation_function=None):
        self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))
        self.b = theano.shared(np.zeros((out_size,)) + 0.1)#注:不能是np.zeros((out_size,1))+1),因為偏置不用來乘的,是加到權重乘以輸入之後的值
        self.Wx_plus_b = T.dot(inputs, self.W) + self.b
        self.activation_function = activation_function
        if activation_function is None:
            self.outputs = self.Wx_plus_b
        else:
            self.outputs = self.activation_function(self.Wx_plus_b)

#---------------------------------僞造資料-------------------------------------#
# Make up some fake data
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]#[:,np.newaxis]做成矩陣
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise        # y = x^2 - 0.5 + wihtenoise
print(np.linspace(-1,1,300))
print(x_data)
print(np.linspace(-1,1,300).shape)
print(x_data.shape)

#-----------------------------------畫出資料--------------------------------------#
# show the fake data
plt.scatter(x_data, y_data)
plt.show()

#------------------------------------搭建網絡---------------------------------------#
x = T.dmatrix('x')
y = T.dmatrix('y')
#定義層
l1 = Layer(x,1,10,T.nnet.relu)
l2 = Layer(l1.outputs,10,1,None)
#計算代價
cost = T.mean(T.square(l2.outputs - y))
#計算梯度
gW1,gb1,gW2,gb2 = T.grad(cost,[l1.W,l1.b,l2.W,l2.b])
#訓練過程
learning_rate = 0.05
train = theano.function(
    inputs=[x, y],
    updates=[(l1.W, l1.W - learning_rate * gW1),
             (l1.b, l1.b - learning_rate * gb1),
             (l2.W, l2.W - learning_rate * gW2),
             (l2.b, l2.b - learning_rate * gb2)],
    outputs=cost)
#預測值
#predict = theano.function(inputs=[x],outputs=l2.outputs)
#訓練
for i in range(1000):
    err = train(x_data,y_data)
    if i%50 == 0:
        print(err)
           

結果:

theano學習搭建自己的神經網絡之類層

繼續閱讀