import torch
print(torch.__version__) #注意是双下划线
1.9.1+cu102
!pip install torch.optim.optimize
import time
import torch.optim.optimizer
import torchvision
from torch import nn
from torch.nn import Sequential
from torch.utils.data import DataLoader
#from mymodel import *
from torch.utils.tensorboard import SummaryWriter
%load_ext tensorboard
The tensorboard extension is already loaded. To reload it, use:
%reload_ext tensorboard
class MyModel(nn.Module):
def __init__(self) -> None:
super().__init__()
#初始化各层
self.model=Sequential(
nn.Conv2d(3,32,5,1,2),
nn.MaxPool2d(2),
nn.Conv2d(32,32,5,1,2),
nn.MaxPool2d(2),
nn.Conv2d(32,64,5,1,2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4,64),
nn.Linear(64,10)
)
def forward(self,x):
output =self.model(x)
return output
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./data/cifar-10-python.tar.gz
Execution error
ImportError: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
start_time=time.time()
#开始训练与测试
for i in range(epoch):
print("-------------------------第{}轮训练开始-----------------------".format(i + 1))
#训练步骤开始
#设置训练状态
mymodel.train()
for data in train_dataloader:
imgs, targets = data
imgs=imgs.cuda()
targets=targets.cuda()
outputs = mymodel(imgs)
loss = loss_fn(outputs, targets)
#优化器优化模型
optimizer.zero_grad()
loss.backward()
optimizer.step()
#打印每一百次的损失值,观察趋势
total_train_step = total_train_step + 1
if total_train_step % 100 == 0:
end_time=time.time()
print("cost_time is : {}".format(end_time-start_time))
writer.add_scalar("train_loss", loss.item(), total_train_step)
print("------------------训练次数:{},Loss:{}-------------------".format(total_train_step, loss.item()))
#测试步骤开始
total_test_loss = 0
total_accuracy = 0
#设置验证状态
mymodel.eval()
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
imgs=imgs.cuda()
targets=targets.cuda()
outputs = mymodel(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test_loss + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuracy = total_accuracy + accuracy
writer.add_scalar("test_loss", total_test_loss, total_test_step)
writer.add_scalar("test_accuracy", total_accuracy.item()/test_datasize, total_test_step)
print("整体测试集上的loss:{}".format(total_test_loss))
print("整体测试集上的正确率:{}".format(total_accuracy.item()/test_datasize))
total_test_step = total_test_step + 1
#保存每一轮训练的参数结果
torch.save(mymodel.state_dict(), "mymodel_{}.pth".format(i + 1))
print("****模型已经保存****")
-------------------------第1轮训练开始-----------------------
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.)
return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
cost_time is : 1.4546654224395752
------------------训练次数:100,Loss:2.2825417518615723-------------------
cost_time is : 2.869929552078247
------------------训练次数:200,Loss:2.279968023300171-------------------
cost_time is : 4.253515720367432
------------------训练次数:300,Loss:2.242830514907837-------------------
cost_time is : 5.624562740325928
------------------训练次数:400,Loss:2.1473894119262695-------------------
cost_time is : 6.9922051429748535
------------------训练次数:500,Loss:2.0266218185424805-------------------
cost_time is : 8.414440393447876
------------------训练次数:600,Loss:2.021709680557251-------------------
cost_time is : 9.76365852355957
------------------训练次数:700,Loss:2.0173964500427246-------------------
整体测试集上的loss:314.5604351758957
整体测试集上的正确率:0.2823
****模型已经保存****
-------------------------第2轮训练开始-----------------------
cost_time is : 13.398088216781616
------------------训练次数:800,Loss:1.8770461082458496-------------------
cost_time is : 14.782817125320435
------------------训练次数:900,Loss:1.852764368057251-------------------
cost_time is : 16.13579821586609
------------------训练次数:1000,Loss:1.9199415445327759-------------------
cost_time is : 17.553563356399536
------------------训练次数:1100,Loss:1.9562405347824097-------------------
cost_time is : 18.91718888282776
------------------训练次数:1200,Loss:1.6867256164550781-------------------
cost_time is : 20.355039834976196
------------------训练次数:1300,Loss:1.6568413972854614-------------------
cost_time is : 21.757181882858276
------------------训练次数:1400,Loss:1.7446850538253784-------------------
cost_time is : 23.120391607284546
------------------训练次数:1500,Loss:1.8102542161941528-------------------
整体测试集上的loss:304.17450881004333
整体测试集上的正确率:0.3116
****模型已经保存****
-------------------------第3轮训练开始-----------------------
cost_time is : 26.658477306365967
------------------训练次数:1600,Loss:1.7450729608535767-------------------
cost_time is : 28.060527324676514
------------------训练次数:1700,Loss:1.6615324020385742-------------------
cost_time is : 29.394994258880615
------------------训练次数:1800,Loss:1.9233914613723755-------------------
cost_time is : 30.739256381988525
------------------训练次数:1900,Loss:1.7114081382751465-------------------
cost_time is : 32.17873811721802
------------------训练次数:2000,Loss:1.9417394399642944-------------------
cost_time is : 33.59120726585388
------------------训练次数:2100,Loss:1.524072289466858-------------------
cost_time is : 35.0124773979187
------------------训练次数:2200,Loss:1.4777741432189941-------------------
cost_time is : 36.3960976600647
------------------训练次数:2300,Loss:1.7922765016555786-------------------
整体测试集上的loss:262.713906288147
整体测试集上的正确率:0.3896
****模型已经保存****
-------------------------第4轮训练开始-----------------------
cost_time is : 40.04635739326477
------------------训练次数:2400,Loss:1.7308539152145386-------------------
cost_time is : 41.407012939453125
------------------训练次数:2500,Loss:1.3432682752609253-------------------
cost_time is : 42.765970945358276
------------------训练次数:2600,Loss:1.5785584449768066-------------------
cost_time is : 44.15843343734741
------------------训练次数:2700,Loss:1.659026026725769-------------------
cost_time is : 45.57533669471741
------------------训练次数:2800,Loss:1.4734179973602295-------------------
cost_time is : 46.93645119667053
------------------训练次数:2900,Loss:1.6070826053619385-------------------
cost_time is : 48.319117069244385
------------------训练次数:3000,Loss:1.3285980224609375-------------------
cost_time is : 49.71404957771301
------------------训练次数:3100,Loss:1.515210509300232-------------------
整体测试集上的loss:258.5771898031235
整体测试集上的正确率:0.4081
****模型已经保存****
-------------------------第5轮训练开始-----------------------
cost_time is : 53.32498002052307
------------------训练次数:3200,Loss:1.3044883012771606-------------------
cost_time is : 54.68355584144592
------------------训练次数:3300,Loss:1.4851174354553223-------------------
cost_time is : 56.06484627723694
------------------训练次数:3400,Loss:1.4341320991516113-------------------
cost_time is : 57.44633078575134
------------------训练次数:3500,Loss:1.5524694919586182-------------------
cost_time is : 58.81807827949524
------------------训练次数:3600,Loss:1.5234549045562744-------------------
cost_time is : 60.19964027404785
------------------训练次数:3700,Loss:1.369845986366272-------------------
cost_time is : 61.5733757019043
------------------训练次数:3800,Loss:1.301695704460144-------------------
cost_time is : 62.985485553741455
------------------训练次数:3900,Loss:1.4900155067443848-------------------
整体测试集上的loss:249.59872841835022
整体测试集上的正确率:0.4247
****模型已经保存****
-------------------------第6轮训练开始-----------------------
cost_time is : 66.63380026817322
------------------训练次数:4000,Loss:1.4052904844284058-------------------
cost_time is : 68.00632095336914
------------------训练次数:4100,Loss:1.4434432983398438-------------------
cost_time is : 69.34769630432129
------------------训练次数:4200,Loss:1.4870821237564087-------------------
cost_time is : 70.68448495864868
------------------训练次数:4300,Loss:1.1990349292755127-------------------
cost_time is : 72.06165361404419
------------------训练次数:4400,Loss:1.1721410751342773-------------------
cost_time is : 73.46936082839966
------------------训练次数:4500,Loss:1.3538200855255127-------------------
cost_time is : 74.87904596328735
------------------训练次数:4600,Loss:1.363562822341919-------------------
整体测试集上的loss:238.1324301958084
整体测试集上的正确率:0.4509
****模型已经保存****
-------------------------第7轮训练开始-----------------------
cost_time is : 78.4708297252655
------------------训练次数:4700,Loss:1.3469716310501099-------------------
cost_time is : 79.82069849967957
------------------训练次数:4800,Loss:1.4809948205947876-------------------
cost_time is : 81.18264365196228
------------------训练次数:4900,Loss:1.3498345613479614-------------------
cost_time is : 82.52971053123474
------------------训练次数:5000,Loss:1.43301260471344-------------------
cost_time is : 83.89839458465576
------------------训练次数:5100,Loss:0.9814057350158691-------------------
cost_time is : 85.24964547157288
------------------训练次数:5200,Loss:1.3567657470703125-------------------
cost_time is : 86.59544825553894
------------------训练次数:5300,Loss:1.2017059326171875-------------------
cost_time is : 87.9313383102417
------------------训练次数:5400,Loss:1.374663233757019-------------------
整体测试集上的loss:226.89615046977997
整体测试集上的正确率:0.4778
****模型已经保存****
-------------------------第8轮训练开始-----------------------
cost_time is : 91.52201056480408
------------------训练次数:5500,Loss:1.2032146453857422-------------------
cost_time is : 92.95591735839844
------------------训练次数:5600,Loss:1.2389296293258667-------------------
cost_time is : 94.38527774810791
------------------训练次数:5700,Loss:1.1559687852859497-------------------
cost_time is : 95.75567126274109
------------------训练次数:5800,Loss:1.2559820413589478-------------------
cost_time is : 97.14851975440979
------------------训练次数:5900,Loss:1.3414711952209473-------------------
cost_time is : 98.52188873291016
------------------训练次数:6000,Loss:1.5757970809936523-------------------
cost_time is : 99.93702745437622
------------------训练次数:6100,Loss:1.0377321243286133-------------------
cost_time is : 101.3215606212616
------------------训练次数:6200,Loss:1.1358979940414429-------------------
整体测试集上的loss:214.96796941757202
整体测试集上的正确率:0.5086
****模型已经保存****
-------------------------第9轮训练开始-----------------------
cost_time is : 104.9155740737915
------------------训练次数:6300,Loss:1.4523265361785889-------------------
cost_time is : 106.2784309387207
------------------训练次数:6400,Loss:1.1625995635986328-------------------
cost_time is : 107.64034652709961
------------------训练次数:6500,Loss:1.558074712753296-------------------
cost_time is : 108.98485684394836
------------------训练次数:6600,Loss:1.1246799230575562-------------------
cost_time is : 110.35576486587524
------------------训练次数:6700,Loss:1.0796072483062744-------------------
cost_time is : 111.7425491809845
------------------训练次数:6800,Loss:1.1666195392608643-------------------
cost_time is : 113.11383152008057
------------------训练次数:6900,Loss:1.1315326690673828-------------------
cost_time is : 114.53430342674255
------------------训练次数:7000,Loss:0.9667822122573853-------------------
整体测试集上的loss:204.29894053936005
整体测试集上的正确率:0.5329
****模型已经保存****
-------------------------第10轮训练开始-----------------------
cost_time is : 118.14721727371216
------------------训练次数:7100,Loss:1.3183833360671997-------------------
cost_time is : 119.50576066970825
------------------训练次数:7200,Loss:0.9356701374053955-------------------
cost_time is : 120.85891699790955
------------------训练次数:7300,Loss:1.1434377431869507-------------------
cost_time is : 122.2334258556366
------------------训练次数:7400,Loss:0.8158775568008423-------------------
cost_time is : 123.58963584899902
------------------训练次数:7500,Loss:1.2399603128433228-------------------
cost_time is : 124.9990131855011
------------------训练次数:7600,Loss:1.2415118217468262-------------------
cost_time is : 126.40948128700256
------------------训练次数:7700,Loss:0.8808146119117737-------------------
cost_time is : 127.76332759857178
------------------训练次数:7800,Loss:1.2474720478057861-------------------
整体测试集上的loss:195.46184331178665
整体测试集上的正确率:0.5587
****模型已经保存****
!tensorboard --logdir cnn_demo_logs #定位tensorboard读取的文件目录
2021-09-27 08:05:33.552751: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-27 08:05:34.006879: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-27 08:05:34.007687: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
NOTE: Using experimental fast data loading logic. To disable, pass
"--load_fast=false" and report issues on GitHub. More details:
https://github.com/tensorflow/tensorboard/issues/4784
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.6.0 at http://localhost:6006/ (Press CTRL+C to quit)
^C
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
--2021-09-27 08:10:29-- https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
Resolving bin.equinox.io (bin.equinox.io)... 52.202.168.65, 54.237.133.81, 54.161.241.46, ...
Connecting to bin.equinox.io (bin.equinox.io)|52.202.168.65|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13832437 (13M) [application/octet-stream]
Saving to: ‘ngrok-stable-linux-amd64.zip’
ngrok-stable-linux- 100%[===================>] 13.19M 29.2MB/s in 0.5s
2021-09-27 08:10:29 (29.2 MB/s) - ‘ngrok-stable-linux-amd64.zip’ saved [13832437/13832437]
Archive: ngrok-stable-linux-amd64.zip
inflating: ngrok
LOG_DIR = '/content/cnn_demo_logs'
get_ipython().system_raw(
'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
.format(LOG_DIR)
)
get_ipython().system_raw('./ngrok http 6006 &')
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
https://ca713606be9d.ngrok.io