InvalidArgumentError:断言失败:[Condition x == y did not hold element-wise:]

Posted

技术标签:

【中文标题】InvalidArgumentError:断言失败:[Condition x == y did not hold element-wise:]【英文标题】:InvalidArgumentError: assertion failed: [Condition x == y did not hold element-wise:] 【发布时间】:2021-01-06 11:38:59 【问题描述】:

我正在尝试使用 LSTM 进行分类(进行事件预测)我有 320 个类。数据集由时间序列数据组成,使用以下代码创建和训练:

def create_dataset(dataset,y, look_back=1):
   dataX, dataY = [], []
   for i in range(len(dataset)-look_back):
       a = dataset.iloc[i:(i+look_back)].to_numpy()
       dataX.append(a)
       dataY.append(y.iloc[i + look_back])
   return np.array(dataX), np.array(dataY)

   train_size = int(len(df1) * 0.7)
   test_size = len(df1) - train_size
   train, test = df1.iloc[0:train_size], df1.iloc[train_size:len(df1)]
   print(train.shape, test.shape)
   # reshape into X=t and Y=t+1
   look_back = 10
   #X, Y = create_dataset(dataset, look_back)
   train.msg_code = to_categorical(train.msg_code)
   test.msg_code = to_categorical(test.msg_code)
   trainX, trainY = create_dataset(train,train.msg_code, look_back)
   #print(trainX)
   #print(trainY)
   testX, testY = create_dataset(test,test.msg_code, look_back)
   model = Sequential()
   adam = Adam(lr=0.01)
   #LSTM layers
   chk = ModelCheckpoint('best_model13.pkl', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
   model.add(LSTM(128, return_sequences=True, input_shape=(trainX.shape[1],trainX.shape[2])))
   #model.add(LSTM(200, return_sequences=True))
   #model.add(LSTM(200, return_sequences=True))
   #model.add(LSTM(200, return_sequences=True))
   # model.add(LSTM(200, return_sequences=True))
   #Dense layer
   model.add(Dense(320, activation = 'relu'))
   #outputlayer
   model.add(Dense(320,activation='softmax'))
   model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
   #model fitting
   history = model.fit(trainX, trainY,validation_data=(testX,testY), epochs=500, batch_size=32,callbacks=[chk],shuffle=False)
   model = load_model('best_model13.pkl')
   scores = model.evaluate(testX, testY, verbose=0)

我收到以下错误:

InvalidArgumentError:  assertion failed: [Condition x == y did not hold element-wise:] [x (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [32 1] [y (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [32 10]
     [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at <ipython-input-21-2a944dbcd5e3>:220) ]] [Op:__inference_train_function_22113]

Function call stack:
train_function

【问题讨论】:

你确定模型的输出和标签的形状是一样的吗? 我在这里找到了答案i added a line before the dense layer @Sarah Rara,如果您找到了很好的解决方案。但是,如果您可以在下面的答案部分中提供工作代码以造福于社区,那就太好了。谢谢! 这能回答你的问题吗? Tensorflow 2.0 InvalidArgumentError: assertion failed: [Condition x == y did not hold element-wise:] 【参考方案1】:

我在这里找到了答案Tensorflow 2.0 InvalidArgumentError: assertion failed: [Condition x == y did not hold element-wise:]?

我在密集层之前添加了这一行:

model.add(tf.keras.layers.Flatten())

【讨论】:

嗨@Sara,欢迎来到*** 社区。为您的答案添加一些解释会更好,而不仅仅是依赖于 URL,因为它将来可能无法访问,所以一些解释会很棒。最好的?

以上是关于InvalidArgumentError:断言失败:[Condition x == y did not hold element-wise:]的主要内容,如果未能解决你的问题,请参考以下文章

有朋友遇到“断言失败”错误提示吗,怎么解决呢?

findcontours 断言失败

UIWindowController 断言失败

[C#FluentAssertions在断言失败后继续

C# FluentAssertions 在断言失败后继续

选择器视图上的断言失败