Math Problem Statement

import numpy as np import matplotlib.pyplot as plt

x、y坐标

X = np.arange(-1.0, 1.0, 0.1) # 元素数量是20个 Y = np.arange(-1.0, 1.0, 0.1)

权重

w_im = np.array([[1.0,2.0], [2.0,3.0]]) # 中间层 2x2的矩阵 w_mo = np.array([[-1.0,1.0], [1.0,-1.0]]) # 输出层 2x2的矩阵

偏置

b_im = np.array([0.3,-0.3]) # 中间层 b_mo = np.array([0.4,0.1]) # 输出层

中间层

def middle_layer(x, w, b): u = np.dot(x, w) + b return 1/(1+np.exp(-u)) # sigmoid関数

输出层

def output_layer(x, w, b): u = np.dot(x, w) + b return np.exp(u)/np.sum(np.exp(u)) # SoftMax函数

保存分类结果的列表

x_1 = [] y_1 = [] x_2 = [] y_2 = []

在网格的各个方格中进行神经网络的运算

for i in range(20): for j in range(20):

正向传播

inp = np.array([X[i], Y[j]]) mid = middle_layer(inp, w_im, b_im) out = output_layer(mid, w_mo, b_mo)

对概率的大小进行比较并分类

if out[0] > out[1]: x_1.append(X[i]) y_1.append(Y[j]) else: x_2.append(X[i]) y_2.append(Y[j])

分布图的显示

plt.scatter(x_1, y_1, marker="+") plt.scatter(x_2, y_2, marker="o") plt.showimport numpy as np import matplotlib.pyplot as plt X = np.arange(-1.0, 1.0, 0.1) Y = np.arange(-1.0, 1.0, 0.1) w_im = np.array([[1.0,2.0], [2.0,3.0]]) w_mo = np.array([[-1.0,1.0], [1.0,-1.0]]) b_im = np.array([0.3,-0.3]) b_mo = np.array([0.4,0.1]) def middle_layer(x, w, b): u = np.dot(x, w) + b return 1/(1+np.exp(-u)) def output_layer(x, w, b): u = np.dot(x, w) + b return np.exp(u)/np.sum(np.exp(u)) x_1 = [] y_1 = [] x_2 = [] y_2 = [] for i in range(20): for j in range(20): inp = np.array([X[i], Y[j]]) mid = middle_layer(inp, w_im, b_im) out = output_layer(mid, w_mo, b_mo) if out[0] > out[1]: x_1.append(X[i]) y_1.append(Y[j]) else: x_2.append(X[i]) y_2.append(Y[j]) plt.scatter(x_1, y_1, marker="+") plt.scatter(x_2, y_2, marker="o") plt.show() () 请改写上述代码实现正向传播以下要求 要求: 1、可以控制隐藏层的数目,每个隐藏层的神经元的数目等; 2、要有可视化结果 3、程序截图,结果也要截图 4、程序里面要有个人的姓名和学号。

Solution

下面是改写后的代码,满足以下要求:

  1. 支持隐藏层的数量和神经元数量的控制
  2. 增加了可视化结果功能
  3. 包含截图注释的地方可用来展示姓名和学号
python
import numpy as np import matplotlib.pyplot as plt # 参数设置 name = "姓名: 张三" # 请替换为真实姓名 student_id = "学号: 12345678" # 请替换为真实学号 # 定义网格点 X = np.arange(-1.0, 1.0, 0.1) Y = np.arange(-1.0, 1.0, 0.1) # 定义神经网络参数 hidden_layer_sizes = [2, 3] # 每个隐藏层的神经元数目,支持多层 weights = [] biases = [] # 随机初始化权重和偏置 input_dim = 2 # 输入层维度 layer_dims = [input_dim] + hidden_layer_sizes + [2] # 加上输出层的维度 for i in range(len(layer_dims) - 1): weights.append(np.random.rand(layer_dims[i], layer_dims[i + 1]) * 2 - 1) biases.append(np.random.rand(layer_dims[i + 1]) * 2 - 1) # 定义激活函数 def sigmoid(x): return 1 / (1 + np.exp(-x)) def softmax(x): exp_x = np.exp(x - np.max(x)) # 稳定SoftMax return exp_x / np.sum(exp_x) # 定义前向传播 def forward_propagation(x, weights, biases): activations = x for w, b in zip(weights[:-1], biases[:-1]): activations = sigmoid(np.dot(activations, w) + b) output = softmax(np.dot(activations, weights[-1]) + biases[-1]) return output # 分类点的存储 x_1, y_1 = [], [] x_2, y_2 = [], [] # 计算每个网格点 for i in range(len(X)): for j in range(len(Y)): inp = np.array([X[i], Y[j]]) out = forward_propagation(inp, weights, biases) if out[0] > out[1]: x_1.append(X[i]) y_1.append(Y[j]) else: x_2.append(X[i]) y_2.append(Y[j]) # 结果可视化 plt.figure(figsize=(8, 8)) plt.scatter(x_1, y_1, marker="+", label="Class 1") plt.scatter(x_2, y_2, marker="o", label="Class 2") plt.title(f"Neural Network Classification\n{name}, {student_id}") plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.legend() plt.grid() plt.show()

使用说明:

  1. 隐藏层控制:更改 hidden_layer_sizes 的值可以控制隐藏层数量和每层的神经元数目。例如 [3, 4, 2] 表示三层隐藏层,分别有 3、4、2 个神经元。
  2. 权重初始化:代码会根据 hidden_layer_sizes 的值自动生成对应的权重和偏置矩阵。
  3. 可视化图形:运行后会生成图表,展示两类点的分布情况。

截图展示

请运行程序,生成图像并截图标记包含您的姓名和学号。


提问引导

  1. 如何调整隐藏层神经元数量对结果的影响?
  2. 为什么选择 Sigmoid 和 SoftMax 作为激活函数?
  3. 权重和偏置的随机初始化如何影响结果?
  4. 如何验证该模型的分类准确性?
  5. 如何扩展支持更多分类的输出?

小提示:在机器学习模型中,初始权重的设置可能对优化和收敛速度有很大影响,可以尝试正态分布或 Xavier 初始化方法。

Ask a new question for Free

By Image

Drop file here or Click Here to upload

Math Problem Analysis

Mathematical Concepts

Neural Networks
Activation Functions
Matrix Operations
Classification Visualization

Formulas

Sigmoid function: f(x) = 1 / (1 + exp(-x))
Softmax function: softmax(x) = exp(x_i) / sum(exp(x))
Forward propagation: u = np.dot(inputs, weights) + biases

Theorems

Universal Approximation Theorem (related to neural networks)

Suitable Grade Level

University Level (Computer Science/Mathematics/Engineering)