pytorch笔记

In [1]: import torchvision                                                      

In [2]: import torch                                                            

In [3]: import torch.nn as nn                                                   

In [4]: alex = torchvision.models.alexnet(pretrained=False)                     

In [5]: alex?                                                                   
Signature:       alex(*input, **kwargs)
Type:            AlexNet
String form:    
AlexNet(
           (features): Sequential(
           (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), pa <...> rue)
           (5): ReLU(inplace)
           (6): Linear(in_features=4096, out_features=1000, bias=True)
           )
           )
File:            ~/anaconda3/lib/python3.6/site-packages/torchvision/models/alexnet.py
Docstring:       <no docstring>
Class docstring:
Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in
a tree structure. You can assign the submodules as regular attributes::

    import torch.nn as nn
    import torch.nn.functional as F

    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.conv1 = nn.Conv2d(1, 20, 5)
            self.conv2 = nn.Conv2d(20, 20, 5)

        def forward(self, x):
           x = F.relu(self.conv1(x))
           return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their
parameters converted too when you call :meth:`to`, etc.

In [6]: torchvision.models.AlexNet?                                             
Init signature: torchvision.models.AlexNet(num_classes=1000)
Docstring:     
Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in
a tree structure. You can assign the submodules as regular attributes::

    import torch.nn as nn
    import torch.nn.functional as F

    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.conv1 = nn.Conv2d(1, 20, 5)
            self.conv2 = nn.Conv2d(20, 20, 5)

        def forward(self, x):
           x = F.relu(self.conv1(x))
           return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their
parameters converted too when you call :meth:`to`, etc.
File:           ~/anaconda3/lib/python3.6/site-packages/torchvision/models/alexnet.py
Type:           type

In [7]: class realex(alex): 
   ...:     def __init__(self): 
   ...:         super().__init__() 
   ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
   ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
   ...:     def forward(self, x): 
   ...:         x = F.relu(self.conv1(x)) 
   ...:         return F.relu(self.conv2(x)) 
   ...:                                                                                                                                                                                                             
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-e07645723006> in <module>
----> 1 class realex(alex):
      2     def __init__(self):
      3         super().__init__()
      4         self.conv1 = nn.Conv1d(1, 20, 5)
      5         self.conv2 = nn.Conv2d(20, 20, 6)

TypeError: __init__() takes from 1 to 2 positional arguments but 4 were given

In [8]: class realex(nn.Module): 
   ...:     def __init__(self): 
   ...:         super().__init__() 
   ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
   ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
   ...:     def forward(self, x): 
   ...:         x = F.relu(self.conv1(x)) 
   ...:         return F.relu(self.conv2(x)) 
   ...:                                                                                                                                                                                                             

In [9]: class realex(torchvision.models.AlexNet): 
   ...:     def __init__(self): 
   ...:         super().__init__() 
   ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
   ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
   ...:     def forward(self, x): 
   ...:         x = F.relu(self.conv1(x)) 
   ...:         return F.relu(self.conv2(x)) 
   ...:                                                                                                                                                                                                             

In [10]: realex                                                                                                                                                                                                     
Out[10]: __main__.realex

In [11]: realexx = realex()                                                                                                                                                                                         

In [12]: realexx                                                                                                                                                                                                    
Out[12]: 
realex(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
    (1): ReLU(inplace)
    (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (4): ReLU(inplace)
    (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (7): ReLU(inplace)
    (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (9): ReLU(inplace)
    (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace)
    (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
  (classifier): Sequential(
    (0): Dropout(p=0.5)
    (1): Linear(in_features=9216, out_features=4096, bias=True)
    (2): ReLU(inplace)
    (3): Dropout(p=0.5)
    (4): Linear(in_features=4096, out_features=4096, bias=True)
    (5): ReLU(inplace)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)

In [13]: class realex(torchvision.models.AlexNet): 
    ...:     def __init__(self): 
    ...:         #super().__init__() 
    ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
    ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
    ...:     def forward(self, x): 
    ...:         x = F.relu(self.conv1(x)) 
    ...:         return F.relu(self.conv2(x)) 
    ...:                                                                                                                                                                                                            

In [14]: realexx                                                                                                                                                                                                    
Out[14]: 
realex(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
    (1): ReLU(inplace)
    (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (4): ReLU(inplace)
    (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (7): ReLU(inplace)
    (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (9): ReLU(inplace)
    (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace)
    (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
  (classifier): Sequential(
    (0): Dropout(p=0.5)
    (1): Linear(in_features=9216, out_features=4096, bias=True)
    (2): ReLU(inplace)
    (3): Dropout(p=0.5)
    (4): Linear(in_features=4096, out_features=4096, bias=True)
    (5): ReLU(inplace)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)

In [15]: import os                                                                                                                                                                                                  

In [16]: os.system('vim ~/anaconda3/lib/python3.6/site-packages/torchvision/models/alexnet.py')                                                                                                                     
Out[16]: 0

In [17]: class realex(torchvision.models.AlexNet): 
    ...:     def __init__(self): 
    ...:         super().__init__() 
    ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
    ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
    ...:     def forward(self, x): 
    ...:         x = F.relu(self.conv1(x)) 
    ...:         return F.relu(self.conv2(x)) 
    ...:                                                                                                                                                                                                            

In [18]: real = realex()                                                                                                                                                                                            

In [19]: real                                                                                                                                                                                                       
Out[19]: 
realex(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
    (1): ReLU(inplace)
    (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (4): ReLU(inplace)
    (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (7): ReLU(inplace)
    (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (9): ReLU(inplace)
    (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace)
    (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
  (classifier): Sequential(
    (0): Dropout(p=0.5)
    (1): Linear(in_features=9216, out_features=4096, bias=True)
    (2): ReLU(inplace)
    (3): Dropout(p=0.5)
    (4): Linear(in_features=4096, out_features=4096, bias=True)
    (5): ReLU(inplace)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)

In [20]: class realex(nn.Module): 
    ...:     def __init__(self): 
    ...:         super().__init__() 
    ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
    ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
    ...:     def forward(self, x): 
    ...:         x = F.relu(self.conv1(x)) 
    ...:         return F.relu(self.conv2(x)) 
    ...:                                                                                                                                                                                                            

In [21]: real = realex()                                                                                                                                                                                            

In [22]: real                                                                                                                                                                                                       
Out[22]: 
realex(
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)

In [23]: nn.Module?                                                                                                                                                                                                 
Init signature: nn.Module()
Docstring:     
Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in
a tree structure. You can assign the submodules as regular attributes::

    import torch.nn as nn
    import torch.nn.functional as F

    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.conv1 = nn.Conv2d(1, 20, 5)
            self.conv2 = nn.Conv2d(20, 20, 5)

        def forward(self, x):
           x = F.relu(self.conv1(x))
           return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their
parameters converted too when you call :meth:`to`, etc.
File:           ~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py
Type:           type

In [24]: os.system('vim ~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py')                                                                                                                        
Out[24]: 0

In [25]: class realex(nn.Module): 
    ...:     def __init__(self): 
    ...:         super().__init__() 
    ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
    ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
    ...:     def forward(self, x): 
    ...:         x = F.relu(self.conv1(x)) 
    ...:         return x 
    ...:                                                                                                                                                                                                            

In [26]: real = realex()                                                                                                                                                                                            

In [27]: real                                                                                                                                                                                                       
Out[27]: 
realex(
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)

In [28]: class realex(nn.Module): 
    ...:     def __init__(self): 
    ...:         super().__init__() 
    ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
    ...:         nn.ReLU(inplace=True) 
    ...:         nn.Dropout(inplace=True) 
    ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
    ...:     def forward(self, x): 
    ...:         x = F.relu(self.conv1(x)) 
    ...:         return x 
    ...:                                                                                                                                                                                                            

In [29]: rea = realex()                                                                                                                                                                                             

In [30]: rea                                                                                                                                                                                                        
Out[30]: 
realex(
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)

In [31]: class realex(nn.Module): 
    ...:     def __init__(self): 
    ...:         super().__init__() 
    ...:         self.conv1 = nn.Conv1d(1, 20, 5) 
    ...:         self.relu = nn.ReLU(inplace=True) 
    ...:         nn.Dropout(inplace=True) 
    ...:         self.conv2 = nn.Conv2d(20, 20, 6) 
    ...:     def forward(self, x): 
    ...:         x = F.relu(self.conv1(x)) 
    ...:         return x 
    ...:                                                                                                                                                                                                            

In [32]: rea = realex()                                                                                                                                                                                             

In [33]: rea                                                                                                                                                                                                        
Out[33]: 
realex(
  (conv1): Conv1d(1, 20, kernel_size=(5,), stride=(1,))
  (relu): ReLU(inplace)
  (conv2): Conv2d(20, 20, kernel_size=(6, 6), stride=(1, 1))
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值