Tensorflow validation_data error for multi-input model - tensorflow

My tensorflow 2.6 model has two inputs. When I train this model without validation data - a la model.fit(x=[train_data1, train_data2], y= train_target)- it works perfectly. When I try to add some validation data, however, I receive errors.
model.fit(x=[train_data1, train_data2], y= train_target,
validation_data=([val_data1, val_data2], val_target))
throws the following error:
Layer Input__ expects 2 input(s), but it received 3 input tensors.
The closest thing I got for help is this question. There, the answerer suggests doing exactly as I have done. What can be done so that this model can use validation_data?

After an hour of beating my head against the wall, I restarted the kernel then tried
model.fit(x=[train_data1, train_data2], y= train_target,
validation_data=([val_data1, val_data2], val_target))
again, just like in the question. It worked...
Like every IT person in the history of the human race will remind you, "Did you try turning it off and on again?" Lesson learned.

try wrapping it in a numpy array or a tensor like this:
validation_data=(np.array([val_data1, val_data2]), val_target)

Related

ValueError: No gradients provided for any variable Huggingface

Hi I am following the Huggingface course for Question Answering.
I built my own Dataset and all the features are present and I get the exact same results up until fitting the model.
There I get the above error.
After some research it seems this is caused by not having the columns in the correct order.
The tokenizer does output it in a different order and I changed it, but neither the order in the course nor the order of the tokenizer seem to work.
Can someone think of another issue?
I don't have the Data Collator as it's deprecated now.
Token Type Ids are commented out because the tokenizer does not return them.
I'm using "distilbert-base-cased-distilled-squad" because I just want to try and that seems like the fastest (smallest) model.
tf_train_dataset = train_dataset.to_tf_dataset(
columns=[
"attention_mask",
"end_positions",
"input_ids",
"start_positions",
#"token_type_ids",
],
shuffle=True,
batch_size=4,
)
Thank you very much!
edit: I get the same error with the model from the tutorial.

Predict probability of predicted class

ml beginner here.
I have a dataset containing the GPA, GRE, TOEFL, SOP&LOR Ranking(out of 5)etc. (all numerical) , and a final column that states whether or not they were admitted to a university(0 or 1), which is what we'll use as y_train.
I'm supposed to not just classify the predicted labels, but also calculate the probability of each person getting admitted.
edit: so from the from the first comment, I built a Logistic Regression model, and with some googling I found 'predict_proba' from sklearn and tried implementing it. There werent any syntactical errors but the code values given by predict_proba were horribly wrong.
Link: https://github.com/tarunn2799/gre-pred/blob/master/GRE%20Admission%20Probability-%20Extraaedge.ipynb
please help me finding where I've gone wrong, and also tips to reduce the loss
thank you!
I read your notebook, but I'm confused why you think the predict_proba are horribly wrong..
Is the predict accuracy not good, or the format of predict_proba not as you expected?
You could use sklearn.metrics.accuracy_score(), sklearn.metrics.confusion_matrix() to check your predict label, or use sklearn.metrics.roc_auc_score() to check the result of predict_proba. Check both train & text parts are better.
I think the format of predict_proba is correct, or maybe you could try the predict_log_proba() to calculate the log probability?
Hope this could help you.

Tensorflow Lite export looks like it do not add weigths and add unsupported operations

I want to reload some of my model variables with the saved weight in the chheckpoint and then export it to the tflite file.
The question is a bit tricky without see code, so I made this Colab jupyter notebook with the complete code to explain it better (All code is working, you can actually copy in a new collab and change if you want):
https://colab.research.google.com/drive/1wSor4CxEz36LgElVi4y_N8uiSt4-j9b2#scrollTo=XKBQzoW_wd4A
I got it working but with two issues:
The exported .tflite file is like 3Ks, so I do not believe it is the entire model with the weights in it. Only the input is an image of 128x128x3, one weight for each is more than 3K.
When I finally import the model in Android, I have this error: "Didn't find custom op for name 'VariableV2' /n Didn't find custom op for name 'ReorderAxes' /n Registration failed."
Maybe the last error is cause the save/restore operations? They look like are there when I save the graph definition.
Thanks in advance.
I realize my problem.. I'm trying to convert to TFLITE a model without previously freezing it, TFLITE do not allow "VariableV2" nodes cause they should not be there..
All the problem is corrected freezing the model like this:
output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), ["output"])
I lost some time looking for that, hope it helps.

Getting loss for test image

I'm trying to get the loss from a test image in Faster R-CNN.
If I run copy.copy(trainer.previous_minibatch_loss_average) right after trainer.train_minibatch(data) then I can get the loss out for the trained image(mb=1).
When I try to do the exact same after trainer.test_minibatch(data) I get: This Value object is invalid and can no longer be accessed.
I've been looking around and it seems that other may have accomplish with something similar. Here.
Anyone know what to do to get the loss of a test image?
results = []
results.append(trainer.previous_minibatch_loss_average)
The above should work.

Gradient for Each Example Using map_fn

I want to get the gradient of a layer with respect to a parameter matrix for each example. Normally, I would need a Jacobian, but following this idea, I decided to use map_fn so I could feed forward data in a batch rather than one by one. This gives me a problem I do not understand, unfortunately. With the code
get_grads = tf.map_fn(lambda x: tf.gradients(x, W['1'])[0], softmax_probs)
sess.run(get_grads, feed_dict={x: images[0:100]})
I get this error
InvalidArgumentError: TensorArray map_21/TensorArray_36#map_21/while/gradients: Could not write to TensorArray index 0 because it has already been read.
W['1'] is a variable in the graph. Ideas?
It seems like your issue may be connected with the bug
https://github.com/tensorflow/tensorflow/issues/7643
One commenter posts a possible fix at the end. You could try that out.
Alternatively, if you what you want is the jacobian, then you can check out this solution:
https://github.com/tensorflow/tensorflow/issues/675#issuecomment-362853672
although it appears that it will not work when nested.
I don't think this will work because x in this case is a loop variable which TensorFlow does not know how to connect to softmax_probs.