I’m trying to implement SPSA(Simultaneous perturbation stochastic approximation) as a subclass of the optimizer class in TensorFlow. When I call loss in compute_gradients() method I get an error “TypeError: ‘Tensor’ object is not callable”. How can this be fixed?
def compute_gradients(self, loss, var_list, tape=None):
if not callable(loss) and tape is None:
raise ValueError(
"`tape` is required when a `Tensor` loss is passed. "
f"Received: loss={loss}, tape={tape}."
)
if tape is None:
tape = tf.GradientTape()
if callable(loss):
with tape:
if not callable(var_list):
tape.watch(var_list)
loss = loss()
if callable(var_list):
var_list = var_list()
grads = []
gamma = tf.cast(self.gamma, dtype=tf.float32)
for var in var_list:
if var is not None:
step = tf.cast(self.iterations + 1, dtype=tf.float32)
c_t = self.c / step ** gamma
delta = np.random.uniform(0, 1, size=var.shape)
delta[delta < 0.5] = -1
delta[delta > 0.5] = 1
y_plus = loss(var + tf.multiply(c_t, delta))
y_minus = loss(var - tf.multiply(c_t, delta))
delta_norm = tf.cast(tf.norm(delta), dtype=tf.float32)
grad = (y_plus - y_minus)
grad /= (2*tf.norm(c_t)*delta_norm)
else:
grad = None
grads.append(grad)
return list(zip(grads, var_list))
New contributor
daffeu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.