history=model.fit_generator() Why is keras history empty?{} - keras

I can't obtain history from model.fit_generator()
net_history=densenet_model.fit_generator(
generator=train_loader,
steps_per_epoch=len(train_dataset),
max_queue_size=500,
workers=1,
validation_data=val_loader,
epochs=10,
validation_steps=len(val_dataset),
verbose=0,
callbacks=[checkpointer, tensorboard]
)
print(net_history.history)
print(net_history.history.keys())
result:
{}
dict_keys([])
But I can obtain history information from model.fit()
net_history2=densenet_model.fit(
x_train,
y_train,
validation_split=0.3,
epochs=4,
verbose=0,
batch_size=4,
callbacks=[checkpointer, tensorboard]
)
print(net_history2.history)
result:
{'val_loss': [1.0166720993050904, 0.8762002832421633, 0.9079110455290179, 0.8696109705439238],
'val_categorical_crossentropy': [0.8133353590965271, 0.677170991897583, 0.7131518721580505,
0.6792631149291992], 'val_categorical_accuracy': [0.5887850522994995, 0.6355140209197998,
0.5794392228126526, 0.6074766516685486], 'loss': [0.8654926233446067, 0.8416075144219495,
0.8553338176325748, 0.8491003025881192], 'categorical_crossentropy': [0.6599225, 0.6403823,
0.6583931, 0.6564969], 'categorical_accuracy': [0.6396761, 0.659919, 0.5991903, 0.562753]}
Why does this happen?
How can I get the loss and accuracy information from model.fit_generator?
ps: I failed to visualize iterative process through log event because each time I try to open it in tensorboard, the browser gets stuck in 'namespace hierarchy finding similar subgraphs' and then crashes... QAQ

You can stick with model.fit becuase it supports generators.
For TensorBoard you may want to wait a little bit longer (I thought that tensorboard stucked in 'namespace hierarchy finding similar subgraphs' but it was running and it took some time).
Note: for training and validation steps you should get the length of the generators which is equal to trainORval_dataset_size / batch size, not the length of the dataset. You don't want the same images to be repeated in the same epoch.
steps_per_epoch=len(train_loader)
validation_steps=len(val_loader)

As for why exactly this is happening I can only speculate that it is because model.fit_generator is deprecated, in TensorFlow 2.2 and higher you can just use model.fit because this now supports generators.
Source:
https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit_generator

Related

LSTM Autoencoder for Anomaly detection in time series, correct way to fit model

I'm trying to find correct examples of using LSTM Autoencoder for defining anomalies in time series data in internet and see a lot of examples, where LSTM Autoencoder model are fitted with labels, which are future time steps for feature sequences (as for usual time series forecasting with LSTM), but I suppose, that this kind of model should be trained with labels which are the same sequence as sequence of features (previous time steps).
The first link in the google by this searching for example - https://towardsdatascience.com/time-series-of-price-anomaly-detection-with-lstm-11a12ba4f6d9
1.This function defines the way to get labels (y feature)
def create_sequences(X, **y**, time_steps=TIME_STEPS):
Xs, ys = [], []
for i in range(len(X)-time_steps):
Xs.append(X.iloc[i:(i+time_steps)].values)
ys.append(y.iloc**[i+time_steps]**)
return np.array(Xs), np.array(ys)
X_train, **y_train** = create_sequences(train[['Close']], train['Close'])
X_test, y_test = create_sequences(test[['Close']], test['Close'])
2.Model is fitted as follow
history = model.fit(X_train, **y_train**, epochs=100, batch_size=32, validation_split=0.1,
callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, mode='min')], shuffle=False)
Could you kindly comment the way how Autoencoder is implemented in the link on towardsdatascience.com/?
Is it correct method or model should be fitted following way ?
model.fit(X_train,X_train)
Thanks in advance!
This is time series auto-encoder. If you want to predict for future, it goes this way. The auto-encoder / machine learning model fitting is different for different problems and their solutions. You cannot train and fit one model / workflow for all problems. Time-series / time lapse can be what we already collected data for time period and predict, it can be for data collected and future prediction. Both are differently constructed. Like time series data for sub surface earth is differently modeled, and for weather forecast is differently. One model cannot work for both.
By definition an autoencoder is any model attempting at reproducing it's input, independent of the type of architecture (LSTM, CNN,...).
Framed this way it is a unspervised task so the training would be : model.fit(X_train,X_train)
Now, what she does in the article you linked, is to use a common architecture for LSTM autoencoder but applied to timeseries forecasting:
model.add(LSTM(128, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(RepeatVector(X_train.shape[1]))
model.add(LSTM(128, return_sequences=True))
model.add(TimeDistributed(Dense(X_train.shape[2])))
She's pre-processing the data in a way to get X_train = [x(t-seq)....x(t)] and y_train = x(t+1)
for i in range(len(X)-time_steps):
Xs.append(X.iloc[i:(i+time_steps)].values)
ys.append(y.iloc[i+time_steps])
So the model does not per-se reproduce the input it's fed, but it doesn't mean it's not a valid implementation since it produce valuable prediction.

Cnn prediction is very poor inspite of 70% of accuracy

I am new to stackoverflow and deep learning.I have been following a example of multi label image classification from below.
https://www.analyticsvidhya.com/blog/2019/04/build-first-multi-label-image-classification-model-python/
I reduced the images to 500...with 2 epochs...It gives accuracy of around 72% in test set and training test.This I did because I was getting memory error.
But the predictions are very very poor...I took a image from training set..Even for that the prediction is completely wrong...
Please can you help me out.Apologies in case I missed something from my end.
Thanks.
#Jankey, 2 epochs is not enough to properly train a neural network, you can start with 50 for MNIST. If you have memory issues, try to decrease batch size to 8 or 16.
model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), batch_size=16)
You can also decrease the number of convolution filters to 32 and number of nodes in Dense layer to 32. Try to make it more efficient according to your infrastructure limitations.
Thanks for suggestion and guidance .I did the following changes.
1) model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), batch_size=32)
2) kept two convolutional layers with 32 filters
3) Change input shape as (64,64,3) tested then changed to (224,224,3).
With these I am able to get better predictions.Not 100%.But far better.
Will try to tune and test the model further.
Again Thanks for quick help.

Early Stopping, Model has gone through how many epochs?

I am using Keras. I am training my Neural Network and using Early Stopping. My patience is 10 and the epoch with the lowest validation loss is 15. My network runs til 25 epochs and stops however my model is the one with 25 epochs not 15 if I understand correctly
Is there an easy way to revert to the 15 epoch model or do I need to re-instantiate the model and run 15 epochs?
Yes, there is one, the restore_best_weights parameter in the EarlyStopping callback, set this to True and Keras will keep track of the weights producing the best loss:
callback = EarlyStopping(..., restore_best_weights=True)
See all the parameters for this callback here.
Yes, you get the model (weights) corresponding to the epoch when it stops. A commonly used strategy is to save the model whenever the validation loss/acc improves.
Early Stopping doesn't work the way you are thinking, that it should return the lowest loss or highest accuracy model, it works if there is no improvement in model accuracy or loss, for about x epochs (10 in your case, the patience parameter) then it will stop.
you should use callback modelcheckpoint functions instead e.g.
keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1)
https://keras.io/callbacks/
This will save or checkpoint best model encountered during the training history.

How Keras really fits the models via epochs

I am a bit confused on how Keras fits the models. In general, Keras models are fitted by simply using model.fit(...) something like the following:
model.fit(X_train, y_train, epochs=300, batch_size=64, validation_data=(X_test, y_test))
My question is: Because I stated the testing data by the argument validation_data=(X_test, y_test), does it mean that each epoch is independent? In other words, I understand that at each epoch, Keras train the model using the training data (after getting shuffled) followed by testing the trained model using the provided validation_data. If that's the case, then no matter how many epochs I choose, I only take the results of the last epoch!!
If this scenario is correct, so we do we need multiple epoches? Unless these epoches are dependent somwhow where each epoch uses the same NN weights from the previous epoch, correct?
Thank you
When Keras fit your model it pass throught all the dataset at each epoch by a step corresponding to your batch_size.
For exemple if you have a dataset of 1000 items and a batch_size of 8, the weight of your model will be updated by using 8 items and this until it have seen all your data set.
At the end of that epoch, the model will try to do a prediction on your validation set.
If we have made only one epoch, it would mean that the weight of the model is updated only once per element (because it only "saw" one time the complete dataset).
But in order to minimize the loss function and by backpropagation, we need to update those weights multiple times in order to reach the optimum loss, so pass throught all the dataset multiple times, in other word, multiple epochs.
I hope i'm clear, ask if you need more informations.

How do I show both Training loss and validation loss on the same graph in tensorboard through keras?

I'm using Keras with the Tensorflow back end to train a CNN, and I'm using tensorboard to visualize the loss functions and accuracy. I would like to see the loss function of both the training data and validation data on the same graph, but I've only found ways to do so when using Tensorflow and not through keras.
Is there a way to do so?
Edit 1:
I tried writing loss/acc in the Regex but instead of putting both of the graphs together it shows them side by side like so:
http://imgur.com/a/oLIcL
Ive added what I use to log to tensor board:
tbCallBack=keras.callbacks.TensorBoard(log_dir='C:\\logs', histogram_freq=0, write_graph=False, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
model.fit_generator(train_generator,
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=(x_test, y_test))
You can add a regex in the text box in the upper left corner of the Tensorboard window.
Add acc for accuracy of both train/validation data. Add lossfor the loss values. This works for me for Keras as well as Tensorflow.
Got this from this nice tutorial on TB: https://www.youtube.com/watch?v=eBbEDRsCmv4
As a code snippet I use this:
logdir = "_tf_logs/" + now.strftime("%Y%m%d-%H%M%S") + "/"
tb = TensorBoard(log_dir=logdir)
callbacks=[tb]
...
model.fit(X_train, Y_train, validation_data=val_data, epochs=10, verbose=2, callbacks=callbacks)
I found this from Github for this exact purpose but without using tensorboard. Hope this helps!
live loss plot for keras

Resources