Using expand_dims in pytorch
I'm trying to tile a length 18 1 hot vector into a 40x40 grid.
Looking at pytorch docs, expand dims seems to be what i need.
But I cannot get it to work. Any idea what I'm doing wrong?
one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3
I'm expecting a tensor of shape (1, 18, 40,40)
pytorch
add a comment |
I'm trying to tile a length 18 1 hot vector into a 40x40 grid.
Looking at pytorch docs, expand dims seems to be what i need.
But I cannot get it to work. Any idea what I'm doing wrong?
one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3
I'm expecting a tensor of shape (1, 18, 40,40)
pytorch
1
How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?
– Jacques Kvam
Dec 30 '18 at 5:02
Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.
– Duane
Dec 30 '18 at 6:12
add a comment |
I'm trying to tile a length 18 1 hot vector into a 40x40 grid.
Looking at pytorch docs, expand dims seems to be what i need.
But I cannot get it to work. Any idea what I'm doing wrong?
one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3
I'm expecting a tensor of shape (1, 18, 40,40)
pytorch
I'm trying to tile a length 18 1 hot vector into a 40x40 grid.
Looking at pytorch docs, expand dims seems to be what i need.
But I cannot get it to work. Any idea what I'm doing wrong?
one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3
I'm expecting a tensor of shape (1, 18, 40,40)
pytorch
pytorch
asked Dec 30 '18 at 4:51
DuaneDuane
4911515
4911515
1
How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?
– Jacques Kvam
Dec 30 '18 at 5:02
Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.
– Duane
Dec 30 '18 at 6:12
add a comment |
1
How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?
– Jacques Kvam
Dec 30 '18 at 5:02
Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.
– Duane
Dec 30 '18 at 6:12
1
1
How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?
– Jacques Kvam
Dec 30 '18 at 5:02
How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?
– Jacques Kvam
Dec 30 '18 at 5:02
Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.
– Duane
Dec 30 '18 at 6:12
Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.
– Duane
Dec 30 '18 at 6:12
add a comment |
1 Answer
1
active
oldest
votes
expand
works along singleton dimensions of the input tensor. In your example, you are trying to expand
a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size
==1) you have is the first dimension.
fix
one_hot = torch.zeros(1,18,1,1, dtype=torch.float) # create the tensor with all singleton dimensions in place
one_hot[0,1,0,0] = 1.
one_hot.expand(-1,-1,40,40)
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%2f53975352%2fusing-expand-dims-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
expand
works along singleton dimensions of the input tensor. In your example, you are trying to expand
a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size
==1) you have is the first dimension.
fix
one_hot = torch.zeros(1,18,1,1, dtype=torch.float) # create the tensor with all singleton dimensions in place
one_hot[0,1,0,0] = 1.
one_hot.expand(-1,-1,40,40)
add a comment |
expand
works along singleton dimensions of the input tensor. In your example, you are trying to expand
a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size
==1) you have is the first dimension.
fix
one_hot = torch.zeros(1,18,1,1, dtype=torch.float) # create the tensor with all singleton dimensions in place
one_hot[0,1,0,0] = 1.
one_hot.expand(-1,-1,40,40)
add a comment |
expand
works along singleton dimensions of the input tensor. In your example, you are trying to expand
a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size
==1) you have is the first dimension.
fix
one_hot = torch.zeros(1,18,1,1, dtype=torch.float) # create the tensor with all singleton dimensions in place
one_hot[0,1,0,0] = 1.
one_hot.expand(-1,-1,40,40)
expand
works along singleton dimensions of the input tensor. In your example, you are trying to expand
a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size
==1) you have is the first dimension.
fix
one_hot = torch.zeros(1,18,1,1, dtype=torch.float) # create the tensor with all singleton dimensions in place
one_hot[0,1,0,0] = 1.
one_hot.expand(-1,-1,40,40)
answered Dec 30 '18 at 5:56
ShaiShai
69.5k22136243
69.5k22136243
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%2f53975352%2fusing-expand-dims-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
1
How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?
– Jacques Kvam
Dec 30 '18 at 5:02
Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.
– Duane
Dec 30 '18 at 6:12