@themoonboy


We will have to remove the embedding parameter in the Decoder init and accept it as part of the Decoder forward function


We will have to get the input indices in the Decoder Forward as well so that we know which embedding value to use for the processing


import numpy as np
import pandas as pd
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable

class Encoder(nn.Module):
    def __init__(self, num_indices):
        super(Encoder, self).__init__()
        self.embedding = nn.Embedding(num_indices, 100) 
        self.l1 = nn.Linear(200+100, 64)
        self.l2 = nn.Linear(64, 32)
        self.b1 = nn.BatchNorm1d(64)
        self.b2 = nn.BatchNorm1d(32)
        
    def forward(self, x, indices):
        cur_embedding = self.embedding(indices)
        x = torch.hstack((x, cur_embedding))
        x = F.relu(self.b1(self.l1(x)))
        x = self.b2(self.l2(x))
        return x, self.embedding
        

class Decoder(nn.Module):
    def __init__(self):
        super(Decoder, self).__init__()
        self.l1 = nn.Linear(100 + 32, 200)
        self.b1 = nn.BatchNorm1d(200)
        
    def forward(self, x, embedding, indices):
        self.embedding_B = embedding(indices)
        x= torch.hstack((x, self.embedding_B))
        x = self.b1(self.l1(x))
        return x
    
num_indices = 100
a = pd.DataFrame(np.random.randint(num_indices, size=(1000)), columns=['index'])
a['vector'] = np.random.rand(1000, 200).tolist()

encoder = Encoder(num_indices)
decoder = Decoder()

print("Models compiled")


loss = torch.nn.MSELoss()
optimizer = optim.Adam(list(encoder.parameters()) + list(decoder.parameters()), lr=0.001)

X = torch.FloatTensor(a['vector'].values.tolist())
indices = torch.LongTensor(a['index'].values.tolist())

for cur_epoch in range(10):
    optimizer.zero_grad()
    encoder_output, embeddings = encoder(X, indices)
    decoder_output = decoder(encoder_output, embeddings, indices)
    cur_loss = loss(decoder_output, X)
    cur_loss.backward()
    optimizer.step()
    print("Epoch {0} Loss is {1}".format(cur_epoch, cur_loss.item()))