LSTM 输入整形

问题描述 投票:0回答:1

我是一名深度学习初学者,正在研究 LSTM 以解决多分类问题,我的数据集具有 features=10,timestepts=1,目标类别是 5 个类别(0,1,2,3,4)。

我不确定我是否正确地进行了输入重塑,因为当我想计算模型延迟时出现错误+我得到的好结果让我怀疑。

#Feature scaling 
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
train_data= scaler.fit_transform(train_data)
test_data= scaler.transform(test_data)



epochs = 10
batch_size = 128
feature_num=10 # number of features 
timesteps=1

# Reshape the input to shape (num_instances, timesteps, num_features)
train_data = np.reshape(train_data, (train_data.shape[0], timesteps, feature_num))
test_data=np.reshape(test_data, (test_data.shape[0], timesteps, feature_num))

# convert the target labels to one-hot encoded format
train_labels = to_categorical(train_labels, num_classes=5)
test_labels = to_categorical(test_labels, num_classes=5)


# build the model
model = Sequential()
model.add(LSTM(64, input_shape=(timesteps,feature_num), return_sequences=True, activation='sigmoid'))
model.add(Flatten())
model.add(Dense(5, activation='softmax')) 

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])



callback = EarlyStopping(patience=3)

history = model.fit(train_data, train_labels,
                    epochs=epochs,
                    batch_size=batch_size,
                    validation_data=(test_data, test_labels),
                    callbacks=[callback])




# Evaluate the model
y_pred = model.predict(test_data)
y_pred_classes = np.argmax(y_pred, axis=1)
y_test_classes = np.argmax(test_labels, axis=1)
print(classification_report(y_test_classes, y_pred_classes))



report = classification_report(y_test_classes, y_pred_classes, output_dict=True)

# extract the class names and metrics from the report
class_names = list(report.keys())[:-3]
metrics = ['precision', 'recall', 'f1-score']

# calculate the confusion matrix
conf_mat = confusion_matrix(y_test_classes, y_pred_classes)

# create a heatmap of the confusion matrix
sns.heatmap(conf_mat, annot=True, cmap='Blues')

# set the axis labels and title
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix')

# show the plot
plt.show()



#Measure model Latency 
start_time = time.time()
y_pred = model.predict(np.expand_dims(test_data, axis=0))
end_time = time.time()
latency = end_time - start_time
print(f"Latency: {latency} seconds")

我在测量延迟时遇到了这个错误

2023-03-07 15:31:53.584508: W tensorflow/core/framework/op_kernel.cc:1780] OP_REQUIRES failed at transpose_op.cc:142 : INVALID_ARGUMENT: transpose expects a vector of size 4. But input(1) is a vector of size 3

提前谢谢你。

python deep-learning data-science lstm recurrent-neural-network
1个回答
0
投票

尝试测量 model.predict() 的延迟时会发生错误。正如错误消息中所建议的那样。您正在使用 np.expand_dims(test_data, axis=0) 扩展 test_data 数组的维度。当您测量延迟时这是因为 model.predict() 需要具有形状(num_instances、timesteps、num_features)的 3D 输入。调试此问题,您可以打印 test_data 和 np.expand_dims() 输出的形状以确保它们具有预期的形状。您也可以尝试删除 np.expand_dims() 调用并使用原始 test_data 数组来测量延迟。

© www.soinside.com 2019 - 2024. All rights reserved.