tensorflow boosted tree classifier multi class - python

In the current version of TF (2.2.0) there is an option
to do multi class classification (i.e., more than two classes, by changing
n_classes to the relevant number in the estimator params).
However, all previous examples that I saw, for example the formal one here:
https://www.tensorflow.org/tutorials/estimator/boosted_trees_model_understanding
present binary classification. So I'm not sure what to do with the target (classes) vector.
If I keep him in the range of [0,...,num_classes-1], when I try to train the model, I get the error (comes from TF gradients.py file):
"'int' object has no attribute 'is_compatible_with'". It feels like a dimension\shape error with respect to class vector, but I
couldn't find the default loss function and what the this model expects to get. I don't think I need to convert the class vector to binary matrix (one hot encoding). Appreciate any help!

Indeed when I've changed the TF code manually everything worked.
Then, I found out there is a bug report on the issue here:
https://github.com/tensorflow/tensorflow/issues/40063

Related

Improve the result of EfficientNet

Halo there, I'm still struggling in python.
Now I'm going to use the EfficientNet model to detect the ripeness of palm oil.
I'm using 5852 training picture which is divided into 4 class (1463 per class) with 132 testing picture (33 per class).
After testing with 200 epoch, the result is far from good.
Is there any solution for me to improve the result?
Here's the result of my model accuracy and model loss.
Here's my code
https://colab.research.google.com/drive/18AtIP7aOycHPDR84PuQ7iS8aYUdclZIe?usp=sharing
your help means a lot to me.
You have rescaling in your generators, and it may be the root of the problem.
Tensorflow implementation of Efficientnets already contain rescaling layer, so you mustn't rescale images in your ImageDataGenerator. You can check this via .summary() method.
Official documentation says:
Note: each Keras Application expects a specific kind of input preprocessing. For EfficientNet, input preprocessing is included as part of the model (as a Rescaling layer), and thus tf.keras.applications.efficientnet.preprocess_input is actually a pass-through function. EfficientNet models expect their inputs to be float tensors of pixels with values in the [0-255] range
Resnets, for example, don't have this layer, and you should rescale images before feeding them to the model. It's tricky to remember those things for every single network from tf.keras.applications, so I suggest to just check them before using new models.

how to create and use weighted metrics in Keras?

I want to make a weighted metric, and print it out as Keras trains my data. I however fail to find any working examples of how to do this.
When running:
metrics = [MyClass.MyWeightedMetric]
model.compile(optimizer=RMSprop,loss="mean_squared_error",metrics=metrics)
where
class MyClass:
#staticmethod
def MyWeightedMetric(y_true,y_pred,sample_weight=None):
print(sample_weight)
#do stuff that doesn't even use sample_weight for now
then it prints None all the time. If I change my compilation line into
model.compile(optimizer=RMSprop,loss="mean_squared_error",weighted_metrics=metrics)
then I get the errors:
(0) Invalid argument: Can not squeeze dim[0], expected a dimension of 1, got 16384
when calling model.fit(). It's not clear to me what I'm doing wrong or what I should be doing instead. I tried making a subclass as in this example https://www.tensorflow.org/api_docs/python/tf/keras/metrics/Metric but that came with its own set of errors that I could not resolve.
Is there somewhere a working example of weighted metrics in keras, that I can provide at model compile stage? I find many examples of unweighted usages, but working weighted metric example seems impossible to find at the moment. For what it's worth, I'm using Tensorflow 2.0
When you pass weighted_metrics to model.compile during the training, the model expects a sample_weight column vector to compute the weighted metric. In your case, it seems like you are passing flatten tensor. The shape should be (16384, 1) instead of (16384). Further information
https://www.tensorflow.org/guide/keras/train_and_evaluate#using_a_validation_dataset

How to get predictions out of tensorflow model after you've used tf.group on your optimizers

I'm trying to write something similar to google's wide and deep learning after running into difficulties of doing multi-class classification(12 classes) with the sklearn api. I've tried to follow the advice in a couple of posts and used the tf.group(logistic_regression_optimizer, deep_model_optimizer). It seems to work but I was trying to figure out how to get predictions out of this model. I'm hoping that with the tf.group operator the model is learning to weight the logistic and deep models differently but I don't know how to get these weights out so I can get the right combination of the two model's predictions. Thanks in advance for any help.
https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/Cs0R75AGi8A
How to set layer-wise learning rate in Tensorflow?
tf.group() creates a node that forces a list of other nodes to run using control dependencies. It's really just a handy way to package up logic that says "run this set of nodes, and I don't care about their output". In the discussion you point to, it's just a convenient way to create a single train_op from a pair of training operators.
If you're interested in the value of a Tensor (e.g., weights), you should pass it to session.run() explicitly, either in the same call as the training step, or in a separate session.run() invocation. You can pass a list of values to session.run(), for example, your tf.group() expression, as well as a Tensor whose value you would like to compute.
Hope that helps!

No Support Vector Attribute

The project I am currently working on makes use of the sklearn svm.SVC class where at one point in the code instantiate the following:
self.classifier = OneVsRestClassifier(SVC(kernel = 'linear', probability = True))
After fitting the classifier, I then try to inspect the support_vector_ or support_ attributes of the classifier. However, I get the following error:
'SVC' object has no attribute 'support_vectors_'
I tried changing the kernel to 'poly' or 'rbf', but this does not fix the error. Why is this happening? Shouldn't any linear SVM have something (i.e. 'None' at the least) for this attribute? I am using sklearn version 0.15.1 if that helps.
Thanks!
Assuming you obtained the error message by trying to evaluate
self.classifier.estimator.support_vectors_
observe that OneVsRestClassifier clones your estimator as many times as there are classes and fits as many of them to your data. They can be found in the estimators_ variable of the ovr. Try
self.classifier.estimators_[0].support_vectors_
That will give you the support vectors for the first OVR problem.

Scikit-learn model parameters unavailable? If so what ML workbench alternative?

I am doing machine learning using scikit-learn as recommended in this question. To my surprise, it does not appear to provide access to the actual models it trains. For example, if I create an SVM, linear classifier or even a decision tree, it doesn't seem to provide a way for me to see the parameters selected for the actual trained model.
Seeing the actual model is useful if the model is being created partly to get a clearer picture of what features it is using (e.g., decision trees). Seeing the model is also a significant issue if one wants to use Python to train the model and some other code to actually implement it.
Am I missing something in scikit-learn or is there some way to get at this in scikit-learn? If not, what is the a good free machine learning workbench, not necessarily in python, in which models are transparently available?
The fitted model parameters are stored directly as attributes on the model instance. There is a specific naming convention for those fitted parameters: they all end with a trailing underscore as opposed to user-provided constructor parameters (a.k.a. hyperparameters) which don't.
The type of the fitted attributes is algorithm-dependent. For instance for a kernel Support Vector Machine you will have the arrays support vectors, dual coefs and intercepts while for random forests and extremly randomized trees you will have a collection of binary trees (internally represented in memory as contiguous numpy arrays for performance matters: structure of arrays representation).
See the Attributes section of the docstring of each model for more details, for instance for SVC:
http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC
For tree based models you also have a helper function to generate a graphivz_export of the learned trees:
http://scikit-learn.org/stable/modules/tree.html#classification
To find the importance of features in forests models you should also have a look at the compute_importances parameter, see the following examples for instance:
http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html#example-ensemble-plot-forest-importances-py
http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances_faces.html#example-ensemble-plot-forest-importances-faces-py

Categories

Resources