Obtaining SHAP summary plot for xgboost model in R - xgboost

I have an xgboost model built using mlr package. The model is of class "WrappedModel". I would like to get a SHAP summary plot for this but it turns out I can do this only if the model is of class "xgb.Booster".
Is there a way to convert the model from "WrappedModel" to "xgb.Booster" ? Or a package which can get the plot for "WrappedModel" class model directly ?
Thanks

Related

Plot ROC curve using tensorflow model with multiclass

I have trained a CNN model using tensorflow to classify 5 classes.
How do I plot the ROC curve for each of 5 classes with one-versus-rest?
From the scikit page, it says:
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
But tensorflow dataset, I don't have this "y_test" and "y_score" dataset structure
And every other tutorial website just copies the same code
I use "tf.keras.preprocessing.image_dataset_from_directory" to generate a test_ds variable
How do I get the tpr and fpr with model.predict function on test_ds? or other methods?
How do I get this "y_test" and "y_score" from model.predict and test_ds?
Apart from one-versus-rest, how could I plot the ROC for specific class 1 versus class 3?
Thanks!

Updating a BERT model through Huggingface transformers

I am attempting to update the pre-trained BERT model using an in house corpus. I have looked at the Huggingface transformer docs and I am a little stuck as you will see below.My goal is to compute simple similarities between sentences using the cosine distance but I need to update the pre-trained model for my specific use case.
If you look at the code below, which is precisely from the Huggingface docs. I am attempting to "retrain" or update the model and I assumed that special_token_1 and special_token_2 represent "new sentences" from my "in house" data or corpus. Is this correct? In summary, I like the already pre-trained BERT model but I would like to update it or retrain it using another in house dataset. Any leads will be appreciated.
import tensorflow as tf
import tensorflow_datasets
from transformers import *
model = BertModel.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
SPECIAL_TOKEN_1="dogs are very cute"
SPECIAL_TOKEN_2="dogs are cute but i like cats better and my
brother thinks they are more cute"
tokenizer.add_tokens([SPECIAL_TOKEN_1, SPECIAL_TOKEN_2])
model.resize_token_embeddings(len(tokenizer))
#Train our model
model.train()
model.eval()
BERT is pre-trained on 2 tasks: masked language modeling (MLM) and next sentence prediction (NSP). The most important of those two is MLM (it turns out that the next sentence prediction task is not really that helpful for the model's language understanding capabilities - RoBERTa for example is only pre-trained on MLM).
If you want to further train the model on your own dataset, you can do so by using BERTForMaskedLM in the Transformers repository. This is BERT with a language modeling head on top, which allows you to perform masked language modeling (i.e. predicting masked tokens) on your own dataset. Here's how to use it:
from transformers import BertTokenizer, BertForMaskedLM
import torch
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased', return_dict=True)
inputs = tokenizer("The capital of France is [MASK].", return_tensors="pt")
labels = tokenizer("The capital of France is Paris.", return_tensors="pt")["input_ids"]
outputs = model(**inputs, labels=labels)
loss = outputs.loss
logits = outputs.logits
You can update the weights of BertForMaskedLM using loss.backward(), which is the main way of training PyTorch models. If you don't want to do this yourself, the Transformers library also provides a Python script which allows you perform MLM really quickly on your own dataset. See here (section "RoBERTa/BERT/DistilBERT and masked language modeling"). You just need to provide a training and test file.
You don't need to add any special tokens. Examples of special tokens are [CLS] and [SEP], which are used for sequence classification and question answering tasks (among others). These are added by the tokenizer automatically. How do I know this? Because BertTokenizer inherits from PretrainedTokenizer, and if you take a look at the documentation of its __call__ method here, you can see that the add_special_tokens parameter defaults to True.

Create CNN model architecture diagram in Keras

I followed the tutorial to create visually graphical representation of CNN model using this: https://keras.io/visualization/
My code at the moment is as follows:
from keras.utils import plot_model
from keras.applications.resnet50 import ResNet50
import numpy as np
model = ResNet50(weights='imagenet')
plot_model(model, to_file='model.png')
When I use the aforementioned code I am able to create a graphical representation (using Graphviz) of ResNet50 and save it in 'model.png'. But I want to create block diagram of the CNN model with the layers instead. An example of my desired output is as follows:
Any idea how I can achieve the aforementioned block diagram programatically instead of just generating the graph diagram of the CNN model?
Here is a comprehensive list of existing solutions (not only for Keras): How do you visualize neural network architectures?.
Pay attention on Netron.

how to do finetune using pre-trained model in tf.estimator

i got a model converted from caffe by using MMDNN tool, it converted the caffe model into a saved_model tensorflow style. it's a resnet18 model, and i just strip out several layers in the last, i wish i could load this architecture in the model_fn in a tf.estimator, and manually add some extra layers to do my job.
As the tutorial recommended that I could use loader.load method to load the saved_model. But i just want to use it in a estimator, and i need to define the architecture in the model_fn function. I searched out the SO and github but there isn't a very specific workflow to do that thing, somebody could help me out?
Here is one way of fine tuning using tf.Estimator:
Define your model using the SAME variable names/scopes as in your saved model
Use tf.estimator's warm start functions to initialize your new model with the saved weights. Here is a code snippet :
if fine_tuning:
ws = tf.estimator.WarmStartSettings(ckpt_to_initialize_from=path_saved_model,
vars_to_warm_start='.*')
else:
ws = None
estimator = tf.estimator.Estimator(model_fn=model_function,
warm_start_from=ws,
...
)
This will initialize any variable that share names between your currently defined graph and the saved model.

Display Tensorflow Model Summary as like in Keras

We can build the model with tensorflow layers. Is there any way we can display the model summary as like in Keras.
Keras Model Summary
No, there is no such option. TensorFlow is a lot more generic than Keras and allows arbitrary graph architectures, so showing such a structured summary does not make sense for arbitrary TensorFlow graphs. The closest is probably TensorBoard, which has a very handy interactive graph visualization tool.
Keras is part of TensorFlow (for some time) so you can always get nice things like:
model.output_shape # model summary representation
model.summary() # model configuration
model.get_config() # list all weight tensors in the model
model.get_weights() # get weights and biases