拟合RNN LSTM模型时出错 [英] Error in fitting an RNN LSTM model

查看:228
本文介绍了拟合RNN LSTM模型时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码为二进制分类创建RNN LSTM模型

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)
                 )

但是它给了我以下错误

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

在尝试解决错误时,我将"binary_crossentropy"替换为"sparse_categorical_crossentropy",但其准确性达到了63%.对于此错误,还有其他解决方案吗?

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,)

推荐答案

模型的最后一层具有32个单位Dense(32,... ).假设您正在执行二进制分类,则最后一层应具有1个具有S型连接的单位.

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'])

这篇关于拟合RNN LSTM模型时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆