Jul-23-2020, 05:53 PM
Hi, I am communicating through a translator. Do not swear too much for this.
I have a question.
How can you implement a neural network. To analyze and predict the next step of the sequence.
Not a great example.
Sequence:
112233112233112233 ......
or
111211312111211312 ......
You can take any sequence.
But here's how to teach a neural network to predict the next step in a sequence.
Even in such simple sequences as in the example.
I have the following code.
But this is a neural network. Doesn't predict the next step. And repeats the previous ones.
How can this be fixed?
By changing the settings of this neural network. Does not improve results.
I will be grateful for any help.
P.s. I'm learning the language.))
I have a question.
How can you implement a neural network. To analyze and predict the next step of the sequence.
Not a great example.
Sequence:
112233112233112233 ......
or
111211312111211312 ......
You can take any sequence.
But here's how to teach a neural network to predict the next step in a sequence.
Even in such simple sequences as in the example.
I have the following code.
But this is a neural network. Doesn't predict the next step. And repeats the previous ones.
How can this be fixed?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import numpy import pandas as pd import math from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error from sklearn.metrics import accuracy_score # convert an array of values into a dataset matrix def create_dataset(dataset, look_back): dataX, dataY = [], [] for i in range ( len (dataset) - look_back - 1 ): xset = [] for j in range (dataset.shape[ 1 ]): a = dataset[i:(i + look_back), j] xset.append(a) dataX.append(xset) dataY.append(dataset[i + look_back, 0 ]) return numpy.array(dataX), numpy.array(dataY) # fix random seed for reproducibility numpy.random.seed( 1 ) # load the dataset file = 'test123456.xlsx' xl = pd.ExcelFile( file ) dataframe = xl.parse( 'Sheet1' ) dataset = dataframe.values dataset = dataset.astype( 'float32' ) # normalize the dataset scaler = MinMaxScaler(feature_range = ( 0 , 1 )) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int ( len (dataset) * 0.75 ) test_size = len (dataset) - train_size train, test = dataset[ 0 :train_size,:],dataset[train_size: len (dataset),:] # reshape into X=t and Y=t+1 look_back = 1 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = numpy.reshape(trainX, (trainX.shape[ 0 ], 1 ,trainX.shape[ 1 ])) testX = numpy.reshape(testX, (testX.shape[ 0 ], 1 ,testX.shape[ 1 ])) # create and fit the LSTM network model = Sequential() model.add(LSTM( 8 , input_shape = ( 1 , look_back))) model.add(Dense( 1 )) model. compile (loss = 'mean_squared_error' , optimizer = 'Adam' ) model.fit(trainX, trainY, epochs = 10000 , batch_size = 1 , verbose = 2 ) # make predictions trainPredict = model.predict(trainX) testPredict = model.predict(testX) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # print ( "X=%s, Predicted=%s" % (testPredict[ - 1 ],testX[ - 1 ])) print ( "X=%s, Predicted=%s" % (testPredict[ 0 ],testX[ 0 ])) |
I will be grateful for any help.
P.s. I'm learning the language.))