audio = np.frombuffer(b''.join(frames), dtype=np.int16) input_values = tokenizer(audio, return_tensors="pt").input_values RuntimeError: expected scalar type Long but found Float?
时间: 2024-02-12 12:04:52 浏览: 134
这错误通常是由于输入数据类型与模型期望的数据类型不匹配导致的。在这个例子中,模型期望的是长整型数据(dtype=np.long),但是输入的数据类型是16位整型数据(dtype=np.int16)。需要将输入数据的数据类型转换为模型期望的数据类型,可以尝试使用以下代码进行转换:
```
audio = np.frombuffer(b''.join(frames), dtype=np.long)
input_values = tokenizer(audio, return_tensors="pt").input_values.type(torch.LongTensor)
```
这里使用了PyTorch中的LongTensor类型,它对应于64位整型数据。这应该可以解决此问题。
相关问题
audio = np.frombuffer(b''.join(frames), dtype=np.int64) input_values = tokenizer(audio, return_tensors="pt", sampling_rate=16000).input_values.type(torch.LongTensor) with torch.no_grad(): logits = model(input_values).logits RuntimeError: expected scalar type Long but found Float?
这个错误信息提示中也出现了类型错误的问题,期望输入的类型为 Long 类型,但是实际输入的是 Float 类型。可以尝试将 `input_values` 转换为 LongTensor 类型,代码如下:
```
input_values = tokenizer(audio, return_tensors="pt", sampling_rate=16000).input_values
input_values = input_values.type(torch.LongTensor)
```
这样应该可以解决出现的类型错误问题。
audio = np.frombuffer(b''.join(frames), dtype=np.long) input_values = tokenizer(audio, return_tensors="pt").input_values.type(torch.LongTensor) with torch.no_grad(): logits = model(input_values).logits RuntimeError: expected scalar type Long but found Float
看起来你已经解决了上一个问题,但是现在又出现了新的错误。这个错误的原因可能是因为模型的输入数据类型与设备类型不匹配。可以尝试将输入数据和模型都转换为相同的数据类型和设备类型。以下是一些可能有效的代码:
```
audio = np.frombuffer(b''.join(frames), dtype=np.long)
input_values = tokenizer(audio, return_tensors="pt").input_values.type(torch.LongTensor).to(device)
model.to(device)
with torch.no_grad():
logits = model(input_values).logits
```
其中,`device`是指定的PyTorch设备,可以是CPU或GPU。上述代码将输入数据和模型都转换为LongTensor类型,并将它们都移动到指定的设备上。这应该可以解决此问题。
阅读全文
相关推荐
















