facebookresearch / esm

Evolutionary Scale Modeling (esm): Pretrained language models for proteins
MIT License
3.29k stars 644 forks source link

ValueError: too many values to unpack (expected 2) #575

Open limpidzhao opened 1 year ago

limpidzhao commented 1 year ago

I'm trying to extract the attention map from ESM-1b model using the following code:

import torch
import esm

# Check if CUDA is available and set the device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load ESM-1b model
model, alphabet = esm.pretrained.esm1b_t33_650M_UR50S()
model = model.eval().to(device)  # Move the model to GPU

# Prepare data (single sequence or MSA)
sequence = "MKTVRQERLKSIVRILERSKEPVSGAQLAEELSVSRQVIVQDIAYLRSLGYNIVATPRGYVLAGG"
batch_converter = alphabet.get_batch_converter()
data = batch_converter([sequence])

# Move the data to GPU
data = [item.to(device) for item in data]

# Forward pass through the model
with torch.no_grad():
    results = model(*data, repr_layers=[33], return_contacts=True)

# Extract attention map
attention_map = results["attns"]

# Now you can use the attention map as a feature for your own model

But I'm getting a ValueError: too many values to unpack (expected 2) error. Could you please help me understand what's going wrong?

chAwater commented 1 year ago

The batch_converter takes a list of tuple (labels, strings) as input, for example

https://github.com/facebookresearch/esm/blob/2b369911bb5b4b0dda914521b9475cad1656b2ac/examples/variant-prediction/predict.py#L125-L132