牛客题解 | 单神经元

题目

题目链接

单神经元(Single Neuron)是神经网络中的最常见的基本单元。

本题的步骤如下:
  1. 初始化权重和偏置
  2. 前向传播,计算预测值
    z = w ⋅ x + b z = w \cdot x + b z=wx+b
    p r e d i c t i o n s = s i g m o i d ( z ) predictions = sigmoid(z) predictions=sigmoid(z)
  3. 计算损失函数
    m s e = 1 n ∑ i = 1 n ( p r e d i c t i o n s i − l a b e l s i ) 2 mse = \frac{1}{n} \sum_{i=1}^{n} (predictions_i - labels_i)^2 mse=n1i=1n(predictionsilabelsi)2

标准代码如下

def single_neuron_model(features, labels, weights, bias):
    probabilities = []
    for feature_vector in features:
        z = sum(weight * feature for weight, feature in zip(weights, feature_vector)) + bias
        prob = 1 / (1 + math.exp(-z))
        probabilities.append(round(prob, 4))
    
    mse = sum((prob - label) ** 2 for prob, label in zip(probabilities, labels)) / len(labels)
    mse = round(mse, 4)
    
    return probabilities, mse
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值