0

Have not posted much on stackoverflow and new to tensorflow. I followed this tutorial on how to install tensorflow for use on jupyter notebooks (https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/tensorflow-install-mac-jan-2021.ipynb).

When testing the environment I get Python 3.8.11, Tensor Flow 2.5.1 and Keras 2.5.0

Code I am trying to run:

import pandas as pd
import numpy as np
import tensorflow as tf
import re
from nltk.corpus import stopwords
from sklearn.model_selection import train_test_split
from sklearn.metrics import median_absolute_error as mae
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import accuracy_score as acc
import matplotlib.pyplot as plt

from keras.models import Sequential
from keras import initializers
from keras.layers import Dropout, Activation, Embedding, Convolution1D, MaxPooling1D, Input, Dense, Merge, \
                         BatchNormalization, Flatten, Reshape, Concatenate
from keras.layers.recurrent import LSTM, GRU
from keras.callbacks import Callback, ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras.models import Model
from keras.optimizers import Adam, SGD, RMSprop
from keras import regularizers

The error I get is

ImportError                               Traceback (most recent call last)
<ipython-input-4-3a64b96c6d78> in <module>
     10 import matplotlib.pyplot as plt
     11 
---> 12 from keras.models import Sequential
     13 from keras import initializers
     14 from keras.layers import Dropout, Activation, Embedding, Convolution1D, MaxPooling1D, Input, Dense, Merge, \

/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/__init__.py in <module>
     23 
     24 # See b/110718070#comment18 for more details about this import.
---> 25 from keras import models
     26 
     27 from keras.engine.input_layer import Input

/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/models.py in <module>
     18 import tensorflow.compat.v2 as tf
     19 from keras import backend
---> 20 from keras import metrics as metrics_module
     21 from keras import optimizer_v1
     22 from keras.engine import functional

/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/metrics.py in <module>
     25 
     26 import numpy as np
---> 27 from keras import activations
     28 from keras import backend
     29 from keras.engine import base_layer

/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/activations.py in <module>
     18 
     19 from keras import backend
---> 20 from keras.layers import advanced_activations
     21 from keras.utils.generic_utils import deserialize_keras_object
     22 from keras.utils.generic_utils import serialize_keras_object

/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/layers/__init__.py in <module>
    146 
    147 # Normalization layers.
--> 148 from keras.layers.normalization import LayerNormalization
    149 from keras.layers.normalization_v2 import SyncBatchNormalization
    150 

ImportError: cannot import name 'LayerNormalization' from 'keras.layers.normalization' (/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/keras/layers/normalization/__init__.py)

I am aware that there have been posts about mismatch versions being the reason for this not working but not sure how I would go about independently changing the versions for tensorflow etc.. to make this work.

Programmer
  • 35
  • 7
  • you are importing from keras, and with tensorflow 2.0 your import will look like `from tensorflow.keras.models import Sequential`, can you try this if you are still getting error? – simpleApp Aug 11 '21 at 02:21
  • Ok replaced `from keras` everywhere to `from tensorflow.keras` but now get a `ImportError: cannot import name 'Merge' from 'tensorflow.keras.layers' (/opt/miniconda3/envs/tensorflow/lib/python3.8/site-packages/tensorflow/keras/layers/__init__.py)`. Looked it up and has something about Merge not being supported in Keras? – Programmer Aug 11 '21 at 12:19
  • it's deprecated, you can use concatenate for it. refer [documentation](https://keras.io/api/layers/merging_layers/) , [some more ideas](https://stackoverflow.com/questions/51075666/how-to-implement-merge-from-keras-layers) – simpleApp Aug 11 '21 at 12:49

1 Answers1

0

Try again with Tensorflow 2.5 and Keras 2.4.3 .

providing gist for reference