-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are there slice layer and split layer in Keras? #890
Comments
Not yet, but you can try a |
I did not understand how does |
I searched for the same functionality and it wasn't quite obvious how to use the Lambda layer. You can do something like this (using the functional API) to slice out the first channel in x:
As I understand it the Lambda layer can only generate one output, so you have to use multiple Lambdas to slice out all the channels in x. Note that you need to specify the output_shape explicitly. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed. |
I drop this layer implementation here as I got inspiration from @lathen
To slice x as follows, x[:, :, 5:10], just call : |
hello I wanted to feed several LSTM (that has input 2D) with the out put of a convnet2D that has dimension (3D [#filter image, width, hight]). (in my case they are not images they are multivariable time series x-time steps y-variables... conv is for pre processing and lstm to analyze the sequence) The first "imagem" from the first filter would feed the first LSTM and 2nd image(filter) feed the 2nd LSTM and so on ... like shyamupa says :
I am trying to feed this merge : from a layer : Thanks |
@datascienceteam01 you can use tf.split
|
|
You could add a layer like this:
For example, if you want to eliminate the first element in the second dimention: Documentation pages: |
Is there a way to keep dims after slicing? |
If an entry in the third element is |
Why do we need to apply |
@Anton-Velikodnyy
|
The code you pasted had several errors that made it not runable. Please test the code chunk prior to pasting it. I fixed those but not sure if that is what you implementation was doing. However there is fundamental reason it broke. You were using the wrong Keras, the one you are looking for is in tensorflow. below is the code for that, and works with tf 1.11
|
@Anton-Velikodnyy |
@lathen @marc-moreaux I understood what you mean but I am getting unexpected shapes when slicing. I didn't use the lamba function or the crop function but the problem lies in simple slicing such as x[0] or x[0,:,:]. I don't know why it happens and so finding a solution here.. Here is the problem. When I try to slice a tensor of shape (2, None, None) in two halves on 1st dimension using x[0, :, :] or simply x[0] and x[1, :, :] or simply x[1], is get two tensors of shape (None, None, 1). I don't know from where this extra dimension is coming. Can you please help? Here's the piece of code: from keras import backend as K
Here's the output:
The problem:
Thank you... |
Also, posted it on tensorflow issues.. Only see the this comment else you might get confused. |
I have edited the code of @marc-moreaux to accept def Crop(dim, start, end, **kwargs):
# Crops (or slices) a Tensor on a given dimension from start to end
# example : to crop tensor x[:, :, 5:10]
def func(x):
dimension = dim
if dimension == -1:
dimension = len(x.shape) - 1
if dimension == 0:
return x[start:end]
if dimension == 1:
return x[:, start:end]
if dimension == 2:
return x[:, :, start:end]
if dimension == 3:
return x[:, :, :, start:end]
if dimension == 4:
return x[:, :, :, :, start:end]
return Lambda(func, **kwargs) |
I think one could also use |
Why would you use Lambda? It is not portable.... |
I need to share inputs and slice inputs for multiple output layers. Are there slice layer and split layer in Keras such as those in Caffe? Thanks.
The text was updated successfully, but these errors were encountered: