无法定义Keras中DNN的第一层

我正在尝试设计一个多类别分类神经网络。

我的train_x数据集包含23个样本,每个样本有37个特征(维度:23*37)
train_y包含每个样本的输出(维度:23*7)[7个标签/类别]。我对每个样本的输出使用了一键编码。
len(words)是特征的数量。这是我的模型设计:

model = tf.keras.models.Sequential([    tf.keras.layers.Dense(units=len(words), input_shape=[len(words)]),    tf.keras.layers.Dense(8, activation="relu"),    tf.keras.layers.Dense(8, activation="relu"),    tf.keras.layers.Dense(len(labels), activation="softmax")])

我使用了Adam优化器作为优化器,并使用了稀疏分类熵作为损失函数。

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])model.fit(train_x, train_y, epochs=100)

我得到了以下追溯调用:

Epoch 1/100Traceback (most recent call last):  File "main.py", line 83, in <module>    model.fit(train_x, train_y, epochs=100, callbacks=[callbacks])  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper    return method(self, *args, **kwargs)  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit    tmp_logs = train_function(iterator)  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__    result = self._call(*args, **kwds)  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 644, in _call    return self._stateless_fn(*args, **kwds)  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2420, in __call__    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1661, in _filtered_call    return self._call_flat(  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1745, in _call_flat    return self._build_call_outputs(self._inference_function.call(  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 593, in call    outputs = execute.execute(  File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,tensorflow.python.framework.errors_impl.InvalidArgumentError:  logits and labels must have the same first dimension, got logits shape [23,7] and labels shape [161]         [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at main.py:83) ]] [Op:__inference_train_function_709]Function call stack:train_function

我已经在各种网站上搜索了两天。但他们都对第一层展平了输入数据。所有这些都使用灰度图像或RGB图像作为输入。所有这些都要求第一层被展平。但我的输入数据已经是展平的了。

据我所知,我在第一层得到了这个追溯调用。我可能误解了units和input_shape的概念,因此错误地定义了它们。


回答:

sparse_categorical_crossentropy更改为categorical_crossentropy

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注