I want to calculate and adapt the gradient by myself in tensorflow. I want to calculate the gradient for one of the multiple outputs by myself, and adapt 0 for the rest of the outputs.
I have tested building the model with the following simple program and trying to adapt it by setting one gradient to a constant value and the other gradient to 0, but I am not getting the desired output.
What should I give to apply_gradients?
I don't know much about tensorflow, so I would appreciate if you could kindly tell me about it
def cmodel():
in_ = Input(shape=(1,))
x=in_
x=Dense(16)(x)
x=Activation('relu')(x)
x=Dense(2)(x)
x=Activation('linear')(x)
model = Model(inputs=in_, outputs=[x])
return model
model=cmodel()
model.compile(optimizer=Nadam(learning_rate=0.005))
mylist2=[]
for i in range(100):
g=tf.constant([-0.3,0])
model.optimizer.apply_gradients(zip(g, model.trainable_variables))
print('num:',model.predict(np.array([1])))
mylist2.append(model.predict(np.array([1]))[0])