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

SpeechBrain调用encode_batch方法报错'list' object has no attribute 'shape'的问题求助

SpeechBrain调用encode_batch方法报错'list' object has no attribute 'shape'的问题求助

各位大佬好,我最近在用SpeechBrain的预训练说话人识别模型,调用encode_batch方法的时候碰到了这个报错:

Error during encoding: 'list' object has no attribute 'shape'

我是照着官方示例的思路写的代码,先初始化模型,再传入音频文件路径列表调用encode_batch,之后尝试处理返回的embedding并查看它的shape,但每次都会触发上面的错误。以下是我的完整代码:

import numpy as np
from speechbrain.pretrained import SpeakerRecognition

# 初始化说话人识别模型
model = SpeakerRecognition.from_hparams(source="speechbrain/spkrec-ecapa-voxceleb", savedir="pretrained_model")

# WAV文件路径
wav_file = r"c:\audio_files\test.wav"  # 替换成你自己的文件路径

# 测试模型的encode_batch编码方法
try:
    # 模型要求输入为文件路径的列表
    embeddings = model.encode_batch([wav_file])

    # 检查返回的embeddings结构
    if embeddings:
        print("成功获取Embeddings。")

        # 检查embeddings是tensor还是NumPy数组
        if hasattr(embeddings[0], 'cpu'):  # 可能是tensor类型
            first_embedding = embeddings[0].cpu().numpy()  # 转换为numpy数组
            print("第一个Embedding已转换为NumPy数组。")
        elif isinstance(embeddings[0], np.ndarray):  # 已经是numpy数组
            first_embedding = embeddings[0]
            print("第一个Embedding本身就是NumPy数组。")

        # 如果是NumPy数组则显示其shape
        if isinstance(first_embedding, np.ndarray):
            print("第一个Embedding的shape:", first_embedding.shape)
        else:
            print("该Embedding既不是tensor也不是NumPy数组。")
    else:
        print("未返回Embeddings或返回了空列表。")
except Exception as e:
    print(f"编码过程中出错: {e}")

我原本以为encode_batch会返回tensor或者numpy数组,但看起来返回的是一个list?有没有人遇到过类似的问题?或者我哪里误解了模型的返回结构?麻烦各位帮忙指点一下,谢谢!

备注:内容来源于stack exchange,提问作者yassine salhi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 15:24:31