Theano AttributeError: 'module' object has no attribute 'relu' - python

I want to use theano.tensor.nnet.relu, but I keep getting this error:
AttributeError: 'module' object has no attribute 'relu'
I have already updated theano via sudo pip install --upgrade theano command as described in theano's documentation, also i've tried sudo pip install --upgrade --no-deps theano. Neither worked, I still get the same error.
I was trying to do theano -v to confirm that I have installed the latest version but then I get the following error: -bash: theano: command not found
So my two questions here are:
How can I see theano's version?
Am I doing something wrong when updating theano? How can I solve the error first mentioned?

relu is available for theano >= 0.7.1. My guess is that pip linked to theano==0.7.
You can check theano version with pip freeze:
pip freeze | grep Theano
So you have to install latest theano with pointing pip to theano git repo :
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
Also note that relu is function and not module, so to access it you have to use one of imports below:
from theano.tensor.nnet import relu # access `relu` as is ..
import theano.tensor.nnet as theano_nnet #access `relu` as `theano_nnet.relu`

The theano.tensor.nnet module only supports relu in the most recent versions. In order to use it, you need to install the bleeding edge version from github or wait until the next release.
Alternatively, you can implement it like this:
def relu(x):
return T.maximum(x, 0.)
This may not be inplace but it gives you the result and the gradient.

to see theano version you can run the following code:
import theano
print theano.__version__
You should follow the instructions from here to get the bleeding edge version
Actually relu function is simple to code, you can create your own relu function, like eickenberg's answer, or like theano.tensor.nnet style:
def relu(x):
return 0.5 * (x + abs(x))

Related

Setting up keras-rl2 on my M1 Macbook Pro

I am working on a project on Reinforcement Learning - and completely new at this. I installed keras-rl as
pip install keras-rl, however it caused an error as many has mentioned:
TypeError: Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.
And the solution for this is to used keras-rl2. Howver I cannot install keras-rl2 on my Mac M1 as the output is following:
ERROR: Cannot install keras-rl2==1.0.0, keras-rl2==1.0.1, keras-rl2==1.0.2, keras-rl2==1.0.3, keras-rl2==1.0.4 and keras-rl2==1.0.5 because these package versions have conflicting dependencies.
When I tried a specific version, i.e
pip install keras-rl2==1.0.5, the output is:
ERROR: Could not find a version that satisfies the requirement tensorflow (from keras-rl2) (from versions: none)
ERROR: No matching distribution found for tensorflow
I have tensorflow 2.6.0 installed by Apple Developer guide. Any idea how I can install keras-rl2?
1. To install Tensorflow on M1 Macs
https://developer.apple.com/metal/tensorflow-plugin/
2. To Install Keras-rl2
Open a terminal window and run these commands from:
https://github.com/keras-rl/keras-rl (last line changed)
git clone https://github.com/wau/keras-rl2.git
cd keras-rl
python3 setup.py install
The setup.py script will still tell you that it can't find a TensorFlow version but you will still be able to import it using:
import rl

module 'keras.engine' has no attribute 'Layer'

I tried to run matterport/MaskRCNN code but faced the following error
----> 6 from mrcnn.model import MaskRCNN
/usr/local/lib/python3.7/dist-packages/mrcnn/model.py in <module>()
253
254
--> 255 class ProposalLayer(KE.Layer):
256 """Receives anchor scores and selects a subset to pass as proposals
257 to the second stage. Filtering is done based on anchor scores and
AttributeError: module 'keras.engine' has no attribute 'Layer'
I found this in the github issue discussion and it worked for me.
You need to uninstall those :
pip uninstall keras -y
pip uninstall keras-nightly -y
pip uninstall keras-Preprocessing -y
pip uninstall keras-vis -y
pip uninstall tensorflow -y
pip uninstall h5py -y
and impose those versions :
pip install tensorflow==1.13.1
pip install keras==2.0.8
pip install h5py==2.10.0
For lines where you are using Layers like ProposalLayer(KE.Layer)
Instead of using KE.Layer do
import keras.layers as KL
and replace all instances of KE by KL
I encountered this problem when I was running the project.
https://github.com/matterport/Mask_RCNN
In the file model.py,
there was a line
import keras.engine as KE
I changed it to
import keras.engine.topology as KE
and the problem disappeared.
Installing tensorflow with version as following
pip uninstall tensorflow -y
pip uninstall keras -y
pip install tensorflow==2.4.3
pip install keras==2.4.0
After above, some errors will arise. You could solve them by following steps.
#Error: [module 'tensorflow' has no attribute XXXXXXXX]
In the model.py or your code, resolving some api with tf.compat.v1, e.g. tf.compat.v1.Session or import tensorflow.compat.v1 as tf
#Error: [ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.]
mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
replace with this this if-else code block:
if s[1]==None:
mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
else:
mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
#Error: [ValueError: None values not supported.]
indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
replace with
indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
#Error: [AttributeError: module 'keras.engine.saving' has no attribute 'load_weights_from_hdf5_group_by_name']
from keras import saving
replace with
from tensorflow.python.keras.saving import hdf5_format
and
saving.load_weights_from_hdf5_group(f, layers)
saving.load_weights_from_hdf5_group_by_name(f, layers)
replace with
hdf5_format.load_weights_from_hdf5_group(f, layers)
hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
Reference:
Tried to convert 'shape' to a tensor and failed. Error: None values not supported
module 'keras.engine.saving' has no attribute 'load_weights_from_hdf5_group_by_name'
This isn’t strictly a duplicate, but a similar question is found here: AttributeError: module 'keras.engine' has no attribute 'input_layer'
In essence, many of the import and attribute errors from keras come from the fact that keras changes its imports depending on whether you are using a CPU or using a GPU or ASIC. Some of the engine classes don’t get imported in every case.
Instead, use from keras.layers import Layer and use that layer class in place of the one from the engine.
You should write keras.layers
instead keras.engine at import section in model.py file
When running the https://github.com/matterport/Mask_RCNN repository, I faced also all aforementioned issues. After some days, I finally found a way how to run this repository which I would like to share with you:
First, I installed WSL2 + Ubuntu 20.04 GUI (https://medium.com/#japheth.yates/the-complete-wsl2-gui-setup-2582828f4577) and then created the following environment:
conda create tf1_maskrcnn python=3.6 -y
conda activate tf1_maskrcnn
pip install -r requirements.txt
python setup.py install
It should be noted that I have adjusted requirements.txt:
numpy==1.19.5
scipy==1.5.4
Pillow==8.4.0
cython==0.29.28
matplotlib==3.3.4
scikit-image==0.17.2
tensorflow==1.3.0
keras==2.0.8
opencv-python==4.5.5.64
h5py==2.10.0
imgaug==0.4.0
ipykernel
pycocotools
Even though the https://github.com/akTwelve/Mask_RCNN repository which is based upon TensorFlow 2 is available, the pre-trained weights - which are automatically downloaded or can be retrieved from https://github.com/matterport/Mask_RCNN/releases - lead to unsatisfactory results. However, if this repository is used to train the model from scratch it should definitely be preferred over the tf1 version. Nonetheless, if the intention is to see how well this model works on a different dataset, which requires proper pre-trained weights, the tf1 version is the repository to go with.
Peronal option: As most Github repositories concerning deep learning computer vision tasks are tested on Ubuntu, implementing those models on Windows often lead to a multitude of errors which can be avoided by using a virtual machine. The main advantage by using WSL + Ubuntu 20.04 GUI is that it is much faster than using virtual machines. Even though some time needs to be invested at the beginning it is worth investigating this option.
Tried and Tested on 23-10-2022
GoTo matterpot's Mask_RCNN/mrcnn/model.py file
Search for KE. and replace with KL.
the code runs fine and able to download coco weights.
I struggled for a bit with both the outdated Keras and Tensorflow versions on this Mask-RCNN repo.
If you want to use the current versions, instead of replacing lines I recommend you cloning the following repo:
https://github.com/akTwelve/Mask_RCNN
It has been updated to run on tensorflow v2+ and keras v2+.
Where there is KE put KL in the mrcnn/model.py file it worked for me

Why do I get RuntimeError: CUDA error: invalid argument in pytorch?

Recently I've frequently been getting RuntimeError: CUDA error: invalid argument when calling functions like torch.cholesky e.g.:
import torch
a = torch.randn(3, 3, device="cuda:0")
a = torch.mm(a, a.t()) # make symmetric positive-definite
torch.cholesky(a)
This works fine if I use device="cpu" instead. This error isn't very descriptive, so I'm not sure what's wrong here.
I discovered that this error was because the machine I'm running things on has CUDA 10 installed now, but I just installed pytorch as pip install torch. From their website, the proper way to install with pip and CUDA 10 is pip install https://download.pytorch.org/whl/cu100/torch-1.1.0-cp37-cp37m-linux_x86_64.whl.

AttributeError in Theano

I am trying to run the installation test of the Theano with the following code:
import theano
theano.test()
However, I would see the following error corresponding to blas.py:
/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/theano/tensor/blas.py in default_blas_ldflags()
301 try:
302 #if numpy was linked with library that are not installed, we can't reuse them.
--> 303 if all(not os.path.exists(dir) for dir in numpy.distutils.__config__.blas_opt_info['library_dirs']):
304 return "-lblas"
305 return ' '.join(
AttributeError: 'module' object has no attribute '__config__'
I understand that AttributeError is a famous error and there are questions asked about it, but for Theano the only solution I found on the internet was to add:
import numpy.distutils.config
to the blas.py. However, this does not solve the problem and I am still facing the AttributeError.
This is fixed in Theano development version.
Use one of those 2 command to update Theano depending if the installation is just for your user or for the OS:
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git --user
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
Here is the doc for that update:
http://www.deeplearning.net/software/theano/install.html#bleeding-edge-install-instructions

spyder: python: theano: How to disable warnings in spyder?

I running machine learning algo's with theano. I have been getting lot of warnings of DeprecationWarning. coming from numpy package. I want to disable this warnings pls suggest option.
warning nature:fromnumeric.py:932: DeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future
I tried following run configure added command line option -W ignore or -W ignore::DeprecationWarning but none of this is working
alternatively fixing the warning solution is fine for me. looks like its fixed in theano https://groups.google.com/forum/#!topic/theano-users/Hf7soRrnh8w but I dont know where to find this updated version of theano
I am using Anaconda distribution 2.0.1 windows 8.1 - 64 bit
thanks
See this link to update Theano to the development version. I should be fixed:
http://deeplearning.net/software/theano/install.html#bleeding-edge-install-instructions
In resume, run one of those 2 command:
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
or (if you want to install it for the current user only):
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git --user

Categories