Bayesian Network in Pomegranate: ValueError: Sample does not have the same number of dimensions as the model - bayesian-networks

I am trying to model a Bayesian Network in python using Pomegranate package. The network should be learned from data. So I am using .from_samples method. However I am having trouble using the method .predict_proba() and it gives me error.
This is how I build the model:
model = BayesianNetwork.from_samples(X_train, algorithm='chow-liu')
and this is how I do prediction:
model.predict_proba(X_train)
and this is the error I get:
ValueError: Sample does not have the same number of dimensions as the model. Your help would be highly appreciated.

I got the answer: you should define your state_names when calling the from_samples method.
Another question is how do we do classification using this model?

you should use predict() method to predict the state of the not-valued nodes.
Check the documentation for more details.
Also, in the repository you can find some interesting tutorials that will help you.

Please add [] around the sample you are passing

Related

Fine Tuning Blenderbot

I have been trying to fine-tune a conversational model of HuggingFace: Blendebot. I have tried the conventional method given on the official hugging face website which asks us to do it using the trainer.train() method. I also tried it using the .compile() method. I have tried fine-tuning using PyTorch as well as TensorFlow on my dataset. Both methods seem to fail and give us an error saying that there is no method called compile or train for the Blenderbot model.
I have also looked everywhere online to check how Blenderbot could be fine-tuned on my custom data and nowhere does it mention properly that runs without throwing an error. I have gone through Youtube tutorials, blogs, and StackOverflow posts but none answer this question. Hoping someone would respond here and help me out. I am open to using other HuggingFace Conversational Models as well for fine-tuning.
Thank you! :)
Here is a link I am using to fine-tune the blenderbot model.
Fine-tuning methods: https://huggingface.co/docs/transformers/training
Blenderbot: https://huggingface.co/docs/transformers/model_doc/blenderbot
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
mname = "facebook/blenderbot-400M-distill"
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
tokenizer = BlenderbotTokenizer.from_pretrained(mname)
#FOR TRAINING:
trainer = Trainer(
model=model,
args=training_args,
train_dataset=small_train_dataset,
eval_dataset=small_eval_dataset,
compute_metrics=compute_metrics,
)
trainer.train()
#OR
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=5e-5),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=tf.metrics.SparseCategoricalAccuracy(),
)
model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3)
None of these work! :(

Outputting multiple loss components to tensorboard from tensorflow estimators

I am pretty new to tensorflow and I am struggling to get tensorboard to display some of my custom metrics. The model I am working with is a tf.estimator.Estimator, with an associated EstimatorSpec. The first new metric I am trying to log is from my loss function, which is composed of two components: a loss for an age prediction (tf.float32) and a loss for a class prediction (one-hot/multiclass), which I add together to determine a total loss (my model is predicting both a class and an age). The total loss is output just fine during training and shows up on tensorboard, but I would like to track the individual age and the class prediction loss components as well.
I think a solution that is supposed to work is to add a eval_metric_ops argument to the EstimatorSpec as described here (Custom eval_metric_ops in Estimator in Tensorflow). I have not been able to make this approach work, however. I defined a custom metric function that looks like this:
def age_loss_function(labels, ages_pred, ages_true):
per_sample_age_loss = get_age_loss_per_sample(ages_pred, ages_true) ### works fine
#### The error happens on this line:
mean_abs_age_diff, age_loss_update_fn = tf.metrics.Mean(per_sample_age_loss)
######
return mean_abs_age_diff, age_loss_update_fn
eval_metric_ops = {"age_loss": age_loss_function} #### Want to use this in EstimatorSpec
The instructions seem to say that I need both the error metric and the update function which should both be returned from the tf.metrics command as in examples like the one I linked. But this command fails for me with the error message:
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with #tf.function.
I am probably just misusing the APIs. If someone can guide me on the proper usage I would really appreciate it. Thanks!
It looks like the problem was from a version change. I had updated to tensorflow 2.0 while the instructions I was following were from 1.X. Using tf.compat.v1.metrics.mean() instead gets past this problem.

Error incompatible shapes in function model.fit()

I am new in Keras. I want to try U-net. I used this tutorial from tensorflow: https://github.com/tensorflow/models/blob/master/samples/outreach/blogs/segmentation_blogpost/image_segmentation.ipynb.
I used the code for U-net creation with my own dataset. They have got images 256x256x3 and I made my images with same shape.
Now, I got error:
InvalidArgumentError: Incompatible shapes: [1376256] vs. [458752]
[[{{node training/Adam/gradients/loss/conv2d_23_loss/mul_grad/BroadcastGradientArgs}}] ]
It is in function model.fit(). I have got 130 training examples and batch size is 5 (I know, that those number are small...).
Please, Can anybody know, what can cause this error in function model.fit()?
Thank you for your help.
1376256 is exactly 3 x 458752. I suspect you are not correctly accounting for your channels somewhere. As this appears to be on your output layer, it may be that you're trying to predict 3 classes when there are only 1.
In future, or if this doesn't help, please provide more information including the code for your model and number of classes you're trying to predict, so people can better help.

Can I use the `tf.contrib.seq2seq.dynamic_decode` to replace the function `tf.nn.dynamic_rnn` in encoder-decoder framework?

Actually, I want to generate sequences just like the thing that Alex Grave's did. I have the implementation of tensorflow. At the same time, I want to try the attention-based seq2seq model to generate the handwriting. So about the decoder, I did it with tf.nn.dynamic_rnn, it works. Now, I want to use the attentiom-based in tensorflow, so I want to change that to tf.contrib.seq2seq.dynamic_decode. But I get the error below:
TypeError: Cannot convert a list containing a tensor of dtype <dtype: 'int32'> to <dtype: 'float32'> (Tensor is: <tf.Tensor 'vector_rnn/DEC_RNN/transpose_1:0' shape=(100, ?) dtype=int32>)
I check the API documents of both of them. tf.nn.dynamic
tf.contrib.seq2seq.dynamic.decode
About the return of them, I did not get any idea to solve this error.
If you get any idea, please tell me! I would appreciate it very much.
Actually, it works if I use the tf.nn.dynamic_rnn to code the attention layers in decoder of VAE, just different with the tf.contrib.seq2seq.dynamic_decode.

Keras + Tensorflow model convert to coreml exits NameError: global name ... is not defined

I've adapted the VAE example from the keras site to train on my data, and everything runs fine. But I'm unable to convert to coreml. The error is:
NameError: global name `batch_size' is not defined
Since batch_size clearly is defined in the python source, I'm guessing it has to do with how the conversion tool captures variable names. Does anyone know how I can fix it (or whether it is, indeed, possible to fix)?
Many thanks,
J.
I ran into a similar message when using parameters to construct the neural net. This should work:
from keras import models
batch_size = 50
model = models.load_model(filename, custom_objects={'batch_size': batch_size})
See also documentation: https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model