利用自編碼器對線性模型參數加密

from sklearn.datasets import load_boston

 

dataset = load_boston()

x_data = dataset.data 

y_data = dataset.target

 

from sklearn import linear_model

 

model = linear_model.LinearRegression()

model.fit(x_data, y_data)

 

args = model.coef_



 

import torch

from torch import nn, optim

 

class autoencoder(nn.Module):

    def __init__(self):

        super(autoencoder, self).__init__()

        self.encoder = nn.Sequential(nn.Linear(1, 16),

                                     nn.ReLU(True),

                                     nn.Linear(16, 32),

                                     nn.ReLU(True),

                                     nn.Linear(32, 16),

                                     nn.ReLU(True),

                                     nn.Linear(16, 4))

        self.decoder = nn.Sequential(nn.Linear(4, 16),

                                     nn.ReLU(True),

                                     nn.Linear(16, 32),

                                     nn.ReLU(True),

                                     nn.Linear(32, 16),

                                     nn.ReLU(True),

                                     nn.Linear(16, 1))

    def forward(self, x):

        encode = self.encoder(x)

        decode = self.decoder(encode)

        return encode, decode



 

lr = 1e-4

epoches = 3000

model = autoencoder()

 

criterion = nn.MSELoss()

optimizier = optim.Adam(model.parameters(), lr=lr)

 

X = torch.tensor(args).unsqueeze(0).float().reshape(-1, 1)

 

print(X.shape)

for i in range(epoches):

    _, output = model(X)

    loss = criterion(output, X)

    # backward

    optimizier.zero_grad()

    loss.backward()

    optimizier.step()

    print(loss)


 

print(X.reshape(-1, 1))

ciphertext, output = model(X)

print(output.reshape(-1, 1))




 

# 將權重實數值變爲 高緯度向量。

print(ciphertext)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章