C++/Python机器学习—BP神经网络

 一、Python

 

import random
import numpy as np
import matplotlib.pyplot as plt

# 定义激活函数
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# 定义激活函数的导数
def sigmoid_derivative(x):
    sig = sigmoid(x)
    return sig * (1 - sig)

# 定义预测函数
def predict(x, w, b):
    return x * w + b

# 定义训练函数
def train(x, y, w, b, learning_rate, epochs):
    n = x.shape[0]
    for i in range(epochs):
        random_index = random.randint(0, n-1)
        input_val = x[random_index]
        target_val = y[random_index]

        # 前向传播
        output_val = predict(input_val, w, b)

        # 反向传播
        output_error = target_val - output_val
        output_delta = output_error

        # 更新权重和偏置
        w += learning_rate * output_delta * input_val
        b += learning_rate * output_delta

    return w, b

# 定义生成数据函数
def generate_data(true_w, true_b, num_examples, x_range):
    x = np.random.uniform(-x_range, x_range, size=num_examples)
    noise = np.random.normal(scale=0.1, size=num_examples)
    y = true_w * x + true_b + noise
    return x, y

# 定义真实的权重和偏置
true_w = 2
true_b = 4.2
x_range = 10

# 生成数据
num_examples = 1000
x, y = generate_data(true_w, true_b, num_examples, x_range)

# 初始化权重和偏置
w = 0
b = 0

# 设置学习率和迭代次数
learning_rate = 0.01
epochs = 1000

# 训练模型
w, b = train(x, y, w, b, learning_rate, epochs)

# 输出模型权重和偏置
print("Model weights: w={}, b={}".format(w, b))

# 预测新数据
x_pred = np.array([6, 7, 8, 9, 10])
y_pred = predict(x_pred, w, b)

# 输出预测结果
print("Input values: {}".format(x_pred))
print("Predicted values: {}".format(y_pred))

# 绘制数据和模型预测
plt.scatter(x, y, s=0.1)
plt.plot(x_pred, y_pred, color='red')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

二、C++

#include <iostream>
#include <vector>
#include <random>
#include <cmath>

using namespace std;

// 定义激活函数
double sigmoid(double x) {
    return 1 / (1 + exp(-x));
}

// 定义激活函数的导数
double sigmoid_derivative(double x) {
    double sig = sigmoid(x);
    return sig * (1 - sig);
}

// 定义预测函数
vector<double> predict(vector<double>& x, vector<double>& w, double b) {
    vector<double> y_pred;
    for (double input : x) {
        double output = input * w[0] + b; // 计算输出层输出
        y_pred.push_back(output); // 将输出添加到预测向量中
    }
    return y_pred;
}

// 定义训练函数
void train(vector<double>& x, vector<double>& y, vector<double>& w, double& b, double learning_rate, int epochs) {
    int n = x.size();
    default_random_engine generator;
    uniform_int_distribution<int> distribution(0, n - 1);

    for (int i = 0; i < epochs; i++) {
        int random_index = distribution(generator);
        double input = x[random_index];
        double target = y[random_index];

        // 前向传播
        double output = input * w[0] + b; // 计算输出层输出

        // 反向传播
        double output_error = target - output; // 计算输出层误差
        double output_delta = output_error; // 计算输出层误差项

        // 更新权重和偏置
        w[0] += learning_rate * output_delta * input;
        b += learning_rate * output_delta;
    }
}

// 定义生成数据函数
void generate_data(double w, double b, int num_examples, vector<double>& x, vector<double>& y, double x_range) {
    // w: 真实权重
    // b: 真实偏置
    // num_examples: 样本数量
    // x: 特征向量
    // y: 标签向量
    // x_range: 特征范围
    default_random_engine generator; // 定义随机数生成器
    uniform_real_distribution<double> distribution(-x_range, x_range); // 定义均匀分布
    for (int i = 0; i < num_examples; i++) { // 生成数据
        double feature = distribution(generator); // 生成特征
        x.push_back(feature);
        double dot_product = w * feature; // 计算点积
        y.push_back(dot_product + b + distribution(generator)); // 添加噪声
    }
}

int main() {
    // 定义真实的权重和偏置
    double true_w = 2;
    double true_b = 4.2;
    double x_range=2;
    // 生成数据
    vector<double> x;
    vector<double> y;
    int num_examples = 1000;
    generate_data(true_w, true_b, num_examples, x, y,x_range);

    // 初始化权重和偏置
    vector<double> w = {0};
    double b = 0;

    // 设置学习率和迭代次数
    double learning_rate = 0.01;
    int epochs = 1000;

    // 训练模型
    train(x, y, w, b, learning_rate, epochs);

    // 输出模型权重和偏置
    cout << "Model weights: w=" << w[0] << ", b=" << b << endl;
    
    // 预测新数据
    vector<double> x_pred = {6, 7, 8, 9, 10};
    vector<double> y_pred = predict(x_pred, w, b);

    // 输出预测结果
    cout << "Input values: ";
    for (double x_val : x_pred) {
        cout << x_val << " ";
    }
    cout << endl;

    cout << "Predicted values: ";
    for (double y_val : y_pred) {
        cout << y_val << " ";
    }
    cout << endl;

    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值