![](/img/trans.png)
[英]Why is a simple Binary classification failing in a feedforward neural network?
[英]binary subtraction with a simple neural network
我正在嘗試創建一個簡單的ANN,該ANN接受輸入x1
和x2
,計算每個輸入中的位數(ON),然后返回x1 - x2
。 當我使用一個示例時,網絡似乎可以正確訓練。 但是,當我開始添加其他示例時,結果很糟糕。 我試過調整正則化強度,不。 迭代和學習率,但似乎沒有幫助。 我的理解是,隱層神經元的最佳數量是len(x1) + len(x2)
,這是我已經實現的。 任何幫助都會很棒。
import os
import fnmatch
import numpy as np
class Neuron:
def __init__(self,n):
#create weights (+1 is for bias)
self.weights = np.random.randn(n+1) / np.sqrt(n)
self.lr = 1e-2
self.reg = 0.01
def linear(self,x):
#apply linear step
x = np.append(x,1)
return np.dot(self.weights,x)
def output(self,x):
#apply sigmoid step
preact = self.linear(x)
self.out = 1/(1+np.exp(-preact))
return self.out
def apply_gradients(self):
#update weights
self.weights -= self.gradients * self.lr
def calculate_gradients(self,loss):
#calculate gradients
self.gradients = loss * self.out * (1-self.out)
return self.gradients
def _reg(self):
return (self.reg*np.sum((self.weights)))
def main(argv=None):
num_epochs = 1000
#inputs
x1 = [[1,1,1],[1,1,0]]
x2 = [[1,1,0],[1,1,0]]
x = np.append(x1,x2,axis=1)
#labels: binary substraction x1-x2
y = np.array([[0,0,1],[0,0,0]])
_,n = y.shape
hln = 2*n
hidden_out = np.array(np.zeros(hln))
y_ = np.array(np.zeros(n))
reg_value = np.array(np.zeros(n))
loss = np.array(np.zeros(n))
#initialise the hidden layer neurons
hidden_layer = []
for i in range(hln):
hidden_layer.append(Neuron(2*n))
#initialise the output layer neurons
out_layer = []
for i in range(n):
out_layer.append(Neuron(hln))
rows,cols = x.shape
#run through the epochs
for epoch in range(num_epochs):
#run through the samples
for data in range(rows):
#pass the data through the hidden layer
for i in range(len(hidden_layer)):
hidden_out[i]=hidden_layer[i].output(x[data,:])
#pass the hidden layer output through the output layer
for i in range(len(out_layer)):
y_[i] = out_layer[i].output(hidden_out)
#get the reg value
reg_value[i] = out_layer[i]._reg()
#calculate L1 loss
loss = y_ - y[data,:] + reg_value
#calculate gradients of output layer
for i in range(len(out_layer)):
out_layer[i].calculate_gradients(loss[i])
#calculate gradients of hidden layer
for i in range(len(hidden_layer)):
back_vec = 0
for j in range(len(out_layer)):
#sum all the output weights coming back into a hidden neuron
back_vec += out_layer[j].weights[i]*loss[j]
#send the backwards value through the hidden neuron
hidden_layer[i].calculate_gradients(back_vec)
#apply gradient to output layer
for i in range(len(out_layer)):
out_layer[i].apply_gradients()
#apply gradients to hidden layer
for i in range(len(hidden_layer)):
hidden_layer[i].apply_gradients()
#output the final results
if epoch == num_epochs-1:
print((y_))
if __name__ == "__main__":
main()
您需要訓練集來訓練ML模型(在這種情況下為神經網絡)。 僅從兩個訓練示例中得出一個有點復雜的操作(二進制差異)並不是一件容易的事。
我的建議是在開始構建網絡之前創建完整的培訓集。 由於您正在使用定長(3)二進制數組,因此可以輕松創建所有可能性(8 * 8 = 64個訓練樣本),將一小部分留給驗證並與其他人一起訓練。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.