1

I am coding an Autoencoder with keras.

I have in input a symmetric matrix which I split into N sub-matrices in order to train my deep-learning framework.

As this matrix is symmetric, I would like to give in input a lower triangular matrix instead. However, half of the sub-matrices will then be "empty" (equal to 0s), which is not super nice for the training. I can remove all the empty sub-matrices, but the ones coming from the matrix diagonal will still be half empty/half filled. Another option would be to split the lower triangular matrix into triangles instead of squares.

The problem is that I don't know if keras layers support shape another than squares as input of the framework.

kabhel
  • 324
  • 3
  • 11
  • Did you try anything? Keras layers should support non-square input shapes. – Dr. Snoopy Mar 27 '19 at 15:37
  • I am a new user of keras. I have never tried or seen tutorials with inputs different than squares. Could you give me some examples or links where I can have a look? Thank you. – kabhel Mar 27 '19 at 15:45
  • my answer partially taken from here; https://stackoverflow.com/questions/8905501/extract-upper-or-lower-triangular-part-of-a-numpy-matrix – Nathan McCoy Mar 27 '19 at 15:53

1 Answers1

2

Non-square shapes (i.e. a Matrix not N x N) are supported, but it seems your question is;

how can I use the symmetric triangular 
feature matrix and deduplicate along the diagonal?

For example, if you have a upper triangular matrix, just exclude all indexes below the diagonal and flatten.

This will give you a repeatable feature vector representation.

For example, with numpy:

>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> a[np.triu_indices(3, k = 0)]
array([1, 2, 3, 5, 6, 9])
Nathan McCoy
  • 3,092
  • 1
  • 24
  • 46
  • Thank you for your answer. Then, my question would be more : If I have a huge matrix, how can I split it into N sub-triangles without taking the lower of this huge matrix? – kabhel Mar 27 '19 at 16:15
  • oh i see, update your question to reflect, also show a good example. – Nathan McCoy Mar 27 '19 at 16:26