apple / tensorflow_macos

TensorFlow for macOS 11.0+ accelerated using Apple's ML Compute framework.
Other
3.67k stars 309 forks source link

LSTM is broken #157

Open anmanci opened 3 years ago

anmanci commented 3 years ago

after upgrade to alpha 2 version i get wrong results from LSTM module. Please check it.

anna-tikhonova commented 3 years ago

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.

anmanci commented 3 years ago

In the previous email I forgot to tell you the version of Tensorflow I am using:

v0.1alpha2 https://github.com/apple/tensorflow_macos/tree/v0.1alpha2

On 11 Feb 2021, at 03:30, 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 authored the thread. Reply to this email directly, view it on GitHub https://github.com/apple/tensorflow_macos/issues/157#issuecomment-777167830, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASHZENT25VTATPXN5T76VUTS6M6L5ANCNFSM4XGBGNKQ.

David0609 commented 3 years ago

model = Sequential() model.add(LSTM(50, input_shape=(2,11)))

NotImplementedError: Cannot convert a symbolic Tensor (lstm_1/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

chufei-717 commented 3 years ago

模型=顺序()model.add(LSTM(50, input_shape=(2,11)))NotImplementedError:无法将符号张量(lstm_1/strided_slice:0)转换为numpy数组。此错误可能表明您正试图将张量传递给NumPy调用,该调用不受支持

I have encountered the same problem. How can I solve it?

florisvdf commented 3 years ago

Same issue here NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

Riccorl commented 3 years ago

This problem persists in v0.1alpha3

Same issue here NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

Romiroz commented 3 years ago

This problem persists in v0.1alpha3

Same issue here NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

I have the same problem when i update numpy. You need to downgrade it to 1.18.5

I-Thompson commented 3 years ago

This problem persists in v0.1alpha3

Same issue here NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

I have the same problem when i update numpy. You need to downgrade it to 1.18.5

I found downgrading to 1.19.5 (from 1.20) sufficient to solve this problem.

anmanci commented 3 years ago

The problem in LSTM persists even in the new version of Tensorflow alpha 3 :-( The same code on PC (Visual Studio Code for windows 10) and on Colab produces the same results. The same code reproduced on Mac m1 (Visual Studio Code) gives incorrect results!! I am also attaching the code so you can check for yourself:

#########################Program File##########################

import pandas as pd

import numpy as np import matplotlib.pyplot as plt from numpy.core.fromnumeric import ptp, reshape from numpy.core.records import array

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, Bidirectional from tensorflow.keras.optimizers import Adam from tensorflow.keras.metrics import categorical_crossentropy from tensorflow.keras.preprocessing.image import ImageDataGenerator import xlrd import xlsxwriter

from sklearn.preprocessing import MinMaxScaler

from tensorflow.keras.models import load_model

import yfinance

df = yfinance.download('BBBY','1990-01-01','2025-03-30') print(df)

print(df.head()) print(df.tail())

df1=df.reset_index()['Close']

scaler=MinMaxScaler(feature_range=(0,1)) df1=scaler.fit_transform(np.array(df1).reshape(-1,1))

print(df1)

splitting dataset into train and test split

training_size=int(len(df1)*0.65) test_size=len(df1)-training_size train_data,test_data=df1[0:training_size,:],df1[training_size:len(df1),:1]

def create_dataset(dataset, time_step=1): dataX, dataY = [], [] for i in range(len(dataset)-time_step-1): a = dataset[i:(i+time_step), 0] ###i=0, 0,1,2,3-----99 100 dataX.append(a) dataY.append(dataset[i + time_step, 0]) return np.array(dataX), np.array(dataY)

time_step = 100 X_train, y_train = create_dataset(train_data, time_step) X_test, ytest = create_dataset(test_data, time_step)

X_train =X_train.reshape(X_train.shape[0],X_train.shape[1] , 1) X_test = X_test.reshape(X_test.shape[0],X_test.shape[1] , 1)

print(X_train.shape[1],1)

model=Sequential() model.add(LSTM(50, return_sequences=True, input_shape = (X_train.shape[1],1))) model.add(LSTM(50,return_sequences=True)) model.add(LSTM(50)) model.add(Dense(1)) model.compile(loss='mean_squared_error',optimizer='adam')

model.summary() print(tf.version)

model.fit(X_train,y_train, epochs=10)

model.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=10,batch_size=64,verbose=1)

train_predict=model.predict(X_train) test_predict=model.predict(X_test)

print(train_predict) print(test_predict)

train_predict=scaler.inverse_transform(train_predict) test_predict=scaler.inverse_transform(test_predict)

print(train_predict) print(test_predict)

import math from sklearn.metrics import mean_squared_error math.sqrt(mean_squared_error(y_train,train_predict))

Plotting

shift train predictions for plotting

look_back=100 trainPredictPlot = np.empty_like(df1) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict

shift test predictions for plotting

testPredictPlot = np.empty_like(df1) testPredictPlot[:, :] = np.nan testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict

plot baseline and predictions

plt.plot(scaler.inverse_transform(df1)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show()

lung = len(test_data)

x_input=test_data[lung-100:].reshape(1,-1) x_input.shape x_input

temp_input=list(x_input) temp_input=temp_input[0].tolist()

demonstrate prediction for next 10 days

from numpy import array

lst_output=[] n_steps=100 i=0 while(i<30):

if(len(temp_input)>100):
    #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)

day_new=np.arange(1,101) day_pred=np.arange(101,131)

ll = len(df1)

plt.plot(day_new,scaler.inverse_transform(df1[len(df1)-100:])) plt.plot(day_pred,scaler.inverse_transform(lst_output)) plt.show()

df3=df1.tolist() df3.extend(lst_output) lll = len(df3) plt.plot(df3[lll-88:]) plt.show()

df3=scaler.inverse_transform(df3).tolist()

plt.plot(df3) plt.show()

######################################################################################

Dicer-Zz commented 3 years ago

Same issue, please fix it. I have tried many way but all of them don't work.😭