微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Keras 中的目标数组形状

如何解决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 举报,一经查实,本站将立刻删除。