如何创建多层神经网络

时间:2020-08-26 03:09:48

标签: python neural-network perceptron

感谢您为此提供的任何帮助!我得到了一个简单的单层感知器的python代码,其任务是更改代码,因此它是一个多层感知器。我对这一切仍然很陌生,但是据我了解,重复的前馈和反向传播周期是创建隐藏层的原因。给定以下代码,应进行哪些更改以帮助创建这些隐藏层?

# Creating a numerically stable logistic s-shaped definition to call
def sigmoid(x):
    x = np.clip(x, -500, 500)
    if x.any()>=0:
        return 1/(1 + np.exp(-x))
    else:
        return np.exp(x)/(1 + np.exp(x))

# define the dimentions and set the weights to random numbers
def init_parameters(dim1, dim2=1,std=1e-1, random = True):
    if(random):
        return(np.random.random([dim1,dim2])*std)
    else:
        return(np.zeros([dim1,dim2]))
    
# Single layer network: Forward Prop
# Passed in the weight vectors, bias vector, the input vector and the Y
def fwd_prop(W1, bias, X, Y):

    Z1 = np.dot(W1, X) + bias # dot product of the weights and X + bias
    A1 = sigmoid(Z1)  # Uses sigmoid to create a predicted vector

    return(A1)    

#Single layer network: Backprop
def back_prop(A1, W1, bias, X, Y):

    m = np.shape(X)[1] # used the calculate the cost by the number of inputs -1/m
   
    # Cross entropy loss function
    cost = (-1/m)*np.sum(Y*np.log(A1) + (1-Y)*np.log(1-A1)) # cost of error
    dZ1 = A1 - Y                                            # subtract actual from pred weights
    dW1 = (1/m) * np.dot(dZ1, X.T)                          # calc new weight vector
    dBias = (1/m) * np.sum(dZ1, axis = 1, keepdims = True)  # calc new bias vector
    
    grads ={"dW1": dW1, "dB1":dBias} # Weight and bias vectors after backprop
    
    return(grads, cost)

def run_grad_desc(num_epochs, learning_rate, X, Y, n_1):
    
    n_0, m = np.shape(X)
    
    W1 = init_parameters(n_1, n_0, True)
    B1 = init_parameters(n_1,1, True)
    
    loss_array = np.ones([num_epochs])*np.nan # resets the loss_array to NaNs
    
    for i in np.arange(num_epochs):
        A1 = fwd_prop(W1, B1, X, Y)                # get predicted vector
        grads, cost = back_prop(A1, W1, B1, X, Y)    # get gradient and the cost from BP 
        
        W1 = W1 - learning_rate*grads["dW1"]    # update weight vector LR*gradient*[BP weights]
        B1 = B1 - learning_rate*grads["dB1"]    # update bias LR*gradient[BP bias]
        
        loss_array[i] = cost                    # loss array gets cross ent values
        
        parameter = {"W1":W1, "B1":B1}           # assign 
    
    return(parameter, loss_array) 

我们还被要求能够调整隐藏层中的节点。老实说,我在这里完全迷失了方向,甚至不清楚节点所代表的含义,因此也将不胜感激。谢谢大家。

1 个答案:

答案 0 :(得分:0)

看来该网络甚至不是单层的。通常,“单层”是指一层隐藏的神经元。该网络输出通常是隐藏层的激活。

我对您的建议是开始学习神经网络的基础知识。有很多很棒的资源,包括在YouTube上。对于反向传播,一个不错的起点是here

还请注意,如果您急于使用Tensorflow或Pytorch之类的自动分级工具,则​​可以帮助您实现差异化。当然,如果您这样做是为了学习神经网络的细节,那么从头开始构建神经网络会更好。

相关问题