Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15701

logits and labels do not have same shape

$
0
0

this is my code for translation machine(english to spainsh)

# import what i needimport tensorflow as tfimport numpy as npimport matplotlib.pyplot as plturl = 'https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip'path = tf.keras.utils.get_file('spa-eng.zip', origin=url, cache_dir='dataset', extract=True)path = '/tmp\\.keras\\datasets\\spa-eng.zip'from pathlib import Pathtext = (Path(path).with_name('spa-eng') / 'spa.txt').read_text(encoding='utf-8')# this part is for customizationtext = text.replace('¿', '').replace('¡', '')pairs = [line.split('\t') for line in text.splitlines()]np.random.shuffle(pairs)sentences_en, sentences_es = zip(*pairs)# use text vectorization and biuld my modelvocab_size = 10000max_length = 50text_vec_layer_en = tf.keras.layers.TextVectorization(vocab_size,                 output_sequence_length=max_length)text_vec_layer_es = tf.keras.layers.TextVectorization(vocab_size,     output_sequence_length=max_length)text_vec_layer_en.adapt(sentences_en)text_vec_layer_es.adapt([f"startofseq {s} endofseq" for s in sentences_es])X_train = tf.constant(sentences_en[:100_000])X_valid = tf.constant(sentences_en[100_000:])X_tarin_dec = tf.constant([f"startofseq {s}" for s in sentences_es[:100_000]])X_valid_dec = tf.constant([f"startofseq {s}" for s in sentences_es[100_000:]])Y_train = text_vec_layer_es([f"{s} endofseq" for s in sentences_es[:100_000]])Y_valid = text_vec_layer_es([f"{s} endofseq" for s in sentences_es[100_000:]])encoder_inputs = tf.keras.layers.Input(shape=[], dtype=tf.string)decoder_inputs = tf.keras.layers.Input(shape=[], dtype=tf.string)embed_size = 128encoder_input_ids = text_vec_layer_en(encoder_inputs)decoder_input_ids = text_vec_layer_es(decoder_inputs)encoder_embeding_layer = tf.keras.layers.Embedding(vocab_size, embed_size, mask_zero=True)decoder_embeding_layer = tf.keras.layers.Embedding(vocab_size, embed_size, mask_zero=True)encoder_embeddings = encoder_embeding_layer(encoder_input_ids)decoder_embeddings = encoder_embeding_layer(decoder_input_ids)encoder = tf.keras.layers.LSTM(512, return_state=True)encoder_outputs, *encoder_state = encoder(encoder_embeddings)encoder1 = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(512, return_state=True))encoder_outputs1, *encoder_state1 = encoder(encoder_embeddings)encoder_state1 = [tf.concat(encoder_state[::2], axis=-1),                 tf.concat(encoder_state[1::2], axis=-1)]print(encoder_state1)print(encoder_state)decoder = tf.keras.layers.LSTM(512, return_state=True)decoder_outputs, _, _ = decoder(decoder_embeddings, initial_state=encoder_state1)output_layer = tf.keras.layers.Dense(vocab_size, activation='softmax')Y_proba = output_layer(decoder_outputs)model = tf.keras.Model(inputs=[encoder_inputs, decoder_inputs], outputs=[Y_proba])# compile modelmodel.compile(loss='sparse_categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])# after compile we have to fit itmodel.fit((X_train, X_tarin_dec), Y_train, epochs=10,          validation_data=((X_valid, X_valid_dec), Y_valid))`

but when i fit this model it give me this error and i don't know whylogits and labels must have the same first dimension, got logits shape [32,10000] and labels shape [1600][[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_252748]

and i tried to fit this model and it give me this errorlogits and labels must have the same first dimension, got logits shape [32,10000] and labels shape [1600][[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_252748]what is the problem?


Viewing all articles
Browse latest Browse all 15701

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>