python代码:
import numpy as np # 定义 sigmoid 激活函数 def sigmoid(x: np.ndarray) -> np.ndarray: return 1 / (1 + np.exp(-x)) # 定义神经元类 class Neuron: def __init__(self, weights: np.ndarray, bias: float): self.weights = weights self.bias = bias def feedforward(self, inputs: np.ndarray) -> np.ndarray: # 计算权重和输入的点积,并加上偏差 total = np.dot(self.weights, inputs) + self.bias return sigmoid(total) # 初始化权重和偏差 weights = np.array([0, 1]) # 例如两个输入权重 bias = 4 # 偏差值 # 创建神经元实例 n = Neuron(weights, bias) # 输入数据 x = np.array([2, 3]) # 输出结果 print(n.feedforward(x))