#torch.no_grad equivalent in tensorflow - tensorflow

In pytorch, we can define #torch.no_grad before a function in class(nn.Module) to stop gradient, does tensorflow has some similar function?
I found a function
with tf.GradientTape() as tape:
 
  with tape.stop_recording():
in tensorflow, but I am struggled with how to properly feed the tape into the model

Related

How to stop gradient recording in tensorflow

In pytorch we use torch.no_grad() when applying gradients to trainable variables.
But I use tensorflow instead of keras (I use tensorflow graph execution) for my ml projects.Here is the code of train_step function
#tf.function
def train_step(data, target):
y_pred = model(data, training=True)
loss = loss_fn(target, y_pred)
gradients = tf.gradients(loss, model.trainable_variables)
# how to do => with torch.no_grad() <= operation in tensorflow to update model parameters.
Now,want I want to know is how to prevent my variables from being recorded while I applying gradients to model variables
Thank you!

Custom gradient in tensorflow attempts to convert model to tensor

I am trying to use the output of one neural network to compute the loss value for another network. As the first network is approximating another function (L2 distance) I would like to provide the gradients myself, as if it had come from an L2 function.
An example of my loss function in simplified code is:
#tf.custom_gradient
def loss_function(model_1_output):
def grad(dy, variables=None):
gradients = 2 * pred
return gradients
pred = model_2(model_1_output)
loss = pred ** 2
return loss, grad
This is called in a standard tensorflow 2.0 custom training loop such as:
with tf.GradientTape() as tape:
model_1_output = model_1(training_data)
loss = loss_function(model_1_output)
gradients = tape.gradient(loss, model_1.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables)
However, whenever I try to run this I keep getting the error:
ValueError: Attempt to convert a value (<model.model_2 object at 0x7f41982e3240>) with an unsupported type (<class 'model.model_2'>) to a Tensor.
The whole point of using the custom_gradients decorator is that I don't want the model_2 in the loss function to be included in the back propagation as I give it the gradients manually.
How can I make tensorflow completely ignore anything inside the loss function? So that for example I could do non-differetiable operations. I have tried using with tape.stop_recording() but I always result in a no gradients found error.
Using:
OS: Ubuntu 18.04
tensorflow: 2.0.0
python: 3.7

Using kohannenkappa loss function in keras models

Trying to use non keras backend functions for custom loss calculation in keras models.
I am trying to make my keras cnn model use a custom loss function ( KAppa score). However since kappas is not defined in Keras backend , i need to used scikit-learn based kappa implementation. This sklearn function takes array of labels as the argument unlike keras backend functions which take tensors. The loss function call within keras mostly sends tensors Y_pred and Y_true. I did the implementation below using some quide i found online but I get errors .
import keras.backend as K
def cohen_kappa_score_func(y_true, y_pred):
sess = tf.Session()
with sess.as_default():
score = cohen_kappa_score(type(y_true.eval()),type(y_pred.eval()), weights='linear')#idea is to convert the tensor to array
sess.close()
return score
#use this later to compile the keras model with custom loss function as
model.compile(optimizer=optimizers.SGD(lr=0.001, momentum=0.9),
loss=cohen_kappa_score_func,
metrics=['categorical_crossentropy', 'mae','categorical_accuracy'])
This doesnt work and i get the following error
"InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_15_target' with dtype float and shape [?,?]
[[node dense_15_target "
Please give me suggestios to solve this.

What is the default activation function of cudnnlstm in tensorflow

What's the default activation function of cudnnlstm in TensorFlow? How can I set an activation function such as relu? Maybe it's just linear model? I read the document, but I did not find it.
For example, the code is below:
lstmcell=tf.contrib.cudnn_rnn.CudnnLSTM(1,encoder_size,direction="bidirectional")
hq,_ =lstmcell(query)
And I read the document of TensorFlow From this link.
The function is below
__init__(
num_layers,
num_units,
input_mode=CUDNN_INPUT_LINEAR_MODE,
direction=CUDNN_RNN_UNIDIRECTION,
dropout=0.0,
seed=None,
dtype=tf.float32,
kernel_initializer=None,
bias_initializer=None,
name=None
)
And no keyword to set a parameter such as "activation = "tanh" just like tf.nn.rnn_cell.LSTMell.
So what's the default activation function of cudnnlstm in TensorFlow, and how to change it to leaky_relu.
tf.contrib.cudnn_rnn.CudnnLSTM() : Tanh
This was given in the Keras github.
https://github.com/keras-team/keras/issues/8510#issuecomment-429255318
Nvidia documentation.
https://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/
To answer OP's 2nd question which was edited in later, there is currently no way to set a custom activation function for CudnnLSTM and CudnnGRU.

How to implement tensorflow Estimator with multiple models for GAN?

I used to work with tensorflow and then found Keras. It's easy to train multiple models in Keras using API like
for i in range(nb_batches):
model1.train_on_batch(x, y)
model2.train_on_batch(x, y)
Now I need to turn back to tensorflow for its high flexibility. The problem is there is no model in tensorflow, and it seems Estimator is something like model in Keras. However, Estimator API only provides a model_fn and a parameter arguments, which seems restricted us using only one model_fn.
I'm wondering if we can implement Estimator such that we can use some syntax like:
for i in range(nb_batches):
...
my_estimator.partial_fit(x, y, MODEL='gen')
...
my_estimator.partial_fit(x, y, MODEL='cri')