在 Pytorch 中, 用預訓練好的word2vec權值文件(.bin)來初始化卷積層的權重的方法
import torch
import torch.nn as nn
from gensim import models
model = models.KeyedVectors.load_word2vec_format('./file_name.bin', binary=True)
weight = torch.FloatTensor(model.vectors)
# 前提是file_name.bin中包含的權重的維度是[11352, 300, 1, 1]
conv1 = nn.Conv2d(in_channels=11352, out_channels=300, kernel_size=1, stride=1, padding=0, bias=True)
conv1.weight = torch.nn.Parameter(weight)