[英]Keras prediction of simple non-linear regression
this is driving me crazy.这真让我抓狂。 I want to use Keras to predict the value in g for a pressure sensor.
我想使用 Keras 来预测压力传感器的 g 值。 I created this code to predict the values based on the voltage output that I measure.
我创建了这段代码来根据我测量的电压输出预测值。 The accuracy is very low which, from what I understand, is due to the small data sample.
准确率非常低,据我所知,这是由于数据样本小。 Accordingly the prediction output quality is rather low, which is ok for now.
因此预测输出质量相当低,目前还可以。
What I do not figure out : I try to predict a single value and the result is off completely, ie when I input 3.4, the result is around 10, whereas it should be around 1000. When I put more values in the input array X_new , the result quality increases drastically (already with two values).我不明白的是:我尝试预测单个值,结果完全关闭,即当我输入 3.4 时,结果约为 10,而它应该约为 1000。当我在输入数组X_new 中放入更多值时,结果质量急剧增加(已经有两个值)。 What am I missing here?
我在这里缺少什么? Any input would be highly appreciated.
任何输入将不胜感激。
Here is my code :这是我的代码:
import numpy as np
from sklearn import preprocessing, model_selection
from matplotlib import pyplot
from keras.layers import Dense, Activation, LSTM
from keras.models import Sequential, load_model
X = np.array([0.9, 1.75, 2.25, 2.45, 2.7, 2.9, 3.08, 3.2, 3.32, 3.4, 3.45])
y = np.array([10, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000])
X_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
y_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
X_scaled = (X_scaler.fit_transform(X.reshape(-1, 1)))
y_scaled = (y_scaler.fit_transform(y.reshape(-1, 1)))
X_train, X_test, y_train, y_test = model_selection.train_test_split(X_scaled, y_scaled, test_size=0.4, random_state=3)
model = Sequential()
model.add(Dense(128, input_shape=(1,), activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=["accuracy"])
np.random.seed(3)
model.fit(X_train, y_train, epochs=256, batch_size=2, verbose=2)
_, accuracy = model.evaluate(X_train, y_train)
print('Accuracy: %.2f' % (accuracy*100))
model.save("workload_model/model.h5")
predicted = model.predict(X_test)
pyplot.plot(y_scaler.inverse_transform(y_train), color="red")
pyplot.plot(y_scaler.inverse_transform(predicted), color="blue")
pyplot.plot(y_scaler.inverse_transform(y_test), color="green")
print("X=%s\nPredicted=%s" % (X_scaler.inverse_transform(X_test), y_scaler.inverse_transform(predicted)))
# Test with new value
loaded_model = load_model("workload_model/model.h5")
X_new = np.array([3.4])
X_scaled = (X_scaler.fit_transform(X_new.reshape(-1, 1)))
predicted = loaded_model.predict(X_scaled)
print("X=%s\nPredicted=%s" % (X_scaler.inverse_transform(X_scaled), y_scaler.inverse_transform(predicted)))
pyplot.show()
X_new = np.array([3.4])
X_scaled = (X_scaler.fit_transform(X_new.reshape(-1, 1)))
predicted = loaded_model.predict(X_scaled)
this line should be这条线应该是
X_scaled = (X_scaler.transform(X_new.reshape(-1, 1)))
You're changing your scaler, that's why it works better with more data您正在更改定标器,这就是为什么它在处理更多数据时效果更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.