About autograd in pyorch, Adding new user-defined layers, how should I make its parameters update?
everyone !
My demand is a optical-flow-generating problem. I have two raw images and a optical flow data as ground truth, now my algorithm is to generate optical flow using raw images, and the euclidean distance between generating optical flow and ground truth could be defined as a loss value, so it can implement a backpropagation to update parameters.
I take it as a regression problem, and I have to ideas now:
I can set every parameters as (required_grad = true), and compute a loss, then I can loss.backward() to acquire the gradient, but I don’t know how to add these parameters in optimizer to update those.
I write my algorithm as a model. If I design a “custom” model, I can initilize several layers such as nn.Con2d(), nn.Linear() in def init() and I can update parameters in methods like (torch.optim.Adam(model.parameters())), but if I define new layers by myself, how should I add this layer’s parameters in updating parameter collection???
This problem has confused me several days. Are there any good methods to update user-defined parameters? I would be very grateful if you could give me some advice!
computer-vision pytorch autograd
add a comment |
everyone !
My demand is a optical-flow-generating problem. I have two raw images and a optical flow data as ground truth, now my algorithm is to generate optical flow using raw images, and the euclidean distance between generating optical flow and ground truth could be defined as a loss value, so it can implement a backpropagation to update parameters.
I take it as a regression problem, and I have to ideas now:
I can set every parameters as (required_grad = true), and compute a loss, then I can loss.backward() to acquire the gradient, but I don’t know how to add these parameters in optimizer to update those.
I write my algorithm as a model. If I design a “custom” model, I can initilize several layers such as nn.Con2d(), nn.Linear() in def init() and I can update parameters in methods like (torch.optim.Adam(model.parameters())), but if I define new layers by myself, how should I add this layer’s parameters in updating parameter collection???
This problem has confused me several days. Are there any good methods to update user-defined parameters? I would be very grateful if you could give me some advice!
computer-vision pytorch autograd
add a comment |
everyone !
My demand is a optical-flow-generating problem. I have two raw images and a optical flow data as ground truth, now my algorithm is to generate optical flow using raw images, and the euclidean distance between generating optical flow and ground truth could be defined as a loss value, so it can implement a backpropagation to update parameters.
I take it as a regression problem, and I have to ideas now:
I can set every parameters as (required_grad = true), and compute a loss, then I can loss.backward() to acquire the gradient, but I don’t know how to add these parameters in optimizer to update those.
I write my algorithm as a model. If I design a “custom” model, I can initilize several layers such as nn.Con2d(), nn.Linear() in def init() and I can update parameters in methods like (torch.optim.Adam(model.parameters())), but if I define new layers by myself, how should I add this layer’s parameters in updating parameter collection???
This problem has confused me several days. Are there any good methods to update user-defined parameters? I would be very grateful if you could give me some advice!
computer-vision pytorch autograd
everyone !
My demand is a optical-flow-generating problem. I have two raw images and a optical flow data as ground truth, now my algorithm is to generate optical flow using raw images, and the euclidean distance between generating optical flow and ground truth could be defined as a loss value, so it can implement a backpropagation to update parameters.
I take it as a regression problem, and I have to ideas now:
I can set every parameters as (required_grad = true), and compute a loss, then I can loss.backward() to acquire the gradient, but I don’t know how to add these parameters in optimizer to update those.
I write my algorithm as a model. If I design a “custom” model, I can initilize several layers such as nn.Con2d(), nn.Linear() in def init() and I can update parameters in methods like (torch.optim.Adam(model.parameters())), but if I define new layers by myself, how should I add this layer’s parameters in updating parameter collection???
This problem has confused me several days. Are there any good methods to update user-defined parameters? I would be very grateful if you could give me some advice!
computer-vision pytorch autograd
computer-vision pytorch autograd
asked Dec 28 '18 at 12:51
杨键刚杨键刚
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Tensor values have their gradients calculated if they
- Have
requires_grad == True
- Are used to compute some value (usually loss) on which you call
.backward()
.
The gradients will then be accumulated in their .grad
parameter. You can manually use them in order to perform arbitrary computation (including optimization). The predefined optimizers accept an iterable of parameters and model.parameters()
does just that - it returns an iterable of parameters. If you have some custom "free-floating" parameters you can pass them as
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(my_params)
and you can also merge them with the other parameter iterables like below:
model_params = list(model.parameters())
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(model_params + my_params)
In practice however, you can usually structure your code to avoid that. There's the nn.Parameter
class which wraps tensors. All subclasses of nn.Module
have their __setattr__
overridden so that whenever you assign an instance of nn.Parameter
as its property, it will become a part of Module
's .parameters()
iterable. In other words
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.my_param_1 = nn.Parameter(torch.tensor(...))
self.my_param_2 = nn.Parameter(torch.tensor(...))
will allow you to write
module = MyModule()
optim = torch.optim.Adam(module.parameters())
and have the optim
update module.my_param_1
and module.my_param_2
. This is the preferred way to go, since it helps keep your code more structured
- You won't have to manually include all your parameters when creating the optimizer
- You can call
module.zero_grad()
and zero out the gradient on all its childrennn.Parameter
s. - You can call methods such as
module.cuda()
ormodule.double()
which, again, work on all childrennn.Parameter
s instead of requiring to manually iterate through them.
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
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%2f53958898%2fabout-autograd-in-pyorch-adding-new-user-defined-layers-how-should-i-make-its%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
Tensor values have their gradients calculated if they
- Have
requires_grad == True
- Are used to compute some value (usually loss) on which you call
.backward()
.
The gradients will then be accumulated in their .grad
parameter. You can manually use them in order to perform arbitrary computation (including optimization). The predefined optimizers accept an iterable of parameters and model.parameters()
does just that - it returns an iterable of parameters. If you have some custom "free-floating" parameters you can pass them as
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(my_params)
and you can also merge them with the other parameter iterables like below:
model_params = list(model.parameters())
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(model_params + my_params)
In practice however, you can usually structure your code to avoid that. There's the nn.Parameter
class which wraps tensors. All subclasses of nn.Module
have their __setattr__
overridden so that whenever you assign an instance of nn.Parameter
as its property, it will become a part of Module
's .parameters()
iterable. In other words
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.my_param_1 = nn.Parameter(torch.tensor(...))
self.my_param_2 = nn.Parameter(torch.tensor(...))
will allow you to write
module = MyModule()
optim = torch.optim.Adam(module.parameters())
and have the optim
update module.my_param_1
and module.my_param_2
. This is the preferred way to go, since it helps keep your code more structured
- You won't have to manually include all your parameters when creating the optimizer
- You can call
module.zero_grad()
and zero out the gradient on all its childrennn.Parameter
s. - You can call methods such as
module.cuda()
ormodule.double()
which, again, work on all childrennn.Parameter
s instead of requiring to manually iterate through them.
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
add a comment |
Tensor values have their gradients calculated if they
- Have
requires_grad == True
- Are used to compute some value (usually loss) on which you call
.backward()
.
The gradients will then be accumulated in their .grad
parameter. You can manually use them in order to perform arbitrary computation (including optimization). The predefined optimizers accept an iterable of parameters and model.parameters()
does just that - it returns an iterable of parameters. If you have some custom "free-floating" parameters you can pass them as
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(my_params)
and you can also merge them with the other parameter iterables like below:
model_params = list(model.parameters())
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(model_params + my_params)
In practice however, you can usually structure your code to avoid that. There's the nn.Parameter
class which wraps tensors. All subclasses of nn.Module
have their __setattr__
overridden so that whenever you assign an instance of nn.Parameter
as its property, it will become a part of Module
's .parameters()
iterable. In other words
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.my_param_1 = nn.Parameter(torch.tensor(...))
self.my_param_2 = nn.Parameter(torch.tensor(...))
will allow you to write
module = MyModule()
optim = torch.optim.Adam(module.parameters())
and have the optim
update module.my_param_1
and module.my_param_2
. This is the preferred way to go, since it helps keep your code more structured
- You won't have to manually include all your parameters when creating the optimizer
- You can call
module.zero_grad()
and zero out the gradient on all its childrennn.Parameter
s. - You can call methods such as
module.cuda()
ormodule.double()
which, again, work on all childrennn.Parameter
s instead of requiring to manually iterate through them.
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
add a comment |
Tensor values have their gradients calculated if they
- Have
requires_grad == True
- Are used to compute some value (usually loss) on which you call
.backward()
.
The gradients will then be accumulated in their .grad
parameter. You can manually use them in order to perform arbitrary computation (including optimization). The predefined optimizers accept an iterable of parameters and model.parameters()
does just that - it returns an iterable of parameters. If you have some custom "free-floating" parameters you can pass them as
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(my_params)
and you can also merge them with the other parameter iterables like below:
model_params = list(model.parameters())
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(model_params + my_params)
In practice however, you can usually structure your code to avoid that. There's the nn.Parameter
class which wraps tensors. All subclasses of nn.Module
have their __setattr__
overridden so that whenever you assign an instance of nn.Parameter
as its property, it will become a part of Module
's .parameters()
iterable. In other words
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.my_param_1 = nn.Parameter(torch.tensor(...))
self.my_param_2 = nn.Parameter(torch.tensor(...))
will allow you to write
module = MyModule()
optim = torch.optim.Adam(module.parameters())
and have the optim
update module.my_param_1
and module.my_param_2
. This is the preferred way to go, since it helps keep your code more structured
- You won't have to manually include all your parameters when creating the optimizer
- You can call
module.zero_grad()
and zero out the gradient on all its childrennn.Parameter
s. - You can call methods such as
module.cuda()
ormodule.double()
which, again, work on all childrennn.Parameter
s instead of requiring to manually iterate through them.
Tensor values have their gradients calculated if they
- Have
requires_grad == True
- Are used to compute some value (usually loss) on which you call
.backward()
.
The gradients will then be accumulated in their .grad
parameter. You can manually use them in order to perform arbitrary computation (including optimization). The predefined optimizers accept an iterable of parameters and model.parameters()
does just that - it returns an iterable of parameters. If you have some custom "free-floating" parameters you can pass them as
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(my_params)
and you can also merge them with the other parameter iterables like below:
model_params = list(model.parameters())
my_params = [my_param_1, my_param_2]
optim = torch.optim.Adam(model_params + my_params)
In practice however, you can usually structure your code to avoid that. There's the nn.Parameter
class which wraps tensors. All subclasses of nn.Module
have their __setattr__
overridden so that whenever you assign an instance of nn.Parameter
as its property, it will become a part of Module
's .parameters()
iterable. In other words
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.my_param_1 = nn.Parameter(torch.tensor(...))
self.my_param_2 = nn.Parameter(torch.tensor(...))
will allow you to write
module = MyModule()
optim = torch.optim.Adam(module.parameters())
and have the optim
update module.my_param_1
and module.my_param_2
. This is the preferred way to go, since it helps keep your code more structured
- You won't have to manually include all your parameters when creating the optimizer
- You can call
module.zero_grad()
and zero out the gradient on all its childrennn.Parameter
s. - You can call methods such as
module.cuda()
ormodule.double()
which, again, work on all childrennn.Parameter
s instead of requiring to manually iterate through them.
answered Dec 28 '18 at 13:14
JatentakiJatentaki
1,105411
1,105411
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
add a comment |
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
Thank you! It's the answer I am looking for
– 杨键刚
Dec 29 '18 at 2:39
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
@杨键刚, then you should accept the answer.
– Shihab Shahriar
Dec 29 '18 at 10:32
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%2f53958898%2fabout-autograd-in-pyorch-adding-new-user-defined-layers-how-should-i-make-its%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