Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的代码构建深层神经网络代码正确吗?_Python_Neural Network - Fatal编程技术网

Python 我的代码构建深层神经网络代码正确吗?

Python 我的代码构建深层神经网络代码正确吗?,python,neural-network,Python,Neural Network,我自己用python构建了这个神经网络。但无论出于什么原因,当我在数据上测试它时,输出似乎不正确。此时,我不确定数据或设置方式是否有问题,或者代码是否有问题。然而,我花了两天时间编辑代码,它看起来很好。我希望有人能看看并帮助我。非常感谢 X = X_train Y = Y_train L_dims = [X.shape[0],4,5,10,1] # note index[0] is the input layer L = len(L_dims) m_sample

我自己用python构建了这个神经网络。但无论出于什么原因,当我在数据上测试它时,输出似乎不正确。此时,我不确定数据或设置方式是否有问题,或者代码是否有问题。然而,我花了两天时间编辑代码,它看起来很好。我希望有人能看看并帮助我。非常感谢

X = X_train
Y = Y_train

L_dims = [X.shape[0],4,5,10,1]  # note index[0] is the input layer
L = len(L_dims)              
m_sample = X.shape[1] 


def relu_derivative(x):
  x[x<0] = 0
  return x

def compute_cost(yhat,print_cost = 0): #Checked - ok
  cost = (-1/m_sample)*np.sum(np.multiply(np.log(yhat),Y)+np.multiply(np.log(1-yhat),(1-Y)))
  cost = float(np.squeeze(cost))
  return cost
  

def initialize_wb():
  parameter = {}
  for i in range(1,L):
    parameter['w'+ str(i)]  = np.random.randn(L_dims[i],L_dims[i-1])*0.01
    parameter['b'+ str(i)] = np.zeros((L_dims[i],1))
  return parameter

def forward_prop(parameter):
  fwd_cache ={}
  A = X
  fwd_cache['A0'] = A
  #hidden layers fwd prop
  for i in range(1,L-1):
    z = np.dot(parameter['w'+ str(i)],A) + parameter['b'+ str(i)]
    A = np.maximum(0,z) 
    fwd_cache['z'+ str(i)] = z
    fwd_cache['A'+ str(i)] = A
  

  #output layer fwd prop
  zL = np.dot(parameter['w'+str(L-1)],fwd_cache['A'+str(L-2)])+parameter['b'+str(L-1)]
  AL = 1/(1+np.exp(-zL))
  fwd_cache['z'+ str(L-1)] = zL
  fwd_cache['A'+ str(L-1)] = AL
  yhat = AL
  return yhat, fwd_cache

def backward_prop(yhat,fwd_cache,parameter):  #need to make the derivatives of sigmoid interchangeable
  #Output Layer
  dAL = - (np.divide(Y, yhat) - np.divide(1 - Y, 1 - yhat)) 
  s = 1/(1+np.exp(-fwd_cache['z'+str(L-1)])) 
  
  dzL = dAL*s*(1-s) # gz = yhat*(1-yhat) 
  dwL = (1./m_sample)*np.dot(dzL,fwd_cache['A'+str(L-2)].T)
  dbL = (1./m_sample)*np.sum(dzL,axis=1, keepdims=True)
  
  gradiant = {}
  dz_cache = {}

  dz_cache['dz'+str(L-1)] = dzL
  gradiant['db'+str(L-1)] = dbL
  gradiant['dw'+str(L-1)] = dwL
  
  da = np.dot(parameter['w'+str(L-1)].T,dzL)

  #Hidden Layer
  for i in reversed(range(1,L-1)):  
    gz = relu_derivative(fwd_cache['z'+str(i)])
    dz = da*gz
    dw = (1./m_sample)*np.dot(dz,fwd_cache['A'+str(i-1)].T)
    db = (1./m_sample)*np.sum(dz,axis=1, keepdims=True)/m_sample
    da = np.dot(parameter['w'+str(i)].T,dz)

    dz_cache['dz'+str(i)] = dz
    gradiant['dw'+str(i)] = dw
    gradiant['db'+str(i)] = db
  
  return gradiant

def update_parameter(gradiant,parameter, learning_rate):
  for i in range(1,L):
    parameter['w'+str(i)] -= learning_rate*gradiant['dw'+str(i)]
    parameter['b'+str(i)] -= learning_rate*gradiant['db'+str(i)]
  return parameter


def gradiant_desc(parameter,learning_rate, iteration,print_n):

  for i in range(iteration):
    yhat, fwd_cache = forward_prop(parameter) 
    gradiant = backward_prop(yhat,fwd_cache,parameter)
    cost = compute_cost(yhat)
    parameter = update_parameter(gradiant,parameter, learning_rate)
    
    if i % print_n == 0:
      print("Cost after %i iteration %f" %(i,cost))
  
  return parameter

def predict(X,Y, learned_parameter):
    A = X
    m = A.shape[1]
    for i in range(1,L-1):
      z = np.dot(learned_parameter['w'+ str(i)],A) + learned_parameter['b'+ str(i)]
      A = np.maximum(0,z)
    zL = np.dot(learned_parameter['w'+str(L-1)],A)+learned_parameter['b'+str(L-1)]
    AL = 1/(1+np.exp(-zL))
    p = AL>0.5
    ans = float(1- np.sum(abs(p - Y))/m)
    result = print('Accuracy:', str(round(ans, 5)*100)+str('%'))
    return result

parameter = initialize_wb()   
learned_parameter = gradiant_desc(parameter,0.05, 100, 10)
predict(X_test,Y_test, learned_parameter)
X=X\u列车
Y=Y_列车
L_dims=[X.shape[0],4,5,10,1]#注意索引[0]是输入层
L=长度(L_dims)
m_样本=X.shape[1]
def relu_导数(x):
x[x0.5
ans=浮动(1-np.和(绝对值(p-Y))/m)
结果=打印('准确度:',str(圆形(ans,5)*100)+str('%'))
返回结果
参数=初始化_wb()
学习的参数=梯度描述(参数,0.05100,10)
预测(X_检验、Y_检验、学习的_参数)

欢迎使用SO。请发布实际输出和预期输出以及随附的必要样本数据。