PyTorch/DeepLearning 杂记

1.softmax运算:
\(\hat{y_1}, \hat{y_2}, \hat{y_3}=softmax(o_1,o_2,o_3)\)
其中:
\(\hat{y_1}=\dfrac{exp(o_1)}{\sum_{i=1}^{3}exp(o_i)}, \hat{y_2}=\dfrac{exp(o_2)}{\sum_{i=1}^{3}exp(o_i)}, \hat{y_3}=\dfrac{exp(o_3)}{\sum_{i=1}^{3}exp(o_i)}\)

Pytorch中对多维`Tensor`沿某一维度求和的操作:
    """
    tensor矩阵X:
    1 2 3
    4 5 6
    """
    X = torch.tensor([[1,2,3],[4,5,6]])
    X.sum(dim=0, keepdim=True)  #dim=0, 对同一列元素求和
    X.sum(dim=1, keepdim=True)  #dim=1, 对同一行元素求和
                            #keepdim=True表示求和后仍保留这两个维度

2.交叉熵损失函数:
在softmax完成多分类的情况下,我们得到的输出通常是所有类别的概率分布,我们的训练目标是使正确类别的概率最大化,只要目标类别的概率大于其他类别的概率就可以了。如果使用平方损失函数,计算出来的损失过于严格(把其他类别的概率也算了进去),所以这里使用更加适合衡量两个概率分布差异的测量函数,即交叉熵:
\(H(y^{(i)},\hat{y}^{(i)})=-\sum_{j=1}^{q}y_j^{(i)}log\hat{y}_{j}^{(i)}\)
Pytorch实现交叉熵损失函数:

    def cross_entropy(y_hat, y):
        return -torch.log(y_hat.gather(1, y.view(-1,1)))
`torch.gather()`函数的理解:
利用传入的`index`来取出`input`中的某些值

```python 
torch.gather(input, dim, index, out=None)
input (Tensor) – The source tensor
dim (int) – The axis along which to index
index (LongTensor) – The indices of elements to gather
out (Tensor, optional) – Destination tensor
输入一个Tensor,如
[ 1 2 3
  4 5 6]
dim是沿某一维度进行索引, 如dim=1索引就是横向,即列号
index是索引,输出和index的形状是一样的, 假如index是
[ 0 1
  2 2]
那么输出就是
[ 1 2
  6 6]
```

3.Pytorch中的item()方法:
torch.tensor.item():从只有一个值的Tensor中获得一个Python数值

posted @ 2019-11-12 21:13  patrolli  阅读(200)  评论(0编辑  收藏  举报