1 Star 0 Fork 0

焦建军/good_robot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
language_only.py 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
esteng 提交于 2021-03-16 10:16 . pre-pull
import torch
from mlp import MLP
import pdb
class LanguageEncoder(torch.nn.Module):
def __init__(self,
embedder,
encoder,
device):
super(LanguageEncoder, self).__init__()
self.lang_embedder = embedder
self.lang_embedder.device = device
self.lang_encoder = encoder
self.lang_encoder.device = device
self.mlp = MLP(input_dim = encoder.output_size,
hidden_dim = 64,
output_dim = 21,
num_layers = 3,
dropout = 0.20)
self.compute_block_dist = True
self.device = device
def forward(self, data_batch):
#pdb.set_trace()
lang_input = data_batch["command"]
lang_length = data_batch["length"]
# tensorize lengths
lengths = torch.tensor(lang_length).float()
lengths = lengths.to(self.device)
# embed language
lang_embedded = torch.cat([self.lang_embedder(lang_input[i]).unsqueeze(0) for i in range(len(lang_input))],
dim=0)
# encode
# USE CBOW FOR DEBUGGING
#mean_embedding = torch.sum(lang_embedded, dim = 1).repeat(1,2)
#lang_output = {"sentence_encoding": mean_embedding }
lang_output = self.lang_encoder(lang_embedded, lengths)
# get language output as sentence embedding
sent_encoding = lang_output["sentence_encoding"]
logits = self.mlp(sent_encoding)
#print(torch.softmax(logits, dim = 1)[0])
return {"pred_block_logits": logits}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiaojianjun-com/good_robot.git
git@gitee.com:jiaojianjun-com/good_robot.git
jiaojianjun-com
good_robot
good_robot
master

搜索帮助