【Deep-ML系列】Single Neuron(手写基于sigmoid的单神经元)

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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值