简体   繁体   English

如何将嵌入矩阵转换为 torch.Tensor

[英]How to convert embedding matrix to torch.Tensor

I am new to pytorch and not sure how to convert an embedding matrix to a torch.Tensor type我是 pytorch 的新手,不确定如何将嵌入矩阵转换为torch.Tensor类型

I have 240 rows of input text data that I convert to embedding using Sentence Transformer library like below我有 240 行输入文本数据,我使用如下所示的Sentence Transformer库将其转换为嵌入

embedding_model = SentenceTransformer('bert-base-nli-mean-tokens')
features = embedding_model.encode(df.features.values)

Now this features is a numpy.ndarray of shape (240, 768)现在这个features是一个numpy.ndarray形状(240, 768)

I have defined the model as我已经将模型定义为

class NClassifier(nn.Module):

    def __init__(self, input_dim, embedding_dim, hidden_dim, tagset_size):
        super(NClassifier, self).__init__()
        self.hidden_dim = hidden_dim

        self.word_embeddings = nn.Embedding(input_dim, embedding_dim)

        # The LSTM takes word embeddings as inputs, and outputs hidden states
        # with dimensionality hidden_dim.
        self.lstm = nn.LSTM(embedding_dim, hidden_dim)

        # The linear layer that maps from hidden state space to code space (output clases)
        self.hidden2code = nn.Linear(hidden_dim, tagset_size)

    def forward(self, features):
        embeds = self.word_embeddings(features)
        lstm_out, _ = self.lstm(embeds.view(len(features), 1, -1))
        code_space = self.hidden2code(lstm_out.view(len(features), -1))
        code_scores = F.log_softmax(code_space, dim=1)
        return code_scores


INPUT_DIM = 240
EMBEDDING_DIM = 768
HIDDEN_DIM = 256
OUTPUT_DIM = 34

model = NClassifier(INPUT_DIM, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM)

Now when I do scores = model(features) I get error as features is NOT a tensor.现在,当我执行scores = model(features)我收到错误,因为features不是张量。 I see the example of converting the input to tensor here but it is not clear to me.我在这里看到了将输入转换为张量的示例,但我不清楚。

Can anyone please help?有人可以帮忙吗?

A numpy.ndarray can be converted to a torch.Tensor with the torch.tensor() function, like this:numpy.ndarray可以转换为一个torch.Tensortorch.tensor()函数,如下所示:

features_tensor = torch.tensor(features)

Does that work for you?那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM