LSTM + Nvidia GPU causes NotImplementedError - python-3.x

I have Python 3.9.7, TensowFlow == 2.5, NVIDIA-SMI 497.09, Driver Version: 497.09, CUDA Version: 11.5. On trying to define a LSTM model as follows:
n_steps = 500
n_features = 1
# Univariate multi-step time series prediction-
model = Sequential()
model.add(LSTM(units=50, activation='relu', return_sequences=True, input_shape=(n_steps, n_features)))
It first gives a warning:
WARNING:tensorflow:Layer lstm_4 will not use cuDNN kernels since it
doesn't meet the criteria. It will use a generic GPU kernel as
fallback when running on GPU.
And then:
NotImplementedError: Cannot convert a symbolic Tensor
(lstm_4/strided_slice:0) to a numpy array. This error may indicate
that you're trying to pass a Tensor to a NumPy call, which is not
supported
Changing the activation to 'tanh' doesn't change anything.

Check your numpy version. Is it above 1.20? Here's issue #14687 with the same problem, and they point to issue #47691, where they fixed this in tensorflow 2.6.
So, either downgrade to numpy 1.19.2, pip install numpy==1.19.2 or upgrade tensorflow, pip install --upgrade tensorflow. This should hopefully fix the issue!

Related

RuntimeError: cuDNN version incompatibility

I wrote an LSTM NLP classifier with PyTorch, in google colab and it worked well. Now, I run it on google colab pro, but I get this error:
RuntimeError: cuDNN version incompatibility: PyTorch was compiled against (8, 3, 2) but found runtime version (8, 0, 5). PyTorch already comes bundled with cuDNN. One option to resolving this error is to ensure PyTorch can find the bundled cuDNN.one possibility is that there is a conflicting cuDNN in LD_LIBRARY_PATH.
I have no idea how to fix this. I'm using GPU on colab pro.
I've tried this link and it didn't work.
How I declared device:
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
Fixed via upgrading cuDNN to 8.4
reference (https://github.com/JaidedAI/EasyOCR/issues/716)
if you are using google colab uae this command
!pip install --upgrade torch torchvision

Pytorch Training; "Runtime Error:PyTorch and torchvision versions are incompatible ..."

SOLUTION at the bottom!
I want to do Object Detection with this tutorial:
https://towardsdatascience.com/building-your-own-object-detector-pytorch-vs-tensorflow-and-how-to-even-get-started-1d314691d4ae
Although I have compatible versions of Pytorch, Torchvision and Cuda:
conda list torch gives me:
I get the following RunTime Error at the bottom:
RuntimeError: Couldn't load custom C++ ops. This can happen if your
PyTorch and torchvision versions are incompatible, or if you had
errors while compiling torchvision from source. For further
information on the compatible versions, check
https://github.com/pytorch/vision#installation for the compatibility
matrix. Please check your PyTorch version with torch__version__ and
your torchvision version with torchvision__version__ and verify if
they are compatible, and if not please reinstall torchvision so that
it matches your PyTorch install.
when running:
num_epochs = 10
for epoch in range(num_epochs):
train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)#.to_fp16()
lr_scheduler.step()
evaluate(model, data_loader_test, device=device)
Is it really an error resulting from incompatibility of pytorch and torchvision?
Thank you very much.
SOLUTION:
I imported torchvision from the wrong directory. I found out using following:
import torchvision
print(torchvision.__path__)

After installing Tensorflow 2.0 in a python 3.7.1 env, do I need to install Keras, or does Keras come bundled with TF2.0?

I need to use Tensorflow 2.0(TF2.0) and Keras but I don't know if it's necessary to install both seperately or just TF2.0 (assuming TF2.0 has Keras bundled inside it). If I need to install TF2.0 only, will installing in a Python 3.7.1 be acceptable?
This is for Ubuntu 16.04 64 bit.
In Tensorflow 2.0 there is strong integration between TensorFlow and the Keras API specification (TF ships its own Keras implementation, that respects the Keras standard), therefore you don't have to install Keras separately since Keras already comes with TF in the tf.keras package.

module 'tensorflow_hub' has no attribute 'KerasLayer'

When I'm trying to retrain the model with tensorflow it shows an error:
**error module 'tensorflow_hub' has no attribute 'KerasLayer'**
The code is:
print("Building model with", MODULE_HANDLE)
model = tf.keras.Sequential([
hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
trainable=do_fine_tuning),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(train_generator.num_classes,
activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()
The error is like:
1 print("Building model with", MODULE_HANDLE)
2 model = tf.keras.Sequential([
----> 3 hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
4 trainable=do_fine_tuning),
5 tf.keras.layers.Dropout(rate=0.2),
AttributeError: module 'tensorflow_hub' has no attribute 'KerasLayer'
by using the tensorflow hub retrain the previous hub model by adding new dence fully connected layers.when run the code it show the above error.is any have idea about that.please help
Please check the tensorflow version. It should be a recent nightly version.
When I use a version like 1.13.1, I see the following warning before the error, no attribute 'KerasLayer':
W0423 20:04:16.453974 139707130586880 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
After, doing pip install "tf-nightly", everything works fine.
https://www.tensorflow.org/hub
For the BatchNormalizationv1 issue, you can use tf2.0 nightly which should also take care of the original issue.
pip install -U tf-nightly-2.0-preview
https://github.com/tensorflow/tfjs/issues/1255
hub.KerasLayer works with TF2 pre releases:
pip install tf-nightly-2.0-preview --quiet
pip install tensorflow==2.0.0-alpha
pre-release candidate for GPU:
pip install -U --pre tensorflow-gpu

How to use densenet in Keras

I notice densenet has been added to keras (https://github.com/keras-team/keras/tree/master/keras/applications)and I want to apply it in my project but when I tried to import it in jupyter anaconda, I got an error saying:
module 'keras.applications' has no attribute 'densenet'
it seems like densenet has not been incorporated into current version of keras.
Any idea how can I add it myself?
Densenet was added in keras version 2.1.3. What version of keras are you running?
Have you tried to update keras with pip install keras --upgrade since January?

Resources