Error in fitting an RNN LSTM model

冷暖自知 提交于 2019-12-11 01:51:40

问题


I am trying to create an RNN LSTM model for a binary classification using the following code

alldataset = np.loadtxt("FinalKNEEALL.txt", delimiter=",")
num_classes = 2
num_of_sam = alldataset.shape[0]
labels = np.ones((num_of_sam,), dtype='int64')
labels[0:958943]=0
labels[958943:1917887]=1
Y = np_utils.to_categorical(labels,num_classes)
x,y = shuffle (alldataset,Y, random_state=2)
x_train,x_test, y_train,y_test = train_test_split(x,y, test_size=0.3, random_state=4)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

x_train = x_train[:,[1,2,3,4,5,6]]
x_test = x_test[:,[1,2,3,4,5,6]]
y_train = y_train[:,0]
y_test = y_test[:,0]

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
input_width = 32
def windowz(data, size):
    start = 0
    while start < len(data):
        yield start, start + size
        start += (size // 2)

def segment_dap(x_train,y_train,window_size):
    segments = np.zeros(((len(x_train)//(window_size//2))-1,window_size,6))
    labels = np.zeros(((len(y_train)//(window_size//2))-1))
    i_segment = 0
    i_label = 0
    for (start,end) in windowz(x_train,window_size):
        if(len(x_train[start:end]) == window_size):
            m = stats.mode(y_train[start:end])
            segments[i_segment] = x_train[start:end]
            labels[i_label] = m[0]
            i_label+=1
            i_segment+=1
    return segments, labels
train_x, train_y = segment_dap(x_train,y_train,input_width)
test_x, test_y = segment_dap(x_test,y_test,input_width)

print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
print(test_y.shape)

model = Sequential()
model.add(LSTM(64, input_shape=(32, 6), kernel_initializer = 'normal',
               activation='tanh'))
model.add(Dense(32, kernel_initializer = 'normal', activation='sigmoid' ))
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])


hist = model.fit(train_x, train_y, batch_size = 30, epochs = 15,
                 verbose = 2
                 #validation_data=(test_x, test_y)
                 )

but it gives me the following error

ValueError: Error when checking target: expected dense_1 to have shape (32,) but got array with shape (1,)

In trying to solve the error I replace the 'binary_crossentropy' with 'sparse_categorical_crossentropy' but it makes the accuracy 63%. Is there another solution for this error?

This is a sample of my dataset

#(patient number, time in ms, normalization of X Y and Z,kurtosis, skewness, 
#pitch, roll and yaw, label(0 or 1)) respectively.

1,15,-0.248010047716,0.00378335508419,-0.0152548459993,-86.3738760481,0.872322164158,-3.51314800063,0
1,31,-0.248010047716,0.00378335508419,-0.0152548459993,-86.3738760481,0.872322164158,-3.51314800063,0
1,46,-0.267422664673,0.0051143782875,-0.0191247001961,-85.7662354031,1.0928406847,-4.08015176908,0
1,62,-0.267422664673,0.0051143782875,-0.0191247001961,-85.7662354031,1.0928406847,-4.08015176908,0

And this is the shapes of my dataset printing from the code

(1342520, 8)
(575367, 8)
(1342520, 2)
(575367, 2)
(1342520, 6)
(575367, 6)
(1342520,)
(575367,)
(83906, 32, 6)
(83906,)
(35959, 32, 6)
(35959,)

回答1:


The last layer of your model has 32 unitsDense(32,... ). Assuming you are doing binary classification the last layer should have 1 units with sigmoid acivation.

model = Sequential()
model.add(LSTM(64, input_shape=(32, 6), kernel_initializer = 'normal',
               activation='tanh'))
model.add(Dense(32, kernel_initializer = 'normal', activation='relu' ))
model.add(Dense(1,kernel_initializer = 'normal', activation="sigmoid")
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])


来源:https://stackoverflow.com/questions/50848107/error-in-fitting-an-rnn-lstm-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!