import matplotlib.pyplot as plt
# 生成数据
x =[1,2,3,4,5]
y =[10,5,8,4,7]# 创建一个图形
plt.figure()# 绘制线条图
plt.plot(x, y)# 设置标题和轴标签
plt.title('Line Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')# 保存为SVG文件
plt.savefig('line_chart.svg',format='svg')# 显示图形(如果需要)
plt.show()
Adam算法
import matplotlib.pyplot as plt
import numpy as np
import torch
defadam_qhj():
np.random.seed(0)
x = np.random.uniform(0,2,100)
y = x *3+1+ np.random.normal(0,0.5,100)
x = torch.from_numpy(x).float().view(-1,1)
y = torch.from_numpy(y).float().view(-1,1)
model = torch.nn.Linear(1,1)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)# Reduce learning rate
losses =[]for i inrange(100):# Increase the number of epochs
optimizer.zero_grad()# Clear gradients before each epoch
y_pred = model(x)
loss = loss_fn(y_pred, y)
losses.append(loss.item())
loss.backward()
optimizer.step()
plt.plot(losses)
plt.ylim((0,15))# Adjust based on the new loss values
plt.show()defadam_jqh():
np.random.seed(0)
x = np.random.uniform(0,2,100)
y = x *3+1+ np.random.normal(0,0.5,100)
x = torch.from_numpy(x).float().view(-1,1)
y = torch.from_numpy(y).float().view(-1,1)
model = torch.nn.Linear(1,1)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
losses =[]for i inrange(10000):
optimizer.zero_grad()
y_pred = model(x)
loss = loss_fn(y_pred, y)
losses.append(loss.item())
loss.backward()
optimizer.step()
plt.plot(losses)
plt.ylim((0,15))
plt.show()if __name__ =='__main__':
adam_jqh()
adam_qhj()
梯度消失+梯度爆炸+线性回归
import torch
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
defsigmod():
x = torch.arange(-8.0,8.0,0.1, requires_grad=True)
y = torch.sigmoid(x)
y.backward(torch.ones_like(x))
plt.plot(x.detach().numpy(), y.detach().numpy(), label='sigmoid')
plt.plot(x.detach().numpy(), x.grad.numpy(), linestyle=':', label='gradient')
plt.legend()
plt.show()defmatrixCalculation():
Mat = torch.normal(0,1, size=(5,5))for i inrange(100):
Mat = torch.mm(Mat, torch.normal(0,1, size=(5,5)))print(Mat)defgradientRELU():
x = torch.arange(-8.0,8.0,0.1, requires_grad=True)
y = torch.relu(x)
y.backward(torch.ones_like(x))
plt.plot(x.detach().numpy(), y.detach().numpy(), label='relu')
plt.plot(x.detach().numpy(), x.grad.numpy(), linestyle=':', label='gradient')
plt.legend()
plt.show()deflinearRegression():# 设置随机数种子,使得每次运行代码生成的数据相同
np.random.seed(42)# 生成随机数据
x = np.random.rand(100,1)
y =1+2* x +0.1* np.random.randn(100,1)# 将数据转换为 pytorch tensor
x_tensor = torch.from_numpy(x).float()
y_tensor = torch.from_numpy(y).float()# 设置超参数
learning_rate =0.1
num_epochs =1000# 定义输入数据的维度和输出数据的维度
input_dim =1
output_dim =1# 定义模型
model = nn.Linear(input_dim, output_dim)# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)# 开始训练for epoch inrange(num_epochs):# 将输入数据喂给模型
y_pred = model(x_tensor)# 计算损失
loss = criterion(y_pred, y_tensor)# 清空梯度
optimizer.zero_grad()# 反向传播
loss.backward()# 更新参数
optimizer.step()# 输出训练后的参数print('w:', model.weight.data)print('b:', model.bias.data)
plt.plot(x, y,'o')
plt.plot(x_tensor.numpy(), y_pred.detach().numpy())
plt.show()if __name__ =='__main__':
sigmod()
matrixCalculation()
gradientRELU()
linearRegression()