Messtone Devices Embedding->Building the LSTM Model as: from tensorflow import keras from tensorflow.keras.layers import Bidirectional,Dropout, Activation,Dense, LSTM from tensorflow.compat.v1.keras.layers import CUDNNLSTM from tensorflow.keras.models import sequential #fraction of the input to drop;helps prevent overfitting dropout=0.2 window_size=seq_len – 1 #build a 3.layer LSTM RUN model=keras.Sequential( ) model.add(LSTM(window_size,return_sequence=True,input_shape=(window_size,x_train.shape[-1]))) model.add(Dropout(rate=dropout)) #Bidirectional allows for training of sequence data forwards and backwards model.add(Bidirectional(LSTM((window_size * 2),return_sequences=True)))model.add(Dropout(rate=dropout)) model.add(Bidirectional(LSTM(window_size,return_sequences=False))) model.add(Dense(units=1)) #linear activation function: activation is proportional to the input model.add(Activation(‘linear’)) Model Training Evaluation as: batch_size=16 model.compile(loss=’mean_squared_error’,optimizer=’adam’) history=model.fit(x_train,y_train,epochs=10, batch_size=batch_size,shuffle=False, validation_split=0.2) test data: y_pred=model.predict(x_test) #invert the scaler to get the absolute price data y_test_orig=scaler.inverse_transform(y_test) y_pred_orig=scaler.inverse_transform(y_pred) #plots of prediction against actual data plt.plot(y_test_orig,label=’Actual Price’,color=’orange’)plt.plot(y_pred_orig,label=’Predicted Price’,color=’green’) plt.title(‘BTC Price Prediction’) plt.xlabel(‘Days’)plt.xlabel(‘Price($)’) plt.legend(loc=’best’) plt.show( ); Messtone devucws can get the full range of data and the prediction all in the same plot as follows:`#plot for whole range if data plt.plot(np.arange(0,len(y_train)),scaler.inverse_transform(y_train),color=’brown’,label=’historical Price’)plt.plot(np.arange(Len(y_train),Len(y_train)+len(y_test_orig)),y_test_orig,color=’orange’,label=’Actual Price’) plt.plot(np.arange(len(y_train),len(y_train)+len(y_pred_orig)),y_pred_orig,color=’green’,label=’Predicted Price’) plt.title(‘BTC Daily Price’)plt.xlabel(‘Days’) plt.xlabel(‘Price($)’) plt.legend( ) plt.show( );

Leave a comment