import math
import numpy as np
def single_neuron_model(features: list[list[float]], labels: list[int], weights: list[float], bias: float) -> (list[float], float):
features = np.array(features)
weights = np.array(weights)
labels = np.array(labels)
output = [round(np.dot(_, weights) + bias, 4) for _ in features]
probabilities = [round(1 / (1 + math.exp(-_)), 4) for _ in output]
probabilities = np.array(probabilities) # 将列表转换为NumPy数组以便操作
mse = sum((probabilities - labels) ** 2) / len(labels)
return probabilities.tolist(), round(mse, 4) # 返回前转换为列表
if __name__ == '__main__':
features = [[0.5, 1.0], [-1.5, -2.0], [2.0, 1.5]]
labels = [0, 1, 0]
weights = [0.7, -0.4]
bias = -0.1
print(single_neuron_model(features, labels, weights, bias))
【Deep-ML系列】Single Neuron(手写基于sigmoid的单神经元)
最新推荐文章于 2024-10-01 20:17:55 发布