Getting differents results for the identical model FFNN in keras - keras

I am building a model based on FFNN (Feed Forward Neural Network) using Keras.
I built a first version:
def mlp0(input_dim, loss):
model = Sequential()
model.add(Dropout(0.5, input_shape=(input_dim,)))
model.add(Dense(512, activation='sigmoid'))
model.add(Dense(1, activation='relu'))
model.compile(loss=loss, optimizer=Adagrad())
return model
This gives me very good results in k-fold cross-validation, but when I predict on validation set, the performance is bad.
So I tried another version.
def mlp1(input_dim, loss):
inputs = keras.Input(shape=(input_dim,))
x = keras.layers.Dropout(0.5)(inputs)
x = keras.layers.Dense(512, activation='sigmoid')(x)
outputs = keras.layers.Dense(1, activation='relu')(x)
model = keras.Model(inputs, outputs)
model.compile(loss=loss, optimizer=Adagrad())
return model
This second model gives worse results on cross-validation but the results are compatible with the results on the validation set.
To my eyes, they are identical models built in different ways, but for some reason they give me different answers. What am I doing wrong?
Edit:
These models behave the same way:
def mlp0(input_dim, loss):
model = Sequential()
model.add(Dense(512, activation='sigmoid', input_shape=(input_dim,), kernel_regularizer=regularizers.l2(0.01)))
model.add(Dense(1, activation='relu', kernel_regularizer=regularizers.l2(0.01)))
model.compile(loss=loss, optimizer=Adam())
return model
import keras
from keras import regularizers
def mlp1(input_dim, loss):
inputs = keras.Input(shape=(input_dim,))
x = keras.layers.Dense(512, activation='sigmoid', kernel_regularizer=regularizers.l2(0.01))(inputs)
outputs = keras.layers.Dense(1, activation='relu', kernel_regularizer=regularizers.l2(0.01))(x)
model = keras.Model(inputs, outputs)
model.compile(loss=loss, optimizer=Adam())
return model
These make me think there is a catch in prediction phase with the dropout

Related

How to properly stack LSTM and sklearn ML models

I am trying to stack Kears and ML models of sklean library for a regression problem. I am able to stack simple dense layers of Keras with any ML models.
My input type is similar to Boston house price (multi-variete). This code snippet shows how I tackle the problem:
#Scikit-learn Models
rnd_reg =RandomForestRegressor(n_estimators=100, random_state=42)
#Keras Model
def build_nn():
model = Sequential(
[Dense(512, activation='relu', input_shape=[X.shape[1]]),
Dense(256, activation='relu'),
Dropout(0.4),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dropout(0.2),
Dense(1, activation='linear')
])
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['MeanSquaredError', 'MeanAbsolutePercentageError'])
return model
keras_reg = tf.keras.wrappers.scikit_learn.KerasRegressor(build_nn,
epochs=20,
batch_size=1,
verbose=True)
keras_reg._estimator_type = "regressor"
st_reg = StackingRegressor(
estimators=[('rf', rnd_reg),
('Dense',keras_reg)],
final_estimator=XGBRegressor(random_state=42)
)
Everything works jus fine with this part. The issue is when I want to use RNN (LSTM for example) and stack it with sklearn models. I am not sure if this is due to the wrong input shape that I use or LSTM cannot be stacked with ML models of sklearn.
Below is my LSTM structure:
def build_nn1():
model = Sequential(
#[LSTM(50, activation='relu', batch_input_shape=(None, X.shape[1], 1)),
[LSTM(50, activation='relu', input_shape=[X.shape[1], 1]),
BatchNormalization(),
#Dropout(0.2),
Dense(20, activation='relu'),
Dense(1, activation='relu')
])
model.compile(optimizer=Adam(lr=1e-5),
loss='mean_squared_error',
metrics=['MeanSquaredError', 'MeanAbsolutePercentageError'])
return model
enter code here

How to add the Count Vectorizer to Simple RNN model?

For my NLP project I used CountVectorizer to Extract Features from a dataset using vectorizer = CountVectorizer(stop_words='english') and all_features = vectorizer.fit_transform(data.Text) and i also wrote a Simple RNN model using keras but I am not sure how to do the padding and the tokeniser step and get the data be trained on the model.
my code for RNN is:
model.add(keras.layers.recurrent.SimpleRNN(units = 1000, activation='relu',
use_bias=True))
model.add(keras.layers.Dense(units=1000, input_dim = 2000, activation='sigmoid'))
model.add(keras.layers.Dense(units=500, input_dim=1000, activation='relu'))
model.add(keras.layers.Dense(units=2, input_dim=500,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
can someone please give me some advice on this?
Thank you
add ensemble - you don't count vectorize, you use ensemble
https://github.com/dnishimoto/python-deep-learning/blob/master/UFO%20.ipynb
docs=ufo_df["summary"] #text
LABELS=['Egg', 'Cross','Sphere', 'Triangle','Disk','Oval','Rectangle','Teardrop']
#LABELS=['Triangle']
target=ufo_df[LABELS]
#print([len(d) for d in docs])
encoded_docs=[one_hot(d,vocab_size) for d in docs]
#print([np.max(d) for d in encoded_docs])
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
#print([d for d in padded_docs])
model=Sequential()
model.add(Embedding(vocab_size, 8, input_length=max_length))
model.add(Flatten())
model.add(Dense(8, activation='softmax'))
#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(padded_docs, target, epochs=50, verbose=0)

'KerasClassifier' object has no attribute 'save'

I have trained a model on colab using artifical neural network (keras) and in the end I want to save it but getting error. I have tried pydrive method also. I have all the required library previously when I run it on local computer it worked.
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
model = Sequential() # create model
model.add(Dense(6, input_dim = 8, activation = 'relu'))
model.add(Dense(6, activation = 'relu')) # hidden layer
model.add(Dense(1, activation = 'sigmoid')) # output layer
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(X_train, y_train, epochs=100, batch_size=10)
def build_classifier():
model = Sequential() # create model
model.add(Dense(6, input_dim = 8, activation = 'relu'))
model.add(Dense(6, activation = 'relu')) # hidden layer
model.add(Dense(1, activation = 'sigmoid')) # output layer
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model
model = KerasClassifier(build_fn = build_classifier, epochs=100, batch_size=32)
accuracies = cross_val_score(estimator = model,X = X_train, y = y_train, cv = 10, n_jobs = -1)
model.save("model.h5")
but getting error 'KerasClassifier' object has no attribute 'save'. does google colab required different method to save model?
'KerasClassifier' object has no attribute 'save'
Error occur because I tried to save KerasClassifier(Cross-validation function) Since the The purpose of cross-validation is model checking, not model building. So after changing parameter model will be saved.Thanks #MatiasValdenegro for insight.
Better explanation available at
https://stats.stackexchange.com/questions/52274/how-to-choose-a-predictive-model-after-k-fold-cross-validation

Find Most Important Input from a Neural Network

I trained a neural network with 37 Inputs. It has around 85% accuracy. Is it possible for me to find out which Input has the most effect. I tried this code but I cannot figure out how to find most important Input
weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]
One possible solution is to wrap your model with keras.wrappers.scikit_learn and then use Recursive Feature elimination in scikit-learn:
def create_model():
# create model
model = Sequential()
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=128, verbose=0)
rfe = RFE(estimator=model, n_features_to_select=1, step=1)
rfe.fit(X, y)
ranking = rfe.ranking_.reshape(digits.images[0].shape)
# Plot pixel ranking
plt.matshow(ranking, cmap=plt.cm.Blues)
plt.colorbar()
plt.title("Ranking of pixels with RFE")
plt.show()
If you need to visualize weights see here.

Keras translate example to use functional API

I have a working example in Keras which I want to translate to make use of the functional API. I am missing some detail I when I run the code I get ValueError: total size of new array must be unchanged when using embeddings.
Maybe someone sees what I am doing wrong.
The working code looks like this:
EMBEDDING_DIM=100
embeddingMatrix = mu.loadPretrainedEmbedding('englishEmb100.txt', EMBEDDING_DIM, wordMap)
###############
# Build Model #
###############
model = Sequential()
model.add(Embedding(vocabSize+1, EMBEDDING_DIM, weights=[embeddingMatrix], trainable=False))
#model.add(Embedding(vocabSize+1, EMBEDDING_DIM))
model.add(Bidirectional(LSTM(EMBEDDING_DIM, return_sequences=True)))
model.add(TimeDistributed(Dense(maximal_value)))
model.add(Activation('relu'))
# try using different optimizers and different optimizer configs
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
My attempt to re-write it with the functional API:
EMBEDDING_DIM=100
embeddingMatrix = mu.loadPretrainedEmbedding('englishEmb100.txt', EMBEDDING_DIM, wordMap)
input_layer = Input(shape=(longest_sequence,), dtype='int32')
emb = Embedding(vocabSize+1, EMBEDDING_DIM, weights=[embeddingMatrix]) (input_layer)
forwards = LSTM(EMBEDDING_DIM, return_sequences=True) (emb)
backwards = LSTM(EMBEDDING_DIM, return_sequences=True, go_backwards=True) (emb)
common = merge([forwards, backwards], mode='concat', concat_axis=-1)
dense = TimeDistributed(Dense(EMBEDDING_DIM, activation='tanh')) (common)
out = TimeDistributed(Dense(len(labelMap), activation='softmax')) (dense)
model = Model(input=input_layer, output=out)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
There is somewhere an operation that change the size but I am not sure where or why this is happening. I would be happy if someone could help me with this.

Resources