【人工智能导论】实验代码合集

        本帖子主要记录实验的代码,方便日后需要的时候对这些基础架构的手写代码进行复习,虽然代码多数来源于网络和GPT生成,但是在实际做实验的时候也做过很多修改,相对比较熟悉,因此做一份备份

BP神经网络

一个隐藏层,选用sigmoid函数,输出层使用softmax函数,损失函数使用交叉熵

import numpy as np
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split


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


def sigmoid_derivative(x):
    return sigmoid(x) * (1 - sigmoid(x))


def cross_entropy_loss(y_true, y_pred):
    epsilon = 1e-12
    n = len(y_true)
    loss = - np.sum(y_true * np.log(y_pred) + epsilon)
    return loss / n


def softmax(x):
    exp_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
    return exp_x / np.sum(exp_x, axis=-1, keepdims=True)


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

        self.weights_input_hidden = np.random.randn(input_size, hidden_size)
        self.weights_hidden_output = np.random.randn(hidden_size, output_size)

        self.bias_hidden = np.zeros(hidden_size)
        self.bias_output = np.zeros(output_size)

    def forward(self, X):
        self.hidden_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
        self.hidden_output = sigmoid(self.hidden_input)
        self.output = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
        self.output = softmax(self.output)
        return self.output

    def backward(self, X, y, learning_rate):
        error = self.output - y
        n = X.shape[0]
        # 更新输出层的权重和偏置
        self.weights_hidden_output -= learning_rate * np.dot(self.hidden_output.T, error) / n
        self.bias_output -= learning_rate * np.sum(error, axis=0) / n

        # 计算隐藏层的误差
        error_hidden = np.dot(error, self.weights_hidden_output.T)
        delta_hidden = error_hidden * sigmoid_derivative(self.hidden_input)

        # 更新隐藏层的权重和偏置
        self.weights_input_hidden -= learning_rate * np.dot(X.T, delta_hidden) / n
        self.bias_hidden -= learning_rate * np.sum(delta_hidden, axis=0) / n

    def train(self, X, y, epochs, learning_rate):
        y_one_hot = np.eye(self.output_size)[y.astype(int) - 1]
        losses = []
        for epoch in range(epochs):
            output = self.forward(X)
            self.backward(X, y_one_hot, learning_rate)
            loss = cross_entropy_loss(y_one_hot, output)
            losses.append(loss)
            if epoch % 100 == 0:
                print(f'Epoch {epoch:4}, Loss: {loss}')

        plt.plot(losses)
        plt.xlabel('Epoch')
        plt.ylabel('Loss')
        plt.title('Loss over epochs')
        plt.show()

    def predict(self, X):
        output = self.forward(X)
        return np.argmax(output, axis=1) + 1


def read_data(file_name):
    with open(file_name, 'r') as file:
        data = []
        label = {'Iris-setosa': 1, 'Iris-versicolor': 2, 'Iris-virginica': 3}
        for
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值