INNER CODE UNIT · Python
bert_predict
princeton-nlp/WebShop · transfer/app.py:73
def bert_predict(obs, info, softmax=True):
valid_acts = info['valid']
assert valid_acts[0].startswith('click[')
state_encodings = bert_tokenizer(process_str(obs), max_length=512, truncation=True, padding='max_length')
action_encodings = bert_tokenizer(list(map(process_str, valid_acts)), max_length=512, truncation=True, padding='max_length')
batch = {
'state_input_ids': state_encodings['input_ids'],
'state_attention_mask': state_encodings['attention_mask'],
'action_input_ids': action_encodings['input_ids'],
'action_attention_mask': action_encodings['attention_mask'],
'sizes': len(valid_acts),
'images': info['image_feat'].tolist(),
'labels': 0
}
batch = data_collator([batch])
outputs = bert_model(**batch)
if softmax:
idx = torch.multinomial(torch.nn.functional.softmax(outputs.logits[0], dim=0), 1)[0].item()