5

I have an 2D input (or 3D if one consider the number of samples) and I want to apply a keras layer that would take this input and outputs another 2D matrix. So, for example, if I have an input with size (ExV), the learning weight matrix would be (SxE) and the output (SxV). Can I do this with Dense layer?

EDIT (Nassim request):

The first layer is doing nothing. It's just to give an input to Lambda layer:

from keras.models import Sequential
from keras.layers.core import Reshape,Lambda
from keras import backend as K
from keras.models import Model

input_sample = [
[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]
,[[21,22,23,24,25],[26,27,28,29,30],[31,32,33,34,35],[36,37,38,39,40]]
,[[41,42,43,44,45],[46,47,48,49,50],[51,52,53,54,55],[56,57,58,59,60]]
]


model = Sequential()
model.add(Reshape((4,5), input_shape=(4,5)))
model.add(Lambda(lambda x: K.transpose(x)))
intermediate_layer_model = Model(input=model.input,output=model.layers[0].output)
print "First layer:"
print intermediate_layer_model.predict(input_sample)
print ""
print "Second layer:"
intermediate_layer_model = Model(input=model.input,output=model.layers[1].output)
print intermediate_layer_model.predict(input_sample)
Miguel
  • 2,738
  • 3
  • 35
  • 51

2 Answers2

2

What you want is probably autoencoder.

https://blog.keras.io/building-autoencoders-in-keras.html

klubow
  • 126
  • 7
2

It depends on what you want to do. Is it 2D because it's a sequence? Then LSTM are made for that and will return a sequence if desired size if you set return_sequence=True.

CNN's can also work on 2D inputs and will output something of variable size depending on the number of kernels you use.

Otherwise you can reshape it to a (E x V, ) 1D tensor, use a Dense layer with SxV dimension and reshape the output to a (S,V) 2D tensor...

I can not help you more, we need to know your usecase :-) there are too many possibilities with neural networks.

EDIT :

You can use TimeDistributed(Dense(S)). If your input has a shape (E,V), you reshape to (V,E) to have V as the "time dimension". Then you apply TimeDistributed(Dense(S)) which will be a dense layer with weights (ExS), the output will have the shape (V,S) so you can reshape it to (S,V).

Does that make what you want ? The TimeDistributed() layer will apply the same Dense(S) layer to every V lines of your input with shared weights.

EDIT 2:

After looking at the code of keras backend, it turns out that to use the transpose from tensorflow with 'permutation patterns' option available, you need to use K.permute_dimensions(x,pattern). The batch dimension must be included. In your case :

Lambda(lambda x: K.permute_dimensions(x,[0,2,1]))

K.transpose(x) uses the same function internally (for tf backend) but permutations is set to the default value which is [n,n-1,...,0].

Community
  • 1
  • 1
Nassim Ben
  • 11,473
  • 1
  • 34
  • 52
  • Hi, thanks for the reply. Is not a sequence, and so can't use LSTM. I also thought about CNN, but then it doesn't learn a weight (SxE) matrix, because it will share the weights. The possibility to reshape don't work either, because is the opposite, i.e., all weights will be applied simultaneously for each neuron. – Miguel Feb 10 '17 at 13:53
  • Edited with another proposition :) – Nassim Ben Feb 10 '17 at 14:02
  • Is that what you were looking for? – Nassim Ben Feb 10 '17 at 16:32
  • Thanks, I think that's it! It's a nice and clever way to do it. :) – Miguel Feb 11 '17 at 00:34
  • Additional note: the transpose calculation must not be done with Keras Reshape Layer, because the layer does not make the transpose. – Miguel Feb 11 '17 at 12:25
  • You can then make a custom layer (lambda) using the backend : K.transpose() – Nassim Ben Feb 11 '17 at 12:29
  • Yes, but I think that then it tries to transpose the full batch. For example, I was getting this error: "ValueError: could not broadcast input array from shape (5,4,3) into shape (3,4,3)" This is for a test case with a sequence of size 3, each with with dimensions 4x5 – Miguel Feb 11 '17 at 12:44
  • Can you post your code into your question? With the example and what you've tried with K.transpose() and the full error – Nassim Ben Feb 11 '17 at 13:23
  • I eddied the main question. – Miguel Feb 11 '17 at 16:00