题目
单神经元(Single Neuron)是神经网络中的最常见的基本单元。
本题的步骤如下:
- 初始化权重和偏置
- 前向传播,计算预测值
z = w ⋅ x + b z = w \cdot x + b z=w⋅x+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) - 计算损失函数
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=1∑n(predictionsi−labelsi)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

被折叠的 条评论
为什么被折叠?



