Python TypeError:trace():参数';输入';(位置1)必须是张量,而不是方法
当我开始我的脚本时,我遇到了一个奇怪的错误,我真的不知道为什么会出现这个错误,或者是由什么引起的。如果你们中的一些人能帮我解决这个错误,那就太好了:) 错误消息:Python TypeError:trace():参数';输入';(位置1)必须是张量,而不是方法,python,python-3.x,machine-learning,deep-learning,pytorch,Python,Python 3.x,Machine Learning,Deep Learning,Pytorch,当我开始我的脚本时,我遇到了一个奇怪的错误,我真的不知道为什么会出现这个错误,或者是由什么引起的。如果你们中的一些人能帮我解决这个错误,那就太好了:) 错误消息: File "c:/Users/supre/Documents/Python Programs/neuralnet/optimize_graph.py", line 11, in trace traced = torch.trace(model.forward)(xample_inputs=(x, hid
File "c:/Users/supre/Documents/Python Programs/neuralnet/optimize_graph.py", line 11, in trace
traced = torch.trace(model.forward)(xample_inputs=(x, hidden))
TypeError: trace(): argument 'input' (position 1) must be Tensor, not method
这是我的代码:
import argparse
import torch
from model import SpeechRecognition
from collections import OrderedDict
def trace(model):
model.eval()
x = torch.rand(1, 81, 300)
hidden = model._init_hidden(1)
traced = torch.trace(model.forward)(xample_inputs=(x, hidden))
return traced
def main(args):
print("loading model from", args.model_checkpoint)
checkpoint = torch.load(args.model_checkpoint, map_location=torch.device('cpu'))
h_params = SpeechRecognition.hyper_parameters
model = SpeechRecognition(**h_params)
model_state_dict = checkpoint['state_dict']
new_state_dict = OrderedDict()
for k, v in model_state_dict.items():
name = k.replace("model.", "") # remove `model.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
print("tracing model...")
traced_model = trace(model)
#print(type(traced_model))
print("saving to", args.save_path)
traced_model.save(args.save_path)
print("Done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="testing the wakeword engine")
parser.add_argument('--model_checkpoint', type=str, default='NameOfYourCheckpoint', required=False,
help='Checkpoint of model to optimize')
parser.add_argument('--save_path', type=str, default='path/where/you/want/to/save/the/optimized/model', required=False,
help='path to save optmized model')
args = parser.parse_args()
main(args)
感谢您在
tracked=torch.trace(model.forward)(示例输入=(x,隐藏))
中预先提供的所有帮助您正在传递一个方法,即model.forward
您应该使用类似的方法
tracked=torch.trace(model.forward(x))
如果您的模型已经有一个带有参数的实例您是否有到语音识别文档的链接,特别是对于forward
?@accumulation否,但我有一个repo的链接。我应该给你回购链接吗?@accumulation你能把我添加到discord上吗?因为我可以告诉你更多关于错误的信息,以及我认为可能导致错误的原因。名称:CodeForce1#8083获取文档是一个基本步骤。我不明白你怎么能在不知道模块功能的情况下使用它。我在发表评论和回答之间摇摆不定。我认为如果你提出forward()
而不是forward
,很可能会解决问题,但这最终只是猜测。好的,谢谢,我会尝试:)