今天简单实现了一下tied weight autoencoder,但是效果不地,不知道是什么原因,记录一下
emb_dim=2
import os
#os.chdir("/DATA1/zhangjingxiao/yxk/center_loss/pytorch-center-loss-master")
import sys
import argparse
import datetime
import time
import os.path as osp
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import random
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
import argparse
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt
import h5py
import numpy as np
class AutoEncoder(nn.Module):
def __init__(self,input_dim,hidden_dim,embed_dim):
super(AutoEncoder, self).__init__()
self.weight1 = nn.Parameter(torch.randn(input_dim, hidden_dim))
self.weight2 = nn.Parameter(torch.randn(hidden_dim, embed_dim))
def encoder(self,x):
x=F.linear(x,self.weight1.T)
x=F.relu(x)
x=F.linear(x,self.weight2.T)
return x
def decoder(self,x):
x=F.linear(x,self.weight2)
x=F.relu(x)
x=F.linear(x,self.weight1)
return x
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded,encoded
# print(en)
# x = torch.randn(1, 10)
# out,emb = en(x)
# print(out.shape)
hf = h5py.File("/data/wangdongxue/yxk/dataset/USPS/data.h5", 'r')
X_train= np.asarray(hf.get('data'), dtype='float32')
y_train = np.asarray(hf.get('labels'), dtype='int32')
X_train=X_train.reshape((11000,256))
X_train=X_train/255.0
train_set = torch.utils.data.TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train))
batch_size = 128
train_loader = DataLoader(train_set, batch_size=batch_size, num_workers=0,shuffle=True)
en = AutoEncoder(256,64,2)
print(en)
EPOCH=100
optimizer = optim.Adam(en.parameters(),lr=0.001)
loss_func = nn.MSELoss()
train_loss=[]
for epoch in range(EPOCH):
for step,(x,y) in enumerate(train_loader):
b_x = Variable(x)
decoded,encoded = en(b_x)
loss = loss_func(decoded,b_x)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss.append(loss.item())
if step % 100 ==0:
print('Epoch:',epoch,'|train loss:%.4f'%loss.item())
train_loss=np.array(train_loss)
fig=plt.figure(figsize=(18,12))
plt.plot(range(len(train_loss)),train_loss)
plt.show()
train_all=torch.FloatTensor(X_train)
en.eval()
with torch.no_grad():
decoded,encoded = en(train_all)
embeddings=encoded.data.numpy()
target=y_train
fig=plt.figure(figsize=(20,12))
for label in np.unique(target):
plt.scatter(embeddings[label==target,0], embeddings[label==target,1],label=label)
plt.legend(loc="upper left")
plt.show()
结果如下
emb_dim=32
%matplotlib inline
import os
#os.chdir("/DATA1/zhangjingxiao/yxk/center_loss/pytorch-center-loss-master")
import sys
import argparse
import datetime
import time
import os.path as osp
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import random
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
import argparse
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt
import h5py
import numpy as np
class AutoEncoder(nn.Module):
def __init__(self,input_dim,hidden_dim,embed_dim):
super(AutoEncoder, self).__init__()
self.weight1 = nn.Parameter(torch.randn(input_dim, hidden_dim))
self.weight2 = nn.Parameter(torch.randn(hidden_dim, embed_dim))
def encoder(self,x):
x=F.linear(x,self.weight1.T)
x=F.relu(x)
x=F.linear(x,self.weight2.T)
return x
def decoder(self,x):
x=F.linear(x,self.weight2)
x=F.relu(x)
x=F.linear(x,self.weight1)
return x
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded,encoded
# print(en)
# x = torch.randn(1, 10)
# out,emb = en(x)
# print(out.shape)
hf = h5py.File("/data/wangdongxue/yxk/dataset/USPS/data.h5", 'r')
X_train= np.asarray(hf.get('data'), dtype='float32')
y_train = np.asarray(hf.get('labels'), dtype='int32')
X_train=X_train.reshape((11000,256))
X_train=X_train/255.0
train_set = torch.utils.data.TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train))
batch_size = 128
train_loader = DataLoader(train_set, batch_size=batch_size, num_workers=0,shuffle=True)
en = AutoEncoder(256,64,32)
print(en)
EPOCH=50
optimizer = optim.Adam(en.parameters(),lr=0.001)
loss_func = nn.MSELoss()
train_loss=[]
for epoch in range(EPOCH):
for step,(x,y) in enumerate(train_loader):
b_x = Variable(x)
decoded,encoded = en(b_x)
loss = loss_func(decoded,b_x)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss.append(loss.item())
if step % 100 ==0:
print('Epoch:',epoch,'|train loss:%.4f'%loss.item())
train_loss=np.array(train_loss)
fig=plt.figure(figsize=(18,12))
plt.plot(range(len(train_loss)),train_loss)
plt.show()
train_all=torch.FloatTensor(X_train)
en.eval()
with torch.no_grad():
decoded,encoded = en(train_all)
print(encoded.data.numpy().shape)
# embeddings=encoded.data.numpy()
# target=y_train
import umap
from sklearn.manifold import TSNE
target=y_train
reducer=umap.UMAP(random_state=0)
data_umap=reducer.fit_transform(encoded.data.numpy())
fig=plt.figure(figsize=(18,12))
for label in np.unique(target):
plt.scatter(data_umap[label==target,0], data_umap[label==target,1],label=label)
plt.legend(loc="best")
plt.show()
结果如下