Files
task-3-3-2-MLP/kk.py
2026-05-21 16:17:21 +08:00

137 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ====================== 1.1 函数的定义与直观理解 ======================
print("===== 1.1 函数的定义与直观理解 =====")
# 1. 单变量函数PPT里的例子
def f1(x):
"""y = 2x + 1"""
return 2 * x + 1
def f2(x):
"""y = x²"""
return x ** 2
def f3(x):
"""y = sin(x)"""
import math
return math.sin(x)
# 测试单变量函数
x_test = 3
print(f"输入x={x_test}")
print(f"y = 2x + 1 → 输出:{f1(x_test)}")
print(f"y = x² → 输出:{f2(x_test)}")
print(f"y = sin(x) → 输出:{f3(x_test):.4f}\n")
# 2. 多元函数PPT里的例子
def multi_func(x, y):
"""z = x + 2y"""
return x + 2 * y
# 测试多元函数
x = 3
y = 5
z = multi_func(x, y)
print(f"===== 多元函数示例 =====")
print(f"输入x={x}, y={y}")
print(f"z = x + 2y → 输出:{z}\n")
# 模拟神经网络的多输入多输出比如MNIST分类的简化版
def nn_multi_output(x_list):
"""
简化版多输出函数模拟MNIST 784输入→10输出
x_list: 长度为784的输入列表
return: 长度为10的输出列表每个数字的概率这里用随机数模拟
"""
import random
# 这里用简单的线性变换模拟,实际是复杂的神经网络
outputs = []
for i in range(10):
# 简化:用输入的和加一个随机偏移模拟输出
out = sum(x_list) * 0.01 + random.uniform(-0.5, 0.5)
outputs.append(out)
return outputs
# 测试多输入多输出用随机生成的784个输入
import random
input_data = [random.uniform(0, 1) for _ in range(784)]
nn_output = nn_multi_output(input_data)
print(f"===== 神经网络多输入多输出模拟 =====")
print(f"输入维度:{len(input_data)}")
print(f"输出维度:{len(nn_output)}")
print(f"前3个输出值{nn_output[:3]}\n")
# ====================== 1.2 导数(变化的度量) ======================
print("===== 1.2 导数(变化的度量) =====")
def numerical_derivative(f, x, h=1e-5):
"""
用定义式计算函数f在x处的导数
f'(x) = lim(h→0) [f(x+h) - f(x)] / h
"""
return (f(x + h) - f(x)) / h
# 测试:计算几个函数的导数
import math
# 1. 常数函数 f(x) = 5导数应为0
def const_func(x):
return 5
# 2. 线性函数 f(x) = 2x + 1导数应为2
def linear_func(x):
return 2 * x + 1
# 3. 平方函数 f(x) = x²导数应为2x
def square_func(x):
return x ** 2
# 4. sin函数 f(x) = sin(x)导数应为cos(x)
def sin_func(x):
return math.sin(x)
x0 = 1 # 测试点
print(f"在x={x0}处的数值导数:")
print(f"常数函数f(x)=5 → {numerical_derivative(const_func, x0):.4f}")
print(f"线性函数f(x)=2x+1 → {numerical_derivative(linear_func, x0):.4f}")
print(f"平方函数f(x)=x² → {numerical_derivative(square_func, x0):.4f}(理论值:{2*x0}")
print(f"sin函数f(x)=sin(x) → {numerical_derivative(sin_func, x0):.4f}(理论值:{math.cos(x0):.4f}\n")
# ====================== 补充简单的MLP和PPT里的公式对应 ======================
print("===== 补充简单MLP的前向计算和PPT公式对应 =====")
def relu(x):
"""ReLU激活函数"""
return max(0, x)
def simple_mlp(x, W1, b1, W2, b2):
"""
对应PPT里的公式y = ReLU(W2 * ReLU(W1 * x + b1) + b2)
x: 输入向量
W1: 第一层权重
b1: 第一层偏置
W2: 第二层权重
b2: 第二层偏置
"""
# 第一层
hidden = relu(W1 * x + b1)
# 第二层
output = relu(W2 * hidden + b2)
return output
# 测试这个简单MLP
W1 = 2
b1 = 1
W2 = 3
b2 = -1
x = 0.5
y = simple_mlp(x, W1, b1, W2, b2)
print(f"输入x={x}")
print(f"MLP输出{y}")
print(f"计算过程:")
print(f"第一层ReLU({W1}*{x} + {b1}) = ReLU({W1*x + b1}) = {relu(W1*x + b1)}")
print(f"第二层ReLU({W2}*{relu(W1*x + b1)} + {b2}) = ReLU({W2*relu(W1*x + b1) + b2}) = {y}")