【问题标题】:Looking for ways and experiences to improve Keras model accuracy寻找提高 Keras 模型精度的方法和经验
【发布时间】:2021-06-06 06:43:08
【问题描述】:

我是一名电气工程师,我正在寻找一种解决方案来计算永久同步电机的直流电流。所以我决定用 Keras 等检查 ANN 解决方案。长话短说,我会给你看一些测量过的signals 的屏幕截图。

前 5 个信号是测量信号。最后一个是直流电流,我将对其进行估计。在这里,值是在电流钳的帮助下记录的。好的,我开始用 Python 构建模型并尝试了一些我认为会提高模型准确性的方法。但毕竟,我并没有从模型中得到那么好的结果,我希望我可能选择了错误的参数或者不是一个理想的模型。

这是我的代码:

import numpy as np
from keras.layers import Dense, LSTM
from keras.models import Sequential
from keras.callbacks import EarlyStopping
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from matplotlib import pyplot as plt
import seaborn as sns

# Import input (x) and output (y) data, and asign these to df1 and df1
df = pd.read_csv('train_data.csv')
df = df[['rpm','iq','uq','udc','idc']]
X = df[df.columns[:-1]]
Y = df.idc

plt.figure()
sns.heatmap(df.corr(),annot=True)
plt.show()

# Split the data into input (x) training and testing data, and ouput (y) training and testing data, 
# with training data being 80% of the data, and testing data being the remaining 20% of the data
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)#, shuffle=True)

# Scale both training and testing input data
X_train = preprocessing.maxabs_scale(X_train)
X_test = preprocessing.maxabs_scale(X_test)

model = Sequential()
model.add(Dense(4, input_shape=(4,)))
model.add(Dense(4, input_shape=(4,)))
model.add(Dense(1, input_shape=(4,)))

model.compile(optimizer="adam", loss="msle", metrics=['mean_squared_logarithmic_error','accuracy'])

# Pass several parameters to 'EarlyStopping' function and assign it to 'earlystopper'
earlystopper = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto')

model.summary()
history = model.fit(X_train, y_train, epochs = 2000, validation_split = 0.3, verbose = 2, callbacks = [earlystopper])

# Runs model (the one with the activation function, although this doesn't really matter as they perform the same) 
# with its current weights on the training and testing data
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# Calculates and prints r2 score of training and testing data
print("The R2 score on the Train set is:\t{:0.3f}".format(r2_score(y_train, y_train_pred)))
print("The R2 score on the Test set is:\t{:0.3f}".format(r2_score(y_test, y_test_pred)))

df = pd.read_csv('test_two_data.csv')
df = df[['rpm','iq','uq','udc','idc']]
X = df[df.columns[:-1]]
Y = df.idc
X_validate = preprocessing.maxabs_scale(X)

y_pred = model.predict(X_validate)

plt.plot(Y)
plt.plot(y_pred)
plt.show()

(weight_0,bias_0) = model.layers[0].get_weights()
(weight_1,bias_1) = model.layers[1].get_weights()

一个限制是我不能使用 LSTM 层或其他复杂算法,因为我需要稍后在电机应用程序的微控制器中实现经过训练的模型。

我想你可以给我找一些词来让我的模型在准确性上好一点。

最后是一张图,我向您展示了更差的预测性能。橙色是预测值,蓝色是测量值current。

training dataset 就是这个。

各个值之间的相关性可以在here 中找到。由于 id 和 ud 的值与 idc 无关,所以我决定删除它们。

【问题讨论】:

    标签: python tensorflow keras regression


    【解决方案1】:
    1. 在尝试提高模型的准确性时要记住的最重要的事情是始终规范化输入数据,这基本上意味着将实数值属性重新缩放到 0 和 1 的范围内。我我无法理解您向模型提供训练数据的方式。你能解释一下吗?更好地理解和识别更高准确度的范围。

    2. 现在如果我们谈论参数,我建议您为参数添加一个调整算法,以获得每个参数的优化值。 包含可以提供更好特征提取的隐藏层总是一个很好的方法。

    【讨论】:

    • 关于第 1 点:我尝试过 MinMaxScaler 或如上面代码所示的 MaxAbsScaler。但原则上,使用的定标器在微控制器上的实现需要付出更大的努力。因此,我已经尝试将参数除以它们的物理值范围。这也给出了没有很好准确性的结果。训练数据是特征'rpm','iq','uq','udc','idc'。它们分为 X 值('rpm'、'iq'、'uq'、'udc')和 Y 值('idc')。
    • 关于 2:有趣的一点。你能解释一下如何调整参数吗?我已经尝试添加几个隐藏层。准确率没有明显提高。我还想避免使用太多层,因为这需要在微控制器上进行大量计算。
    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 2012-01-29
    相关资源
    最近更新 更多