• PythonAI
  • 在Mojo中编写这个简单的神经网络

@rishiraj
这是我使用纯 Numpy 用 Python 编写的简单神经网络。它的 Mojo 等价物是什么?

%%python
import numpy as np

class NeuralNetwork:
    def __init__(self, input_size, hidden_size, output_size):
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        # Initialize weights and biases
        self.W1 = np.random.randn(self.input_size, self.hidden_size)
        self.b1 = np.zeros((1, self.hidden_size))
        self.W2 = np.random.randn(self.hidden_size, self.output_size)
        self.b2 = np.zeros((1, self.output_size))

    def forward(self, X):
        # Perform forward propagation
        self.z1 = np.dot(X, self.W1) + self.b1
        self.a1 = np.tanh(self.z1)
        self.z2 = np.dot(self.a1, self.W2) + self.b2
        self.a2 = self.sigmoid(self.z2)
        return self.a2

    def backward(self, X, y, learning_rate):
        m = X.shape[0]

        # Compute gradients
        dZ2 = self.a2 - y
        dW2 = (1 / m) * np.dot(self.a1.T, dZ2)
        db2 = (1 / m) * np.sum(dZ2, axis=0)
        dZ1 = np.dot(dZ2, self.W2.T) * (1 - np.power(self.a1, 2))
        dW1 = (1 / m) * np.dot(X.T, dZ1)
        db1 = (1 / m) * np.sum(dZ1, axis=0)

        # Update weights and biases
        self.W2 -= learning_rate * dW2
        self.b2 -= learning_rate * db2
        self.W1 -= learning_rate * dW1
        self.b1 -= learning_rate * db1

    def train(self, X, y, num_epochs, learning_rate):
        for epoch in range(num_epochs):
            # Forward propagation
            output = self.forward(X)

            # Backward propagation
            self.backward(X, y, learning_rate)

            # Print the loss every 100 epochs
            if epoch % 100 == 0:
                loss = self.compute_loss(y, output)
                print(f"Epoch {epoch}, Loss: {loss}")

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

    def compute_loss(self, y, output):
        return np.mean(-y * np.log(output) - (1 - y) * np.log(1 - output))

@mahmoudajawad
这似乎是一个有趣的挑战,看看我能用 mojo 走多远,到目前为止,我能够达到使用的第一线,但我没有看到如何将对象转换为 mojo:numpynumpy

from PythonInterface import Python

struct NeuralNetwork:
    var input_size: Int
    var hidden_size: Int
    var output_size: Int
    
    var W1: FloatLiteral
    var W2: FloatLiteral
    var b1: FloatLiteral
    var b2: FloatLiteral
    
    fn __init__(inout self, input_size: Int, hidden_size: Int, output_size: Int) raises:
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        # Initialize weights and biases
        let np = Python.import_module("numpy")
        self.W1 = np.random.randn(self.input_size, self.hidden_size)

# Rest lines are not yet changed beyond setting function types

我有几个问题要问 mojo 团队:

在每个函数中重新导入是访问库的唯一方法吗?numpy
如何将 return of 转换为 mojo 可以理解的东西?numpy.random.randn

admin 添加 AI 标签