keras Tensorflow中多分类任务的顺序模型形状问题

jv4diomz  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(103)

我正在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,这是不是很糟糕?

aoyhnmkz

aoyhnmkz1#

为了回答你的第一个问题,dropout 没有任何参数,因为它没有任何权重来帮助学习。它在训练时关闭随机神经元。了解更多关于dropout here的信息。现在,来谈谈你的问题。我用你在代码中使用的维度生成了一个随机数据集-

df = pd.DataFrame(np.random.rand(10000, 384))
df["dep_y"] = np.random.randint(0,36, 10000)

df.iloc[:, :-1].shape, df["dep_y"].nunique()
# Output: (10000, 384), 36

字符串
输入的数量是384,输出类的数量是36。然后,

x_train, x_test, y_train, y_test = train_test_split(df.iloc[:, :-1], df["dep_y"])
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)

# Shape of y_train and y_test:- (7500, 36), (2500, 36)


重复使用你所有的代码模型训练完美地开始了,我没有得到任何错误,但是如果我运行语句-

y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)


再次,即我尝试使用tf.keras tf.keras.utils.to_categorical再次转换y_train和y_test的值再次只运行这段代码,我得到了类似的错误。

# The new shapes after running again - (7500, 36, 2), (2500, 36, 2)


错误信息显示类似的形状错误-

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[11], line 1
----> 1 model.fit(x_train, y_train,
      2           epochs=epochs,
      3           validation_data=(x_test, y_test))

File ~\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback..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 C:\Users\RC1AE~1.GOY\AppData\Local\Temp\__autograph_generated_file3d7m5nth.py:15, in outer_factory..inner_factory..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 "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1160, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1146, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1135, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 994, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1052, in compute_loss
        return self.compiled_loss(
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\engine\compile_utils.py", line 265, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\losses.py", line 152, in __call__
        losses = call_fn(y_true, y_pred)
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\losses.py", line 272, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\losses.py", line 1990, in categorical_crossentropy
        return backend.categorical_crossentropy(
    File "C:\Users\r.goyal\AppData\Roaming\Python\Python39\site-packages\keras\backend.py", line 5529, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 36, 2) and (None, 36) are incompatible


因此,在拟合之前可能要运行此语句两次。一个更好的解决方法是-

y_train_categorical = tf.keras.utils.to_categorical(y_train, num_classes=36)
y_test_categorical = tf.keras.utils.to_categorical(y_test, num_classes=36)

model.fit(X_train, y_train_categorical,
          epochs=epochs,
          validation_data=(X_test, y_test_categorical))


使用不同的变量名,这样即使你运行两次,三次,值也不会被一次又一次地修改。

相关问题