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.
Related
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']
as you can see below i try to create an MLP with tensorflow/keras. But unfortunately the loss is always NaN when fitting. Do you have any advice?
as a second error message i get the message "'Functional' object has no attribute 'score'" when trying to measure accuracy with model.score, but i think this is a problem that is triggered by the first one.
thanks to all
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from mpl_toolkits import mplot3d
from sklearn import datasets
from various import printShapes, printNumpy, print_Model_Accuracy, printLARGE, checkFormat
from sklearn.datasets import make_blobs
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
np.random.seed(1234)
#%matplotlib qt
#%matplotlib inline
plt.rcParams["figure.figsize"] = [4*2, 4*2]
if 0:
iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.80, random_state=1234)
if 1:
X, y = make_blobs(n_features=4, centers=3, n_samples=1000, cluster_std = 5.0, random_state=1234)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1234)
print ("Target Label Example: y_train[0]:")
print (y_train[0])
print (type(y_train[0]))
printLARGE("MLP classifier TENSORFLOW")
tf.random.set_seed(1234)
Epochs = 10
inputs = keras.Input(shape=(4,), name="digits")
x = layers.Dense(100, activation="tanh", name="dense_1")(inputs)
x = layers.Dense(4, activation="tanh", name="dense_2")(x)
outputs = layers.Dense(3, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(
optimizer=keras.optimizers.RMSprop(), # Optimizer
loss=keras.losses.SparseCategoricalCrossentropy(), # Loss function to minimize
metrics=[keras.metrics.SparseCategoricalAccuracy()], # List of metrics to monitor
)
printShapes(X_train, "X_train", y_train, "y_train")
# TRAINING
model.fit(X_train, y_train, batch_size=64, epochs=Epochs)
printShapes(X_test, "X_test", y_test, "y_test")
# INFERENCE
y_test_predproba = model.predict(X_test)
print(y_test_predproba)
y_test_pred = np.argmax(y_test_predproba, axis = 1)
print(y_test_pred)
print_Model_Accuracy(model, X_test, y_test, y_test_pred)
Using tanh activation function in the hidden layers does not make
any sense. It should be ReLU.
Using one more hidden layer will be better than using more units in the first layer. [for your task]
However, using more hidden layers makes the model more vulnerable to over-fitting, adding Dropout layers solves the issue.
Finally, your model should be,
inputs = keras.Input(shape=(4,), name="digits")
x = layers.Dense(32, activation="relu", name="dense_1")(inputs)
x = layers.Dropout(0.2)(x)
x = layers.Dense(24, activation="relu", name="dense_2")(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(16, activation="relu", name="dense_2")(x)
outputs = layers.Dense(3, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
For the sake of reproducibility, the training and validations datasets I am using are shared here
The validation_dataset.csv is the ground truth of training_dataset.csv.
What I am doing below is feeding the datasets into a simple CNN layer that extracts the useful features of the images and feed that as 1D into the LSTM network for classification.
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers.convolutional import Conv1D
from keras.layers import LSTM
from keras.layers.convolutional import MaxPooling1D
from keras.layers import TimeDistributed
from keras.layers import Dropout
from keras import optimizers
from keras.callbacks import EarlyStopping
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from confusion_matrix import plot_confusion_matrix
import scikitplot as skplt
from numpy import genfromtxt
train_set = genfromtxt('data/train/training_dataset.csv', delimiter=',')
validation_set = genfromtxt('data/validation/validation_dataset.csv', delimiter=',')
train_set = train_set[..., None]
validation_set = validation_set[..., None]
X_train, X_test, y_train, y_test = train_test_split(train_set, validation_set, test_size=0.30, random_state=0)
batch_size=16
epochs=5
# Create the model
model = Sequential()
model.add(Conv1D(filters=5, kernel_size=3, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(50, return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(10))
model.add(Dense(1,kernel_initializer='random_normal'))
model.add(Activation('relu'))
adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
sgd = optimizers.SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=adam, loss='mean_squared_error', metrics=['mae', 'mape', 'mean_squared_error', 'acc'])
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs)
print(model.summary())
# Evaluate the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
skplt.metrics.plot_confusion_matrix(y_test, scores, x_tick_rotation=50, title=' ', normalize=True)
Finally, I want to plot the confusion matrix of the model using
skplt.metrics.plot_confusion_matrix(y_test, scores, x_tick_rotation=50, title=' ', normalize=True)
However, it is raising an error ValueError: Found input variables with inconsistent numbers of samples: [5394, 5].
How can we fix this error?
The second argument to skplt.metrics.plot_confusion_matrix must be the predicted labels (see https://scikit-plot.readthedocs.io/en/stable/metrics.html). But, you pass scores, which does not contain the predicted labels.
The fix would be to do:
y_pred = model.predict(X_test)
skplt.metrics.plot_confusion_matrix(y_test,
y_pred,
x_tick_rotation=50,
title=' ',
normalize=True)
I was working on SVM few days ago and when i tried to plot confusion matrix the following lines of code worked for me.
predicted=model.predict(X_test) #predicted output
cm=metrics.confusion_matrix(y_test, predicted)
df_cm = pd.DataFrame(cm, range(2), range(2))
sns.set(font_scale=1.4)
sns.heatmap(df_cm, annot=True, annot_kws={"size": 16})
plt.title('CONFUSION MATRIX ',fontdict={'fontsize': 14, 'fontweight': 'bold'})
plt.show()
i'm trying to compute the recall precision and F1 score for a multi label classification
using this code
and got errors for the classification_report statement
ValueError: Shape of passed values is (1617, 1), indices imply (1617, 6)
Wrong number of items passed 1, placement implies 6
Code:
from sklearn.metrics import classification_report
history = model.fit(X_train, y_train, batch_size=128, epochs=1, verbose=1,validation_data=(X_test, y_test), validation_split=0.3)
pred = model.predict(X_test, batch_size=128, verbose=1)
predicted = np.argmax(pred, axis=1)
report = classification_report(y_test, axis=1), predicted,comments_labels)#,comments_labels)
print(report)
my all code is
from numpy import array
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers.core import Activation, Dropout, Dense
from keras.layers import Flatten, LSTM
from keras.layers import GlobalMaxPooling1D
from keras.models import Model
from keras.layers.embeddings import Embedding
from sklearn.model_selection import train_test_split
from keras.preprocessing.text import Tokenizer
from keras.layers import Input
from keras.layers.merge import Concatenate
import nltk
from nltk.tokenize import word_tokenize
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
comments_labels = data[["l1", "l2", "l3", "l4", "l5", "l6"]]
nltk.download('punkt')
all_words = []
for sent in data['comment_text']:
tokenize_word = word_tokenize(sent)
for word in tokenize_word:
all_words.append(word)
unique_words = set(all_words)
print(len(unique_words))
def preprocess_text(sen):
# Remove punctuations and numbers
#sentence = re.sub('[^a-zA-Z]', ' ', sen)
# Single character removal
#sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)
# Removing multiple spaces
sentence = re.sub(r'\s+', ' ', sen)
return sentence
X = []
sentences = list(data['comment_text'])
for sen in sentences:
X.append(preprocess_text(sen))
y = comments_labels
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
tokenizer = Tokenizer(num_words=15784)#5000
tokenizer.fit_on_texts(X_train)
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
vocab_size = len(tokenizer.word_index) + 1
maxlen = 50 ######was 100
X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)
from numpy import array
from numpy import asarray
from numpy import zeros
deep_inputs = Input(shape=(maxlen,))
embedding_layer = Embedding(vocab_size, 100, trainable=False)(deep_inputs)
LSTM_Layer_1 = LSTM(128)(embedding_layer)
dense_layer_1 = Dense(6, activation='sigmoid')(LSTM_Layer_1)#softplus,selu,
model = Model(inputs=deep_inputs, outputs=dense_layer_1)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',recall_m,precision_m,custom_f1])
print(model.metrics_names)
from sklearn.metrics import classification_report
history = model.fit(X_train, y_train, batch_size=128, epochs=1, verbose=1,validation_data=(X_test, y_test), validation_split=0.3)
pred = model.predict(X_test, batch_size=128, verbose=1)
predicted = np.argmax(pred, axis=1)
report = classification_report(y_test, axis=1), predicted,comments_labels)#,comments_labels)
print(report)
test_output = model.predict(X_test, verbose=0)
Did you one hot encode target labels?
I think each label has size 1 and you need to convert it to one-hot labels because of Dense(6).
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?