susanli2016 / NLP-with-Python

Scikit-Learn, NLTK, Spacy, Gensim, Textblob and more
2.74k stars 2.02k forks source link

machine translation code issue #30

Open cmansi5396 opened 3 years ago

cmansi5396 commented 3 years ago

i am getting the error after this code

def simple_model(input_shape, output_sequence_length, english_vocab_size, french_vocab_size): """ Build and train a basic RNN on x and y :param input_shape: Tuple of input shape :param output_sequence_length: Length of output sequence :param english_vocab_size: Number of unique English words in the dataset :param french_vocab_size: Number of unique French words in the dataset :return: Keras model built, but not trained """

TODO: Build the layers

learning_rate = 1e-3
input_seq = Input(input_shape[1:])
rnn = GRU(64, return_sequences = True)(input_seq)
logits = TimeDistributed(Dense(french_vocab_size))(rnn)
model = Model(input_seq, Activation('softmax')(logits))
model.compile(loss = sparse_categorical_crossentropy, 
             optimizer = Adam(learning_rate), 
             metrics = ['accuracy'])

return model

tests.test_simple_model(simple_model)

Reshaping the input to work with a basic RNN

tmp_x = pad(preproc_english_sentences, max_french_sequence_length) tmp_x = tmp_x.reshape((-1, preproc_french_sentences.shape[-2], 1))

Train the neural network

simple_rnn_model = simple_model( tmp_x.shape, max_french_sequence_length, english_vocab_size, french_vocab_size) simple_rnn_model.fit(tmp_x, preproc_french_sentences, batch_size=1024, epochs=10, validation_split=0.2)

Print prediction(s)

print(logits_to_text(simple_rnn_model.predict(tmp_x[:1])[0], french_tokenizer))

error

i am getting assertion error of not using the sparse cross entropy function ....using tensorflow 2.1.0 and python 3.6