1

4bit up counter 的实现。首先,我在没有使用偏差项的情况下实现了模型。该模型似乎工作正常,但在添加偏差项后,模型在初始阶段过度拟合,损失为零。即使对于看不见的数据,该模型也预测与训练数据相同的输出。下面是相同的实现。问题是什么...

import numpy as np
import matplotlib.pyplot as plt
#Batch training
#input & output
x = np.array([[0,0,1,0],[0,0,0,0],[0,0,0,1],[0,0,1,1],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1],[1,0,0,0],[1,0,0,1]]) # 10*4
y = np.array([[0,0,1,1],[0,0,0,1],[0,0,1,0],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1],[1,0,0,0],[1,0,0,1],[1,0,1,0]]) # 10*4

def sigmoid(x):
  return 1/(1+np.exp(-x))

def sigmoid_prime(x):
  return sigmoid(x)*(1-sigmoid(x))

Input_Size = 4
Output_Size = 4
Hidden_Layer_Neurons = 8
Learning_Rate = 0.01

weight1 = np.random.uniform( size = ( Input_Size, Hidden_Layer_Neurons ) ) # 4*8
weight2 = np.random.uniform( size = ( Hidden_Layer_Neurons, Output_Size ) ) # 8*4
loss = []
iteration = []
bias1 = np.random.uniform( size = ( x.shape[0], Hidden_Layer_Neurons ) )
bias2 = np.random.uniform( size = ( x.shape[0], Output_Size ) )

for i in range(30000):
  a1 = x #10*4
  z2 = np.dot( a1, weight1 ) + bias1 # 10*4 ** 4*8 = 10*8
  a2 = sigmoid(z2) # 10*8
  z3 = np.dot( a2, weight2 ) + bias2 # 10*8 ** 8*4 = 10*4
  val = 0
  err1 = 0
  if i > 100:
    for j in range(10):
      for k in range(4):
        val += (y[j][k]-z3[j][k])*(y[j][k]-z3[j][k])
    val = val/(2*10)
    loss.append(val);
    iteration.append(i) 
  del_out = ( z3 - y )  # 10*4 - 10*4 = 10*4
  weight2 -= Learning_Rate*np.dot( a2.T, del_out )#8*10 ** 10*4= 8*4
  bias2 -= Learning_Rate*del_out 
  err = np.dot(del_out, weight2.T)*sigmoid_prime(z2) #10*4 ** 4*8 = 10*8 * 10*8= 10*8
  weight1 -= Learning_Rate*np.dot( a1.T, err ) #4*10 ** 10*8 = 4*8
  bias1 -= Learning_Rate*err


print(z3) 
plt.plot( iteration, loss )
plt.show() 

def model():
    q = np.array([[1,0,1,0],[1,0,1,1],[1,1,0,0], [1,1,0,1], [1,1,1,0], [1,0,0,0],[1,1,1,1],[0,0,1,1],[0,0,0,1],[0,0,1,0]])
    z = np.dot(q, weight1) + bias1 
    act_hidden = sigmoid(z)  
    output = np.dot(act_hidden, weight2) + bias2
    print(output)

model()

为什么添加偏见会在这里产生问题,什么时候应该添加偏见?

4

0 回答 0