Is there a way to do early-stopping and cross validation in CNTK? - python-3.x

As asked in the title, i would like to know if it is possible to make a model early-stop the epochs during training when the error is reduced enough, so i can avoid overfitting and guessing the right number of epochs at each call.
This is the only thing i have found in the official documentation but it is tobe used in brainscript, and i don't know a single thing about it. I'm using Python 3.6 with CNTK 2.6.
Also, is there a way to perform cross validation in a CNTK CNN?? How could this be done?
Thanks in advance.

The CrossValidationConfig class tells CNTK to periodically evaluate the model on a validation data set, and then call a user-specified callback function, which then can be used to update the learning rate or to return False to indicate early stopping.
For examples on how to implement early stopping:
test_session_cv_callback_early_exit function here
Source code for cntk.train.training_session here.

There isn't any native implementation for early stopping in cntk. For cross validation you can look up CrossValidationConfig

Related

How to get the logits for the T5 model when using the `generate` method for inference?

I'm currently using HuggingFace's T5 implementation for text generation purposes. More specifically, I'm using the T5ForConditionalGeneration to solve a text classification problem as generation.
The model's performance is overall very satisfactory after training, but what I am wondering is how I can get the logits for generation?
I'm currently performing inference as is suggested in the documentation via model.generate(**tokenizer_outputs), but this simply outputs the IDs themselves without anything else.
The reason why I want the logits is because I want to measure the model's confidence of generation. I'm not 100% certain if my approach is correct, but I'm thinking that if I can get the logit values of each generated token and average them, I could get the overall confidence score of the generated sequence.
Would anybody know how I could do this? Thanks.
I was struggling with this because I wasn't familiar with how the Transformers library works, but after looking at the source code all you have to do is set the arguments output_scores and return_dict_in_generate to True.
For more information, take a look at the method transformers.generation.utils.GenerationMixin.generate.

How to save model from best iteration in xgboost?

I am using XGBClassifier for my image classification. As i am new to machine learning and xgboost. But recently i got to know that the model i am saving by using pickle library after certain iteration is the last iteration not the best iteration. Can anyone tell me how can i save the model from best iteration? Obviously i am using early stop.
I kindly apologize if i make any mistake in asking questions. Please i need the solution as soon as possible because i need it for my thesis.
And those who are suggesting me older questions for best iteration please my question is different i want to save the best iteration in pickle format so that i can use it in future not just use it in predict later in the same code.
Thank you.
use joblib dump/load to save/load the model, and get the booster of the model, to get the best iteration

LightGBM Python API. Best_iteration and best_score for custom evaluation function (feval)

I'm using lightgbm.train with valid_sets, early_stopping_rounds and feval function for multiclass problem with "objective": "multiclass". I want to find best_iteration and best_score for my custom evaluation function. But it finds them for multi_logloss metrics, which is corresponding to specified objective. So the question is can I find in LightGBM best_iteration and best_score for my feval function and how?
This happens due to the fact that the objective function is included in the list of evaluation metrics by default. Early stopping in LightGBM happens based on any included metric. See a short summary and a link to another issue with a longer discussion in this LightGBM issue.
You can use objective:"multi_error", or also you can combine objectives as
objective: "multi_error", "multi_logloss"
Multi_error will directly focus on the accuracy.

I'm trying to implement 'multi-threading' to do both training and prediction(testing) at the same time

I'm trying to implement 'multi-threading' to do both training and prediction(testing) at the same time. And I'm gonna use the python module 'threading' as shown in https://www.tensorflow.org/api_docs/python/tf/FIFOQueue
And the followings are questions.
If I use the python module 'threading', does tensorflow use more portion of gpu or more portion of cpu?
Do I have to make two graphs(neural nets which have the same topology) in tensorflow one for prediction and the other for training? Or is it okay to make just one graph?
I'll be very grateful to anyone who can answer these questions! thanks!
If you use python threading module, it will only make use of cpu; also python threading not for run time parallelism, you should use multiprocessing.
In your model if you are using dropout or batch_norm like ops which change based on training and validation, it's a good idea to create separate graphs, reusing (validation graph will reuse all training variables) the common variable for validation/testing.
Note: you can use one graph also, with additional operations which changes behaviors based on training/validation.

How to stop training some specific weights in TensorFlow

I'm just beginning to learn TensorFlow and I have some problems with it.In training loop I want to ignore the small weights and stop training them. I've assigned these small weights to zero. I searched the tf API and found tf.Variable(weight,trainable=False) can stop training the weight. If the value of the weight is equal to zero I will use this function. I tried to use .eval() but there occurred an exception ValueError("Cannot evaluate tensor using eval(): No default ". I have no idea how to get the value of the variable when in training loop. Another way is to modify the tf.train.GradientDescentOptimizer(), but I don't know how to do it. Has anyone implemented this code yet or any other methods suggested? Thanks in advance!
Are you looking to apply regularization to the weights?
There is an apply_regularization method in the API that you can use to accomplish that.
See: How to exactly add L1 regularisation to tensorflow error function
I don't know any use-case for stopping training of some variables, probably it's not what you should do.
Anyway, calling tf.Variable() (if I got you right) is not going to help you, because it's called just once when the graph is defined. The first argument is initial_value: as the name suggests, it's assigned only during initialization.
Instead, you can use tf.assign like this:
with tf.Session() as session:
assign_op = var.assign(0)
session.run(assign_op)
It will update the variable during the session, which is what you're asking for.

Resources