Bootstrap

python切片记录

import numpy as np
import torch
from torch.autograd import Variable 

import numpy as np
import torch
from torch.autograd import Variable 

x = Variable(torch.rand(5,5),requires_grad = False)
print(x)
print(x[:,0:1])
print(x[:,4:5])
print(x[:,4])
print(x[:,[-1]].shape)
'''
tensor([[0.0896, 0.1292, 0.2337, 0.8749, 0.7668],
        [0.7987, 0.5104, 0.9536, 0.2373, 0.1417],
        [0.6509, 0.8352, 0.9698, 0.4681, 0.8400],
        [0.2130, 0.5627, 0.5978, 0.9548, 0.8376],
        [0.9211, 0.0043, 0.6379, 0.0815, 0.1777]])
tensor([[0.0896],
        [0.7987],
        [0.6509],
        [0.2130],
        [0.9211]])
tensor([[0.7668],
        [0.1417],
        [0.8400],
        [0.8376],
        [0.1777]])
tensor([0.7668, 0.1417, 0.8400, 0.8376, 0.1777])
torch.Size([5, 1])
'''
;