up:我是土堆
dir() help()
探索工具箱结构,打开工具箱
dir(PyTorch)
加载数据: Dataset Dataloader
- Dataset提供具有编号和label的大量data,有用或无用的都有
- Dataloader为后面的网络提供不同的数据形式
Dataset类
- 获取每一个数据及其label
- 告诉我们总共有多少个数据,方便计算迭代次数,来将整个数据训练完
两种数据集组成形式
- 每个label一个文件夹
- data一个文件夹,label一个文件夹,一一对应
使用 P5 P6
dataset?? 查看说明
重写__getitem__和__lens__
cv2或PIL.Image读取图片
通过pycharm控制台可以查看变量的属性,由类定义的属性
pycharm控制台在定义类时比较有用,方便查看变量属性
some functions
torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
'''Creates a one-dimensional tensor of size steps whose values are evenly spaced from start to end, inclusive.'''
# 就是等分start和end为step个size的一位tensor
torch.unsqueeze(input, dim) → Tensor
'''Returns a new tensor with a dimension of size one inserted at the specified position.'''
# 在dim做为维度索引的地方插入一个1
x = torch.tensor([1, 2, 3, 4]) # (4,)
torch.unsqueeze(x, 1) # (4,1)
>>> tensor([[ 1],
[ 2],
[ 3],
[ 4]])
torch.Tensor.expand(*size) → Tensor
x = torch.tensor([[1], [2], [3]]) #(3,1)
x.expand(3, 4)
# 等效于
x.expand(-1, 4) # -1 means not changing the size of that dimension
>>> tensor([[ 1, 1, 1, 1],
[ 2, 2, 2, 2],
[ 3, 3, 3, 3]])
torch.gather(input, dim, index, *, sparse_grad=False, out=None) → Tensor
'''Gathers values along an axis specified by dim.
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2'''
# 貌似最后会输出为index的size
torch.cat(tensors, dim=0, *, out=None) → Tensor
'''Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.'''
a = torch.randn(12).view([1,3,2,2])
>>> tensor([[[[ 1.2253, -0.0793],
[ 0.7610, -0.2034]],
[[-0.5695, 0.4753],
[-0.7077, -1.3679]],
[[-0.5916, -1.2909],
[ 0.8098, 1.6960]]]])
a[:,:,:,0]
>>> tensor([[[ 1.2253, 0.7610],
[-0.5695, -0.7077],
[-0.5916, 0.8098]]])
a[:,:,:,1]
>>> tensor([[[-0.0793, -0.2034],
[ 0.4753, -1.3679],
[-1.2909, 1.6960]]])
torch.clip(input, min=None, max=None, *, out=None) → Tensor
torch.clamp(input, min=None, max=None, *, out=None) → Tensor
'''Clamps all elements in input into the range [ min, max ].
Letting min_value and max_value be min and max, respectively, this returns:
yi = min(max(xi,min_value_i),max_value_i)'''
# 貌似就是小于min的值会变为min,大于max的值会变为max,其余值不变
a = torch.randn(4)
>>> tensor([-1.7120, 0.1734, -0.0478, -0.0922])
torch.clamp(a, min=-0.5, max=0.5)
>>> tensor([-0.5000, 0.1734, -0.0478, -0.0922])
min = torch.linspace(-1, 1, steps=4)
torch.clamp(a, min=min) # 其实就是应用多个min比较,
>>> tensor([-1.0000, 0.1734, 0.3333, 1.0000])
Tensor.squeeze(dim=None) → Tensor
torch.squeeze(input, dim=None, *, out=None) → Tensor
'''Returns a tensor with all the dimensions of input of size 1 removed.'''
x = torch.zeros(2, 1, 2, 1, 2)
y = torch.squeeze(x)
>>> torch.Size([2, 2, 2])
# 指定dim时,该dim=1才会remove这个dim
y = torch.squeeze(x, 1)
>>> torch.Size([2, 2, 1, 2])
torch.argsort(input, dim=-1, descending=False) → LongTensor
'''Returns the indices that sort a tensor along a given dimension
in ascending order by value.
This is the second value returned by torch.sort().
See its documentation for the exact semantics of this method.'''
a = torch.randn(4, 4)
torch.argsort(a, dim=1)
torch.triu(input, diagonal=0, *, out=None) → Tensor
'''Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.'''
a = torch.randn(3, 3)
torch.triu(a, diagonal=1)
>>> tensor([[ 0.0000, 0.5207, 2.0049],
[ 0.0000, 0.0000, 0.6602],
[ 0.0000, 0.0000, 0.0000]])
torch.triu(a, diagonal=-1)
>>> tensor([[ 0.2309, 0.5207, 2.0049],
[ 0.2072, -1.0680, 0.6602],
[ 0.0000, -0.5211, -0.4573]])
torch.stack(tensors, dim=0, *, out=None) → Tensor
'''Concatenates a sequence of tensors along a new dimension.
All tensors need to be of the same size.'''
torch.cat(tensors, dim=0, *, out=None) → Tensor
'''Concatenates the given sequence of seq tensors in the
given dimension. All tensors must either have the same shape
(except in the concatenating dimension) or be empty.'''
a = [torch.randn(3,3,5) for i in range(8)]
b = torch.stack(a, dim=1]
b.shape
>>> torch.Size([3, 8, 3, 5])
c = torch.cat(a, dim=0)
c.shape
>>>torch.Size([24, 3, 5])
torch.matmul(input, other, *, out=None) → Tensor
'''Matrix product of two tensors.'''
a = torch.matmul(torch.randn(2,4,3), torch.randn(2,3,1))
a.shape
>>> torch.Size([2, 4, 1])
torch.nn.functional.one_hot(tensor, num_classes=- 1) → LongTensor
"""Takes LongTensor with index values of shape (*) and returns
a tensor of shape (*, num_classes) that have zeros everywhere
except where the index of last dimension matches the corresponding
value of the input tensor, in which case it will be 1."""
>>> F.one_hot(torch.arange(0, 5) % 3, num_classes=5)
torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
"""return a 1D arithmetic progression"""
torch.kthvalue(input, k, dim=None, keepdim=False, *, out=None)
"""Returns a namedtuple (values, indices) where values is the k th smallest
element of each row of the input tensor in the given dimension dim.
And indices is the index location of each element found."""
torch.where(condition, input, other, *, out=None) → Tensor
x, y = (torch.randn(3, 3), torch.randn(3, 3))
torch.where(x > 0, y, 0.0)
# return index
torch.where(x<=x.kthvalue(2,keepdim=True)[0])[1].view([-1, 2])
just like the symbol
Tensor.contiguous(memory_format=torch.contiguous_format) → Tensor
'''Returns a contiguous in memory tensor containing the same data
as self tensor. If self tensor is already in the specified memory
format, this function returns the self tensor.'''
torch.sum(dX**2, 3)
torch.sqrt(dX)
torch.topk(input, k, dim=None, largest=True, sorted=True, *, out=None)
some class
torch.nn.NLLLoss(weight=None, size_average=None, ignore_index=- 100, reduce=None, reduction='mean')
'''The negative log likelihood loss. It is useful to train a classification
problem with C classes.'''
m = nn.LogSoftmax(dim=1)
loss_none = nn.NLLLoss(reduction='none')
loss = nn.NLLLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.tensor([1, 0, 4])
loss_none(m(input), target) # select elements as target index shape is [3]
loss(m(input), target) # equal to torch.mean(loss_none(m(input), target))
torch.no_grad
'''Context-manager that disabled gradient calculation.'''
torch.nn.LogSoftmax(dim=None)
m = nn.LogSoftmax(dim=1)
input = torch.randn(3, 5, requires_grad=True)
m(input)
# equal to
in_exp = torch.exp(input)
in_expsum = in_exp.sum(dim=1).unsqueeze(dim=1)
torch.log(in_exp/in_expsum)
torch.nn.parameter.Parameter(data=None, requires_grad=True)
'''A kind of Tensor that is to be considered a module parameter.'''
W = nn.Parameter(torch.randn(1, num_routes, num_capsules, out_channels, in_channels))