I try to incorporate a self-designed optimization algorithm PSGLD into TensorFlow. And that algorithm is similar to the concept of RMSProp. So I didn't create a new Op, but complement PSGLD following RMSProp. My procedure of incorporating is as follows:
In Python side, create a
psgld.py
under the folder oftensorflow\python\training
,which represents the Python wrapper. And inpsgld.py
, define the class ofPSGLDOptimizer
.class PSGLDOptimizer(optimizer.Optimizer)
Then, in
tensorflow\python\training\training_ops.py
, define the shape function of_ApplyPSGLDShape
and_SparseApplyPSGLD
, for dense and sparse circumstances respectively.For C++ side, in
tensorflow\core\ops\training_ops.cc
, define the input, output and attribute of ApplyPSGLD Op:
REGISTER_OP("ApplyPSGLD") .Input("var: Ref(T)") .Input("ms: Ref(T)") .Input("mom: Ref(T)") .Input("lr: T") .Input("decay: T") .Input("epsilon: T") .Input("grad: T") .Output("out: Ref(T)") .Attr("T: numbertype") .Attr("use_locking: bool = false")
Meanwhile, also define
ApplyPSGLD
in the header file oftensorflow\core\kernels\training_ops.h
template <typename Device, typename T> struct ApplyPSGLD { ... };
To realize the computation of our algorithm on C++ side, complement corresponding code in the kernel of
tensorflow\core\kernels\training_ops.cc
.
After all, when I run tensorflow/models/image/mnist/convolutional.py
, and the optimizer is adjusted,
optimizer = tf.train.PSGLDOptimizer(learning_rate).minimize(loss, global_step=batch)
an AttributeError happens:
AttributeError: 'module' object has no attribute 'PSGLDOptimizer'
And the environment is TF-0.9, cudnn5. So I ask if someone can give me any advice on this issue or the whole procedure of adding an optimizer.