特征解耦,torch.cumprod(),np.random.seed(),plt.scatter

1.infoGAN
通常,我们学到的特征是混杂在一起的,如上图所示,这些特征在数据空间中以一种复杂的无序的方式进行编码,但是如果这些特征是可分解的,那么这些特征将具有更强的可解释性,我们将更容易的利用这些特征进行编码。所以,我们将如何通过非监督的学习方式获取这些可分解的特征呢?
前人也通过很多监督非监督的方法学习可分解的特征。在这篇paper中,非监督学习通过使用连续的和离散的隐含因子来学习可分解的特征。
2.特征解耦
实际情况中的特征是非常杂乱无章的,然后我们希望的特征关系是比较整齐明了的,具体哪一列表示什么很清晰,从而便于控制它。而infogan的目的就是将这些杂乱无章的特征清晰化规律化。
特征解耦举例:
我们可以找到某一个控制某个特征对应的神经元,然后去改变它的值进而就可以改变具体某个特征。
3.x.detach()摘自Pytorch中x.data()与x.detach()的区别
阻断梯度回传.
x.data()或x.detach()均会返回与x相同数据的Tensor,并且这个Tensor与原来的Tensor共用内存,一者改变,另一者也会跟着改变,并且新的tensor的requires_grad = False
实例:

class TestDetach(nn.Module):
    def __init__(self, InDim, HiddenDim, OutDim):
        super().__init__()
        self.layer1 = nn.Linear(InDim, HiddenDim, False)
        self.layer2 = nn.Linear(HiddenDim, OutDim, False)

    def forward(self, x, DetachLayer1):
        x = torch.relu(self.layer1(x))
        x = x.detach()
        # x = x.data()
        x = self.layer2(x)
        return x

两层线性层,第一层的输出后detach,那么第一层的参数永远不会更新
4.torch.cumprod()
cumulative product的意思,即累积乘
实例:

import torch
x = torch.Tensor([1, 2, 3, 4, 5])
y = torch.cumprod(x, dim = 0)
print(y)

tensor([ 1., 2., 6., 24., 120.])

5np.random.seed(0)可以产生相同的随机数。
这是一个没有返回值的函数,用来初始化随机数函数。seed()括号里面可以加入一个参数,这个参数会是生成随机数的依据,如果不添加参数的话,依据是系统时间,如果参数不改变,那么随机数的生成将会一致,方便复现实验。
也就是说如果我们使用seed(x),只要x不改变,那么这个random序列永远不会变。

每次调用都需要seed(0)一下,表示种子相同
实例:

import numpy as np
np.random.seed(0)
x = np.random.randn(2,2)
np.random.seed(0)
y = np.random.randn(2,2)
print(x)
print(y)

[[1.76405235 0.40015721]
[0.97873798 2.2408932 ]]
[[1.76405235 0.40015721]
[0.97873798 2.2408932 ]]

import numpy as np
np.random.seed(1)
x = np.random.randn(2,2)
np.random.seed(0)
y = np.random.randn(2,2)
np.random.seed(1)# z同x
z = np.random.randn(2,2)
print(x)
print(y)
print(z) 

[[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]]
[[1.76405235 0.40015721]
[0.97873798 2.2408932 ]]
[[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]]

6.plt.scatter用法

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
x = np.random.rand(20)#x坐标
y = np.random.rand(20)#y坐标

colors = np.random.rand(20)
area = (50 * np.random.rand(20)) ** 2#面积
print("area",area)

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44040169/article/details/128062406