Does it differ to use optimizer.step or model.step in pytorch?
In pytorch, to update the model, should I use optimizer.step()
or model.step()
?
Here is a example snippet:
import torch
import torch nn
class SomeNeuralNet(nn.Module):
def __init__(self,hs,es,dropout):
SomeNeuralNet(ClaimRecognizer, self).__init__()
# Some initialization here
def forward(x):
# forward propagation here
model = SomeNeuralNet(es,hs,dp)
optimizer = optim.Adam(model.parameters())
loss_function = nn.NLLLoss()
for epoch in N:
for x in data:
logp = model(x)
loss = loss_function(logp,gold_outs)
loss.backward()
# Which one I should call ? Optimizer.step() or model.step() or both ?
optimizer.step()
model.step()
python pytorch
add a comment |
In pytorch, to update the model, should I use optimizer.step()
or model.step()
?
Here is a example snippet:
import torch
import torch nn
class SomeNeuralNet(nn.Module):
def __init__(self,hs,es,dropout):
SomeNeuralNet(ClaimRecognizer, self).__init__()
# Some initialization here
def forward(x):
# forward propagation here
model = SomeNeuralNet(es,hs,dp)
optimizer = optim.Adam(model.parameters())
loss_function = nn.NLLLoss()
for epoch in N:
for x in data:
logp = model(x)
loss = loss_function(logp,gold_outs)
loss.backward()
# Which one I should call ? Optimizer.step() or model.step() or both ?
optimizer.step()
model.step()
python pytorch
add a comment |
In pytorch, to update the model, should I use optimizer.step()
or model.step()
?
Here is a example snippet:
import torch
import torch nn
class SomeNeuralNet(nn.Module):
def __init__(self,hs,es,dropout):
SomeNeuralNet(ClaimRecognizer, self).__init__()
# Some initialization here
def forward(x):
# forward propagation here
model = SomeNeuralNet(es,hs,dp)
optimizer = optim.Adam(model.parameters())
loss_function = nn.NLLLoss()
for epoch in N:
for x in data:
logp = model(x)
loss = loss_function(logp,gold_outs)
loss.backward()
# Which one I should call ? Optimizer.step() or model.step() or both ?
optimizer.step()
model.step()
python pytorch
In pytorch, to update the model, should I use optimizer.step()
or model.step()
?
Here is a example snippet:
import torch
import torch nn
class SomeNeuralNet(nn.Module):
def __init__(self,hs,es,dropout):
SomeNeuralNet(ClaimRecognizer, self).__init__()
# Some initialization here
def forward(x):
# forward propagation here
model = SomeNeuralNet(es,hs,dp)
optimizer = optim.Adam(model.parameters())
loss_function = nn.NLLLoss()
for epoch in N:
for x in data:
logp = model(x)
loss = loss_function(logp,gold_outs)
loss.backward()
# Which one I should call ? Optimizer.step() or model.step() or both ?
optimizer.step()
model.step()
python pytorch
python pytorch
edited Nov 15 '18 at 5:48
Milo Lu
1,61511427
1,61511427
asked Nov 14 '18 at 14:39
zwlayerzwlayer
6731818
6731818
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To make the gradient descent step, you normally use just optimizer.step()
.
Here is also an example taken from the documentation (same link at bottom), what it looks like in general:
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
I don't know where you got this model.step()
? Does did you try it?
If your model really possesses some kind of step()
-functionality, it probably does something different.
But unless you define something extra, your model
gets its functions from nn.Module
and this does not have step
function!
See this example from the the Pytorch Documentation:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
model = Model()
model.step()
Trying to call step()
result in an AttributeError
:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-b032813f7eda> in <module>
13
14 model = Model()
---> 15 model.step()
~/miniconda3/envs/py37/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
530 return modules[name]
531 raise AttributeError("'' object has no attribute ''".format(
--> 532 type(self).__name__, name))
533
534 def __setattr__(self, name, value):
AttributeError: 'Model' object has no attribute 'step'
To sum it up, normally your model
should not have a step()
function, optimizer.step()
is the way to go if you want to do the optimization step.
See also here:
https://pytorch.org/docs/stable/optim.html#taking-an-optimization-step
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302713%2fdoes-it-differ-to-use-optimizer-step-or-model-step-in-pytorch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To make the gradient descent step, you normally use just optimizer.step()
.
Here is also an example taken from the documentation (same link at bottom), what it looks like in general:
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
I don't know where you got this model.step()
? Does did you try it?
If your model really possesses some kind of step()
-functionality, it probably does something different.
But unless you define something extra, your model
gets its functions from nn.Module
and this does not have step
function!
See this example from the the Pytorch Documentation:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
model = Model()
model.step()
Trying to call step()
result in an AttributeError
:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-b032813f7eda> in <module>
13
14 model = Model()
---> 15 model.step()
~/miniconda3/envs/py37/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
530 return modules[name]
531 raise AttributeError("'' object has no attribute ''".format(
--> 532 type(self).__name__, name))
533
534 def __setattr__(self, name, value):
AttributeError: 'Model' object has no attribute 'step'
To sum it up, normally your model
should not have a step()
function, optimizer.step()
is the way to go if you want to do the optimization step.
See also here:
https://pytorch.org/docs/stable/optim.html#taking-an-optimization-step
add a comment |
To make the gradient descent step, you normally use just optimizer.step()
.
Here is also an example taken from the documentation (same link at bottom), what it looks like in general:
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
I don't know where you got this model.step()
? Does did you try it?
If your model really possesses some kind of step()
-functionality, it probably does something different.
But unless you define something extra, your model
gets its functions from nn.Module
and this does not have step
function!
See this example from the the Pytorch Documentation:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
model = Model()
model.step()
Trying to call step()
result in an AttributeError
:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-b032813f7eda> in <module>
13
14 model = Model()
---> 15 model.step()
~/miniconda3/envs/py37/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
530 return modules[name]
531 raise AttributeError("'' object has no attribute ''".format(
--> 532 type(self).__name__, name))
533
534 def __setattr__(self, name, value):
AttributeError: 'Model' object has no attribute 'step'
To sum it up, normally your model
should not have a step()
function, optimizer.step()
is the way to go if you want to do the optimization step.
See also here:
https://pytorch.org/docs/stable/optim.html#taking-an-optimization-step
add a comment |
To make the gradient descent step, you normally use just optimizer.step()
.
Here is also an example taken from the documentation (same link at bottom), what it looks like in general:
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
I don't know where you got this model.step()
? Does did you try it?
If your model really possesses some kind of step()
-functionality, it probably does something different.
But unless you define something extra, your model
gets its functions from nn.Module
and this does not have step
function!
See this example from the the Pytorch Documentation:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
model = Model()
model.step()
Trying to call step()
result in an AttributeError
:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-b032813f7eda> in <module>
13
14 model = Model()
---> 15 model.step()
~/miniconda3/envs/py37/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
530 return modules[name]
531 raise AttributeError("'' object has no attribute ''".format(
--> 532 type(self).__name__, name))
533
534 def __setattr__(self, name, value):
AttributeError: 'Model' object has no attribute 'step'
To sum it up, normally your model
should not have a step()
function, optimizer.step()
is the way to go if you want to do the optimization step.
See also here:
https://pytorch.org/docs/stable/optim.html#taking-an-optimization-step
To make the gradient descent step, you normally use just optimizer.step()
.
Here is also an example taken from the documentation (same link at bottom), what it looks like in general:
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
I don't know where you got this model.step()
? Does did you try it?
If your model really possesses some kind of step()
-functionality, it probably does something different.
But unless you define something extra, your model
gets its functions from nn.Module
and this does not have step
function!
See this example from the the Pytorch Documentation:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
model = Model()
model.step()
Trying to call step()
result in an AttributeError
:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-b032813f7eda> in <module>
13
14 model = Model()
---> 15 model.step()
~/miniconda3/envs/py37/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
530 return modules[name]
531 raise AttributeError("'' object has no attribute ''".format(
--> 532 type(self).__name__, name))
533
534 def __setattr__(self, name, value):
AttributeError: 'Model' object has no attribute 'step'
To sum it up, normally your model
should not have a step()
function, optimizer.step()
is the way to go if you want to do the optimization step.
See also here:
https://pytorch.org/docs/stable/optim.html#taking-an-optimization-step
edited Nov 14 '18 at 15:29
answered Nov 14 '18 at 14:58
blue-phoenoxblue-phoenox
4,181101643
4,181101643
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302713%2fdoes-it-differ-to-use-optimizer-step-or-model-step-in-pytorch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown