我正在使用这个数据集:https://www.kaggle.com/gabisato/league-of-legends-ranked-games/data
我使用’win’列作为我的目标变量,将数据转换成了两个分类的一热向量;一个代表第一队获胜,另一个代表第二队获胜。这些存储在我的trainY (600,1,1) 和 testY (56,1,1) numpy数组中。
我想使用其他88列作为比赛结果的预测因子。因此,我的trainX是(600,88),我的testX是(56,88)。(我刚刚想到但不确定其价值的一点:我是否应该为这些特征创建一个长度为1的向量,即我的数组是否应该是(600,1,1,1,1…..1)?)
在我目前上的神经网络课程中,我们主要使用了线性、卷积、池化和 dropout 层。我正在使用keras,并尝试使用一些随机层作为起点来构建模型:
#定义模型model = Sequential()model.add(Conv1D(filters=5, kernel_size=2, padding='same', activation='relu',input_shape=(88,1)))model.add(MaxPooling1D(pool_size=2))model.add(Conv1D(filters=10, kernel_size=2, padding='same', activation='relu'))model.add(MaxPooling1D(pool_size=2))model.add(Conv1D(filters=15, kernel_size=3, padding='same', activation='relu'))model.add(MaxPooling1D(pool_size=3))model.add(Conv1D(filters=30, kernel_size=3, padding='same', activation='relu'))model.add(Conv1D(filters=30, kernel_size=3, padding='same', activation='relu'))model.add(Flatten())model.add(Dense(units=2, activation='softmax'))model.summary()model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=['accuracy'])
模型编译得很好,但我遇到了输入缺少一个维度的问题。
ValueError: Input 0 of layer sequential_30 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 88]
我在网上还读到卷积主要用于图像处理和时间数据,这让我开始质疑我是否应该使用卷积层。我的问题是,在我的课程中,我们只讨论过与图像识别/标记相关的例子,所以我真的不知道如何处理我的想法。我可以将卷积层应用到我的数据上吗?如果可以,如何实现Keras所说我需要的额外维度?如果我不应该使用卷积层,你能推荐在层设计或数据预处理方面的方法吗?所有建议都非常欢迎,我并不是在寻找代码,而是概念上关于如何在这种数据上构建神经网络的最佳方法。
这是我的模型训练代码:
hist = model.fit(trainX, trainY, epochs = 10, batch_size=16,validation_data=(testX,testY))
因为这可能有用,我也会添加我用来分割数据的代码:
#将数据框转换为numpy数组challenger = challenger_df.to_numpy()#将原始标签1和2转换为0和1challenger[:,0]=challenger[:,0]-1#定义标签LabelMeaning=['0=Team 1', '1=Team 2']#为网络预处理特征scaler = MinMaxScaler(feature_range=(0,1))for i in range(88): challenger[:,i+1]=scaler.fit_transform((challenger[:,i+1]).reshape(-1,1)).reshape(1,-1)#分成训练和测试集train = challenger[:600]test = challenger[600:]print(np.shape(train))print(np.shape(test))print()#分成X和YtrainX = train[:,1:]trainY = train[:,0]testX = test[:,1:]testY = test[:,0]#检查形状是否有差异print(np.shape(trainX))print(np.shape(trainY))print()print(np.shape(testX))print(np.shape(testY))print()#将标签转换为一热向量trainY=np_utils.to_categorical(trainY)testY=np_utils.to_categorical(testY)print(np.shape(trainY))print(np.shape(testY))
回答:
神经网络的输入trainX
的形状为[batch_size, 88]
。卷积神经网络期望输入是三维的。从这里的文档中,它期望的维度是[batch, steps, channels]
。然而,提供的输入只有2个维度,因此出现了错误。
为了使输入成为三维,你可以像这样trainX[:, :, None]
为输入数据添加一个额外的维度。这会在第三个维度上添加一个1
。然而,channels
为1
的含义是你需要弄清楚的。