1. 代码实现

波士顿房价数据集下载

1.1 一元线性回归模型训练

import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader, random_split
from tensorboardX import SummaryWriter

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
batch_size = 2
num_epochs = 200

writer = SummaryWriter()

model = nn.Linear(1, 1).to(device)
nn.init.normal_(model.weight, mean=0, std=0.01)
nn.init.constant_(model.bias, 0)
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)

data = np.load('dataset/boston_housing/boston_housing.npz')
X = torch.tensor(data['x'][:, 0].reshape(-1, len(model.weight)), dtype=torch.float, device=device)
y = torch.tensor(data['y'].reshape(-1, 1), dtype=torch.float, device=device)
dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)

for epoch in range(num_epochs):
    for _X, _y in dataloader:
        _X, _y = _X.to(device), _y.to(device)
        loss = criterion(model(_X), _y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    loss = criterion(model(X), y)
    torch.save(model.state_dict(), 'model/linearRegression.pt')
    model.load_state_dict(torch.load('model/linearRegression.pt'))
    writer.add_scalar('Loss/train', loss, epoch)
    writer.add_scalar('W/train', model.weight, epoch)
    writer.add_scalar('b/train', model.bias, epoch)
writer.close()

2. 代码解读

2.1. tensorboardX

tensorboardX是一种能将训练过程可视化工具

2.1.1. tensorboardX的安装

安装命令

pip install tensorboardX

VSCode集成了TensorBoard支持,不过事先要安装torchtbprofiler,安装命令

pip install torch-tb-profiler

安装完成后,在Python源文件tensorboardX模块导入处,点击启动TensorBoard会话按钮然后选择运行事件所在目录默认选择当前目录即可tensorboard自动当前目录查找运行事件,由此即可启动TensorBoard。
启动TensorBoard会话
logdir
此外,也可以通过以下命令在浏览器查看tensorboard可视化结果

# logdir运行事件所在目录
> tensorboard logdir=runs
TensorFlow installation not found - running with reduced feature set.
I1202 20:37:50.824767 15412 plugin.py:429] Monitor runs begin
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.14.0 at http://localhost:6006/ (Press CTRL+C to quit)
# 手动打开命令输出提供的本地服务器地址,如http://localhost:6006/

2.1.2. tensorboardX的使用

from tensorboardX import SummaryWriter
writer = SummaryWriter()
# writer.add_scalar():添加监控变量
writer.close()
from tensorboardX import SummaryWriter
with SummaryWriter() as writer:
	# writer.add_scalar():添加监控变量

原文地址:https://blog.csdn.net/weixin_45725295/article/details/134756961

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_31894.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注