使用块的网络(VGG)


VGG16精度稍有提升.

1 Like


尺寸改为96,batchsize=64,训练速度有所提升,精度没差多少.

1 Like

第一个1 是批量数,第二个1 是通道数,通道数以及后面的高宽都是不断变化的。

1 Like

问题:中文版本的pytorch Colab运行单元格报错
详细:
1.从网站主页面进入


2.点击【代码执行程序】-》【全部运行】报错:

可能的原因:
colab中d2l版本的问题

额外的尝试:
1.卸载重新安装d2l–没有解决:
!pip uninstall d2l
!pip install d2l==0.17.6
2.运行英文版本的colab是正常的,但是代码中删除了数据输出,不能直观看到每秒处理的样本数量,训练精度,测试精度。

1 Like

我问chatgpt了,上面是这样回答的:
这个问题是因为Colaboratory默认情况下没有安装matplotlib-inline模块,需要手动安装才能使用。

你可以尝试以下两种方法中的任意一种:

方法一:使用魔术命令安装

在代码开头加入以下两行代码:
%matplotlib inline
!pip install matplotlib-inline
这将会安装matplotlib-inline模块,并且使用魔术命令将Matplotlib设置为内联模式,这将会使绘图直接显示在notebook中。

方法二:使用普通命令安装

在代码中加入以下一行代码:
!pip install matplotlib-inline
然后在代码开头加入以下一行代码:
import matplotlib_inline
这将会安装matplotlib-inline 模块并且导入它,你可以通过在绘图前加入以下一行代码来将Matplotlib设置为内联模式:
%matplotlib inline

用第一个方法试了,发现可以

1 Like

有具体的VGG16的net吗或者他的网络架构,感谢

我的打印里可以看到11个层的结构啊,为啥习题1说只有8层?

Sequential output shape: torch.Size([1, 64, 112, 112])
Sequential output shape: torch.Size([1, 128, 56, 56])

Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 10])

这里的输出是13行,但是本质上输出的是8层神经网络。
其中Sequential5层,其中Linear3层ReLU,Dropout这些层都不算的,因为它们不是神经元。


原始VGG网络有5个卷积块,其中前两个块各有一个卷积层,后三个块各包含两个卷积层。 第一个模块有64个输出通道,每个后续模块将输出通道数量翻倍,直到该数字达到512。由于该网络使用8个卷积层和3个全连接层,因此它通常被称为VGG-11。

我们可以得知前两个Sequential层包含了1个卷积层,后三个Sequential层包含了两个卷积层
为什么不会输出出来呢,是因为代码不行,我们打印的代码为:

X = torch.randn(size=(1, 1, 224, 224))
for blk in net:
    X = blk(X)
    print(blk.__class__.__name__,'output shape:\t',X.shape)

这段代码只会打印某层的输出,不会打印子层的输出。将代码修改为如下,就可以了

X = torch.randn(size=(1, 1, 224, 224))

def print_net(net):
    global X
    for blk in net:
        if blk.__class__.__name__ == "Sequential":
            print_net(blk)
            print()
        else:
            X = blk(X)
            print(blk.__class__.__name__,'output shape:\t',X.shape)

print_net(net)

# 我的输出示例:
Conv2d output shape:	 torch.Size([1, 16, 224, 224])
ReLU output shape:	 torch.Size([1, 16, 224, 224])
MaxPool2d output shape:	 torch.Size([1, 16, 112, 112])

Conv2d output shape:	 torch.Size([1, 32, 112, 112])
ReLU output shape:	 torch.Size([1, 32, 112, 112])
MaxPool2d output shape:	 torch.Size([1, 32, 56, 56])

Conv2d output shape:	 torch.Size([1, 64, 56, 56])
ReLU output shape:	 torch.Size([1, 64, 56, 56])
Conv2d output shape:	 torch.Size([1, 64, 56, 56])
ReLU output shape:	 torch.Size([1, 64, 56, 56])
MaxPool2d output shape:	 torch.Size([1, 64, 28, 28])

Conv2d output shape:	 torch.Size([1, 128, 28, 28])
ReLU output shape:	 torch.Size([1, 128, 28, 28])
Conv2d output shape:	 torch.Size([1, 128, 28, 28])
ReLU output shape:	 torch.Size([1, 128, 28, 28])
MaxPool2d output shape:	 torch.Size([1, 128, 14, 14])

Conv2d output shape:	 torch.Size([1, 128, 14, 14])
ReLU output shape:	 torch.Size([1, 128, 14, 14])
Conv2d output shape:	 torch.Size([1, 128, 14, 14])
ReLU output shape:	 torch.Size([1, 128, 14, 14])
MaxPool2d output shape:	 torch.Size([1, 128, 7, 7])

Flatten output shape:	 torch.Size([1, 6272])
Linear output shape:	 torch.Size([1, 4096])
ReLU output shape:	 torch.Size([1, 4096])
Dropout output shape:	 torch.Size([1, 4096])
Linear output shape:	 torch.Size([1, 4096])
ReLU output shape:	 torch.Size([1, 4096])
Dropout output shape:	 torch.Size([1, 4096])
Linear output shape:	 torch.Size([1, 10])
6 Likes

其实可以从报错信息得知某个模块缺少,安装缺失的模块即可。

1 Like

!pip install matplotlib_inline

1 Like

!pip install matplotlib_inline 即可

1 Like

台式机的RTX 3080显卡还是可以的,VGG模型跑了6分钟出结果

4 Likes

nn.Linear(out_channels * 7 * 7, 4096)
为什么这里要out_channels * 7 * 7
而不是直接in_channels?

为什么我的最后结果展示不出来图(train loss/acc, test acc),只有<Figure size 350x250 with 1 Axes>
Snipaste_2023-04-24_16-37-03

chatgpt这么秀的吗 :joy:。。。。。。。。。。。。。。。。。

因为前面有一个Flatten层,所以第一个线性层的输入维度是:通道数x宽x高

我的都跑到55℃了 :rofl:字数字数字数字数字数字数字数字数字数字数字数字数

alexnet 我最开始用cpu跑了335分钟(intel gpu没cuda),换笔记本集显4分钟跑完了,惊叹之余再一看视频课,老师半分钟就跑完了 :joy:


想问一下我设置的是用GPU在跑,怎么看任务管理器CPU占用这么多啊,运行时也确实输出了在cuda上训练

我之前colab忘记关会话了,免费的计算单元用光了,现在在用国内的autoDL,个人感觉还不错,可以试一试

您好,我现在直接点教材右上角的colab然后运行程序卡在第一步下载d2l要怎么解决呢