单神经元(Single Neuron)是神经网络中的最常见的基本单元。
本题的步骤如下:
- 初始化权重和偏置
- 前向传播,计算预测值
- 计算损失函数
标准代码如下
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