Jeremiah Johnson
Jeremiah Johnson

Reputation: 143

What is the most efficient way to get the upper triangle of a symmetric matrix in Tensorflow/Keras?

I have an intermediate model that outputs symmetric arrays. Those arrays are then used as input to another model. I'd like to just flatten the arrays and discard the lower triangles, since they're symmetric. Is there a best/most efficient way to do this?

Edit: I want the triangle extraction to be handled similar to any other Keras layer, so that the output of the first model can be input directly to the second model and trained end-to-end.

Upvotes: 4

Views: 1259

Answers (1)

Joe
Joe

Reputation: 7131

Tensorflow and Keras use Numpy to handle the data. Consider using Numpy functions triu or triu_indices

Upvotes: 2

Related Questions