题目

题目链接

单神经元(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
Logo

脑启社区是一个专注类脑智能领域的开发者社区。欢迎加入社区,共建类脑智能生态。社区为开发者提供了丰富的开源类脑工具软件、类脑算法模型及数据集、类脑知识库、类脑技术培训课程以及类脑应用案例等资源。

更多推荐