ValueError: Error when checking input: expected lstm_input - python

My data just contains just 30 rows. I have attached the screenshots of my data. I need to predict the 31st value with pervious 30 vaues in pow column in that data screenshot
Code:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# from keras.models import Sequential
# from keras.layers import Dense
# from keras.layers import LSTM
# from keras.layers import Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
data_training = pd.read_csv('G:\Agrima\Obj_Rec\power.csv')
data_training = data_training.iloc[:, 2:3].values
scaler = MinMaxScaler()
data_training = scaler.fit_transform(data_training)
print(data_training[0:10])
X_train = []
y_train = []
for i in range(10, data_training.shape[0]):
X_train.append(data_training[i-10:i])
y_train.append(data_training[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
print(X_train.shape)
print(y_train.shape)
regressior = Sequential()
regressior.add(LSTM(units = 60, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[0], 5)))
regressior.add(Dropout(0.2))
regressior.add(LSTM(units = 60, activation = 'relu', return_sequences = True))
regressior.add(Dropout(0.2))
regressior.add(LSTM(units = 80, activation = 'relu', return_sequences = True))
regressior.add(Dropout(0.2))
regressior.add(LSTM(units = 120, activation = 'relu'))
regressior.add(Dropout(0.2))
regressior.add(Dense(units = 1))
print(regressior.summary())
regressior.compile(optimizer='adam', loss = 'mean_squared_error')
regressior.fit(X_train, y_train, epochs=50, batch_size=32)
regressior.save_weights('my_model_weights.h5') #Dude this is imp.. Dont train each time.. Try googling how to load weight..or call me
#input new values to get new predicitons
new = 62 #this must be input of the last day data da --- like this it shld be : new = data_training[-1]
inputs = scaler.transform(new)
y_pred = regressior.predict(input)
print(y_pred)
# Visualising the results
plt.figure(figsize=(14,5))
plt.plot(X_train, color = 'red', label = 'Real Data')
plt.plot(y_pred, color = 'blue', label = 'Predicted Data')
plt.title('Power')
plt.xlabel('Time')
plt.ylabel('Power')
plt.legend()
plt.show()
Error log :
ValueError: Error when checking input: expected lstm_input to have
shape (20, 5) but got array with shape (10, 1)

Related

i am trying to forcasting crypto with tensorflow but its not working

i am trying to forcasting crypto with tensorflow but its not working
whats the problem in this code?
why its not working?
thats my dataset:
and its my code:
x_train = df['o'].values
y_train = df['c'].values
x_train = x_train[::resolution]
y_train = y_train[::resolution]
Normalize the data
from sklearn.preprocessing import minmax_scale
x_train = minmax_scale(x_train, feature_range=(0, 1))
x_train = x_train.reshape(-1, 1)
split test and train
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.1)
Tensorflow
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape=x_train.shape))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))
regressor.add(Dense(units = 1))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(x_train, y_train, epochs = 100, batch_size = 32)
pred = regressor.predict(x_test)
import matplotlib.pyplot as plt
plt.plot(x_test, label='real data')
plt.plot(pred, label='predicted data')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()
its a problem:

LSTM prediction. How can I start a prediction?

I have a problem with the prediction. The program is designed to predict stock market prices. Here EUR / USD.
I trained the model and tested it with the test data. The results look good. At this point I can't get any further.
How can I make a prediction?
I tested the program with data from yesterday.
How can I predict today's data?
What should I enter and in what format?
import math
import matplotlib.pyplot as plt
import keras
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.layers import *
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from keras.callbacks import EarlyStopping
daten=open("C:")
df = pd.read_csv(daten)
df
training_set = df.iloc[436065:438073, 4:5]
test_set = df.iloc[438074:438170, 4:5]
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
X_train = []
y_train = []
for i in range(70, 2000):
X_train.append(training_set_scaled[i-70:i, 0])
y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
n=50
dop=0.1
model = Sequential()#1
model.add(LSTM(units = n, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(dop))#2
model.add(LSTM(units = n, return_sequences = True))
model.add(Dropout(dop))#3
model.add(LSTM(units = n, return_sequences = True))
model.add(Dropout(dop))#4
model.add(LSTM(units = n, return_sequences = True))
model.add(Dropout(dop))#5
model.add(LSTM(units = n, return_sequences = True))
model.add(Dropout(dop))#6
model.add(LSTM(units = n, return_sequences = True))
model.add(Dropout(dop))#7
model.add(LSTM(units = n))
model.add(Dropout(dop))
model.add(Dense(units = 1))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
model.fit(X_train, y_train, epochs = 100, batch_size =32)
dataset_total = pd.concat((training_set, test_set), axis = 0)
inputs = dataset_total[len(dataset_total) - len(test_set) - 70:].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
type(inputs.shape)
X_test = []
for i in range(70, 167):
X_test.append(inputs[i-70:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
print(X_test.shape)
predicted_stock_price = model.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
predicted_stock_price.shape
plt.plot(df.loc[438074:438170, "Close"])
plt.grid(True)
plt.plot(predicted_stock_price)
plt.grid(True)

Sklearn error cannot reshape array of size 6912 into shape (614,154)

I watched this video on how to build your first neural network but got stuck with this error at 27:00 ValueError: cannot reshape array of size 6912 into shape (614,154)
https://www.youtube.com/watch?v=S2sZNlr-4_4
The code is below:
# This algorithm detects if a person has diabetes or not
# Load libraries
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import pandas as pd
# load data
from google.colab import files
uploaded = files.upload()
# Store the dataset
df = pd.read_csv('datasets_4511_6897_diabetes.csv')
# convert the data into an array
dataset = df.values
# get all of the rows from the first eight columns of the dataset
X = dataset[:,0:8]
y = dataset[:,8]
# process the data
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
# split the data into 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X_scale, y, test_size = 0.2, random_state = 4)
# build the model
model = Sequential([
Dense(12, activation ='relu', input_shape = (8,)),
Dense(15, activation = 'relu'),
Dense(1, activation = 'sigmoid')
])
#Compile the model (sgd = stochastic gradient descent)
model.compile(
optimizer = 'sgd',
loss = 'binary_crossentropy',
metrics=['accuracy']
)
# train the model
hist = model.fit(X_train, y_train, batch_size = 57, epochs = 1000, validation_split=0.2)
# evaluate the model on the training data set
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
pred = model.predict(X_train)
pred = [1 if y >= 0.5 else 0 for y in prediction]
#df = df.values.reshape(614,154)
print('confusion_matrix : /n', confusion_matrix(y_train, pred))
Do you have an idea on how to deal with this issue?
Thank you in advance

Do I have to preproces new data again to predict the model?

I have a save model and I want to load the model for new data predictions. I have new data and I have predicted the model, but the result of the prediction is completely wrong. Do I have to preproces new data again to predict the model?
This my save model code:
import numpy as np
from numpy import loadtxt
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Data_Sensor.csv')
dataset.head()
X = dataset.iloc[:, 1:3].values
y = dataset.iloc[:, -1].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential
model.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 2))
model.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
model.fit(X_train, y_train, batch_size = 10, epochs = 100)
model.save('model.h5')
y_pred = model.predict(X_test)
print(y_pred)
y_pred = (y_pred > 0.5)
print(y_pred)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
and this is my load model for predict new data, but the results wrong:
import numpy as np
import pandas as pd
import sklearn
from tensorflow.keras.models import load_model
model = load_model('model.h5')
import pandas as pd
dataset = pd.read_csv('Data_Sensor.csv')
dataset.head()
X = dataset.iloc[:, 1:3].values
print(X)
model.predict(X)
Yes, new data must be pre-processed before prediction exactly as you did with the training data.
For your example, you need to retain the fitted StandardScaler, e.g. by using get_params and set_params to restore it.
As suggested in the comments below, a better way of doing this with Keras is to add a BatchNormalization layer at the beginning of the model. This does the same transformation as the standard scaler and is saved together with the rest of the model:
model = Sequential()
model.add(BatchNormalization())
model.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 2))
model.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

What should be the input to Convolution neural network (CNN) using keras and tensorflow?

I'm trying to create CNN model using keras ad tensorflow as backend.
below is code for the same..
Cannot understand what input it is expecting...
import cv2,os
import glob
import numpy as np
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Input, Convolution2D, MaxPooling2D, Dense, Dropout, Flatten
PATH = os.getcwd()
data_path = PATH + '/data1/cat/*.PNG'
files = glob.glob(data_path)
X_data = []
for myFile in files:
image = cv2.imread (myFile)
image_resize = cv2.resize(image,(128,128))
X_data.append (image_resize)
image_data = np.array(X_data)
image_data = image_data.astype('float32')
image_data /= 255
print('X_data shape:', image_data.shape)
#Class ani labels
class_num = 2
total_Images = image_data.shape[0]
labels = np.ones((total_Images),dtype='int64')
labels[0:30] = 0
labels[31:] = 1
Y = to_categorical(labels,class_num)
#print(Y);
# Shuffle the dataset
x, y = shuffle(image_data, Y, random_state=2)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
input_shape = image_data[0].shape
#print(input_shape)
model = Sequential()
conv1 = Convolution2D(32,(3,3),padding='same',activation='relu')(input_shape)
conv2 = Convolution2D(32,(3,3),padding='same',activation='relu')(conv1)
pool_1 = MaxPooling2D(pool_size=(2,2))(conv2)
drop1 = Dropout(0.5)(pool_1)
conv3 = Convolution2D(64,(3,3),padding='same',activation='relu')(drop1)
conv4 = Convolution2D(64,(3,3),padding='same',activation='relu')(conv3)
pool_2 = MaxPooling2D(pool_size=(2,2))(conv4)
drop2 = Dropout(0.5)(pool_2)
flat = Flatten()(drop2)
hidden = Dense(64,activation='relu')(flat)
drop3 = Dropout(0.5)(hidden)
out = Dense(class_num,activation='softmax')(drop3)
model.compile(loss = 'categorical_crossentropy', optimizer= 'adam', metrics=['accuracy'])
model.fit(X_train,y_train,batch_size=16,nb_epoch=20, verbose=1, validation_data=(X_test,y_test))
model.evaluate(X_test,y_test,verbose=1)
Error: ValueError: Layer conv2d_1 was called with an input that isn't a
symbolic tensor. Received type: <class 'tuple'>. Full input: [(128, 128,3)].
All inputs to the layer should be tensors.
You are attempting to use the functional API and the sequential model all at once you need to first eliminate this line
model = Sequential()
Then, from the documentation of the functional API, we add an Input(channels,rows,columns) layer, and fill in the size values from your X_train matrix.
input_shape = Input()

Categories

Resources