使用的编程语言是Python 3系列,并使用了TensorFlow。
这是我编写的代码。
from keras.models import Sequentialfrom keras.layers import Densefrom sklearn.datasets import make_regressionfrom sklearn.preprocessing import MinMaxScalerfrom numpy import arrayimport numpy as npimport randomxx1 = array(random.sample(range(0,1000), 1000))xx2 = array(random.sample(range(0,2000), 1000))xx3 = array(random.sample(range(0,4000), 1000))xx = np.dstack((xx1, xx2, xx3))yy = np.array(xx1*xx2+xx3 +5)model = Sequential()model.add(Dense(4, input_dim=3, activation='relu'))model.add(Dense(4, activation='relu'))model.add(Dense(1, activation='linear'))model.compile(loss='mse', optimizer='adam')model.fit(xx[0], yy, epochs=1000, verbose=0)## xx[0] : 1000*3 input dataset, yy : 1000*1 output dataset# new instance where we do not know the answerXnew = array([[15, 8, 3]])##predicted value : 15*8+3+5 = 128# make a predictionynew = model.predict(Xnew)# show the inputs and predicted outputsprint("xx[0] : %s" % (xx[0]))print("yy : %s" % (yy))print("XinPut=%s, Predicted=%s" % (Xnew[0], ynew[0]))
这是我编写的代码。xx[0]是一个1000 * 3的输入数据集,yy是一个1000 * 1的输出数据集。
我将yy定义为xx1 * xx2 + xx3 + 5,其中xx1是xx[0,0],xx2是xx[0,1],xx3是xx[0,2]。
我想预测[15, 8, 3]的输出。我认为结果应该接近128,即15 * 8 + 3 + 5的结果,但实际上与此相差甚远,具体来说,超过10000。我希望能更准确地预测这个输出,但我不知道该怎么做。我该怎么办?
回答:
如果你试图构建一个用于序列预测的模型,可以使用循环神经网络。因为你在使用Keras,可以添加一些LSTM或GRU层以获得更好的结果。你可以在下面的链接中了解更多关于这些层的 информация。