Python - ImportError: cannot import name 'Input' from 'tensorflow.keras.models [closed]
Tried the following code below. Tried uninstalling/ installing tensorflow and changing to from tensorflow rather than import. And using a previous version of tensorflow.
# Import packages and print version
import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential,Input,Model
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
#Libraries for vectorisation
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.model_selection import GridSearchCV
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5764/3752405166.py in <module>
4 import numpy as np
5 import pandas as pd
----> 6 from tensorflow.keras.models import Sequential,Input,Model
7 from tensorflow.keras.layers import Dense, Dropout, Flatten
8 from tensorflow.keras.layers import Conv2D, MaxPooling2D
ImportError: cannot import name 'Input' from 'tensorflow.keras.models' (C:\Users\liamd\anaconda3\lib\site-packages\tensorflow\keras\models\__init__.py)```
Solution 1:
Input is not from the model API but is defined as tensorflow.keras.Input
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras import Input
Alternativley, you could use the InputLayer() from tensorflow.keras.layers.InputLayer and import it as:
from tensorflow.keras.layers import InputLayer