import numpy as np

定义sigmoid函数

def sigmoid(x): return 1 / (1 + np.exp(-x))

定义MLP神经网络

class MLP: def init(self, input_size, hidden_size, output_size): self.W1 = np.random.randn(input_size, hidden_size) self.b1 = np.random.randn(hidden_size) self.W2 = np.random.randn(hidden_size, output_size) self.b2 = np.random.randn(output_size)

def forward(self, X):
    # 第一层
    self.z1 = np.dot(X, self.W1) + self.b1
    self.a1 = sigmoid(self.z1)

    # 第二层
    self.z2 = np.dot(self.a1, self.W2) + self.b2
    self.a2 = sigmoid(self.z2)

    return self.a2

测试代码

X = np.random.randn(7) mlp = MLP(7, 10, 2) y = mlp.forward(X) print(y)

写一段python来实现MLP神经网络7个输入2个输出

原文地址: https://www.cveoy.top/t/topic/bBz4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录