I want to predict outputs using two inputs using LSTM. my code is as follow:
from keras.models import Sequential
from keras.layers import Dense, LSTM
from numpy import array
from numpy.random import uniform
from numpy import hstack
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import pandas as pd
from sklearn.preprocessing import StandardScaler
import seaborn as sns
dataset_train = pd.read_csv('E:/Research/Summer22/Disaggregation/hourly1.csv')
cols = dataset_train[['TotalE','Outdoor_temp_F','electric.appliances..kWhs.','hvac']]
#Normalization
scaler = StandardScaler()
scaler = scaler.fit(cols)
df_for_training_scaled = scaler.transform(cols)
df_for_training_scaled= pd.DataFrame(data=df_for_training_scaled)
x = array(df_for_training_scaled.iloc[:, [0,1]] )
x = x.reshape(x.shape[0],x.shape[1])
x=x.astype(float)
y= array(df_for_training_scaled.iloc[:, [2,3]] )
y=y.astype(float)
print(y.shape[1])
out_dim = y.shape[1]
in_dim = x.shape[0]
plt.plot(y)
plt.show()
xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)
model = Sequential()
model.add(LSTM(64, input_shape=in_dim, activation="relu"))
model.add(Dense(out_dim))
model.compile(loss="mse", optimizer="adam")
model.summary()
History=model.fit(xtrain, ytrain, validation_split=0.7, epochs=10, batch_size=16, verbose=10)
When I try to build the model and save it to history I face an error:
Input 0 of layer "sequential_85" is incompatible with the layer: expected shape=(None, 2750, 2), found shape=(None, 2)
I know there is a problem with my input/output shape but I don't know how to fix it to predict ['electric.appliances..kWhs.','hvac'] using ['TotalE','Outdoor_temp_F']
Related
i try to feed my model with dataset values but i get a shape error. Can somebody help with it and explain how it works with datasets? Many thanks
import pandas as pd
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Model
dataset = tf.data.experimental.make_csv_dataset(
"mydata/*.csv",
batch_size=64,
field_delim=",",
num_epochs=1,
select_columns=['X1', 'X2', 'X3', 'Y'],
label_name='Y',
shuffle=False,
prefetch_buffer_size=64)
dataset = dataset.map(lambda x, y: (tf.concat(
[tf.expand_dims(x['X1'], axis=-1), tf.expand_dims(x['X2'], axis=-1), tf.expand_dims(x['X3'], axis=-1)], axis=-1), y))
inputs = Input(shape=(None, 3))
hidden1 = SimpleRNN(3, activation='relu', input_shape=(3, 1))(inputs)
outputs = Dense(units=1, activation='linear')(hidden1) # hidden layer 1
model_rnn = Model(hidden1, outputs)
model_rnn.compile(loss='mean_absolute_error', optimizer="adam", metrics=['mean_squared_error'])
history = model_rnn.fit(dataset, epochs=2, shuffle=False)
When I am writing the code for the Rainfall prediction using the Australia dataset I am getting the error during the fitting the ann model and running the epoch value for 10 .I am using the libraries such as numpy, pandas , matplotlib , seaborn as importing .For the running of the model I am using the Keras for the dense and sequential search.I am also using the standard scalar for normalizing the value of x.
I am getting the error in this line -
ann.fit(x_train,y_train, batch_size = 10, nb_epoch = 10, verbose = 1)
Below is my error -
ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (2,)
Below is my code-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('weatherAUS.csv')
df.head()
df.columns
plt.figure(figsize=(14,6))
df['MaxTemp'].plot()
plt.figure(figsize=(12,7))
sns.boxplot(x='RainToday',y='MaxTemp',data = df,palette ='winter')
df['Rainfall'].plot(kind= 'hist', bins=30, color='orange', figsize= (16,7))
from sklearn.model_selection import train_test_split
df.info()
df.dropna(inplace= True)
X=df[['Rainfall','MaxTemp','MinTemp']]
y=df[['RainToday','RainTomorrow']]
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2, random_state= 41)
#normalization to X values
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
xtrain = scaler.fit_transform(x_train)
xtest = scaler.transform(x_test)
import keras
from keras.layers import Dense
from keras.models import Sequential
ann = Sequential()
ann.add(Dense(units=32, init='uniform', activation='relu', input_dim = 3))
ann.add(Dense(units=16, init='uniform', activation='relu'))
ann.add(Dense(units=1, init='uniform', activation='sigmoid'))
ann.compile(optimizer ='adam', loss= 'mean_squared_error', metrics= ['accuracy'])
ann.fit(x_train,y_train, batch_size = 10, nb_epoch = 10, verbose = 1)
Y_pred = ann.predict(x_test)
Y_pred = [1 if y>=0.5 else 0 for y in Y_pred]
print(Y_pred)
you have the code
y=df[['RainToday','RainTomorrow']]
y should be a single column. You are trying to predict if it will rain tomorrow. RainToday is a feature that should be in X_train. I suggest you go an excellent tutorial that uses this dataset. It is located here.
I am running this model to ensemble a Neural Network and a Random Forest.
I got the the accuracy for the Random Forest model. However, after that I got a error that says.
ValueError: Filler values must be provided when X has more than 2 training features.
enter image description here
import itertools
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from mlxtend.classifier import StackingClassifier
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score, train_test_split
from mlxtend.plotting import plot_learning_curves
from mlxtend.plotting import plot_decision_regions
import pickle
import numpy as np
from keras.utils import to_categorical
from keras import backend as K
from sklearn.metrics import accuracy_score
pick = open('data.pickle','rb')
data = pickle.load(pick)
pick.close()
epochs = 23
batch_size = 15
features = []
labels = []
for feature , label in data:
features.append(feature)
labels.append(label)
X=np.array(features)
y= np.array(labels)
#Activation Function
def swish (x):
return K.sigmoid(x)*x
def build_ann():
CNN= Sequential()
CNN.add(Conv2D(32, kernel_size=(3,3), input_shape =(224,224,1)))
CNN.add(Activation(swish))
CNN.add(Conv2D(32, kernel_size=(3,3)))
CNN.add(Activation(swish))
CNN.add(MaxPooling2D(pool_size=(2,2)))
CNN.add(Conv2D(64, kernel_size=(3,3)))
CNN.add(Activation(swish))
CNN.add(Conv2D(64, kernel_size=(3,3)))
CNN.add(Activation(swish))
CNN.add(MaxPooling2D(pool_size=(2,2)))
#Turns inputs into a vector
CNN.add(Flatten())
# Fully connected layer
CNN.add(Dense(512))
CNN.add(Activation(swish))
CNN.add(Dense(2))
CNN.add(Activation('sigmoid'))
CNN.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return CNN
clf1=RandomForestClassifier(n_estimators=100, n_jobs=-1,
criterion='gini')
clf2= KerasClassifier(build_fn=build_ann, nb_epoch=epochs,
batch_size=batch_size, verbose=0)
lr = LogisticRegression()
sclf = StackingClassifier(classifiers=[clf1, clf2],
meta_classifier=lr)
label = ['Random Forest', 'KerasNet', 'Stacking Classifier']
clf_list = [clf1, clf2, sclf]
fig = plt.figure(figsize=(10,8))
gs = gridspec.GridSpec(2, 2)
grid = itertools.product([0,1],repeat=2)
clf_cv_mean = []
clf_cv_std = []
for clf, label, grd in zip(clf_list, label, grid):
scores = cross_val_score(clf, X, y, cv=3, scoring='accuracy')
print ("Accuracy: %.2f (+/- %.2f) [%s]" %(scores.mean(),
scores.std(), label))
clf_cv_mean.append(scores.mean())
clf_cv_std.append(scores.std())
clf.fit(X, y)
ax = plt.subplot(gs[grd[0], grd[1]])
fig = plot_decision_regions(X=X, y=y, clf=clf)
plt.title(label)
plt.show()
#plot classifier accuracy
plt.figure()
(_, caps, _) = plt.errorbar(range(4), clf_cv_mean,
yerr=clf_cv_std, c='blue', fmt='-o', capsize=5)
for cap in caps:
cap.set_markeredgewidth(1)
plt.xticks(range(3), ['RF', 'KN', 'Stacking'])
plt.ylabel('Accuracy'); plt.xlabel('Classifier'); plt.title('Stacking Ensemble');
plt.show()
#plot learning curves
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
plt.figure()
plot_learning_curves(X_train, y_train, X_test, y_test, sclf, print_model=False, style='ggplot')
plt.show()
can someone please help me. I have tried many unsuccessfully ways of ensembling using KerasClassifier.
I have been trying to implement a transfer learning model using the Xception model and fine-tune it. But when I tried to train the model in the last section of the code it is showing the following error - AttributeError: 'numpy.ndarray' object has no attribute '_in_multi_worker_mode'.Can someone help me solve this error or any code to train the model.My code is given below.
# Install TensorFlow
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as to
print(tf.__version__)
from tensorflow import keras
tf.random.set_seed(42)
import numpy as np
np.random.seed(42)
# Pandas and Numpy for data structures and util functions
import numpy as np
import pandas as pd
from numpy.random import rand
pd.options.display.max_colwidth = 600
# Scikit Imports
from sklearn.model_selection import train_test_split
# Matplot Imports
import matplotlib.pyplot as plt
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
import glob
import PIL
from PIL import Image
plt.rcParams.update(params)
%matplotlib inline
# pandas display data frames as tables
from IPython.display import display, HTML
import warnings
warnings.filterwarnings('ignore')
import sys
import os
from tensorflow.keras import utils as np_utils
from tensorflow.keras.utils import multi_gpu_model
from tensorflow.keras.utils import Sequence
from tensorflow.keras.models import Model
from tensorflow.keras import layers
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras import regularizers
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.layers import Dense, Dropout, Activation,
BatchNormalization, Flatten
from tensorflow.keras.models import Sequential,load_model
from tensorflow.python.keras.utils.data_utils import Sequence
from tensorflow.keras.preprocessing.image import ImageDataGenerator,
img_to_array, load_img
from tensorflow.keras.preprocessing.image import NumpyArrayIterator
from keras.applications import Xception
from tensorflow.keras.preprocessing import image
from tensorflow.keras import backend as K
imgFiles = glob.glob("dataset/*/*.jpg")
for items in imgFiles[:8]:
print(items)
X = []
y = []
for fName in imgFiles:
X_i = Image.open(fName)
X_i = X_i.resize((299,299))
X_i = np.array(X_i) / 255.0
X.append(X_i)
label = fName.split("/")
y_i = label[-2]
y.append(y_i)
print(set(y))
from sklearn.preprocessing import LabelEncoder
lEncoder = LabelEncoder()
y = lEncoder.fit_transform(y)
print(set(y))
print(lEncoder.classes_)
X = np.array(X)
y = np.array(y)
print(X.shape)
print(y.shape)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
stratify=y,
random_state=42)#splitting train and test
images to 70% and 30% reps.
print("X_train_shape: {}".format(X_train.shape))
print("X_test_shape: {}".format(X_test.shape))
mu = X_train.mean()
std = X_train.std()
X_train_std = (X_train-mu)/std
X_test_std = (X_test-mu)/std
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train,
test_size=0.15, stratify=y_train,
random_state=42)#splitting 15% of
train images for validation
print("X_val_shape: {}".format(X_val.shape))
# hyper parameters for model
nb_classes = 6 # number of classes
based_model_last_block_layer_number = 126
img_width, img_height = 299, 299
num_channels= 3
batch_size = 32
nb_epoch = 15 # number of iteration the algorithm gets trained.
transformation_ratio = .05 # how aggressive will be the data
augmentation/transformation
#data augmentation
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip='true',
vertical_flip='true')
train_generator = train_datagen.flow(X,y, shuffle=False,
batch_size=batch_size, seed=1)
validation_datagen = ImageDataGenerator(rescale=1. / 255)
val_generator = train_datagen.flow(X,y, shuffle=False,
batch_size=batch_size, seed=1)
# Pre-Trained CNN Model using imagenet dataset for pre-trained weights
# Transfer Learning!!
# Importing Xception pre trained model on ImageNet
base_model = keras.applications.xception.Xception(include_top=False,
weights='imagenet',
input_shape=(img_width, img_height, num_channels))
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all layers of the based model that is already pre-trained.
for layer in base_model.layers:
layer.trainable = False
# Top Model Block which is to be stacked over xception model
out = base_model.output
out = GlobalAveragePooling2D()(out)
out = Dense(1024, activation='relu')(out)
out = Dense(512, activation='relu')(out)
total_classes = y.shape[0]
predictions = Dense(total_classes, activation='softmax')(out)
model = keras.models.Model(inputs=base_model.input, outputs=predictions)
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=
['accuracy'])
model.summary()
callbacks_list = [keras.callbacks.ModelCheckpoint("bestTL.h5",
save_best_only=True)]
# Train the model
batch_size = batch_size
train_steps_per_epoch = X_train.shape[0] // batch_size
val_steps_per_epoch = X_val.shape[0] // batch_size
#training cnn up to 15 epoch
history = Model.fit(X_train_std,y_train,
steps_per_epoch=train_steps_per_epoch,
validation_data=val_generator,
validation_steps=val_steps_per_epoch,
callbacks=callback_list
epochs=15,
verbose=1)
How about using only tensorflow.keras.xxxx or keras.xxxx instead of using both of them at one time?
I'm trying to train a baseline ANN model for a binary classification with Keras (tensorflow backend) and Jupyter notebooks.
The code is the following:
array=df6.values
X= array[:,0:384]
Y = array[:,385]
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
seed = 7
np.random.seed(seed)
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
def create_baseline():
model = Sequential()
model.add(Dense(60, input_dim=60, kernel_initializer='normal', activation='relu'))
model.add(Dense(10, kernel_initializer='normal', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=create_baseline, epochs=100, batch_size=5, verbose=0)
kfold = StratifiedKFold(n_splits=2, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, encoded_Y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
Finally the error is the following:
ValueError: Error when checking input: expected dense_5_input to have shape (None, 60) but got array with shape (8, 384)
Also my dataset has 18 rows and 385 columns
I would like to know how to reshape correctly for a correct estimation of results. Thank you so much!
input_dim = 384
This argument refers to the shape of your input, which is X.