Training features with different dimensions - keras

I have two types of features that should be concatenated and trained, features are as follow:
Images with dimension (277,217,3)
vector of real numbers (40,1)
What I did is after training the model on all images, I extracted the output of predict() function of all samples, this outputs vector, then I concatenated this vector with the real number features. But the problem is when I trained the values of predict() alone, the result was lower than the base model's result.
So I am trying to add the features after the convolutional layers while the model is training
Thanks in advance!!

Related

How to do backpropagation only on a select few labels instead of all labels in a multilabel classification?

I am using a pretrained neural network (resnet) on multiple datasets.
This neural network would have in it's output all the labels that are present in all the datasets,that is, like an union of all labels.
For example:
If dataset A has labels x,y,z,w
Dataset B has labels -m,l,n,o,x,y
Dataset C has labels-w,z,m,o
Then the neural network would have all labels in it's final layer-that is->m,l,n,o,w,x,y,z.
Now depending on which dataset I have, I want the model to train only on the dataset's own labels and not do backpropagation on other labels.
How can this be achieved?
I am working in Pytorch.
Maybe use three loss functions each of which has different values for the argument pos_weight such that it has zeros corresponding to the classes not included in a dataset.
Why do you care if the network does backpropagation on other labels? That is how it is supposed to work.
If the idea is to reduce the number of output features from what they have in the pretrained network, just remove the last layer of the network and add in your own with the desired output features. Then train as you would.

A multi-input (text and numeric) model for regression giving same output

I have a problem where I need to predict the number of clicks based on Date, CPC(cost per click), Market(US or UK) and keywords(average length 3-4 words, max 6). So the features that I decided to input the model were:
Day(1-31) WeekDay(0-6) Month(1-12) US-Market(0 or 1) UK-Market(0 or 1) CPC(continuous numeric)
And the output is the continuous numeric Number of Clicks.
For Keywords I used keras tokenizer to convert to sequences and the padded those sequences. The I used the glove word embeddings and created an embedding matrix and fed to the nueral Network model as described here in pretrained glove embeddings section.
The model that I used is:
The last Dense layer has linear activation. The model has two inputs (nlp_input for text data) and meta_input for (numerical,categorical data). Both models are concatenated after the Bidirectional LSTM to the nlp_input
The loss is :
model.compile(loss="mean_absolute_percentage_error", optimizer=opt,metrics=['acc'])
where opt = Adam(lr=1e-3, decay=1e-3 / 200)
I trained the model for 100 epochs and the loss was close to 8000 at that point.
But when I apply prediction to the test they result in the same number for all test inputs and that number is even negative -4.5 * e^-5. Could someone guide me as to how should I approach this problem and what improvements could I do to the model.

Probability Distribution of batch in keras

I am trying to train a CNN model on imbalanced dataset. I wanted to know how well a batch approximates the distribution in the training dataset. Is there any parameter in an inbuilt function in keras which could be specified to maintain the same distribution in batches?
It's possible to train and get good results depending on how severe the imbalance is.
But yes, there are easy ways to compensate this, such as using sample_weight and class_weight in the fit method.
From the documentation on the fit method:
class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.
sample_weight: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile().
So, you can compensate three kinds of imbalance:
Class imbalance: when you have classes (results) that are more present than others
Sample imbalance: when some of the inputs are more important than others
Temporal (or 2D) imbalance: when some steps in a sequence are more important than others

Feed an unseen example to a pre-trained model made in Keras

I've implemented a neural network using Keras. Once trained and tested for final test accuracy, using a matrix with a bunch of rows containing features (plus corresponding labels), I have a model which I should be able to use for prediction.
How can I feed a single unseen example, meaning a feature vector to the model, to obtain a class prediction?
I've looked at their documentation here but could not find a method for it.
What you want is the predict method, it takes a batch of input samples and produces predictions, which are the outputs computer by your network. To feed a single example you can just put it inside a numpy ndarray wrapper.

predict() returns image similarities with SVM in scikit learn

A silly question: after i train my SVM in scikit-learn i have to use predict function: predict(X) for predicting at which class belongs? (http://scikit-learn.org/dev/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC.predict)
X parameter is the image feature vector?
In case i give an image not trained (not trained because SVM ask at least 3 samples for class), what returns?
First remark: "predict() returns image similarities with SVM in scikit learn" is not a question. Please put a question in the header of Stack Overflow entries.
Second remark: the predict method of the SVC class in sklearn does not return "image similarities" but a class assignment prediction. Read the http://scikit-learn.org documentation and tutorials to understand what we mean by classification and prediction in machine learning.
X parameter is the image feature vector?
No, X is not "the image" feature vector: it is a set of image feature vectors with shape (n_samples, n_features) as explained in the documentation you refer to. In your case a sample is an image hence the expected shape would be (n_images, n_features). The predict API was design to compute many predictions at once for efficiency reason. If you want to compute a single prediction, you will have to wrap your single feature vector in an array with shape (1, n_features).
For instance if you have a single feature vector (1D) called my_single_image_features with shape (n_features,) you can call predict with:
predictions = clf.predict([my_single_image_features])
my_single_prediction = predictions[0]
Please note the [] signs around the my_single_image_features variable to turn it into a 2D array.
my_single_prediction will be an integer whose meaning depends on the integer values provided by you when calling the clf.fit(X_train, y_train) method in the first place.
In case i give an image not trained (not trained because SVM ask at least 3 samples for class), what returns?
An image is not "trained". Only the model is trained. Of course you can pass samples / images that are not part of the training set to the predict method. This is the whole purpose of machine learning: making predictions on new unseen data based on what you learn from the statistical regularities seen in the past training data.

Resources