I'm looking to do SVD for a custom optimizer in Keras (specifically, I want to port the the Shampoo optimizer to Keras.
In Tensorflow, I would use tensorflow.python.ops.linalg_ops.svd(), however, there is no function like this in keras.backend.
Can SVD be carried out in a purely Keras setting, or can I somehow use the Tensorflow function directly (and if so, how)?
EDIT: Just for future reference, there actually exists a wrapper function allowing the direct use of a native tf optimizer in Keras:
import keras as ks
from tensorflow.contrib.opt import AdamWOptimizer
tfopt = AdamWOptimizer()
ksopt = ks.optimizers.TFOptimizer(tfopt)
Unfortunately though, it does not seem to work with the Shampoo optimizer specifically.
If you are using keras with a tensorflow backend, than keras backend is tensorflow.
This means that when you call a method from keras backend, it actually calls a method of tensorflow.
Therefore you could use both keras backend operations and tensorflow together and interchangeably.
For example, in the given code:
tensor = ...
m = K.mean(tensor)
...
I could change the line K.mean(tensor) to tf.mean(tensor)
tensor = ...
m = tf.mean(tensor)
...
So you can just use the tensorflow SVD operation as you would use it if it was a function of keras backend :)
For example if you would like to have
tensor = ...
res = K.some_submodule.svd(tensor)
...
Than you can instead just do
tensor = ...
res = tensorflow.python.ops.linalg_ops.svd(tensor)
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With