降维自动编码器Pytorch

x6yk4ghg  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(181)

我正在尝试使用自动编码器,您可以看到下面的代码作为降维工具,我想知道我如何“提取”隐藏层,并将其用于我的目的
我的原始数据集采用标准缩放
在这里,我定义了一个字典来集中值

CONFIG = {
        'BATCH_SIZE' : 1024,
        'LR' : 1e-4,
        'WD' : 1e-8,
        'EPOCHS': 50
        }

这里我将训练和测试 Dataframe 的值转换为Tensor

t_test = torch.FloatTensor(test.values)
t_train = torch.FloatTensor(train.values)

在这里,我创建了数据加载器

loader_test = torch.utils.data.DataLoader(dataset = t_test,
                                 batch_size = CONFIG['BATCH_SIZE'],
                                     shuffle = True)

loader_train = torch.utils.data.DataLoader(dataset = t_train,
                                     batch_size = CONFIG['BATCH_SIZE'],
                                     shuffle = True)

在这里,我创建类AutoEncoder(AE)

class AE(torch.nn.Module):
    def __init__(self):
        super().__init__()

        self.encoder = torch.nn.Sequential(
            torch.nn.Linear(31,16),
            torch.nn.ReLU(),
            torch.nn.Linear(16, 8),
            torch.nn.ReLU(),
            torch.nn.Linear(8, 4),
        )

        self.decoder = torch.nn.Sequential(  
            torch.nn.Linear(4, 8),
            torch.nn.ReLU(),
            torch.nn.Linear(8, 16),
            torch.nn.ReLU(),
            torch.nn.Linear(16, 31),

        )

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded

这里我定义了模型loss_funcion和优化器

model = AE()

loss_function = torch.nn.MSELoss()

optimizer = torch.optim.Adam(model.parameters(),
                             lr = CONFIG['LR'],
                             weight_decay = CONFIG['WD'])

这里我计算算法

epochs = CONFIG['EPOCHS']
dict_list = []
for epoch in range(epochs):
    for (ix, batch) in enumerate(loader_train):

        model.train()
        reconstructed = model(batch)

        loss = loss_function(reconstructed, batch)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        temp_dict = {'Epoch':epoch,'Batch_N':ix,'Batch_L':batch.shape[0],'loss':loss.detach().numpy()}
        dict_list.append(temp_dict)

df_learning_o = pd.DataFrame(dict_list)
hgqdbh6s

hgqdbh6s1#

您不仅可以返回解码的输出,还可以返回编码的嵌入层,如下所示:

class AE(torch.nn.Module):
    def __init__(self):
        super().__init__()

        self.encoder = torch.nn.Sequential(
            torch.nn.Linear(31,16),
            torch.nn.ReLU(),
            torch.nn.Linear(16, 8),
            torch.nn.ReLU(),
            torch.nn.Linear(8, 4),
        )

        self.decoder = torch.nn.Sequential(  
            torch.nn.Linear(4, 8),
            torch.nn.ReLU(),
            torch.nn.Linear(8, 16),
            torch.nn.ReLU(),
            torch.nn.Linear(16, 31),

        )

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return encoded, decoded

当您将某些内容传递给模型时(例如,在列车循环中),您必须将其更改为以下内容:

encoded, reconstructed = model(batch)

现在你可以对编码嵌入做任何你想做的事情,也就是说,它是维数减少的输入。

相关问题