create representation of questions using LSTM via a pre-trained word embedding such as GloVe
I am new in LSTM and python. My goal is to represent the sentence using LSTM.
Could u tell me I am doing the right? how to fix the error when running the following code ?
&quot;TypeError: embedding(): argument 'indices' (position 2) must be Tensor, not str&quot;
import torch
import torch.nn as nn
import numpy as np
from torch import optim
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
import torchvision.datasets as datasets  # Standard datasets
import torchvision.transforms as transforms
import json

class RNN_LSTM(nn.Module)
    
    def __init__(self, input_size, hidden_size, num_layers, num_classes, vocab_size,
                 lstm_dropout, device, word_emb_file):
        super(RNN_LSTM, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm_dropout = lstm_dropout
        self.lstm_drop = nn.Dropout(p=self.lstm_dropout)
        self.word_emb_file = word_emb_file
        self.device = device
        
        # initialize text embeddings
        self.word_embeddings = nn.Embedding(vocab_size, input_size)
        self.word_embeddings.weight = nn.Parameter(
                torch.from_numpy(
                    np.pad(np.load(self.word_emb_file), ((0, 1), (0, 0)), 'constant')).type(
                    'torch.FloatTensor'))
        
    
        
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)   
    
    def forward(self, sentence, question_len):
        embeds = self.word_embeddings(sentence)
        packed_output = pack_padded_sequence(embeds, question_len, batch_first=True)
        outputs, (hidden, cell_state) = self.lstm(packed_output)
        outputs, outputs_length = pad_packed_sequence(outputs, batch_first=True)
        outputs = torch.cat([hidden[0,:,:], hidden[1,:,:]], dim=-1)
        return outputs

lstm_dropout = 0.3
input_size = 300
hidden_size = 256
num_classes = 10
num_layers = 1
device = 'cpu'
vocab_size = 2000
word_emb_file = &quot;/home/project/word_emb_300d.npy&quot;

model = RNN_LSTM(input_size, hidden_size, num_layers, num_classes, vocab_size, lstm_dropout, device, word_emb_file)

model.word_embeddings('Blue Skye')


Please see torch embedding tutorial and use embedding with keras for knowledge about word embeddings.
Basically nn.Embedding(vocab_size, embed_size) is a matrix vocab_size*embed_size, where each row corresponds to a word's representation.
In order to know which word correspond to which row you should define a vocabulary that can transform a word into an index (for example a python dictionary {'hello': 0, 'word': 1}).
And before that to transform a sentence into word (in order to compute the vocabulary) you need to tokenize the sentence (using nltk.word_tokenize or str.split() for example).
Though torch.nn.Embedding expects a Tensor, so if you want to process multiple sentence into batches, and the sentence have different length you will need to pad the sentence with empty tokens in order to fit them into a Tensor.
# This is pseudo code

# Preprocess data
documents = ['first sentence', 'the second sentence']
tokenized_documents = [d.split(' ') for d in documents]
# Create vocabulary and add a special token for padding
words = [w for d in documents for word in tokenized_documents]
vocabulary = {w: i+1 for i, w in enumerate(set(words))}
vocabulary['PAD'] = 0
indexed_documents = [[vocabulary[w] for w in d] for d in tokenized_documents]
# indexed_documents will look like : [[0, 1], [2, 3, 1]]
padded_documents = torch.nn.utils.rnn.pad_sequence(
    indexed_documents,
    padding_value=vocabulary['PAD'])

# Data can be fed to the neural network
model.word_embeddings(torch.tensor(padded_documents))

