【发布时间】: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