for some complicated reasons I use both tensorflow and theano in my python code, and I have 2 gpus which I want them to share, but as stated in another question there is some problem, I want to know if there's some trick to achieve that, like specifying tensorflow to use only 1 gpu while theano using another?
for now I can only disable theano's gpu usage by os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float64', and let tensorflow use all
os.environ['KERAS_BACKEND'] = 'theano'
os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float64'
import tensorflow as tf
import keras as ks
I haven't tried this. However if you have multiple GPUs, you can force to run the code on GPU using the following trick:
import tensorflow as tf
with tf.device('/gpu:0'):
# Run the tensorflow code
import tensorflow as tf
with tf.device('/gpu:1'):
# Run the theano code
Hope this helps!
Related
Intellisense works fine on importing phrase
But when it comes with chaining method, it shows different suggestions
Python & Pylance extensions are installed.
From this issue on github
try adding this to the bottom of your tensorflow/__init__.py (in .venv/Lib/site-packages/tensorflow for me)
# Explicitly import lazy-loaded modules to support autocompletion.
# pylint: disable=g-import-not-at-top
if _typing.TYPE_CHECKING:
from tensorflow_estimator.python.estimator.api._v2 import estimator as estimator
from keras.api._v2 import keras
from keras.api._v2.keras import losses
from keras.api._v2.keras import metrics
from keras.api._v2.keras import optimizers
from keras.api._v2.keras import initializers
# pylint: enable=g-import-not-at-top
The problem is because keras is a special class that enables lazy loading and not a normal module.
Edit: With updates to tf, vscode, or something else I'm not having this issue and don't need to use the above fix anymore. I just have to use keras = tf.keras instead of from tensorflow import keras and I have Intellisense working now.
did you try clearing the cache on your system?
Try this
Don't import it directly like this
import tensorflow as tf
import tensorflow.keras as keras
Instead Do
import tensorflow as tf
keras = tf.keras
After this change, Everything was fixed and started showing better suggestions including function documentations
tensorflow.python.keras is for developers only and should not be used, but I think it is fine to be used as "type". I have also read it is a different version than the tensorflow.keras so have this in mind.
# Those are the imports, that actualy load the correct code
import tensorflow.keras as tfk
import tensorflow.keras.layers as layers
# This is for typehinting and intllisense
import tensorflow.python.keras as _tfk
import tensorflow.python.keras.layers as _layers
# This gets highlighted as error by my linter, but it runs
tfk: _tfk
layers: _layers
# from now on, the intellisense and docstrings work
# ...
While keras = tf.keras does the trick, I was dumbstruck that IntelliSense on my home machine wasn't working. Turns out, the Jupyter notebook I was using wasn't using the right Python interpreter (conda environment with tf and keras both # 2.11.0) due to a window reload or whatever.
This worked for me using conda with cuda and tensoflow:
import tensorflow as tf
from tensorflow import keras
from keras.api._v2 import keras as KerasAPI
KerasAPI.applications.ResNet50()
I am very confused after reading a lot about keras and tensorFlow, and still have some basic questions in my mind.
My confusion started from the answer of this question, where he writes keras standalone and from tensorflow.keras import keras.
1- (Python case):
Does keras use any backend when I write this line of code import keras, and No single line of code related to tensorflow e.g tf.keras or tf.keras.layers in my full implementation of the model, but only import keras? if it does, then is there any way to check what backend is being used?
2- Same question in the case of R Language.
3 - Is TensorFlow only used as backend when we write import tensorflow as tf and import tf.keras ?
4- Does import keras and import tf.keras have any discrepency in performance and accuracy in case of python?
5- Does versions of keras and tensorFlow have an impact in performance and accuracy in both language (R and Python) ?
6- What could be the reasons to have 5% accuracy difference in R and Python. Python gives 94%, while the same implementation in R gives 89% accuracy. The versions of keras & tensorFlow in R are 2.3.0, 2.2.0, while the versions in Python are : tf: 2.3.0, keras: 2.4.3. Please see this one.
I have successfully set up TensorFlow 2.1.0 with access to my GPU:
If I use Keras (from tensorflow import keras) to fit some Sequential model (like in example here), will by default be used GPU or CPU for that? Is there some command to see which one is in use by Keras and can I somehow set this up myself? I would really like to see some very basic Keras model trained on GPU vs CPU to have a better feeling about the difference in performance.
Since TensorFlow 2.1, GPU and CPU packages are together in the same package, tensorflow, not like in previous versions which had separate versions for CPU and GPU : tensorflow and tensorflow-gpu.
You can test to have a better feeling in this way:
#Use only CPU
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
Or you can make your video card visible to TensorFlow by either allowing the default configurations just like above, or to force it via:
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
Note that in the above setting, if you had 4 GPUs for example, you would set:
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3'
I'm am getting this error just in the being of importing my packages. I haven't been able to find the correct remedy to fix the issue. Any help is greatly appreciated.
From what I can tell it looks to maybe be a Tensorflow issue?
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from datetime import datetime
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
found this
Background
Colab has two versions of TensorFlow pre-installed: a 2.x version and a 1.x version. Colab uses TensorFlow 2.x by default, though you can switch to 1.x by the method shown below.
Specifying the TensorFlow version
Running import tensorflow will import the default version (currently 2.x). You can use 1.x by running a cell with the tensorflow_version magic before you run import tensorflow.
[ ]
%tensorflow_version 1.x
TensorFlow 1.x selected.
more detail :
https://colab.research.google.com/notebooks/tensorflow_version.ipynb#scrollTo=NeWVBhf1VxlH
You seem to have TensorFlow 2.x while the bert module uses TensorFlow 1.x. You can verify here that Tensorflow 1.x has the tf.train.Optimizer module while according to this, Tensorflow 2.x has no such module.
Make sure you install the Tensorflow version that bert requires
I have a problem with Keras and multiprocessing. I have already searched a lot and I found a lot of questions with the same subjects:
Importing Keras breaks multiprocessing
Keras + Tensorflow and Multiprocessing in Python
(and lot more)
I tried these solutions, so basically importing Keras after the multiprocessing has been instantiated. In actual fact, I see this message:
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Using TensorFlow backend.
Before this message was only printed one time, so I assume that the backend is different, however, my processes are running on the same core. If run again the main process, it creates more processes that run in the same processor as well. It seems that something blocks the execution on different processors.
Any idea on how to fix it?
PS: I am using the second solution I have linked, in particular the following :
DO NOT LOAD KERAS TO YOUR MAIN ENVIRONMENT
The problem was in the installation of tensorflow and keras. The methods for achieving parallelization are correct.
The tensorflow documentation clearly states that is highly suggested to install the package using pip as the conda package is maintained only by the community (https://www.tensorflow.org/install/pip).
I fixed the problem uninstalling keras and tensorflow and reinstalling them with:
pip install tensorflow
pip install keras