TypeError: trace(): argument 'input' (position 1) must be Tensor, not method

Hey guy’s,
when I try to run my script I get this weird error and I don’t know how to fix it or what this error is caused by. This is the error:TypeError: trace(): argument 'input' (position 1) must be Tensor, not method
I hope that some of you guy’s can help me with this error and explain to me what this error is caused by.

This is my code:

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)(example_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='checkpoint_file', required=False,
                        help='Checkpoint of model to optimize')
    parser.add_argument('--save_path', type=str, default='path/where/the/optimized/model/should/be/saved', required=False,
                        help='path to save optmized model')

    args = parser.parse_args()
    main(args)

This is the full error message:

Traceback (most recent call last):
  File "c:/Users/supre/Documents/Python Programme/neuralnet/optimize_graph.py", line 45, in <module>
    main(args)
  File "c:/Users/supre/Documents/Python Programme/neuralnet/optimize_graph.py", line 30, in main
    traced_model = trace(model)
  File "c:/Users/supre/Documents/Python Programme/neuralnet/optimize_graph.py", line 11, in trace
    traced = torch.trace(model.forward)(example_inputs=(x, hidden))
TypeError: trace(): argument 'input' (position 1) must be Tensor, not method

Thank’s for every help in advance:)

It is because your inputs to the forward function are in the wrong parenthesis. You are just passing the forward method into the trace function not the output. To fix it just do this

     traced = torch.trace(model.forward(example_inputs=(x, hidden)))