微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何创建多层神经网络

如何解决如何创建多层神经网络

感谢您为此提供的任何帮助!我得到了一个简单的单层感知器的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,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,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,Y)                # get predicted vector
        grads,cost = back_prop(A1,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) 

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

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?