多标签分类-ValueError:logits和标签必须具有相同的形状((None,5)vs(None,20))

时间:2020-07-14 08:11:58

标签: python pandas tensorflow keras deep-learning

关于背景知识,我遵循的是多标签分类的简短指南,其中,我有几个最终类别,可以将一个预测与多个最终类别相关联:https://towardsdatascience.com/multi-label-image-classification-with-neural-network-keras-ddc1ab1afede

不幸的是,我遇到以下错误:

Epoch 1/300 
Traceback (most recent call last): 
  File "learn.py", line 124, in <module> 
    model.fit(train, train_labels, epochs=300)
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit
    tmp_logs = train_function(iterator)
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__
    result = self._call(*args, **kwds) 
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py", line 627, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py", line 505, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py", line 2446, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs) 
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py", line 2777, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs) 
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py", line 2657, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\func_graph.py", line 981, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs) 
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py", line 441, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\func_graph.py", line 968, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py:571 train_function  *
        outputs = self.distribute_strategy.run(
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py:532 train_step  **
        loss = self.compiled_loss(
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\compile_utils.py:205 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\losses.py:143 __call__    
        losses = self.call(y_true, y_pred)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\losses.py:246 call        
        return self.fn(y_true, y_pred, **self._fn_kwargs)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\losses.py:1595 binary_crossentropy
        K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\backend.py:4692 binary_crossentropy
        return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
    C:\Users\xwb18152\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\nn_impl.py:171 sigmoid_cross_entropy_with_logits
        raise ValueError("logits and labels must have the same shape (%s vs %s)" %

    ValueError: logits and labels must have the same shape ((None, 5) vs (None, 20))

供参考:这些是train和train_labels形状的输出:

train.shape          <- (66633, 15) 
train_labels.shape   <- (66633, 20)

但是,我知道train_labels.shape显示20,但是在进行一次热编码并将其传递到sklearn的{​​{1}}函数之前,标签表的标题如下:

train_test_split

我不确定为什么它突然从5个班级增加到20个班级。

我用于模型的代码如下:

Movement  Distance  Speed  Delay  Loss 

               1         1     25      0     0
               1         1     25      0     0
               1         1     25      0     0
               1         1     25      0     0
               1         1     25      0     0

编辑:数据集和标签最初是## Pre-process ## # Normalise sc = MinMaxScaler() dataset = sc.fit_transform(dataset) # One-hot encode (OHE) ohe = OneHotEncoder() labels = ohe.fit_transform(labels).toarray() # Split into train and test train, test, train_labels, test_labels = train_test_split(dataset, labels, test_size=0.2) #80% train split ## Define model architecture ## # Declare model format model = Sequential() model.add(Dense(30, input_dim=15, activation='relu')) model.add(Dense(5, activation='sigmoid')) # Sigmoid for multi-label classification model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) ## Fit the model ## model.fit(train, train_labels, epochs=300) 个数据框。

0 个答案:

没有答案