Open traderpedroso opened 3 years ago
LSTM is broken.
Thank you very much for reporting this issue. Could you please provide a reproducible test case? It would be helpful to be able to reproduce what you are seeing locally.
Hello below you can find the python 3.8.2 code executed in Visual studio code (version: 1.54.0-insider Commit: cd97629c4458c05b16a1fd444d21c61295ba2529 Date: 2021-02-09T08:36:56.595Z Electron: 11.2.3 Chrome: 87.0.4280.141 Node.js: 12.18.3 V8: 8.7.220.31-electron.0 OS: Darwin arm64 20.4.0)
I run the code on a Mac mini M1, Big Sur Version 11.3 beta(20E5172i) I think the problem is in the Dropout instruction.
Attached you will find the Excel file with the data
I have also attached the file where you can see the error in the data interpolation:
import numpy as np import matplotlib.pyplot as plt
import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D, LSTM, Dropout from tensorflow.keras.optimizers import Adam from tensorflow.keras.metrics import categorical_crossentropy from tensorflow.keras.preprocessing.image import ImageDataGenerator import xlrd
######### Serve per importare nelle matrici XX YY da un file EXCEL########
import numpy as np import xlrd def import_excel_matrix(path,s): table=xlrd.open_workbook(path).sheets()[s] #Get the first sheet table row=table.nrows #number of rows col=table.ncols #number of columns datamatrix=np.zeros((row, col)) #Generate an initial matrix of nrows rows*ncols columns for i in range(col):#traverse the column cols=np.matrix(table.col_values(i)) #convert list to matrix for matrix operation datamatrix[:, i]=cols #store data into matrix by column return datamatrix
#########################################################################################################
##########################################################################################################
data_file="/Users/andrea/Desktop/BBBY.xlsx" #excel file storage location df = import_excel_matrix(data_file,0) print(df)
print(df[0])
print(df.shape)
plt.figure(figsize=(16,8)) plt.ylabel('High') plt.title('Orginal Plot') plt.plot(df, color = 'black', label = 'Prezzo Effettivo') plt.show()
print(df.shape)
################costruisce il vettore training pai ad esempio all'80% del vettore totale#################### dataset = df training_data_len = int(np.ceil(len(dataset) * .80)) print(training_data_len) ################################################################################################
data = df
max = np.max(dataset) min = np.min(dataset) scaled_data = np.array([(x - min) / (max - min) for x in dataset])
print(scaled_data)
inter = 60
t = scaled_data.shape[0]
train_x = [] train_y = [] train_data = scaled_data[0:int(training_data_len),:]
for i in range(60,len(train_data)): train_x.append(train_data[i-60:i,0]) train_y.append(train_data[i,0]) if i<= 61: print(train_x) print(train_y)
train_x,train_y = np.array(train_x) , np.array(train_y)
train_x.shape
train_x = np.reshape(train_x,(train_x.shape[0], train_x.shape[1], 1))
print(train_x.shape[1],1)
#################################################################################################
model = Sequential() model.add(LSTM(units = 60 , return_sequences=True , input_shape = (train_x.shape[1],1))) model.add(LSTM(units = 60 , return_sequences = True)) model.add(Dropout(0.2)) model.add(LSTM(units = 60 , return_sequences = False)) model.add(Dense(25)) model.add(Dense(1))
model.compile(optimizer='adam' , loss='mean_squared_error')
history = model.fit(train_x,train_y, epochs=20) #batch_size=32
##############################################################################################
test_data = scaled_data[training_data_len - 60:,:] test_x = [] test_y = dataset[training_data_len: , :] for i in range(60,len(test_data)): test_x.append(test_data[i-60:i,0])
test_x = np.array(test_x)
test_x = np.reshape(test_x,(test_x.shape[0] , test_x.shape[1] , 1))
test_x.shape
print(test_x)
prediction = model.predict(test_x)
#############
loss=history.history['loss']
plt.plot(range(20),loss) plt.grid(True) plt.show()
############# print(prediction)
prediction = np.array([x * (max - min) + min for x in prediction])
print(prediction)
rmse = np.sqrt(np.mean(((prediction - test_y) ** 2)))
print(rmse) print(test_y)
train = data[:training_data_len] actual = data[training_data_len:]
plt.figure(figsize=(16,8)) plt.title('Model') plt.xlabel('Date', fontsize=18) plt.ylabel('Open Price USD ($)', fontsize=18)
plt.plot(actual) plt.plot(prediction) plt.legend(['Actual', 'Predictions'], loc='lower right') plt.show()
l = len(test_data) print(l)
x_input=test_data[(l - inter):].reshape(1,-1) x_input.shape print(x_input)
temp_input=list(x_input) temp_input=temp_input[0].tolist()
print(temp_input)
from numpy import array
lst_output=[] n_steps=inter i=0 while(i<30):
if(len(temp_input)>inter):
#print(temp_input)
x_input=np.array(temp_input[1:])
print("{} day input {}".format(i,x_input))
x_input=x_input.reshape(1,-1)
x_input = x_input.reshape((1, n_steps, 1))
#print(x_input)
yhat = model.predict(x_input, verbose=0)
print("{} day output {}".format(i,yhat))
temp_input.extend(yhat[0].tolist())
temp_input=temp_input[1:]
#print(temp_input)
lst_output.extend(yhat.tolist())
i=i+1
else:
x_input = x_input.reshape((1, n_steps,1))
yhat = model.predict(x_input, verbose=0)
print(yhat[0])
temp_input.extend(yhat[0].tolist())
print(len(temp_input))
lst_output.extend(yhat.tolist())
i=i+1
print(lst_output) lst_output = np.array(lst_output)
day_new=np.arange(1,inter+1) day_pred=np.arange(inter+1,inter+1+30)
l_dataset = len(dataset)
un_lst_output = np.array([x * (max - min) + min for x in lst_output])
un_scaled_data = np.array([x * (max - min) + min for x in scaled_data])
plt.figure(figsize=(16,8)) plt.plot(day_new,un_scaled_data[(l_dataset - inter):]) plt.plot(day_pred,un_lst_output) plt.grid(True) plt.show()
On 11 Feb 2021, at 03:32, anna-tikhonova notifications@github.com wrote:
Thank you very much for reporting this issue. Could you please provide a reproducible test case? It would be helpful to be able to reproduce what you are seeing locally.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/apple/tensorflow_macos/issues/154#issuecomment-777168388, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASHZENUZDMZ4KOSKWAIANPTS6M6VFANCNFSM4XCAKGQQ.
well the version a1 was running and on version a2 last update I got long value on val_loss I tested again on a1 and its ok
834/834 [==============================] - 0s 186us/sample - loss: 0.7029 - accuracy: 0.5048 - val_loss: 158605511374124346404503552.0000 - val_accuracy: 0.4886 Epoch 8/40 672/834 [=======================>......] - ETA: 0s - loss: 0.6857 - accuracy: 0.5774 Epoch 00008: val_accuracy did not improve from 0.52273 834/834 [==============================] - 0s 185us/sample - loss: 0.6929 - accuracy: 0.5612 - val_loss: 0.6616 - val_accuracy: 0.5227 Epoch 9/40 672/834 [=======================>......] - ETA: 0s - loss: 0.6930 - accuracy: 0.5253 Epoch 00009: val_accuracy did not improve from 0.52273 834/834 [==============================] - 0s 181us/sample - loss: 0.6910 - accuracy: 0.5396 - val_loss: 195273379.3502 - val_accuracy: 0.5227