Wrong tensorflow version used after !pip install tf-nightly - python

I'm using some code written by my lecturer & I'm running it on a GPU in Google Colab. At the beginning of the code the tf-nightly package is installed using !pip install tf-nightly. Afterwards, the following packages are imported and the version of tf used is printed out as follows:
from tensorflow import keras
from tensorflow.keras import layers
# Helper libraries
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from mpl_toolkits.axes_grid1 import make_axes_locatable
print(tf.__version__)
For some reason, sometimes the version of tf used isn't always 2.3.xxx but instead sometimes it's 2.2 - but not always. Usually this happens when the tab has been open for a while but I haven't been using it.
This then causes the error
"AttributeError: module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'" when I try to use this package.
Can anyone tell me how to stop this from happening/what to do if it does happen?
Thank you very much in advance.

Related

ModuleNotFoundError: No module named 'tensorflow' on Jupyter Notebook

when I try to import keras on a Jupyter notebook I receive this error message: ModuleNotFoundError: No module named 'tensorflow'.
I need to use keras to build an LSTM model fora project and am a beginner in python so I don't know how to get around this error. I have tried to install tenserflow and keras into an environment on anaconda but I receive the same error. For reference, I am using a Mac.
My code:
#Import libraries used
import math
import pandas as pd
import pandas_datareader.data as web
import datetime as dt
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, LSTM
Any help would be greatly appreciated!

module 'sklearn.preprocessing' has no attribute 'Normalization'

I try to to use the preprocessing method from sklearn. I found other threads with the same issue but it didn't helped me to uninstall my existing sklearn versions. I think my problem is still that I have somewhere on my mac a scikit package. I just wonder how to fix this error?
My imports:
!pip install -q git+https://github.com/tensorflow/docs
import matplotlib.pyplot as plt
import numpy as np
import pandas as p
My code where the error line is dropped:
normalizer = preprocessing.Normalization(axis=-1)
Error:
AttributeError: module 'sklearn.preprocessing' has no attribute 'Normalization'

Why does %config line give me syntax error in Python 3.7?

I am creating a system in order to predict earthquakes. There's this code in which there is a line %config InlineBackend.figure_format = 'retina'.
Whenever I run my python code it gives the error as ("% invalid syntax").
Earlier I had the problem of %matplotlib statement which I figured out through google. But, this problem is sucking me.
https://www.kaggle.com/pablocastilla/predict-earthquakes-with-lstm ("The source from where the code has been taken")
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from subprocess import check_output
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from sklearn.cross_validation import train_test_split
import time #helper libraries
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from numpy import newaxis
from geopy.geocoders import Nominatim
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
import warnings
warnings.filterwarnings('ignore')
%config InlineBackend.figure_format = 'retina'
# Any results you write to the current directory are saved as output.
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))
So it is showing "error invalid syntax " and the % sign before the word config has been highlighted.
The expected output is that I should not recieve an error and any results that are written to the directory are given out.
That % syntax is defining a IPython Magic Command. Read: Magic commands
Give this a try
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')
Taken from Configure the backend of Ipython to use retina display mode with code
You need to install Ipython library in order to do this. Also if you get "No module named ipykernel" then install ipykernel.
pip install ipython
pip install ipykernel
Edit: but I highly recommend using jupyter notebooks for example give google colab a try : https://colab.research.google.com/notebooks/welcome.ipynb
Commands starting with % are not valid python commands, they are only supported by ipython/jupyter interpreter, not by the official python interpreter.

In-code solution to remove warnings in my Python code

I have a deep learning python code (Anaconda3, Ubuntu 16.04) which uses TensorFlow for some machine learning algorithms on video inputs. It imports the following packages:
import time, os, glob, subprocess
import skvideo.io
import numpy as np
import tensorflow as tf
import time, cv2, librosa
import skvideo.io
import numpy as np
import tensorflow as tf
from sklearn.externals import joblib
When running, it get the following warning. How can I remove this warning (or hide it from printing)?
/home/user/anaconda3/lib/python3.5/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator SVC from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
UserWarning)

How to import packages from Anaconda instead of default pip directory

When using iPython and Jupyter Notebook, the commands
import numpy as np
import pandas as pd
always try to import the packages from the default python directory:
C:/Users/sx449_000/AppData/Roaming/Python/Python36/site-packages
How can I change it to make it import from my anaconda site packages as below?
C:/programdata/miniconda3/lib/site-packages
First, make sure you are using the right kernel in your ipython notebook, otherwise, you can change it from kernel >> change kernel
You can also try,
import sys
sys.path.append('C:/programdata/miniconda3/lib/packages/numpy')
import numpy as np

Categories

Resources