一、网络结构
一个输入层,一个隐藏层,一个输出层,隐藏层和输出层的激活函数为sigmoid。
二、具体实现
class nnmodule():
def __init__(self,input_size=2,hidden_size=4,output_size=1,batch_size = 4):
self.W1 = np.random.randn(hidden_size,input_size)
self.W2 = np.random.randn(hidden_size, output_size)
self.b1 = np.random.randn(1,hidden_size).T
self.b2 = np.random.randn(output_size, 1)
self.batch_size = batch_size
self.input_size = input_size
self.hidden_size = hidden_size
def dataprocess(self,x,y):
data = np.concatenate((x,y), axis=1)
indices = np.random.choice(len(data), size=data.shape[0] %self.batch_size, replace=False)
random_values = data[indices]
data = np.concatenate((data, random_values))
return data
def split(self,data,rate=0.8):
np.random.shuffle(data)
data = data.reshape(-1,self.batch_size,self.input_size + 1)
n = data.shape[0]
n_samples = int(n * rate)
index = np.array(range(n))
indices = np.random.choice(n, size=n_samples, replace=False)
train_set = data[indices]
mask = np.in1d(index, indices)
test_index = index[~mask]
test_set = data[test_index]
return train_set,test_set
def sigmoid(self,x):
return 1 / (1 + np.exp(-x))
def p_sigmoid(self,x):
return self.sigmoid(x) * (1 - self.sigmoid(x))
def Backpropagation(self,x,y,learning_rate = 0.05):
h_r,y_p = self.forward(x)
for i,y_hat in enumerate(y_p):
h1 = h_r[i].reshape(1,self.hidden_size).T
dL_dh2 = y_hat - y[i]
dL_dh1 = dL_dh2 * self.W2
p_h1 = self.p_sigmoid(h1)
p_h2 = self.p_sigmoid(y_hat)
dL_db_1 = np.multiply(dL_dh1, p_h1)
dL_dw_1 = dL_db_1 * x[i]
dL_dw_2 = np.multiply(dL_dh1 * p_h2, h1)
dL_db2 = dL_dh2 * p_h2
self.W1 -= learning_rate * dL_dw_1
self.b1 -= learning_rate * dL_db_1
self.W2 -= learning_rate * dL_dw_2
self.b2 -= learning_rate * dL_db2
return self.W1,self.b1,self.W2,self.b2
def forward(self,x):
z1 = self.W1 @ x.T + self.b1
a1 = self.sigmoid(z1).T
z2 = a1 @ self.W2 + self.b2
y_hat = self.sigmoid(z2)
return a1,y_hat
def train(self,x,y,epoch=4,method='mse',learning_rate = 0.05,rate = 0.8):
data = self.dataprocess(x,y)
for i in tqdm(range(int(epoch))):
train,test = self.split(data,rate)
x_t = train[:,:,0:self.input_size]
y_t = train[:,:,self.input_size]
x_te = test[:,:,0:self.input_size]
y_te = test[:,:,self.input_size]
for j in range(len(x_t)):
self.Backpropagation(x_t[j],y_t[j],learning_rate = 0.05)
if method == 'mse':
print("mse:" + str(self.mse(x_te,y_te)))
else:
print("accuracy" + str(self.accuracy(x_te,y_te)))
def mse(self,x,y):
x = x.reshape(-1,self.input_size)
y = y.reshape(-1,1)
return (self.forward(x)[1].reshape(-1,) - y).sum()
def accuracy(self,x,y):
x = x.reshape(-1,self.input_size)
y = y.reshape(-1,1)
return (self.forward(x)[1].reshape(-1,) == y).sum()/y.shape[0]
def predict(self,x):
return self.forward(x)[1]
三、尝试调用
x1 = np.random.randn(1000)
x2 = np.random.randn(1000)
x3 = np.random.randn(1000)
y = np.random.randn(1000,1)
X = np.stack((x1, x2,x3), axis=1)
model = nnmodule(3,6,1,8)
model.train(X,y,1,learning_rate=0.1,rate=0.95)