Data reshape with Keras and Jupyter - python

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.

Related

ValueError: 'logits' and 'labels' must have the same shape

I am working on my first neural network, and i'm stuck on one error. Here is the code:
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv('iris.csv')
X = pd.get_dummies(df.drop(['variety'], axis=1))
y = df['variety'].apply(lambda x: 0 if x=='Setosa' else (1 if x=='Versicolor' else 2))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
print(y_train.head())
from keras.models import Sequential, load_model
from keras.layers import Dense
from sklearn.metrics import accuracy_score
model = Sequential()
model.add(Dense(units=8, activation='relu', input_dim=len(X_train.columns)))
model.add(Dense(units=3, activation='sigmoid'))
model.add(flatten())
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy')
model.fit(X_train, y_train, epochs=50, batch_size=1)
I am working off of a tutorial on tensorflow, and am using https://www.kaggle.com/datasets/arshid/iris-flower-dataset as the dataset to train on. I used the code from the tutorial, but changed it to fit my dataset. Still, I get the ValueError. Any help?

Problem with simple CNN in Keras, ValueError: Shapes (None, 1) and (None, 10) are incompatible

I'm trying to fit a simple CNN on the Fashion MNIST dataset. However, I run into a ValueError. I've browsed the web on this issues and found a lot about changning loss-function and/or activation function on the output layer. But I want to have my label-variable as a one-hot-encoded and have therefore choosed 'sigmoid' and 'categorical_crossentropy'.
Help that could push me in the right direction would be much appreciated.
import tensorflow as tf
import keras
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
(X_train_val, y_train_val), (X_test, y_test) = fashion_mnist.load_data()
X_train, X_val, y_train, y_val = train_test_split(X_train_val,
y_train_val,
test_size=0.15,
random_state=42)
X_train = X_train/255
X_val = X_val/255
X_test = X_test/255
y_train_ohe = keras.utils.np_utils.to_categorical(y_train, 10)
y_val_ohe = keras.utils.np_utils.to_categorical(y_val, 10)
y_test_ohe = keras.utils.np_utils.to_categorical(y_test, 10)
model = keras.models.Sequential([
keras.layers.Conv2D(64,7, activation='relu', input_shape=(28, 28, 1)),
keras.layers.MaxPooling2D((2)),
keras.layers.Flatten(),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(10, activation='sigmoid')
])
model.compile(loss="categorical_crossentropy", optimizer="nadam", metrics=["accuracy"])
model.summary()
history = model.fit(X_train, y_train_ohe, epochs=2, validation_data=(X_val, y_val))

How to split dataset into (X_train, y_train), (X_test, y_test)?

The training and validations datasets I am using are shared here for the sake of reproducibility.
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
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 numpy import genfromtxt
df_train = genfromtxt('data/train/training_dataset.csv', delimiter=',')
df_validation = genfromtxt('data/validation/validation_dataset.csv', delimiter=',')
#train,test = train_test_split(df_train, test_size=0.20, random_state=0)
df_train = df_train[..., None]
df_validation = df_validation[..., None]
batch_size=8
epochs=5
model = Sequential()
model.add(Conv1D(filters=5, kernel_size=3, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
#model.add(TimeDistributed(Flatten()))
model.add(LSTM(50, return_sequences=True, recurrent_dropout=0.2))
model.add(Dropout(0.2))
model.add(LSTM(10))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
model.compile(optimizer="rmsprop", loss='mse', metrics=['accuracy'])
callbacks = [EarlyStopping('val_loss', patience=3)]
model.fit(df_train, df_validation, batch_size=batch_size)
print(model.summary())
scores = model.evaluate(df_train, df_validation, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
I want to split the training and validation dataset into (X_train, y_train), (X_test, y_test) so that I can use both datasets for training and testing. I tried the split function of the Scikit-learn library -train,test = train_test_split(df_train, test_size=0.20, random_state=0) but it is giving me the following error after we invoke the model.fit() function.
ValueError: Data cardinality is ambiguous:
x sizes: 14384
y sizes: 3596
Please provide data which shares the same first dimension.
How can we split the dataset into (X_train, y_train), (X_test, y_test) sharing the same dimension?
One way is to have X and Y sets. Here, I assume the column name for Y is 'target'.
target = df_train['target']
df_train = df_train.drop(columns=['target'])
X_train, X_test, y_train, y_test = train_test_split(df_train, target, test_size=0.20, random_state=0)
--
It seems that I had initially misunderstood your problem, and "validation_dataset.csv" is your label data. I apologize for not reading correctly.
In this case, you do not need a "target" variable, as that is what df_validation would be. Therefore, I think the following may work:
X_train, X_test, y_train, y_test = train_test_split(df_train, df_validation, test_size=0.20, random_state=0)

Confusion Matrix - ValueError: Found input variables with inconsistent numbers of samples

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()

when i train the RNN using keras, i have a error

I train the KDD CUP Dataset using Keras, RNN.
When I train, I have this error.
"ValueError: ('Bad input argument to theano function with name
"/usr/local/lib/python2.7/dist-packages/Keras-1.0.5-py2.7.egg/keras/backend/theano_backend.py:527"
at index 0 (0-based)', "invalid literal for long() with base 10: 'SF'")"
This is my code:
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
import keras
from keras.preprocessing import sequence
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Embedding
from keras.layers import LSTM, SimpleRNN, GRU
import pandas as pd
max_features = 5000000
maxlen = 50 # cut texts after this number of words (among top max_features most common words)
batch_size = 32
nb_epoch = 100
print('Loading data...')
train_data=pd.read_csv('./data.csv')
labels = (train_data.ix[:,41:42].values)
train = (train_data.ix[:,0:41].values)
#labels = np_utils.to_categorical(labels)
test_data=pd.read_csv('./testdata.csv')
test = (test_data.ix[:,0:41].values)
'''
print('labels')
print(labels)
print('train')
print(train)
print('test')
print(test)
'''
print(train_data.shape[1])
print(train.shape)
print(labels.shape)
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2)) # try using a GRU instead, for fun
model.add(Dense(1))
model.add(Activation('sigmoid'))
# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print('Train...')
model.fit(train, labels, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=0.25, verbose=1, shuffle=True)
store = model.predict(test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

Categories

Resources