Search code examples
pythontensorflowmachine-learningsequential

need more accuracy while using tensorflow, Sequential predicting


language is Python 3-series. Using Tensorflow.

Here is code I made.

from keras.models import Sequential
from keras.layers import Dense
from sklearn.datasets import make_regression
from sklearn.preprocessing import MinMaxScaler
from numpy import array
import numpy as np
import random

xx1 = 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 answer
Xnew = array([[15, 8, 3]])
##predicted value : 15*8+3+5 = 128
# make a prediction
ynew = model.predict(Xnew)
# show the inputs and predicted outputs

print("xx[0] : %s" % (xx[0]))
print("yy : %s" % (yy))
print("XinPut=%s, Predicted=%s" % (Xnew[0], ynew[0]))

I made this code. xx[0] is 1000 * 3 Input dataset. yy is 1000 * 1 Output dataset.

I made yy as xx1 * xx2 + xx3 + 5, xx1 is xx[0,0], xx2 is xx[0,1], xx3 is xx[0,2]

I want to predict the output of [15, 8, 3]. As I think, It should be near 128, result of 15 * 8 + 3, but in reality, it is so far from that. Exactly, over 10000. I want to make this output more accuracily, but don't know how. What should I do?


Solution

  • If you are trying to build model to predict on series, use a recurrent neural networks for that. Since you are using Keras, add some LSTM or GRU layers for better results. You can read more about these layers in the below link

    https://keras.io/layers/recurrent/#rnn