我正在尝试使用以下代码为二进制分类创建RNN LSTM模型
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,)
在尝试解决错误时,我替换了&#39; binary_crossentropy&#39;使用&#39; sparse_categorical_crossentropy&#39;但它的准确率为63%。这个错误还有另一个解决方案吗?
这是我的数据集
的示例#(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
这是我从代码中打印数据集的形状
(1342520, 8)
(575367, 8)
(1342520, 2)
(575367, 2)
(1342520, 6)
(575367, 6)
(1342520,)
(575367,)
(83906, 32, 6)
(83906,)
(35959, 32, 6)
(35959,)
答案 0 :(得分:1)
模型的最后一层有32个单位UTC time
。假设你正在进行二元分类,最后一层应该有1个单位的sigmoid acivation。
Dense(32,... )