keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.94k stars 19.46k forks source link

AttributeError: 'Sequential' object has no attribute 'predict_classes' #15838

Closed aritraMandal02 closed 2 years ago

aritraMandal02 commented 2 years ago
seed_text = "I've got a bad feeling about this"
next_words = 100

for _ in range(next_words):
    token_list = tokenizer.texts_to_sequences([seed_text])[0]
    token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
    predicted = model.predict_classes(token_list, verbose=0)
    output_word = ""
    for word, index in tokenizer.word_index.items():
        if index == predicted:
            output_word = word
            break
    seed_text += " " + output_word
print(seed_text)

This codeblock is giving me the error shown below:

AttributeError                            Traceback (most recent call last)
<ipython-input-52-70399a5f93d3> in <module>()
      5         token_list = tokenizer.texts_to_sequences([seed_text])[0]
      6         token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
----> 7         predicted = model.predict_classes(token_list, verbose=0)
      8         output_word = ""
      9         for word, index in tokenizer.word_index.items():

AttributeError: 'Sequential' object has no attribute 'predict_classes'

How can I solve this error? Please help.

Here is the link of the full code: https://www.youtube.com/redirect?event=video_description&redir_token=QUFFLUhqbDVSTGdJczNoa2ZBeXpObWxobGJldGd5Q2pLd3xBQ3Jtc0trSVRobkVBM2EtV3lTajlQNzhXbEhGTjN3WXhFTGo3d1U4b3h1Rjl4Rk9ubUVVWG95cUNfaWFoTHpmRlpEM1VmM2NrOXFqc21WYkRKZ0c3WWgtcE5VRV9kZF9pZDZMUDVmWnRtNUtMS3NzUnZodFB3RQ&q=https%3A%2F%2Fgoo.gle%2F3aSTLGx)

sanatmpa1 commented 2 years ago

@aritraMandal02,

model.predict_classes is deprecated, So you can try replacing

predicted = model.predict_classes(token_list) to predict_x=model.predict(token_list) classes_x=np.argmax(predict_x,axis=1)

Can you also take a look at this link about the similar issue and let us know if it helps? Thanks!

aritraMandal02 commented 2 years ago

Thanks that solved my issue.

MohamadMehdiDabestani commented 2 years ago

why it is not working for me?

dataset = np.loadtxt('file.txt', delimiter=',')
x = dataset[:, 0:8]
y = dataset[:, 8]
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='adam', metrics=['accuracy'])
model.fit(x, y, epochs=70, batch_size=10)
predict_x = model.predict(x)
classes_x = np.argmax(predict_x,axis=1)
print("predict_x" , predict_x)
print("classes_x" , classes_x)

classes_x [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

predict_x [[0.49203077] [0.07500568] [0.37030452] [0.09472606] [0.64866674] [0.2638986 ] [0.18764019] [0.7474557 ] [0.8919148 ] [0.10682949] [0.06606364] [0.651214 ] [0.32097298] [0.9999901 ] [0.6062717 ] [0.5620571 ] [0.19560724] [0.31234646] [0.6331504 ] [0.21437225] [0.22549185] [0.02449828] [0.53270286] [0.29088446] [0.4279429 ] [0.41213727] [0.51117325] [0.10630435] [0.30330193] [0.11688715] [0.23745826] [0.53026116] [0.06647921] [0.02466977] [0.37263715] [0.37355483] [0.5604041 ] [0.25651008] [0.13858646] [0.5075644 ] [0.0629288 ] [0.50203794] [0.18522486] [0.33644176] [0.3404432 ] [0.6730449 ] [0.29660803] [0.0333465 ] [0.37137544] [0.08009928] [0.07539281] [0.03325912] [0.0410842 ] [0.70359313] [0.7203751 ] [0.13647297] [0.8240596 ] [0.05365902] [0.36127076] [0.18801743] [0.04891112] [0.4412208 ] [0.00351229] [0.3970764 ] [0.3966767 ] [0.15769958] [0.0712733 ] [0.01592776] [0.02968842] [0.24510863] [0.13417959] [0.51597047] [0.35065368] [0.12774864] [0.0270839 ] [0.27457935] [0.00157684] [0.15893301] [0.64871716] [0.21410093] [0.30164844] [0.07197151] [0.12937903] [0.10113785] [0.12591475] [0.1758604 ] [0.3615888 ] [0.11376673] [0.59122914] [0.16255116] [0.13110352] [0.15128598] [0.1338543 ] [0.12726638] [0.0487338 ] [0.44398552] [0.14642444] [0.11460206] [0.28672808] [0.19052741] [0.4353519 ] [0.25800452] [0.1226044 ] [0.04411831] [0.09610215] [0.36611193] [0.01209903] [0.5570642 ] [0.14680856] [0.02895755] [0.5323387 ] [0.6728659 ] [0.05285063] [0.06288961] [0.7762819 ] [0.183503 ] [0.3688547 ] [0.348826 ] [0.2541103 ] [0.05034983] [0.35718247] [0.3010724 ] [0.14591551] [0.13330719] [0.18410346] [0.5130885 ] [0.20990771] [0.2889651 ] [0.12301946] [0.02248269] [0.48979896] [0.5784284 ] [0.67939055] [0.19185707] [0.05151424] [0.24879071] [0.07787675] [0.13592032] [0.23430151] [0.16646245] [0.0987682 ] [0.18156934] [0.18080649] [0.47654012] [0.5249664 ] [0.04674169] [0.04273543] [0.3281201 ] [0.23409048] [0.10770512] [0.19682372] [0.18462601] [0.5192756 ] [0.37915474] [0.70646656] [0.55274975] [0.17399156] [0.23461068] [0.06587207] [0.8275904 ] [0.24506271] [0.2995416 ] [0.11705029] [0.19611889] [0.18102312] [0.18106738] [0.5058393 ] [0.3130275 ] [0.36124566] [0.07043394] [0.08348447] [0.30821097] [0.61886746] [0.19138429] [0.09814841] [0.81108165] [0.01419103] [0.04879206] [0.5270101 ] [0.43628073] [0.02736542] [0.1555799 ] [0.03301483] [0.06367233] [0.23688164] [0.6343088 ] [0.8848474 ] [0.06198022] [0.34860224] [0.35271978] [0.2313475 ] [0.5512845 ] [0.41557428] [0.9147663 ] [0.2889132 ] [0.46886563] [0.21767363] [0.04501361] [0.26380783] [0.5025272 ] [0.13695496] [0.35172495] [0.16266453] [0.04591405] [0.28489178] [0.20511979] [0.82925284] [0.31168702] [0.12321055] [0.5854893 ] [0.12761372] [0.1208756 ] [0.4042639 ] [0.24990243] [0.24543521] [0.69016516] [0.34402493] [0.37872157] [0.13190868] [0.43725199] [0.63090074] [0.1493907 ] [0.44359165] [0.7535577 ] [0.07219386] [0.03827026] [0.13944462] [0.65884626] [0.863129 ] [0.07463318] [0.49301523] [0.35567605] [0.05278012] [0.42772043] [0.08496901] [0.63118255] [0.6027952 ] [0.5326352 ] [0.56643826] [0.11962044] [0.11356515] [0.136886 ] [0.3122139 ] [0.48304155] [0.354519 ] [0.45135054] [0.4606227 ] [0.80151916] [0.40296647] [0.09259668] [0.45856127] [0.21446428] [0.04882395] [0.06583875] [0.3941326 ] [0.19427538] [0.31472927] [0.22369304] [0.74567986] [0.68598473] [0.2086207 ] [0.51174134] [0.12515321] [0.21875802] [0.34654558] [0.10412806] [0.5575199 ] [0.36823437] [0.21326286] [0.41170686] [0.29290032] [0.07846203] [0.15121996] [0.00888073] [0.25214386] [0.12863785] [0.37864482] [0.14943677] [0.16346833] [0.14258826] [0.37515143] [0.36820018] [0.17990476] [0.30288655] [0.08581775] [0.51249385] [0.5299172 ] [0.1551469 ] [0.08935782] [0.30175132] [0.021328 ] [0.18789488] [0.2697826 ] [0.5273298 ] [0.0832915 ] [0.5641378 ] [0.5120692 ] [0.16309625] [0.252921 ] [0.19813883] [0.46852604] [0.50347316] [0.10115331] [0.04581788] [0.15035206] [0.21473515] [0.59506834] [0.20317957] [0.2266134 ] [0.2874856 ] [0.12937808] [0.16073859] [0.143967 ] [0.1434795 ] [0.26218194] [0.15178648] [0.0618102 ] [0.33804807] [0.2849396 ] [0.16656044] [0.28932852] [0.1692017 ] [0.20986426] [0.15626901] [0.13256776] [0.3595376 ] [0.3021292 ] [0.58680886] [0.07724854] [0.26504028] [0.3313056 ] [0.08052969] [0.6285514 ] [0.17626742] [0.09469515] [0.43666142] [0.3814085 ] [0.28617117] [0.561165 ] [0.5322119 ] [0.17593643] [0.12185869] [0.3328343 ] [0.2707752 ] [0.04389548] [0.3306476 ] [0.19208989] [0.3733888 ] [0.17296872] [0.46500212] [0.03137472] [0.31990558] [0.02667862] [0.03764868] [0.04645887] [0.348445 ] [0.5303666 ] [0.8842505 ] [0.323565 ] [0.7093346 ] [0.7976657 ] [0.20603389] [0.01651388] [0.28212816] [0.34690762] [0.26567423] [0.33994207] [0.1275315 ] [0.03740522] [0.14964756] [0.6971065 ] [0.04701567] [0.07031366] [0.27762902] [0.51107174] [0.659328 ] [0.0558531 ] [0.15265903] [0.6584571 ] [0.0442667 ] [0.17117423] [0.10200959] [0.13398227] [0.13050488] [0.2356978 ] [0.03277892] [0.27960807] [0.1197131 ] [0.4488584 ] [0.1558437 ] [0.20905933] [0.67228836] [0.2294212 ] [0.13010266] [0.3879903 ] [0.39314196] [0.37657878] [0.23648143] [0.06583822] [0.55298054] [0.2645683 ] [0.19101325] [0.37756708] [0.03378379] [0.37633273] [0.37466776] [0.2278597 ] [0.15380403] [0.38158268] [0.70061237] [0.06303665] [0.17082599] [0.16638467] [0.05075407] [0.39731845] [0.44561902] [0.14395007] [0.4001595 ] [0.09136978] [0.37240845] [0.14136401] [0.10124648] [0.10069007] [0.29781044] [0.4502857 ] [0.6740236 ] [0.03680435] [0.68880004] [0.10875922] [0.09157038] [0.30962625] [0.08958298] [0.03818592] [0.20833078] [0.11904716] [0.6221672 ] [0.5721312 ] [0.3658723 ] [0.11758158] [0.08987081] [0.28717202] [0.08422038] [0.24258742] [0.4352703 ] [0.37969387] [0.44724146] [0.08730036] [0.06901076] [0.1325747 ] [0.05646604] [0.07686898] [0.30126747] [0.100425 ] [0.1173057 ] [0.19344714] [0.77836394] [0.13264579] [0.13087174] [0.70397013] [0.11402717] [0.09440079] [0.03941327] [0.20309743] [0.08864522] [0.15800717] [0.23256224] [0.03650752] [0.16856027] [0.55935204] [0.45871708] [0.2636451 ] [0.24757516] [0.28849837] [0.26455384] [0.33824015] [0.14272472] [0.20941123] [0.20328578] [0.41695517] [0.1857633 ] [0.50435716] [0.06726268] [0.13461739] [0.03430495] [0.6606084 ] [0.33566207] [0.42659026] [0.5835547 ] [0.15589517] [0.21146798] [0.09849453] [0.04122698] [0.14449528] [0.30861205] [0.08271527] [0.17446795] [0.2816994 ] [0.05544913] [0.74805254] [0.6015199 ] [0.06393871] [0.08041468] [0.83095694] [0.23404536] [0.08676997] [0.00377086] [0.08611774] [0.30141103] [0.15812731] [0.16111138] [0.23944288] [0.29226416] [0.04594359] [0.19792873] [0.18754065] [0.18530074] [0.5529164 ] [0.27433616] [0.06028888] [0.36978078] [0.07170957] [0.21921012] [0.0551534 ] [0.47977784] [0.37773317] [0.15267867] [0.13987017] [0.13125038] [0.22671339] [0.1690155 ] [0.23028284] [0.23609537] [0.1288279 ] [0.52810025] [0.13950872] [0.59650886] [0.020861 ] [0.00130337] [0.21885192] [0.20000467] [0.30263534] [0.24815676] [0.09308675] [0.03458449] [0.08577698] [0.6684534 ] [0.7052849 ] [0.25599915] [0.19019815] [0.3594862 ] [0.16656464] [0.12122923] [0.03817585] [0.0838941 ] [0.08534881] [0.44525293] [0.10798696] [0.12389082] [0.5024597 ] [0.04404104] [0.30210024] [0.6951618 ] [0.10859761] [0.11299351] [0.03430372] [0.14478388] [0.06654075] [0.27978966] [0.5028829 ] [0.2500115 ] [0.01477832] [0.17247787] [0.10678431] [0.10467124] [0.25905308] [0.417194 ] [0.44623685] [0.30677098] [0.41340822] [0.4380554 ] [0.13766515] [0.34657776] [0.3559045 ] [0.09021327] [0.99819064] [0.18343723] [0.48186475] [0.28389594] [0.59777296] [0.2280668 ] [0.4082288 ] [0.10421506] [0.28846884] [0.1764647 ] [0.48074192] [0.36597165] [0.00289845] [0.22823387] [0.32812896] [0.34339803] [0.08253032] [0.4215526 ] [0.15334126] [0.5178542 ] [0.46681708] [0.38418308] [0.60262895] [0.08078712] [0.31818217] [0.14607129] [0.2114613 ] [0.6629017 ] [0.6315632 ] [0.19197741] [0.51012754] [0.19696864] [0.12702253] [0.02288395] [0.27743393] [0.44456097] [0.16433612] [0.11088103] [0.5183995 ] [0.07041535] [0.2930503 ] [0.06824285] [0.19514337] [0.2746421 ] [0.328691 ] [0.1687733 ] [0.36294293] [0.08373252] [0.26150972] [0.12289158] [0.37005258] [0.36323184] [0.14138138] [0.07133436] [0.16566527] [0.02854455] [0.06241634] [0.4378474 ] [0.2562319 ] [0.43644232] [0.197018 ] [0.4342935 ] [0.44452375] [0.72299325] [0.501532 ] [0.18917179] [0.17281649] [0.21852663] [0.2952299 ] [0.27116686] [0.1307393 ] [0.5170479 ] [0.12464225] [0.35927236] [0.04769778] [0.07503471] [0.30594936] [0.6833092 ] [0.51493484] [0.6110882 ] [0.42589617] [0.08838788] [0.21999204] [0.38037562] [0.3436875 ] [0.41205132] [0.6697174 ] [0.22251806] [0.01703686] [0.06180534] [0.00499055] [0.43859777] [0.23724991] [0.21815571] [0.4663281 ] [0.17292944] [0.07965428] [0.5739426 ] [0.13373676] [0.32114363] [0.01964825] [0.22568747] [0.24501297] [0.3168981 ] [0.32511234] [0.4097364 ] [0.26243347] [0.5285125 ] [0.22595787] [0.6041891 ] [0.17600751] [0.61406857] [0.34130841] [0.3134416 ] [0.09774709] [0.5107324 ] [0.17765278] [0.2733459 ] [0.32205164] [0.5329927 ] [0.1360561 ] [0.08115935] [0.16387928] [0.33361682] [0.43128097] [0.14371192] [0.36155558] [0.04345062] [0.72365415] [0.29994154] [0.17714456] [0.8828443 ] [0.6443802 ] [0.24073601] [0.24329075] [0.16258428] [0.02849185] [0.21177092] [0.49378422] [0.18104133] [0.0272907 ] [0.17892781] [0.14476028] [0.20781317] [0.20161876] [0.30286044] [0.15152583] [0.30159962] [0.39860576] [0.23740768] [0.09597602] [0.25467634] [0.12339389] [0.03102151] [0.10540241] [0.09548837] [0.40551353] [0.25767127] [0.16500673] [0.40356958] [0.57739806] [0.28896627] [0.17812571] [0.10541439] [0.6155447 ] [0.22235128] [0.4275792 ] [0.21092084] [0.24998054] [0.4888219 ] [0.5284901 ] [0.2598216 ] [0.262183 ] [0.2187764 ] [0.18954653] [0.27225232] [0.06964585] [0.7750742 ] [0.2726289 ] [0.52034444] [0.3087816 ] [0.25557217] [0.19995359] [0.06784683]]

dmbikash commented 2 years ago

did u solve the problem, brother?

Cyfore commented 2 years ago

did u solve the problem, brother?

https://www.codegrepper.com/code-examples/whatever/%27Sequential%27+object+has+no+attribute+%27predict_classes%27

NAMSKN commented 2 years ago

@aritraMandal02,

model.predict_classes is deprecated, So you can try replacing

predicted = model.predict_classes(token_list) to predict_x=model.predict(token_list) classes_x=np.argmax(predict_x,axis=1)

Can you also take a look at this link about the similar issue and let us know if it helps? Thanks!

U are a savior

dmbikash commented 2 years ago

it works...thanks. would u explain me the lines? it would be a great help for me. thanks in advanced

On Mon, Sep 12, 2022 at 4:35 PM Namratha Nagendra @.***> wrote:

@aritraMandal02 https://github.com/aritraMandal02,

model.predict_classes is deprecated, So you can try replacing

predicted = model.predict_classes(token_list) to predict_x=model.predict(token_list) classes_x=np.argmax(predict_x,axis=1)

Can you also take a look at this link https://stackoverflow.com/a/68841446/11530462 about the similar issue and let us know if it helps? Thanks!

U are a savior

— Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/15838#issuecomment-1243542398, or unsubscribe https://github.com/notifications/unsubscribe-auth/APMXZMY3JKWIHUABLRFCQ73V54BOVANCNFSM5K244IFA . You are receiving this because you commented.Message ID: @.***>

Pratik-12a commented 2 years ago

def image_processing(img): model = load_model('./model/TSR.h5') data=[] image = Image.open(img) image = image.resize((30,30)) data.append(np.array(image)) X_test=np.array(data) Y_pred = model.predict_classes(X_test) return Y_pred

I'm getting error of File "d:\Traffic_Sign_Recognition\Traffic_app.py", line 62, in image_processing Y_pred = model.predict_classes(X_test) AttributeError: 'Sequential' object has no attribute 'predict_classes'

can you please help us to solve this issue

AKB0410 commented 1 year ago

y_predict = np.argmax(model.predict(x_test), axis=-1)

DiegoHurtad0 commented 1 year ago

y_pred_prob = model.predict(X_test) y_pred = np.round(y_pred_prob)

DiegoHurtad0 commented 1 year ago

y_pred_prob = model.predict(X_test) y_pred = np.round(y_pred_prob)

KnightTaa commented 1 year ago

`img = image_utils.load_img(path, target_size=(224,224))

input_arr = image_utils.img_to_array(img)/255

input_arr.shape

input_arr = np.expand_dims(input_arr, axis=0)

pred = model.predict_classes(input_arr)[0][0] pred`


AttributeError Traceback (most recent call last) in 9 input_arr = np.expand_dims(input_arr, axis=0) 10 ---> 11 pred = model.predict_classes(input_arr)[0][0] 12 pred

AttributeError: 'Sequential' object has no attribute 'predict_classes'

please help to slove that