Reputation: 135
I would like to invert a bunch of tensors in a list using cholesky decomposition in tensorflow 2, but the resulting code is quite ugly. is there any elegant / more pythonic way to do something like this :
iMps = []
for Mp in Mps :
cholMp = tf.linalg.cholesky(Mp)
icholMp = tf.linalg.inv(cholMp)
iMp = tf.tensordot(tf.transpose(icholMp),icholMp)
iMps.append(iMp)
is it possible to replace for loop with other stuff ?, Mps is list of tensors with different size (can i represent it as something else?). is there any way to make it more elegant ?
Upvotes: 1
Views: 584
Reputation:
You can achieve this using python Map function.
I have modified your code to create Map function like below.
def inverse_tensors(Mp):
cholMp = tf.linalg.cholesky(Mp)
icholMp = tf.linalg.inv(cholMp)
iMp = tf.tensordot(tf.transpose(icholMp),icholMp,axes=0)
return iMp
iMps = list(map(inverse_tensors,list_tensors))
Hope this answers your question, Happy Learning!
Upvotes: 1