题解|具有反向传播的单神经元

具有反向传播的单神经元

https://www.nowcoder.com/practice/aa0368be0d184d5ba03f6c8815aafc8f?tpId=377&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj

具有反向传播的单神经元(Single Neuron with Backpropagation)是神经网络中的最常见的基本单元。 本算法的步骤如下:

  1. 初始化权重和偏置
  2. 前向传播,计算预测值
  3. 反向传播,计算梯度
    本题选择的均方误差作为损失函数,公式为
    因此,梯度计算公式为
  4. 更新权重和偏置
  5. 重复步骤2-4,直到达到最大迭代次数

标准代码如下

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

def train_neuron(features, labels, initial_weights, initial_bias, learning_rate, epochs):
    weights = np.array(initial_weights)
    bias = initial_bias
    features = np.array(features)
    labels = np.array(labels)
    mse_values = []

    for _ in range(epochs):
        z = np.dot(features, weights) + bias
        predictions = sigmoid(z)
        
        mse = np.mean((predictions - labels) ** 2)
        mse_values.append(round(mse, 4))

        # Gradient calculation for weights and bias
        errors = predictions - labels
        weight_gradients = (2/len(labels)) * np.dot(features.T, errors * predictions * (1 - predictions))
        bias_gradient = (2/len(labels)) * np.sum(errors * predictions * (1 - predictions))
        
        # Update weights and bias
        weights -= learning_rate * weight_gradients
        bias -= learning_rate * bias_gradient

        # Round weights and bias for output
        updated_weights = np.round(weights, 4)
        updated_bias = round(bias, 4)

    return updated_weights.tolist(), updated_bias, mse_values
全部评论

相关推荐

站队站对牛:还是浙江学校欢迎
投递海康威视等公司10个岗位
点赞 评论 收藏
分享
09-20 22:39
中南大学
故事和酒66:意思就是用了AI辅助也不一定做得出来,还是有区分度,不然他不会让你用的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务