简体   繁体   中英

ValueError: Dimensions must be equal, but are 100 and 19 with input shapes: [?,100], [?,100,19]

I have an error in my code, and I've done read the documentation but it still error, How this error can be fixed?

Code:

import tensorflow.keras.backend as K
import tensorflow_addons as tfa
from tensorflow_addons.layers import CRF
from keras_crf import CRFModel
def create_model(): #
  max_words=length_long_sentence
  MAX_SENTENCE_NUM=100
  embedding_size=100
  lstm_size=128
  learn_rate=0.01
  output_size=len(unique_tag_set)

  current_input=Input(shape=(MAX_SENTENCE_NUM,max_words,)) 
  emb_current = Embedding(vocab_size, embedding_size, weights= 
  [embedding_matrix],input_length=max_words, name='current_embed',trainable=False)(current_input)
  hidden_vectors=TimeDistributed(Bidirectional(LSTM(units=lstm_size, return_sequences=False))) 
  (emb_current ) 
  hidden_vectors=Bidirectional(LSTM(units=lstm_size, return_sequences=True))(hidden_vectors ) 
  
  base = tf.keras.Model(inputs=current_input, outputs=hidden_vectors)
  model = CRFModel(base, 19)
  opt = tf.keras.optimizers.Adam(learning_rate=learn_rate)
  model.compile(optimizer=opt, metrics=['acc'])
  print(model.summary())
  return model
model_2=create_model()

and here is the model summary: 在此处输入图像描述

Here is the code to fit in training data:

history_2=model_2.fit(x_train_split,y_train_split,
                    epochs=1,batch_size=16,
                    shuffle = False, verbose = 1,
                    validation_split=0.2,
                    sample_weight=sample_weights)

And I got this error:

ValueError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras_crf/crf_model.py", line 49, in train_step
    crf_loss = -tfa.text.crf_log_likelihood(potentials, y, sequence_length, kernel)[0]
File "/usr/local/lib/python3.7/dist-packages/tensorflow_addons/text/crf.py", line 242, in crf_log_likelihood
    inputs, tag_indices, sequence_lengths, transition_params
File "/usr/local/lib/python3.7/dist-packages/tensorflow_addons/text/crf.py", line 104, in crf_sequence_score
    return tf.cond(tf.equal(tf.shape(inputs)[1], 1), _single_seq_fn, _multi_seq_fn)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_addons/text/crf.py", line 97, in _multi_seq_fn
    unary_scores = crf_unary_score(tag_indices, sequence_lengths, inputs)
File "/usr/local/lib/python3.7/dist-packages/tensorflow_addons/text/crf.py", line 277, in crf_unary_score
    flattened_tag_indices = tf.reshape(offsets + tag_indices, [-1])

ValueError: Dimensions must be equal, but are 100 and 19 for '{{node cond/add_1}} = AddV2[T=DT_INT32](cond/add, cond/add_1/Cast)' with input shapes: [?,100], [?,100,19].

This could be because you have 19 classes. But your y vector has digits: 0, ..., 18 . Your model is outputting a 19 dimensional vector.

So, try tf.keras.utils.to_categorical . Link: https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical

Essentially:

y_train_split = tf.keras.utils.to_categorical(y_train_split)
# code to fit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM