簡體   English   中英

ValueError: 形狀為 (72148, 23) 的目標數組被傳遞給形狀為 (None, 826, 23) 的 output,同時用作損失`categorical_crossentropy`

[英]ValueError: A target array with shape (72148, 23) was passed for an output of shape (None, 826, 23) while using as loss `categorical_crossentropy`

我正在嘗試在心跳數據上訓練一維 CNN 來對病理心跳進行分類。 輸入和 output 形狀是

X_train = np.zeros((72148, 828, 1))
y_train = np.zeros((72148, 23))

但是,使用這些形狀擬合 model 會導致錯誤,因為“對於形狀的 output(無、826、23)”,我尤其感到困惑。 任何關於為什么會發生這種情況的建議都將受到高度贊賞:完整的錯誤代碼:

Traceback (most recent call last):
  File "C:/1SchoolProjects/2019-2020/Term4/NN/NNHeartbeats/CNN_heatbeats.py", line 81, in <module>
    model.fit(X_train, y_train, epochs=3, batch_size= batch_size)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 235, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 593, in _process_training_inputs
    use_multiprocessing=use_multiprocessing)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 646, in _process_inputs
    x, y, sample_weight=sample_weights)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2383, in _standardize_user_data
    batch_size=batch_size)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2489, in _standardize_tensors
    y, self._feed_loss_fns, feed_output_shapes)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 810, in check_loss_and_target_compatibility
    ' while using as loss `' + loss_name + '`. '
ValueError: A target array with shape (72148, 23) was passed for an output of shape (None, 826, 23) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.

model總結:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, 826, 512)          2048      
_________________________________________________________________
dense (Dense)                (None, 826, 256)          131328    
_________________________________________________________________
dropout (Dropout)            (None, 826, 256)          0         
_________________________________________________________________
dense_1 (Dense)              (None, 826, 23)           5911      
=================================================================
Total params: 139,287
Trainable params: 139,287
Non-trainable params: 0
_________________________________________________________________

代碼:

import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split

def create_model(n_timesteps, n_features, n_outputs):
    tf.keras.backend.set_floatx('float16')
    dtype='float16'

    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv1D(input_shape=(n_timesteps, n_features), kernel_size=3, filters=512),
        #tf.keras.layers.Dense(input_shape=(828,1), units=828, activation='relu', dtype=dtype),
        #tf.keras.layers.Dropout(0.2, dtype=dtype),
        tf.keras.layers.Dense(256, activation='relu', dtype=dtype),
        tf.keras.layers.Dropout(0.2, dtype=dtype),
        tf.keras.layers.Dense(n_outputs, activation='softmax', dtype=dtype)
    ])

    return model



if __name__ == "__main__":
    X_train = np.zeros((72148, 828, 1))
    y_train = np.zeros((72148, 23))
    n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], y_train.shape[1]
    model = create_model(n_timesteps, n_features, n_outputs)
    #optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
    #loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
    #model.compile(loss=loss_fn, optimizer=optimizer)
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    print(model.summary())



    batch_size = 32

    model.fit(X_train, y_train, epochs=3, batch_size= batch_size)

這些是從 3d 傳遞到 2d 的一些示例

用 Flatten... 你可以把它放在你的網絡中 output 層之前

n_timesteps, n_features, n_outputs = 826, 1, 23
n_samples = 1000

X = np.random.uniform(0,1, (n_samples, n_timesteps, n_features))
y = pd.get_dummies(np.random.randint(0,n_outputs, n_samples)).values

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv1D(input_shape=(n_timesteps, n_features), kernel_size=3, filters=16),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(n_outputs, activation='softmax')
])
model.compile('adam', 'categorical_crossentropy')

model.fit(X,y, epochs=3)

使用壓縮時間維度的 GlobalPooling 層

n_timesteps, n_features, n_outputs = 826, 1, 23
n_samples = 1000

X = np.random.uniform(0,1, (n_samples, n_timesteps, n_features))
y = pd.get_dummies(np.random.randint(0,n_outputs, n_samples)).values

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv1D(input_shape=(n_timesteps, n_features), kernel_size=3, filters=16),
    tf.keras.layers.GlobalAveragePooling1D(), # also GlobalMaxPooling1D() is ok
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(n_outputs, activation='softmax')
])
model.compile('adam', 'categorical_crossentropy')

model.fit(X,y, epochs=3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM