pytroch学习(三)

 


In [2]: import torch                                                               

In [3]:  a = torch.rand(3,4)                                                       

In [4]: a                                                                          
Out[4]: 
tensor([[0.5908, 0.6273, 0.3101, 0.2421],
        [0.3320, 0.3958, 0.2769, 0.0974],
        [0.9102, 0.2624, 0.0901, 0.6660]])

In [5]: b = torch.rand(4)                                                          

In [6]: b                                                                          
Out[6]: tensor([0.3801, 0.2355, 0.5266, 0.1033])

In [7]: a+b                                                                        
Out[7]: 
tensor([[0.9709, 0.8628, 0.8368, 0.3454],
        [0.7122, 0.6312, 0.8036, 0.2007],
        [1.2903, 0.4979, 0.6168, 0.7693]])

In [8]: #  这里会加入broadingcast机制,扩充之后再相加                              

In [9]: torch.add(a,b)                                                             
Out[9]: 
tensor([[0.9709, 0.8628, 0.8368, 0.3454],
        [0.7122, 0.6312, 0.8036, 0.2007],
        [1.2903, 0.4979, 0.6168, 0.7693]])

In [10]: torch.all(torch.eq(a-b,torch.sub(a,b)))                                   
Out[10]: tensor(1, dtype=torch.uint8)

In [11]: torch.all(torch.eq(a*b,torch.mul(a.b)))                                   
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-d1d0c6693472> in <module>
----> 1 torch.all(torch.eq(a*b,torch.mul(a.b)))

AttributeError: 'Tensor' object has no attribute 'b'

In [12]: torch.all(torch.eq(a*b,torch.mul(a,b)))                                   
Out[12]: tensor(1, dtype=torch.uint8)

In [13]: torch.all(torch.eq(a/b,torch.div(a,b)))                                   
Out[13]: tensor(1, dtype=torch.uint8)

In [14]:  # An example                                                             

In [15]: a = torch.rand(4,784)                                                     

In [16]: x = torch.rand(4,784)                                                     

In [17]: w = torch.rand(512,784)                                                   

In [18]: (z@w.t()).shape                                                           
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-b7189967acea> in <module>
----> 1 (z@w.t()).shape

NameError: name 'z' is not defined

In [19]: (x@w.t()).shape                                                           
Out[19]: torch.Size([4, 512])

In [20]:  # @ 矩阵相乘                                                             

In [21]: w = torch.rand(512,784)   # 512 ch-out 784-ch-in 所以要将w转置相乘        

In [22]:                                                                           

In [22]: a = torch.rand(4,3,28,64)                                                 

In [23]: b = torch.rand(4,3,64,32)                                                 

In [24]: torch.mm(a,b).shape                                                       
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-24-18c616c4b668> in <module>
----> 1 torch.mm(a,b).shape

RuntimeError: matrices expected, got 4D, 4D tensors at /pytorch/aten/src/TH/generic/THTensorMath.cpp:935

In [25]: torch.matmul(a,b).shape                                                   
Out[25]: torch.Size([4, 3, 28, 32])

In [26]: b= torch.rand(4,1,64,32)                                                  

In [27]: torch.matmul(a,b).shape                                                   
Out[27]: torch.Size([4, 3, 28, 32])

In [28]: a = torch.full([2,2],3)                                                   

In [29]: a.pow(2)                                                                  
Out[29]: 
tensor([[9., 9.],
        [9., 9.]])

In [30]: a**2                                                                      
Out[30]: 
tensor([[9., 9.],
        [9., 9.]])

In [31]: aa = a**2                                                                 

In [32]: aa.sqrt()                                                                 
Out[32]: 
tensor([[3., 3.],
        [3., 3.]])

In [33]: aa.sqrt()                                                                 
Out[33]: 
tensor([[3., 3.],
        [3., 3.]])

In [34]: aa.rsqrt()                                                                
Out[34]: 
tensor([[0.3333, 0.3333],
        [0.3333, 0.3333]])

In [35]: aa*0.5                                                                    
Out[35]: 
tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])

In [36]: aa**0.5                                                                   
Out[36]: 
tensor([[3., 3.],
        [3., 3.]])

In [37]: #  calmp  裁剪                                                            

In [38]: # gradientcliping                                                         

In [39]: # (民)                                                                  

In [40]: # (min)                                                                   

In [41]: #(min,max)                                                                

In [42]: grad = torch.rand(2,3)*15                                                 


TypeError: max expected 1 arguments, got 0

In [44]: grad.max()                                                                
Out[44]: tensor(13.1476)

In [45]: grad.median(0 
    ...: )                                                                         
Out[45]: (tensor([6.8900, 2.1205, 2.4496]), tensor([0, 0, 0]))

In [46]: grad.median()                                                             
Out[46]: tensor(6.8900)

In [47]: grad.calmp(10)                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-eda0edebd5e3> in <module>
----> 1 grad.calmp(10)

AttributeError: 'Tensor' object has no attribute 'calmp'

In [48]: grad.clamp(10)                                                            
Out[48]: 
tensor([[10.0000, 10.0000, 10.0000],
        [10.0000, 13.1476, 10.0000]])

In [50]: # norm 表示的是范数,而不是标准化                                         

In [51]:                                                                           

In [51]:                                                                           

In [51]:                                                                           

In [51]: # 范数                                                                    

In [52]:                                                                           

In [52]: a = torch.full([8],1)                                                     

In [53]: b = a.view(2,4)                                                           

In [54]: c = a.view(2,2,2)                                                         

In [55]: b                                                                         
Out[55]: 
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.]])

In [56]: c                                                                         
Out[56]: 
tensor([[[1., 1.],
         [1., 1.]],

        [[1., 1.],
         [1., 1.]]])

In [57]: a.norm(1),b.norm(1),c.norm(1)                                             
Out[57]: (tensor(8.), tensor(8.), tensor(8.))

In [58]: a.norm(2),b.norm(2),c.norm(2)                                             
Out[58]: (tensor(2.8284), tensor(2.8284), tensor(2.8284))

In [59]: b.norm(1,dim=1)                                                           
Out[59]: tensor([4., 4.])

In [60]: b.norm(1,dim=0)                                                           
Out[60]: tensor([2., 2., 2., 2.])

In [61]: # dim =1 在行上, dim=0 列                                                

In [62]: a = torch.arange(8).view(2,4).float()                                     

In [63]: a                                                                         
Out[63]: 
tensor([[0., 1., 2., 3.],
        [4., 5., 6., 7.]])

In [64]: a.min(),a.max(),a.mean(),a.prod()                                         
Out[64]: (tensor(0.), tensor(7.), tensor(3.5000), tensor(0.))

In [65]: a.sum()                                                                   
Out[65]: tensor(28.)

In [66]: a.argmax(),a.argmin()#得到最大最小值的索引                                
Out[66]: (tensor(7), tensor(0))

In [67]: a.argmax(dim=1)                                                           
Out[67]: tensor([3, 3])

In [68]: keepdim                                                                   
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-68-2fec37f5e6ed> in <module>
----> 1 keepdim

NameError: name 'keepdim' is not defined

In [69]: #keepdim                                                                  

In [70]: # keepdim 使原来的dim和与原来的维度一样                                   

In [71]: # top -k or k-th                                                          

In [72]: # 找出前几个,并且返回                                                    

In [73]: # a.kthvalue98,dim=1) # 返回指定第几小的数机器索引                       

In [74]:  a                                                                        
Out[74]: 
tensor([[0., 1., 2., 3.],
        [4., 5., 6., 7.]])

In [75]: b                                                                         
Out[75]: 
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.]])

In [76]: b = a                                                                     

In [77]: torch.equal(a,b)                                                          
Out[77]: True

In [78]: # Where 根据条件的选取                                                    

In [79]: cond                                                                      
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-79-b6564e36deb2> in <module>
----> 1 cond

NameError: name 'cond' is not defined

In [80]: torch.rand(2,2)                                                           
Out[80]: 
tensor([[0.8516, 0.6694],
        [0.2280, 0.7641]])

In [81]: a = torch.rand(2,2)                                                       

In [82]: cond = torch.rand(2,2)                                                    

In [83]: cond                                                                      
Out[83]: 
tensor([[0.0257, 0.9165],
        [0.7928, 0.5730]])

In [84]: a = torch.full([2,2],0)                                                   

In [85]: a                                                                         
Out[85]: 
tensor([[0., 0.],
        [0., 0.]])

In [86]: b = torch.full([2,2],1)                                                   

In [87]: b                                                                         
Out[87]: 
tensor([[1., 1.],
        [1., 1.]])

In [88]: torch.where(cond>0.5,a,b)                                                 
Out[88]: 
tensor([[1., 0.],
        [0., 0.]])

In [89]: # gather                                                                  

In [90]:  # 收集                                                                   

In [91]:                                                                           
Socket error Event: 32 Error: 10053.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(实验室) at 12:17:28.

In [2]: prob = torch.randn(4,10)                                                
  File "<ipython-input-2-e00c5bbb00d1>", line 1
    prob = torch.randn(4,10)
                           ^
SyntaxError: invalid character in identifier


In [3]: prob = torch.randn(4,10)                                                  
  File "<ipython-input-3-20bed8c82b51>", line 1
    prob = torch.randn(4,10)
                          ^
SyntaxError: invalid character in identifier


In [4]: prob = torch.randn(4,10)                                                   

In [5]: idx = prob.topk(dim=1,k=3)                                                 

In [6]: idx                                                                        
Out[6]: 
(tensor([[0.4642, 0.3793, 0.3367],
         [2.7668, 1.2775, 1.1597],
         [1.3556, 0.9241, 0.5383],
         [1.5087, 1.2672, 1.1772]]), tensor([[3, 2, 6],
         [7, 4, 2],
         [1, 3, 8],
         [4, 3, 0]]))

In [7]: idx=idx[1]                                                                 

In [8]: idx                                                                        
Out[8]: 
tensor([[3, 2, 6],
        [7, 4, 2],
        [1, 3, 8],
        [4, 3, 0]])

In [9]: label = torch.arange(10)+100                                               

In [10]: torch.gather(label.expand(4,10),dim=1,index = idx.long())                
  File "<ipython-input-10-c4765c145cd0>", line 1
    torch.gather(label.expand(4,10),dim=1,index = idx.long())
                                 ^
SyntaxError: invalid character in identifier


In [11]: torch.gather(label.expand(4,10),dim=1,index = idx.long())                 
Out[11]: 
tensor([[103, 102, 106],
        [107, 104, 102],
        [101, 103, 108],
        [104, 103, 100]])

In [12]: label                                                                     
Out[12]: tensor([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])

In [13]: a = torch.linspace(1-100,100,10)                                          

In [14]: a                                                                         
Out[14]: 
tensor([-99.0000, -76.8889, -54.7778, -32.6667, -10.5556,  11.5556,  33.6667,
         55.7778,  77.8889, 100.0000])

In [15]: a = torch.linspace(-100,100,10)                                           

In [16]: a                                                                         
Out[16]: 
tensor([-100.0000,  -77.7778,  -55.5556,  -33.3333,  -11.1111,   11.1111,
          33.3333,   55.5555,   77.7778,  100.0000])

In [17]: torch.sigmoid(a)                                                          
Out[17]: 
tensor([0.0000e+00, 1.6655e-34, 7.4564e-25, 3.3382e-15, 1.4945e-05, 9.9999e-01,
        1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00])

In [18]:     


In [1]: import torch

In [2]: out = torch.randn([1,16,14,14])

In [3]: out
Out[3]:
tensor([[[[ 0.0856, -0.0232,  0.5058,  ...,  0.6548,  0.1804, -0.6117],
          [-0.4951,  0.6891, -0.0491,  ..., -0.7344, -1.7007,  1.2367],
          [ 0.4483,  0.3059,  1.2524,  ..., -0.5353, -0.8612, -0.4458],
          ...,
        ]]])

In [4]: out.size()
Out[4]: torch.Size([1, 16, 14, 14])

In [5]: x = out

In [6]: layer = torch.nn.MaxPool2d(2,stride=2)

In [7]: out = layer(x)

In [8]: out.size()
Out[8]: torch.Size([1, 16, 7, 7])

In [9]: import torch.nn.functional as F

In [10]: out = F.avg_pool2d(x,2,stride=2)

In [11]: out.size()
Out[11]: torch.Size([1, 16, 7, 7])

In [12]: x = out

InIn [13]: out = F.interpolate(x,scale_factor=2,mode'nearest') # unsample 最近邻插值,也可以使用双线性插值
  File "<ipython-input-13-42ba9a141a29>", line 1
    out = F.interpolate(x,scale_factor=2,mode'nearest') # unsample 最近邻插值,也可以使用双线性插值
                                                     ^
SyntaxError: invalid syntax


In [14]: out = F.interpolate(x,scale_factor=2,mode='nearest') # unsample 最近邻插值,也可以使用双线性插值

In [15]: out.shape
Out[15]: torch.Size([1, 16, 14, 14])

In [16]: out = F.interpolate(x,scale_factor=3,mode='nearest') # unsample 最近邻插值,也可以使用双线性插值

In [17]: out.shape
Out[17]: torch.Size([1, 16, 21, 21])

In [18]: x.shape
Out[18]: torch.Size([1, 16, 7, 7])



In [21]: layer =torch.nn.ReLU(inplace=True)

In [22]: # inplace 减少内存的使用,节省空间

In [23]: out = layer(x)

In [24]: out.shape
Out[24]: torch.Size([1, 16, 7, 7])

In [25]: # batch norm  ch0 ch1 ch2 mean [6 3 784] ->[3,3]

In [26]: # layer norm  ch0 ch1 ch2 mean [6 3 784] ->[6]

In [27]: # batch norm

In [28]: # [6,3,784]

In [29]: x = torch.rand(100,16,784)

In [30]: layer = torch.nn.BatchNorm1d(16)

In [31]: out = layer(x)

In [32]: layer.running_mean
Out[32]:
tensor([0.0501, 0.0500, 0.0500, 0.0500, 0.0500, 0.0500, 0.0500, 0.0499, 0.0501,
        0.0501, 0.0501, 0.0500, 0.0500, 0.0500, 0.0500, 0.0500])

In [33]: layer.running_var
Out[33]:
tensor([0.9084, 0.9083, 0.9083, 0.9083, 0.9083, 0.9083, 0.9083, 0.9083, 0.9083,
        0.9083, 0.9084, 0.9083, 0.9084, 0.9084, 0.9084, 0.9083])

In [34]: x.shape
Out[34]: torch.Size([100, 16, 784])

In [35]: x = torch.randn([1,16,7,7])

In [36]: x.shape
Out[36]: torch.Size([1, 16, 7, 7])

In [37]: layer = torch.nn.BatchNorm2d(16)

In [38]: out = layer(x)

In [39]: out.size()
Out[39]: torch.Size([1, 16, 7, 7])

In [40]: layer.weight
Out[40]:
Parameter containing:
tensor([0.9622, 0.5374, 0.0605, 0.1296, 0.1465, 0.2552, 0.6615, 0.1194, 0.8710,
        0.4916, 0.5754, 0.8713, 0.5680, 0.4618, 0.1996, 0.7925],
       requires_grad=True)

In [41]: layer.weight.shape
Out[41]: torch.Size([16])

In [42]: layer.bias.shape
Out[42]: torch.Size([16])

In [43]: vars(layer)
Out[43]:
{'_backend': <torch.nn.backends.thnn.THNNFunctionBackend at 0x2349a550470>,
 '_backward_hooks': OrderedDict(),
 '_buffers': OrderedDict([('running_mean',
               tensor([-0.0102,  0.0032,  0.0029,  0.0237, -0.0144, -0.0157, -0.0044, -0.0006,
                       -0.0250, -0.0217, -0.0159, -0.0200, -0.0044, -0.0061,  0.0083, -0.0111])),
              ('running_var',
               tensor([1.0117, 1.0146, 1.0199, 0.9865, 1.0384, 1.0006, 0.9928, 0.9919, 0.9904,
                       1.0052, 0.9692, 0.9994, 0.9813, 0.9970, 0.9995, 0.9665])),
              ('num_batches_tracked', tensor(1))]),
 '_forward_hooks': OrderedDict(),
 '_forward_pre_hooks': OrderedDict(),
 '_load_state_dict_pre_hooks': OrderedDict(),
 '_modules': OrderedDict(),
 '_parameters': OrderedDict([('weight', Parameter containing:
               tensor([0.9622, 0.5374, 0.0605, 0.1296, 0.1465, 0.2552, 0.6615, 0.1194, 0.8710,
                       0.4916, 0.5754, 0.8713, 0.5680, 0.4618, 0.1996, 0.7925],
                      requires_grad=True)), ('bias', Parameter containing:
               tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                      requires_grad=True))]),
 '_state_dict_hooks': OrderedDict(),
 'affine': True,
 'eps': 1e-05,
 'momentum': 0.1,
 'num_features': 16,
 'track_running_stats': True,
 'training': True}

In [44]: # test 将方差和均值  runing 代替

In [45]: layer.eval()
Out[45]: BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)

In [46]: out = layer(x)

In [47]: out.size
Out[47]: <function Tensor.size>

In [48]: device = torch.device('cuda')

In [49]: net =Net()

In [50]: net.to(device)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值