真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

tensorboard可視化-創(chuàng)新互聯(lián)

tensorboard可視化
  • Tensorboard導入與可視化圖片
  • 模型網(wǎng)絡結(jié)構(gòu)的可視化
  • 標量數(shù)據(jù)的可視化

創(chuàng)新互聯(lián)公司主營河口網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都app開發(fā),河口h5小程序開發(fā)搭建,河口網(wǎng)站營銷推廣歡迎河口等地區(qū)企業(yè)咨詢Tensorboard導入與可視化圖片

以手寫數(shù)字分類mnist數(shù)據(jù)集為例:
下載mnist數(shù)據(jù)集,構(gòu)造dataset:

train_ds = datasets.MNIST(
                          'data/',
                          train=True,
                          transform=transformation,
                          download=True  
)
test_ds = datasets.MNIST(
                          'data/',
                          train=False,
                          transform=transformation,
                          download=True  
)

在這里插入圖片描述
查看mnist數(shù)據(jù)集包含的圖片:

def imshow(img):
    npimg = img.numpy()
    npimg = np.squeeze(npimg)
    plt.imshow(npimg)
plt.figure(figsize=(10, 1))
for i, img in enumerate(imgs[:10]):
    plt.subplot(1, 10, i+1)
    imshow(img)

在這里插入圖片描述
導入tensorboard:
可視化兩步驟:
1.在代碼中將需要可視化的數(shù)據(jù)寫入磁盤
2.在命令行中打開tensorboard,并指定寫入的文件位置,進行可視化

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter('my_log/mnist')  # 指定寫入位置

顯示圖片:

images, labels = next(iter(train_dl))

# create grid of images
img_grid = torchvision.utils.make_grid(images[-8:]) # 將多張圖片合并在一起成一張圖片
npimg = img_grid.permute(1, 2, 0).numpy()
plt.imshow(npimg)

在這里插入圖片描述
寫入圖片到tensorboard:

writer.add_image('eight_mnist_images', img_grid)

在命令行窗口輸入以下指令:

tensorboard --logdir=D:\PycharmProjects\PythonScript\Pytorch_Course_Study\my_log

在這里插入圖片描述
打開給出的網(wǎng)址:http://localhost:6006/
查看到動態(tài)顯示的圖片:
在這里插入圖片描述

模型網(wǎng)絡結(jié)構(gòu)的可視化

創(chuàng)建模型:

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)   
        self.pool = nn.MaxPool2d((2, 2))
        self.conv2 = nn.Conv2d(6, 16, 5) 
        self.liner_1 = nn.Linear(16*4*4, 256)
        self.liner_2 = nn.Linear(256, 10)
    def forward(self, input):
        x = F.relu(self.conv1(input))
        x = self.pool(x)
        x = F.relu(self.conv2(x))
        x = self.pool(x)
#        print(x.size())    # torch.Size([64, 16, 4, 4])
        x = x.view(-1, 16*4*4)
        x = F.relu(self.liner_1(x))
        x = self.liner_2(x)
        return x
model = Model()

顯示模型:

writer.add_graph(model, images) # images為輸入模型的數(shù)據(jù)

在tensorboard中查看模型:
在這里插入圖片描述
雙擊model可查看模型內(nèi)部結(jié)構(gòu):
在這里插入圖片描述

標量數(shù)據(jù)的可視化

動態(tài)顯示訓練過程中的loss 和 acc的變化:

model.to(device)
loss_fn = torch.nn.CrossEntropyLoss()  # 損失函數(shù)

使用write.add_scalar()方法:

def fit(epoch, model, trainloader, testloader):
    correct = 0
    total = 0
    running_loss = 0
    for x, y in trainloader:
        x, y = x.to(device), y.to(device)
        y_pred = model(x)
        loss = loss_fn(y_pred, y)
        optim.zero_grad()
        loss.backward()
        optim.step()
        with torch.no_grad():
            y_pred = torch.argmax(y_pred, dim=1)
            correct += (y_pred == y).sum().item()
            total += y.size(0)
            running_loss += loss.item()
        
    epoch_loss = running_loss / len(trainloader.dataset)
    epoch_acc = correct / total
    
    writer.add_scalar('training loss',
                        epoch_loss,
                        epoch)
        
        
    test_correct = 0
    test_total = 0
    test_running_loss = 0 
    
    with torch.no_grad():
        for x, y in testloader:
            x, y = x.to(device), y.to(device)
            y_pred = model(x)
            loss = loss_fn(y_pred, y)
            y_pred = torch.argmax(y_pred, dim=1)
            test_correct += (y_pred == y).sum().item()
            test_total += y.size(0)
            test_running_loss += loss.item()
    
    epoch_test_loss = test_running_loss / len(testloader.dataset)
    epoch_test_acc = test_correct / test_total
    
    writer.add_scalar('test loss',
                        epoch_test_loss,
                        epoch)
    
        
    print('epoch: ', epoch, 
          'loss: ', round(epoch_loss, 3),
          'accuracy:', round(epoch_acc, 3),
          'test_loss: ', round(epoch_test_loss, 3),
          'test_accuracy:', round(epoch_test_acc, 3)
             )
        
    return epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc
optim = torch.optim.Adam(model.parameters(), lr=0.001)
epochs = 20
train_loss = []
train_acc = []
test_loss = []
test_acc = []

for epoch in range(epochs):
    epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc = fit(epoch,
                                                                 model,
                                                                 train_dl,
                                                                 test_dl)
    train_loss.append(epoch_loss)
    train_acc.append(epoch_acc)
    test_loss.append(epoch_test_loss)
    test_acc.append(epoch_test_acc)

在這里插入圖片描述

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧


當前題目:tensorboard可視化-創(chuàng)新互聯(lián)
新聞來源:http://weahome.cn/article/hoici.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部