Encoder Model last_hidden_state

#13
by karrr0n - opened

Hi @juierror ,
do you know how to get the encoder models output in python?
I would like to check the encoder last_hidden_state object.

Best regards!

Hi @cioo , you can try this code

from typing import List
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("juierror/flan-t5-text2sql-with-schema")
model = AutoModelForSeq2SeqLM.from_pretrained("juierror/flan-t5-text2sql-with-schema")

def prepare_input(question: str, table: List[str]):
    table_prefix = "table:"
    question_prefix = "question:"
    join_table = ",".join(table)
    inputs = f"{question_prefix} {question} {table_prefix} {join_table}"
    input_ids = tokenizer(inputs, max_length=512, return_tensors="pt").input_ids
    return input_ids

input_data = prepare_input(question="get people name with age equal 25", table=["id", "name", "age"])
input_data = input_data.to(model.device)
last_hidden_state = model.get_encoder()(input_data).last_hidden_state

the result should look similar to this

image.png

Thank you very much!

Sign up or log in to comment