I have installed keras followed by tensorflow.
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
When I execute keras sequential model, I get an error message stating that
Import Error Trackback (most recent call last)
ImportError: cannot import name ‘Sequential’ from ‘keras.models’ (C:Usersmurthy.pAppDataLocalContinuumanaconda4libsite-packageskerasmodels__init__.py)
Vivek Mehta
2,6022 gold badges18 silver badges30 bronze badges
asked Dec 18, 2019 at 8:31
0
Firstly, if you’re importing more than one thing from say keras.models or keras.layers put them on one line.
For this specific problem, try importing it from tensorflow which is essentially the keras API. I’m quite confident it should work!
from tensorflow.keras import Sequential
To install tensorflow: pip install tensorflow==2.0.0
answered Dec 18, 2019 at 10:12
DUDANFDUDANF
2,5271 gold badge12 silver badges40 bronze badges
1
I think you didn’t install keras properly you can install it in the command line of the environment you are using by applying the following code
pip install keras
answered Dec 18, 2019 at 10:40
first neural network with keras make predictions
from numpy import loadtxt
#from keras.models import Sequential
from tensorflow.keras.models import Sequential
from keras.layers import Dense
load the dataset
dataset = loadtxt(‘pima-indians-diabetes.csv’, delimiter=’,’)
split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]
define the keras model
model = Sequential()
model.add(Dense(12, input_dim=8, activation=’relu’))
model.add(Dense(8, activation=’relu’))
model.add(Dense(1, activation=’sigmoid’))
compile the keras model
model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])
»’# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=10, verbose=0)»’
fit the keras model on the dataset without progress bars
model.fit(X, y, epochs=150, batch_size=10, verbose=0)
evaluate the keras model
_, accuracy = model.evaluate(X, y, verbose=0)
make class predictions with the model
predictions = model.predict_classes(X)
summarize the first 5 cases
for i in range(5):
print(‘%s => %d (expected %d)’ % (X[i].tolist(), predictions[i], y[i]))
I am also getting the same error. I am using Linux
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import skipgrams
from keras.models import Sequential
from keras.layers import Embedding, Dense, Flatten
from keras.utils import np_utils
sentences = [line.strip() for line in open('alice_in_wonderland.txt') if line != 'n']
tokenizer = Tokenizer()
tokenizer.fit_on_texts(sentences)
corpus = tokenizer.texts_to_sequences(sentences)
V = len(tokenizer.index_word)+1
window_size = 1
model = Sequential()
model.add(Embedding(V, 64, input_length = 1))
model.add(Dense(V, activation = 'softmax'))
model.compile(loss= 'categorical_crossentropy', optimizer = 'rmsprop')
model.summary()
def generate_data(corpus, window_size, V):
for sequence in corpus:
idknow = skipgrams(sequence, V, window_size, negative_samples=0., shuffle= True)
X, y = zip(*idknow[0])
yield X, y
for epoch in range(10):
loss = 0.
for x, y in generate_data(corpus, window_size, V):
print(x)
print(y)
loss += model.train_on_batch(x, y)
print(loss)
Во-первых:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 1, 64) 169408
_________________________________________________________________
dense_1 (Dense) (None, 1, 2647) 172055
=================================================================
Почему кол-во параметров не одинаково?
Во-вторых:
Traceback (most recent call last):
File «xtest.py», line 38, in
loss += model.train_on_batch(x, y)
File «C:UsersadelsAppDataLocalProgramsPythonPython36libsite-packageskerasenginetraining.py», line 1209, in train_on_batch
class_weight=class_weight)
File «C:UsersadelsAppDataLocalProgramsPythonPython36libsite-packageskerasenginetraining.py», line 749, in _standardize_user_data
exception_prefix=’input’)
File «C:UsersadelsAppDataLocalProgramsPythonPython36libsite-packageskerasenginetraining_utils.py», line 91, in standardize_input_data
data = [standardize_single_array(x) for x in data]
File «C:UsersadelsAppDataLocalProgramsPythonPython36libsite-packageskerasenginetraining_utils.py», line 91, in
data = [standardize_single_array(x) for x in data]
File «C:UsersadelsAppDataLocalProgramsPythonPython36libsite-packageskerasenginetraining_utils.py», line 26, in standardize_single_array
elif x.ndim == 1:
AttributeError: ‘tuple’ object has no attribute ‘ndim’
Что за ошибка?
While importing keras (from keras.models import Sequential), I am getting the following error:
Traceback (most recent call last):
File "/home/afzal/Deep cnn/cnn_deeplearningpgm/keras_cnn_new3.py",
line 2, in <module>
import keras File "/usr/local/lib/python2.7/dist-packages/Keras-1.1.0-py2.7.egg/keras/__init__.py",
line 2, in <module>
from . import backend File "/usr/local/lib/python2.7/dist-packages/Keras-1.1.0-py2.7.egg/keras/backend/__init__.py",
line 29, in <module>
_config = json.load(open(_config_path)) File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw) File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx) ValueError: Expecting object: line 5 column 25 (char 108)
I have installed theano. Could anyone help me solve this?
1 answer to this question.
Hi@akhtar,
I think this problem is related to the environment. Your TensorFlow module may be installed in a different env. and Keras is in a different env. So, try to uninstall the Keras module and reinstall it.
$ pip uninstall keras $ pip install keras
Hope this will solve your error.
Build a career in Artificial Intelligence with our Post Graduate Diploma in AI and Machine Learning courses.
answered
Apr 23, 2020
by
MD
• 95,440 points
edited
Aug 11, 2021
by Soumya
Related Questions In Machine Learning
- All categories
-
ChatGPT
(11) -
Apache Kafka
(84) -
Apache Spark
(596) -
Azure
(145) -
Big Data Hadoop
(1,907) -
Blockchain
(1,673) -
C#
(141) -
C++
(271) -
Career Counselling
(1,060) -
Cloud Computing
(3,469) -
Cyber Security & Ethical Hacking
(162) -
Data Analytics
(1,266) -
Database
(855) -
Data Science
(76) -
DevOps & Agile
(3,608) -
Digital Marketing
(111) -
Events & Trending Topics
(28) -
IoT (Internet of Things)
(387) -
Java
(1,247) -
Kotlin
(8) -
Linux Administration
(389) -
Machine Learning
(337) -
MicroStrategy
(6) -
PMP
(423) -
Power BI
(516) -
Python
(3,193) -
RPA
(650) -
SalesForce
(92) -
Selenium
(1,569) -
Software Testing
(56) -
Tableau
(608) -
Talend
(73) -
TypeSript
(124) -
Web Development
(3,002) -
Ask us Anything!
(66) -
Others
(2,231) -
Mobile Development
(395) -
UI UX Design
(24)
Subscribe to our Newsletter, and get personalized recommendations.
Already have an account? Sign in.

























