简体   繁体   English

ValueError:尺寸必须相等,但输入形状为 100 和 19:[?,100], [?,100,19]

[英]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:这是 model 摘要: 在此处输入图像描述

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: ValueError:在用户代码中:

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.这可能是因为您有 19 个班级。 But your y vector has digits: 0, ..., 18 .但是你的y向量有数字: 0, ..., 18 Your model is outputting a 19 dimensional vector.您的 model 正在输出 19 维向量。

So, try tf.keras.utils.to_categorical .所以,试试tf.keras.utils.to_categorical Link: https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical链接: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:尺寸必须相等,输入形状 - ValueError: Dimensions must be equal, with input shapes ValueError:尺寸必须相等,但对于'MatMul_1'(op:'MatMul'),输入形状为784和500:[?,784],[500,500] - ValueError: Dimensions must be equal, but are 784 and 500 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [500,500] ValueError:尺寸必须相等,但对于具有输入形状的“mul_18”(操作:“Mul”)为 2 和 80:[?,?,?,5,2], [?,?,?,5,80] - ValueError: Dimensions must be equal, but are 2 and 80 for 'mul_18' (op: 'Mul') with input shapes: [?,?,?,5,2], [?,?,?,5,80] ValueError:检查输入时出错:预期 conv2d_3_input 具有形状 (100, 100, 1) 但得到形状为 (100, 100, 3) 的数组 - ValueError: Error when checking input: expected conv2d_3_input to have shape (100, 100, 1) but got array with shape (100, 100, 3) ValueError:尺寸必须相等(keras) - ValueError: Dimensions must be equal (keras) 重复错误:形状 (1,3) 和 (100,) 未对齐:3 (dim 1) != 100 (dim 0) - Repetitive Error: shapes (1,3) and (100,) not aligned: 3 (dim 1) != 100 (dim 0) ValueError:两个形状中的尺寸2必须相等,但分别为3和32 - ValueError: Dimension 2 in both shapes must be equal, but are 3 and 32 ValueError:形状不匹配:标签的形状(收到的 (200,))应该等于 logits 的形状,除了最后一个维度(收到的 (100, 2)) - ValueError: Shape mismatch: The shape of labels (received (200,)) should equal the shape of logits except for the last dimension (received (100, 2)) 如何确定具有 100 个元素的列表中的 6 个元素是否相等 - How to determine if 6 elements are equal in a list with 100 eelements InvalidArgumentError(回溯,请参见上面的内容):张量必须为4-D,且最后为1、3或4,而不是[5,100,100,120] - InvalidArgumentError (see above for traceback): Tensor must be 4-D with last dim 1, 3, or 4, not [5,100,100,120]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM