Keras 中的目标数组形状

问题描述

我正在尝试构建一个对网络攻击(DDoS 或 BENIGN 攻击)进行分类的模型。为此,我使用来自 https://www.unb.ca/cic/datasets/ids-2017.html 的“ISCX 2017”数据集。一切顺利,直到我适合模型。

我收到此错误 ValueError: A target array with shape (180568,80) was passed for an output of shape (None,8,80) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.

我在网上找不到有关如何解决此问题的任何信息。我是 Keras 的新手,我将不胜感激任何指导或建议。

这是我的代码片段


import os
import math
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder,MinMaxScaler,StandardScaler,RobustScaler
from tensorflow import keras
from numpy import mean
from numpy import std
layers = keras.layers


from keras.models import Sequential
from keras.layers import Dense,Conv1D,Conv2D,MaxPool1D,Dropout,Lambda,MaxPooling1D,Flatten,GlobalAveragePooling1D
from tensorflow.keras import datasets,layers,models
from tflearn.layers.normalization import local_response_normalization
from keras.layers import Dropout

# The next step is to split training and testing data. For this we will use sklearn function train_test_split().
features_train,features_test,labels_train,labels_test = train_test_split(features,labels,test_size=.2)

features_train.shape,features_test.shape,labels_train.shape,labels_test.shape 

((180568,80),(45143,(180568,),))



n_timesteps,n_features,n_outputs = features_train.shape[0],features_train.shape[1],labels_train.shape[0]

X_train = np.zeros((180568,80,1))
y_train = np.zeros((180568,80))
n_timesteps,n_outputs = X_train.shape[1],X_train.shape[2],y_train.shape[1]
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),activation='relu',kernel_size=2,filters=32),tf.keras.layers.MaxPooling1D(strides=3),#tf.nn.local_response_normalization((1,1),depth_radius=5,bias=1,alpha=1,beta=0.5,name=None),tf.keras.layers.Layernormalization(axis=1),tf.keras.layers.Conv1D(input_shape=(n_timesteps,filters=64),# also GlobalMaxPooling1D() is ok
tf.keras.layers.Layernormalization(axis=1),tf.keras.layers.Dense(80,activation='softmax')
]) 


model.compile('adam','categorical_crossentropy',metrics=['accuracy'])
model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None,79,32)            96        
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None,26,32)            0         
_________________________________________________________________
layer_normalization (LayerNo (None,32)            52        
_________________________________________________________________
conv1d_1 (Conv1D)            (None,25,64)            4160      
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None,64)             0         
_________________________________________________________________
layer_normalization_1 (Layer (None,64)             16        
_________________________________________________________________
dense (Dense)                (None,80)             5200      
=================================================================
Total params: 9,524
Trainable params: 9,524
Non-trainable params: 0
______________________________

model.fit(X_train,y_train,epochs=10,verbose=1)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)