定义数据集
import torch
import random
定义函数
# 生成数据
def get_rancledata():
width=random.random()
height=random.random()
s=width*height
return width,height,s
get_rancledata()
(0.1571327616035657, 0.5335562021159256, 0.08383915950918565)
生成数据集
class dataset(torch.utils.data.Dataset):
def __init__(self):
pass
def __len__(self):
return 1000
def __getitem__(self,i):
width,height,s=get_rancledata()
x=torch.FloatTensor([width,height])
# 这里注意也是需要转换成tensor的,否则训练会报类型错误
y=torch.FloatTensor([s])
return x,y
dataset=dataset()
len(dataset),dataset[4999]
(1000, (tensor([0.2137, 0.6781]), tensor([0.1449])))
使用Dataloader加载dataset
loader=torch.utils.data.DataLoader(
dataset=dataset,
shuffle=True,
batch_size=9
)
len(loader),next(iter(loader))
(112,
[tensor([[0.7389, 0.1202],
[0.5764, 0.7888],
[0.7244, 0.0229],
[0.5102, 0.0755],
[0.8550, 0.4998],
[0.9992, 0.5890],
[0.1704, 0.0162],
[0.2132, 0.9157],
[0.7946, 0.8907]]),
tensor([[0.0888],
[0.4546],
[0.0166],
[0.0385],
[0.4273],
[0.5885],
[0.0028],
[0.1953],
[0.7077]])])
定义神经网络
定义
# 输入两个,输出一个
class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.fb=torch.nn.Sequential(
torch.nn.Linear(in_features=2,out_features=32),
torch.nn.ReLU(),
torch.nn.Linear(in_features=32,out_features=32),
torch.nn.ReLU(),
torch.nn.Linear(in_features=32,out_features=1)
)
def forward(self,x):
return self.fb(x)
实例化
model=Model()
torch.rand(4,2)
tensor([[0.4510, 0.1455],
[0.4963, 0.2974],
[0.9480, 0.9913],
[0.9053, 0.4228]])
查看是否是输出的一个
# 测试
model(torch.rand(8,2)).shape
torch.Size([8, 1])
训练
编写trian方法
def train():
# 选择损失函数
loss_fn=torch.nn.MSELoss()
# 选择优化器
optimizer=torch.optim.Adam(model.parameters(),lr=1e-4)
#遍历多少轮
for epoch in range(100):
#全量遍历
for i ,(x,y) in enumerate(loader):
#计算损失
#计算梯度
#优化参数
#优化梯度清零
out=model(x)
loss=loss_fn(out,y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if epoch % 20 ==0:
print(epoch,loss.item())
torch.save(model,"huigui.model")
训练并保存模型
train()
0 0.03260539472103119
0 0.06368591636419296
0 0.08260147273540497
0 0.04632813110947609
0 0.08333451300859451
0 0.10992465913295746
0 0.12929300963878632
0 0.061169371008872986
0 0.08229123800992966
0 0.0604255348443985
0 0.11475709825754166
0 0.13913851976394653
0 0.09228374809026718
0 0.10618235915899277
0 0.12170673906803131
0 0.05438697338104248
0 0.11730150133371353
0 0.07718850672245026
0 0.11877405643463135
0 0.0647420659661293
0 0.1062769666314125
0 0.08034960925579071
0 0.06462960690259933
0 0.029708124697208405
0 0.19415663182735443
0 0.022178875282406807
0 0.023824863135814667
0 0.06074700132012367
0 0.014404748566448689
0 0.015829702839255333
0 0.07006165385246277
0 0.0908271074295044
0 0.023783870041370392
0 0.09584006667137146
0 0.16521167755126953
0 0.09473344683647156
0 0.12153694033622742
0 0.030839459970593452
0 0.019292233511805534
40 8.071886259131134e-05
40 2.137169212801382e-05
40 0.00010651862248778343
40 7.332033419515938e-05
40 0.00010564295371295884
40 4.790672755916603e-05
40 3.7615245673805475e-05
40 3.413142985664308e-05
40 6.713613402098417e-05
40 0.0006545005016960204
测试模型结果
构造数据
# 从loader加载一批数据来测试
x,y=next(iter(loader))
x,y
测试
# 方法一
# out=model(x)
# 方法二 加载模型
model1=torch.load("huigui.model")
out=model1(x)
# 打印在一起,便于观察,
# 这个cat函数很有用注意
torch.cat([out,y],dim=1)
结论
从上面结果看文章来源:https://www.toymoban.com/news/detail-617107.html
[ 0.6257, 0.6214],
[ 0.5435, 0.5454],
[ 0.0227, 0.0203],
[-0.0044, 0.0033],
[ 0.5257, 0.5296],
[ 0.4749, 0.4805],
[ 0.4665, 0.4649],
[ 0.4143, 0.4141],
[ 0.0130, 0.0138]]
第一列是预测的,第二列是实际的,可以查看两列值相差很小,说明模型有效文章来源地址https://www.toymoban.com/news/detail-617107.html
到了这里,关于python-pytorch基础之神经网络回归的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!