How to apply gradient clipping in TensorFlow?

Gradient clipping needs to happen after computing the gradients, but before applying them to update the model’s parameters. In your example, both of those things are handled by the AdamOptimizer.minimize() method. In order to clip your gradients you’ll need to explicitly compute, clip, and apply them as described in this section in TensorFlow’s API documentation. … Read more

Failed to get convolution algorithm. This is probably because cuDNN failed to initialize,

I’ve seen this error message for three different reasons, with different solutions: 1. You have cache issues I regularly work around this error by shutting down my python process, removing the ~/.nv directory (on linux, rm -rf ~/.nv), and restarting the Python process. I don’t exactly know why this works. It’s probably at least partly … Read more

How do you create a custom activation function with Keras?

Credits to this Github issue comment by Ritchie Ng. # Creating a model from keras.models import Sequential from keras.layers import Dense # Custom activation function from keras.layers import Activation from keras import backend as K from keras.utils.generic_utils import get_custom_objects def custom_activation(x): return (K.sigmoid(x) * 5) – 1 get_custom_objects().update({‘custom_activation’: Activation(custom_activation)}) # Usage model = Sequential() model.add(Dense(32, … Read more

Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)

TL;DR Several possible errors, most fixed with x = np.asarray(x).astype(‘float32′). Others may be faulty data preprocessing; ensure everything is properly formatted (categoricals, nans, strings, etc). Below shows what the model expects: [print(i.shape, i.dtype) for i in model.inputs] [print(o.shape, o.dtype) for o in model.outputs] [print(l.name, l.input_shape, l.dtype) for l in model.layers] The problem’s rooted in using … Read more

Could not load dynamic library ‘cudart64_101.dll’ on tensorflow CPU-only installation

Tensorflow 2.1+ What’s going on? With the new Tensorflow 2.1 release, the default tensorflow pip package contains both CPU and GPU versions of TF. In previous TF versions, not finding the CUDA libraries would emit an error and raise an exception, while now the library dynamically searches for the correct CUDA version and, if it … Read more

How to get reproducible results in keras

You can find the answer at the Keras docs: https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development. In short, to be absolutely sure that you will get reproducible results with your python script on one computer’s/laptop’s CPU then you will have to do the following: Set the PYTHONHASHSEED environment variable at a fixed value Set the python built-in pseudo-random generator at a … Read more