2

I have a question about Python syntax when I'm learning PyTorch. The following codes are an example from the PyTorch document.

m = nn.Linear(20, 30)
input = autograd.Variable(torch.randn(128, 20))
output = m(input)
print(output.size())

This first line is to create an instance m, but why this instance m can directly receive a parameter like the line 3? I think it should use method to deal with parameter like m.method(input).

1 Answer 1

2

In python, any object can define a __call__ method that allows it to be used as a function like in your example.

Reference: https://docs.python.org/2/reference/datamodel.html#object.call

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I am not very familiar with Python, so I didn't know it before.
Glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.