Keras sample weights

GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account. I'm trying to fit a model to a set of time series X with shape40, 2 to an output ywith sample weights40 - note that the output is a floating value and not a class label so this is regression not classification:. ValueError: Error when checking target: expected lt to have shape 1, but got array with shape. In future please open StackOverflow questions - this place is not an appropriate place for implementation questions.

You are misunderstanding what sample weight does: It weights outputs specifically their losses not inputs. This means that40 is the appropriate shape here since there are time series, each of length 40 time steps.

This is correct but there is a minor nitpick which might make it clearer: "will apply a different weight to every timestep of every sample" is supposed to be "will apply a different weight to every output timestep of every sample". If you are trying to make a prediction for every timestep you input the networks should look like this:.

So your output is supposed to be just 1 value per sample? Then you have no output timesteps to weight since each output is just 1 value. If not, would you mind writing just a simple description with mock numbers of how you see this working. For instance this is what I feel like you are describing currently:.

Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. New issue. Jump to bottom. Labels To investigate. Copy link Quote reply. I've tried forcing y to be: y. This comment has been minimized. Sign in to view. Maybe I was a bit quick to put the label. Some advice for the script: Don't use custom data or custom paths. Use random arrays or even np. Don't load external data.

The example should run with Keras and deps alone.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. I am using Keras to segment images to road and background pixels.

As you can imagine percentage of road pixels are much lower than that of background pixels. But, when I do this I got the following error:. How can I weight the classes in Keras? But, class labels have also second and third dimensions for image height and width. And to weight the class types, I should weight the pixel labels not just the whole image. It is quite nice if you know what you have to do. Unfortunately, the documentation is not really clear on this, presumably because this feature was originally added for time series data.

But this is not very clear. Hi rdelassus. However, the way I understand uschmidt83 's suggestion you need to use:. Hope it helps, please let us know how it goes. And if anyone knows more feel free to chime in. Figured out where some changes could happen to make progress in this direction. I have a binary pixel-wise classification task that i want to perform that takes in x images and outputs the same resolution images basically.

On final layer I reshape the output so it is the same as in mptorr arcitecture above. Here is my arch:.To get started, read this guide to the Keras Sequential model.

The model will not be trained on this data. A History object. Its History. The attribute model. Trains the model on data generated batch-by-batch by a Python generator or an instance of Sequence. The generator is run in parallel to the model, for efficiency. For instance, this allows you to do real-time data augmentation on images on CPU in parallel to training your model on GPU.

The use of keras. Sequence object in order to avoid duplicate data when using multiprocessing. The output of the generator must be either. This tuple a single output of the generator makes a single batch. Therefore, all arrays in this tuple must have the same length equal to the size of this batch. Different batches may have different sizes. For example, the last batch of the epoch is commonly smaller than the others, if the size of the dataset is not divisible by the batch size.

The generator is expected to loop over its data indefinitely. Total number of steps batches of samples to yield from generator before declaring one epoch finished and starting the next epoch. It should typically be equal to the number of samples of your validation dataset divided by the batch size. If name and index are both provided, index will take precedence. Keras Documentation. Arguments optimizer : String name of optimizer or optimizer instance. See optimizers.

See losses. If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses. If a list, it is expected to have a mapping to the model's outputs.

If a dict, it is expected to map output names strings to scalar coefficients.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Basically if I train the model for about 10 epochs, I get great results.

I'm guessing the model just detects more of the dominant class to the detriment of the smaller classes. The model has two inputs, for word embeddings and character embeddings, and the input is one of 7 possible classes from 0 to 6.

With the padding, the shape of my input layer for word embeddings isand the input layer for word embeddings is, The output is a matrix with one of the 7 categories assigned for each word of the training samples, so the size is, 7.

So I create aarray with a concatenation of the weights of every word of each sequence:. I first got an error telling me the dimension was wrong, however after generating the weights for only the training sample, I end up with aarray that I can use to fit my model.

Checking the docs a bit we can see the differences between them:. That means that you should pass a 1D array with the same number of elements as your training samples indicating the weight for each of those samples. In case you are using temporal data you may instead pass a 2D array, enabling you to give weight to each timestep of each sample. This means you should pass a weight for each class that you are trying to classify. Furthermore, this parameter expects a dictionary to be passed to it not an array, that is why you got that error.

For example consider this situation:. In this case a binary classification problem you are giving 50 times as much weight or "relevance" to your samples of class 1 compared to class 0. This way you can compensate for imbalanced datasets. Here is another useful post explaining more about this and other options to consider when dealing with imbalanced datasets. Probably you are over-fitting, and something that may be contributing to that is the imbalanced classes your dataset has, as you correctly suspected.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. Hi there, I am trying to implement a classification problem with three classes: 0,1 and 2.

I would like to fine tune my cost function so that missclassification is weighted some how. In particular, predicting 1 instead of 2 should give twice the cost than predicting 0. I really like keras framework, it would be nice if it is possible to implement it and not having to dig into tensorflow or theano code. Sorry, the table has lost its format, I am sending an image:. Similar: You are absolutely right, I'm sorry I misunderstood your question.

I will try to come back with something tomorrow using partial to define the weights. What you want to achieve should be doable with Keras abstract backend. Ok so I had the time to quickly test it.

keras sample weights

This is a fully reproducible example on mnist where we put a higher cost when a 1 is missclassified as a 7 and when a 7 is missclassified as a 1. So if you want to pass constants included in the cost function, just build a new function with partial.

Try to test it on a toy example to verify that it actually works. If it's what you are looking for, feel free to close the issue! Keras 1. Sure, sorry I was using Theano functionnalities. I replaced the following line in my previous example:.

Sounds the way to go, I was using tensorflow as backend.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up. However the training error is much lower than before, and according to Keras' documentation :. As I understand it, this option only calculates the loss function differently without training the model with weights sample importance so how do I train a Keras model with different importance weights for different samples.

This is a similar question xgboost: give more importance to recent samples but I would like an applicable answer to Keras. If I understand by error you mean the average loss calculated for each batch. Penalizing neural networks for specific examples means give them a high probability to be included in the batches. Gradually transforming the training task, from an easy one maybe convex where examples illustrate the simpler concepts, to the target one with more difficult examples The basic idea is to start small, learn easier aspects of the task or easier sub-tasks, and then gradually increase the difficulty level.

From the point of view of building representations, advocated here, the idea is to learn representations that capture low-level abstractions first, and then exploit them and compose them to learn slightly higher-level abstractions necessary to explain more complex structure in the data.

By choosing which examples to present and in which order to present them to the learning system, one can guide training and remarkably increase the speed at which learning can occur. Well, I had tried something similar. Before I go into it, I want to mention that my error actually went up - I haven't had the time to go into why that happened.

Train a Keras model

For simplicity, lets say you know the weights you need for each class and can pass it as a dictionary. I'm using that fact to reverse engineer which class each row is pointing to and adding a weight for it. You essentially need to pass an array of weights mapping to each label so the same length as your training data when you fit the model.

Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered.

keras sample weights

Asked 1 year, 11 months ago. Active 1 year, 11 months ago. Viewed 12k times. What I currently have is: trainingWeights which is the desired importance I want to give to each sample. Code snippets are encouraged.

Active Oldest Votes. However the training error is much lower than before If I understand by error you mean the average loss calculated for each batch. This is a similar question xgboost: give more importance to recent samples but I would like an applicable answer to Keras Penalizing neural networks for specific examples means give them a high probability to be included in the batches.

Fadi Bakoura Fadi Bakoura 3 3 silver badges 13 13 bronze badges. Training may halt at a point where the gradient becomes small, a point where early stopping ends training to prevent overfitting, or at a point where the gradient is large but it is difficult to find a downhill step due to problems such as stochasticity or poor conditioning of the Hessian.

Nanda Nanda 1 1 gold badge 6 6 silver badges 8 8 bronze badges.

Loading in your own data - Deep Learning basics with Python, TensorFlow and Keras p.2

Sign up or log in Sign up using Google. Sign up using Facebook.

The Sequential model API

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project?

Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub?

Sign in to your account. Why training the model for a given number of epochs i. Line in e7a9. I see you opened two issue related to the same problem. Would it be possible for you to close one and follow everything on one single thread? Which one do you suggest to close? Thank you. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Sign up. New issue. Jump to bottom.

Subscribe to RSS

Labels type:feature. Copy link Quote reply. This comment has been minimized. Sign in to view. The generator for train or validation simply needs to yield X, y, w. Sign up for free to join this conversation on GitHub.

keras sample weights

Already have an account? Sign in to comment. Linked pull requests. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window.


comments

Leave a Reply

Your email address will not be published. Required fields are marked *