我正在PyTorch中为我的36个类构建一个简单的多类分类器,并使用softmax函数。
#my data
X_train data shape - (155648, 384)
y_train data shape - (155648,)
X_test data shape - (34167, 384)
y_test data shape - (34167,)
# to use 'categorical_crossentropy' on my labels
y_train = tf.keras.utils.to_categorical(y_train, num_classes=36)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=36)
字符串
我的模型是这样定义的:
batch_size = 32
epochs = 50
num_nodes = 64
dropout = 0.1
input_dim = 384
model = tf.keras.models.Sequential([
tf.keras.layers.Input(shape=(384,)),
tf.keras.layers.Dense(64, activation='relu', use_bias = False),
tf.keras.layers.Dropout(dropout),
tf.keras.layers.Dense(36, activation="softmax", use_bias = False)
])
model.summary()
Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_25 (Dense) (None, 64) 24576
dropout_12 (Dropout) (None, 64) 0
dense_26 (Dense) (None, 36) 2304
=================================================================
Total params: 26880 (105.00 KB)
Trainable params: 26880 (105.00 KB)
Non-trainable params: 0 (0.00 Byte)
型
接下来我编译模型:
optimizer=tf.keras.optimizers.Adam()
loss = 'categorical_crossentropy'
metrics =['accuracy', tf.keras.metrics.AUC(multi_label = True)]
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
型
然后在训练时得到这个ValueError:
model.fit(X_train, y_train,
epochs=epochs,
validation_data=(X_test, y_test))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[62], line 1
----> 1 model.fit(X_train, y_train,
2 epochs=epochs,
3 validation_data=(X_test, y_test))
File /usr/local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /tmp/__autograph_generated_filekny6h0ur.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1338, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1322, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1303, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1081, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1139, in compute_loss
return self.compiled_loss(
File "/usr/local/lib/python3.10/site-packages/keras/src/engine/compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.10/site-packages/keras/src/losses.py", line 142, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.10/site-packages/keras/src/losses.py", line 268, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.10/site-packages/keras/src/losses.py", line 2122, in categorical_crossentropy
return backend.categorical_crossentropy(
File "/usr/local/lib/python3.10/site-packages/keras/src/backend.py", line 5560, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (32, 36, 36) and (32, 36) are incompatible
型
有人能告诉我如何修复这个错误吗?还有,我的Dropout层参数为0,这是不是很糟糕?
1条答案
按热度按时间aoyhnmkz1#
为了回答你的第一个问题,dropout 没有任何参数,因为它没有任何权重来帮助学习。它在训练时关闭随机神经元。了解更多关于dropout here的信息。现在,来谈谈你的问题。我用你在代码中使用的维度生成了一个随机数据集-
字符串
输入的数量是384,输出类的数量是36。然后,
型
重复使用你所有的代码模型训练完美地开始了,我没有得到任何错误,但是如果我运行语句-
型
再次,即我尝试使用tf.keras tf.keras.utils.to_categorical再次转换y_train和y_test的值或再次只运行这段代码,我得到了类似的错误。
型
错误信息显示类似的形状错误-
型
因此,在拟合之前可能要运行此语句两次。一个更好的解决方法是-
型
使用不同的变量名,这样即使你运行两次,三次,值也不会被一次又一次地修改。