I'm trying to use sample_weight. From the documentation, it seems I can pass a list or array. My data looks like a (number samples, number classes) matrix of 1s and 0s. What I would really like to do is weight each of the 1s in the matrix by some number. The weights matrix have size (number samples, number classes) with 0s and non-zero numbers in the sample place as the 1s for the data matrix.
When I set sample_weight to be equal to this matrix, keras fits the model, but I'm not sure it's doing exactly what I want it to do. For example, it also runs if I have sample_weight= weightsmatrix:,1 and sample_weight=weightsmatrix:, 0:100, which should have no logical meaning.
I've looked through https://github.com/fchollet/keras/pull/243 (as well as 188 and 177)
https://github.com/fchollet/keras/commit/cc9edcf472766257979b28148727ccef1bcf9b41 in order to try and understand how sample_weight is implemented, but I wasn't able to see exactly it is used.
The way to use sample_weight is with a 1- or 0-dimensional array (or list) with length equal to the number of samples. There is a one-to-one mapping between sample_weights and training samples.
For example, it also runs if I have sample_weight= weightsmatrix:,1 and sample_weight=weightsmatrix:, 0:100, which should have no logical meaning.
But it does, because these shapes are compatible with the task. (0, nb_sample) will be reshape to (nb_sample, 1) and (1,) will be used as a scalar weight (e.g. all loss values will be scaled by this value).
If I try to use a list of weights it gives a error:
AttributeError: 'list' object has no attribute 'shape'
If I try to use a 1D array, it gives the error:
ValueError: Found a sample_weight array with shape (17,) for an input with shape (180, 17). sample_weight cannot be broadcast.
@flamoedo if you use this for temporal model, set sample_weight_mode='temporal'
so if sample_weight takes a 1-D array, with length equal to the number of samples,
that means each sample can have a different weight value assigned, but not each field (feature or column).
My usage or ideal solution is very similar to the OP's.
Like him, I want to input a matrix of weights so that for each sample, the 1 fields can be more 'important' than the 0 fields.
I believe we are both trying to do this, as our use-cases involve a data matrix with majority 0s, and so we are trying to adjust the importance of 0s down, and 1s up.
Also, for each sample (or row) the 1s and 0s are in different places, since they are flags of attributes in the samples.
Is there a way to do this (maybe outside of sample_weight)?
Also, pardon my ignorant question, but what is an example usage of sample_weight in this 1-D array manner? It is for situations where you believe some samples are more important than others?
I have a similar issue, trying to weight the matches in a binary_crossentropy function differently than non-matches, and each sample having different tuples. With a 1d array this doesn't seem to be possible. It would be great if there was a more flexible way to pass in weights and include them in the loss and evaluation function.
Most helpful comment
so if sample_weight takes a 1-D array, with length equal to the number of samples,
that means each sample can have a different weight value assigned, but not each field (feature or column).
My usage or ideal solution is very similar to the OP's.
Like him, I want to input a matrix of weights so that for each sample, the 1 fields can be more 'important' than the 0 fields.
I believe we are both trying to do this, as our use-cases involve a data matrix with majority 0s, and so we are trying to adjust the importance of 0s down, and 1s up.
Also, for each sample (or row) the 1s and 0s are in different places, since they are flags of attributes in the samples.
Is there a way to do this (maybe outside of sample_weight)?
Also, pardon my ignorant question, but what is an example usage of sample_weight in this 1-D array manner? It is for situations where you believe some samples are more important than others?