绘制RUL预测中的测试引擎预测表现
下图为C-MAPSS的4个数据子集测试集的所有发动机引擎的真实RUL与估计的RUL表现,并按照真实RUL值的大小从小到大排列。
# 导出预测rul和真实rul, pred为预测函数, model为训练后的NN模型
pred_rul, real_rul = pred(model, test_iter)
# 将pred_rul和real_rul拼接,以方便进行排序
rul = np.concatenate((pred_rul, real_rul), axis=1)
rul = rul[np.argsort(rul[:, 1])]
# 绘图
font_dict = dict(fontsize=12, color='k', family='Cambria', weight='light', style='normal')
plt.figure(figsize=(9, 7))
plt.plot(range(rul[:, 1].shape[0]), rul[:, 1], 'k-', label='Label Values')
plt.plot(range(rul[:, 0].shape[0]), rul[:, 0], 'r.-', label='Estimated Values')
plt.xlabel('Test Engine ID', loc='center', fontdict=font_dict)
plt.ylabel('RUL(Cycle)', loc='center', fontdict=font_dict)
plt.title(subDataset, fontdict=font_dict)
plt.legend()
plt.show()
savefig_path = 'root_path' + subDataset + '.svg' # set path of figures
plt.savefig(savefig_path)
pred函数可参考下列代码:
# pred()为RUL预测函数
def pred(model, test_iter):
model.eval()
outputs = []
with torch.no_grad():
for x, y in test_iter:
predict_y = model(x)
if outputs == []:
#将原来在gpu上的数据搬到cpu上,并转化为numpy类型
outputs = predict_y.cpu().numpy()
real_y = y.cpu().numpy()
else:
predict_y = predict_y.cpu().numpy()
y = y.cpu().numpy()
outputs = np.concatenate((outputs, predict_y), axis=0)
real_y = np.concatenate((real_y, y), axis=0)
return outputs, real_y