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

I was trying to run a block of code in colab that contained this code here:

seed_text = "Laurence went to dublin"
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)

I get the error mentioned in the above title. There’s also a problem with “if index == predicted:”, as it tells me that " The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

Try

seed_text = "Laurence went to dublin"
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')
	index = np.argmax(model.predict(token_list, verbose=0))
	output_word = tokenizer.index_word[index]
	seed_text += " " + output_word
print(seed_text)
3 Likes

I was stuck on this error. Thanks for this!

This solved my problem too. Thanks a lot for sharing!

@Tejas_Anand

The API was deprecated and later removed from TF.

To solve this and update TF runtime you could update your course material of course 3 which was renewed on 14.03.2022 +.

Hopefully, help :grin: