deephealthproject / eddl

European Distributed Deep Learning (EDDL) library. A general-purpose library initially developed to cover deep learning needs in healthcare use cases within the DeepHealth project.
https://deephealthproject.github.io/eddl/
MIT License
34 stars 10 forks source link

Pad layer does not seem to work with 1D input #289

Closed georgemavrakis-wings closed 3 years ago

georgemavrakis-wings commented 3 years ago

Good afternoon,

I wish you a good month.

I am using the latest version of eddl and pyeddl (1.0.0) and I want to custom pad an input layer, as described in Issue https://github.com/deephealthproject/eddl/issues/270. In summary, my input is 3D, i.e. # segments x # channels x # samples/channel (e.g. [5,18,1280]) and I want to pad the last dimension with 4 zero columns to the left and 3 columns to the right, in order to have a [5,18,1287] layer and pass it to a Conv1D layer with zero padding.

However, I have not managed to make the pad layer to pad the input only in the left and right directions. More specifically, it seems to add an extra dimension with the zero columns (i.e. [5,18,1280,7]), when I pass in the padding parameter: [0,4,0,3].

Thank you in advance.

salvacarrion commented 3 years ago

The pad function works only with 4D tensors (batch, channel, h, w). You could use unsqueeze(0)+pad+squeeze(0) to transform your 3d input to 4d, and then to 3d again

// New 4D tensor
Tensor* t1 = Tensor::ones({1, 1, 3, 3});
[
[1.00 1.00 1.00]
[1.00 1.00 1.00]
[1.00 1.00 1.00]
]

// Top
t_new = t1->pad({3, 0, 0, 0});  t_new->squeeze_(); t_new->print(2);
[
[0.00 0.00 0.00]
[0.00 0.00 0.00]
[0.00 0.00 0.00]
[1.00 1.00 1.00]
[1.00 1.00 1.00]
[1.00 1.00 1.00]
]

// Right
t_new = t1->pad({0, 3, 0, 0});  t_new->squeeze_(); t_new->print(2);
[
[1.00 1.00 1.00 0.00 0.00 0.00]
[1.00 1.00 1.00 0.00 0.00 0.00]
[1.00 1.00 1.00 0.00 0.00 0.00]
]

// Bottom
t_new = t1->pad({0, 0, 3, 0});  t_new->squeeze_(); t_new->print(2);
[
[1.00 1.00 1.00]
[1.00 1.00 1.00]
[1.00 1.00 1.00]
[0.00 0.00 0.00]
[0.00 0.00 0.00]
[0.00 0.00 0.00]
]

// Left
t_new = t1->pad({0, 0, 0, 3});  t_new->squeeze_(); t_new->print(2);
[
[0.00 0.00 0.00 1.00 1.00 1.00]
[0.00 0.00 0.00 1.00 1.00 1.00]
[0.00 0.00 0.00 1.00 1.00 1.00]
]
georgemavrakis-wings commented 3 years ago

Hello @salvacarrion,

you are correct. I managed to produce the desired result.

Thank you.