Importing keras - python-3.x

I'm trying to import keras and the code returns an error about tensorflow.
import numpy
import matplotlib.pyplot as plt
import pandas
import math
from keras.models import Sequential
from keras.layers import Dense
and the error says:
Using TensorFlow backend.
Traceback (most recent call last):
File "C:/Users/gonza/Documents/Projects/jeremiah/neuralNet.py", line 6, in <module>
from keras.models import Sequential
File "C:\Users\gonza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Users\gonza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
from . import conv_utils
File "C:\Users\gonza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
from .. import backend as K
File "C:\Users\gonza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
from .tensorflow_backend import *
File "C:\Users\gonza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

It seems like tensorflow is not found. You need to install tensorflow in order to use keras library.
If you already installed tensorflow, try to uninstall and install it again.
sudo pip3 uninstall tensorflow
pip3 install --upgrade tensorflow
You can verify the install by running this command:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

This error is related to TensorFlow. TensorFlow may not be installed or installed incorrectly. please check TensorFlow installation

Related

error while "from sklearn import datasets"

from sklearn import datasets
When running the above code, I got an error:
`Traceback` (most recent call last):
File "perceptron_ml.py", line 2, in <module>
from sklearn import datasets
File "/home/remember/.local/lib/python3.6/site-packages/sklearn/datasets/__init__.py", line 22, in <module>
from ._twenty_newsgroups import fetch_20newsgroups
File "/home/remember/.local/lib/python3.6/site-packages/sklearn/datasets/_twenty_newsgroups.py", line 45, in <module>
from ..feature_extraction.text import CountVectorizer
File "/home/remember/.local/lib/python3.6/site-packages/sklearn/feature_extraction/__init__.py", line 8, in <module>
from ._hashing import FeatureHasher
ImportError: cannot import name 'FeatureHasher'
Ubuntu: 18.04, Python: 3.6.9, Numpy: 1.17.4, scikit-learn: 0.22
The error is due to a glitch happening during the update due to pip. Related SO post is https://stackoverflow.com/a/59355676/6513708
But long story short, you need to remove the following files which were let by pip during the update:
~/.local/lib/python3.6/site-packages/sklearn/decomposition/_online_lda.cpython-36m-x86_64-linux-gnu.so
~/.local/lib/python3.6/site-packages/sklearn/feature_extraction/_hashing.cpython-36m-x86_64-linux-gnu.so
~/.local/lib/python3.6/site-packages/sklearn/datasets/_svmlight_format.cpython-36m-x86_64-linux-gnu.so

How to fix ' module 'keras.backend.tensorflow_backend' has no attribute '_is_tf_1''

While training the yolov3 framework, there's always this module error
I have tried reinstalling keras and tensorflow, and the version of keras is 2.3.0 and the version of tensorflow is 1.14.0.
Traceback (most recent call last):
File "train.py", line 6, in <module>
import keras.backend as K
File "F:\Anacoda\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "F:\Anacoda\lib\site-packages\keras\utils\__init__.py", line 27, in <module>
from .multi_gpu_utils import multi_gpu_model
File "F:\Anacoda\lib\site-packages\keras\utils\multi_gpu_utils.py", line 7, in <module>
from ..layers.merge import concatenate
File "F:\Anacoda\lib\site-packages\keras\layers\__init__.py", line 4, in <module>
from ..engine.base_layer import Layer
File "F:\Anacoda\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
from .training import Model
File "F:\Anacoda\lib\site-packages\keras\engine\training.py", line 21, in <module>
from . import training_arrays
File "F:\Anacoda\lib\site-packages\keras\engine\training_arrays.py", line 14, in <module>
from .. import callbacks as cbks
File "F:\Anacoda\lib\site-packages\keras\callbacks\__init__.py", line 19, in <module>
if K.backend() == 'tensorflow' and not K.tensorflow_backend._is_tf_1():
AttributeError: module 'keras.backend.tensorflow_backend' has no attribute '_is_tf_1'
I fix this problem by replacing keras.XXX to tensorflow.keras.XXX
try replace
import keras.backend as K
to
import tensorflow.keras.backend as K
import this:
import tensorflow as tf
then use
tf.compat.v1.keras.backend.
as prefix of your desired attribute
I had the same error and tried installing and uninstalling. In the end, I found that the library was not actually installed correctly.
I went through each library in my:
C:\Users\MyName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\
I tracked down the file in within the site-packages in Keras, which was calling from the Tensorflow library, which was calling from another folder. I found the final folder had the get_session(), but this was not being called in. When I checked the directory, I found that get_session wasn't being loaded in. Within the file directory /tensorflow/keras/backend.py it was importing everything, but missed out the get_session.
To fix this I added this line:
from tensorflow.python.keras.backend import get_session
Then saved it. The next time I ran my code it was fine. I would suggest doing similar with your files, locating where the attribute is called in and making sure it is being imported.
pip3 uninstall keras
pip3 install keras --upgrade
https://github.com/keras-team/keras/issues/13352
installing TensorFlow 1.14.0 + Keras 2.2.5 on Python 3.6 fix this
make sure your keras version is right. if your backbend is tensorflow,you can
import tensorflow as tf
print(tf.VERSION)
print(tf.keras.__version__)
get the right version of keras,then install this version,I fix this problem by this way,i hope my answer can help you.

I got import error when I tried to import torchvision

When I tried to import torchvision, I got an import error.
I googled this problem but found nothing except that the problem may be in Cython. StackOverflow search didn't found anything either. How can I solve this?
Torchvision version is 0.3.0.
import torchvision
Traceback (most recent call last):
File "/home/ai/PycharmProjects/untitled/TRANSFER.py", line 4, in
import torchvision
File "/home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/init.py",
line 1, in
from torchvision import models
File "/home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/models/init.py",
line 11, in
from . import detection
File "/home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/models/detection/init.py",
line 1, in
from .faster_rcnn import *
File "/home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/models/detection/faster_rcnn.py",
line 7, in
from torchvision.ops import misc as misc_nn_ops
File "/home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/ops/init.py",
line 1, in
from .boxes import nms, box_iou
File "/home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/ops/boxes.py",
line 2, in
from torchvision import _C
ImportError: /home/ai/anaconda3/envs/reinforcement/lib/python3.5/site-packages/torchvision/_C.cpython-35m-x86_64-linux-gnu.so:
undefined symbol: _PyThreadState_UncheckedGet
Try this for anaconda:
conda install torchvision -c pytorch
Using pip:
pip install torchvision
Problem solved: the problem was in Python 3.5, using Python 3.6 solves problem.

Error while importing datasets from sklearn

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
it gives the following error-
Traceback (most recent call last):
File "/home/songoku/PycharmProjects/untitled/sklearn.py", line 4, in <module>
from sklearn import datasets
File "/home/songoku/PycharmProjects/untitled/sklearn.py", line 4, in <module>
from sklearn import datasets
ImportError: cannot import name 'datasets'
Rename /home/songoku/PycharmProjects/untitled/sklearn.py to something that differs from the SKLearn module name: sklearn.py
Try checking the version of sklearn first. The modules like data-sets and svm is not a part of sklearn packages (0.19 <) i.e less than version number 0.19.0.
You can check this on the command line: pip3 list
it shows you the version of all libraries installed by pip3.
Note: Always double check if you istalled the libraries as a part of anaconda or pip3

Keras-ImportError: cannot import name ctc_ops

I have Anaconda 4.3.1 installed in my laptop after the commands cd /home/username/anaconda3and source bin/activate ~/anaconda3 my package starts .
And I installed keras using pip3 conda install keras inside my anaconda. package.and made a document named keras.json inside my keras folder.
When I start my Python in anaconda3 and import keras this happens
>>> import keras'
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tushar/.local/lib/python3.5/site-packages/keras/__init__.py", line 3, in <module>
from . import activations
File "/home/tushar/.local/lib/python3.5/site-packages/keras/activations.py", line 3, in <module>
from . import backend as K
File "/home/tushar/.local/lib/python3.5/site-packages/keras/backend/__init__.py", line 64, in <module>
from .tensorflow_backend import *
File "/home/tushar/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 6, in <module>
from tensorflow.python.ops import ctc_ops as ctc
ImportError: cannot import name 'ctc_ops'
After following this I could not get any solution. I would appreciate any help.
I had this same exact issue (I was using pip instead of conda to install keras / tensorflow, however). Make sure you have the most up-to-date version of tensorflow (tensorflow-1.0.1 as of March 31st, 2017).
sudo pip install tensorflow --upgrade
fixed the problem for me (I was running tensorflow-0.9.0rc0 before and was getting the same ImportError: cannot import name ctc_ops error as you)

Resources