Keras中时间一维卷积的输入形状错误

时间:2019-02-02 17:17:31

标签: python keras conv-neural-network reshape

关于输入形状-已经使用LSTM已有一段时间了,并且没有任何问题,但是现在我尝试使用1D卷积层来加快处理速度,现在又遇到了麻烦-您能看到问题是什么吗?以下? (此处使用的虚拟数据)

安装时出现错误:

  

ValueError:检查目标时出错:预期density_17具有2   尺寸,但数组的形状为(400,20,2)

我看不到这里出什么问题了!!

代码如下所示

#load packages
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, GRU, 
TimeDistributed
from keras.layers import Conv1D, MaxPooling1D, Flatten, 
GlobalAveragePooling1D
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils

nfeat, kernel, timeStep, length, fs = 36, 8, 20, 100, 100

#data (dummy)
data = np.random.rand(length*fs,nfeat)
classes = 0*data[:,0]
classes[:int(length/2*fs)] = 1

#make correct input shape (batch, timestep, feature)
X = np.asarray([data[i*timeStep:(i + 1)*timeStep,:] for i in 
range(0,length * fs // timeStep)])
#classes
Y = np.asarray([classes[i*timeStep:(i + 1)*timeStep] for i in 
range(0,length * fs // timeStep)])

#split into training and test set
from sklearn.model_selection import train_test_split
trainX, testX, trainY, testY = 
train_test_split(X,Y,test_size=0.2,random_state=0)

#one-hot-encoding
trainY_OHC = np_utils.to_categorical(trainY)
trainY_OHC.shape, trainX.shape

#set up model with simple 1D convnet
model = Sequential()
model.add(Conv1D(8,10,activation=’relu’,input_shape=(timeStep,nfeat)))
model.add(MaxPooling1D(3))
model.add(Flatten())
model.add(Dense(10,activation=’tanh’))
model.add(Dense(2,activation=’softmax’))

model.summary()

#compile model
model.compile(loss=’mse’,optimizer=’Adam’ ,metrics=[‘accuracy’])

#train model

 model.fit(trainX,trainY_OHC,epochs=5,batch_size=4,
 validation_split=0.2)

0 个答案:

没有答案