You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESM-1b Transformer报错TypeError: unhashable type: 'slice'求助

解决ESM-1b中TypeError: unhashable type: 'slice'的问题

错误原因

你标注的循环行并非实际报错点,问题出在访问token_representations的代码上。ESM模型返回的results["representations"]是一个字典,键为模型的层数(比如ESM-1b包含0到33层),而非二维张量。直接用token_representations[i, 1 : len(seq) + 1]这种张量索引方式访问字典,会触发"不可哈希类型:slice"的错误。

修正步骤

  1. 指定要提取的表征层数:
    先从representations字典中取出对应层的张量,通常我们会选用最后一层(键为33,对应ESM-1b的最终输出层)。
  2. 保留原有列表切片与遍历逻辑:
    你对parsed_list的切片操作new_list = parsed_list[:650]完全合法,遍历过程本身没有问题。

修正后的完整代码

batch_labels, batch_strs, batch_tokens = batch_converter(new_list)

with torch.no_grad():
    results = model(batch_tokens, return_contacts=True)
    # 取出最后一层的token表征(按需替换为其他层数,比如0、1等)
    token_representations = results["representations"][33]

sequence_representations = []
for i, (_, seq) in enumerate(new_list):
    # 此时token_representations为二维张量,可正常索引切片
    sequence_representations.append(token_representations[i, 1 : len(seq) + 1].mean(0))

额外说明

  • 若需要提取中间层的表征,将33替换为对应层数即可(ESM-1b的层数范围是0到33)。
  • new_list的切片逻辑无需修改,切片后的列表可以正常通过enumerate遍历。

内容的提问来源于stack exchange,提问作者eneko valero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 01:24:28