Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample Weight Error #7120

Closed
ghost opened this issue Jun 25, 2017 · 11 comments
Closed

Sample Weight Error #7120

ghost opened this issue Jun 25, 2017 · 11 comments

Comments

@ghost
Copy link

ghost commented Jun 25, 2017

Hi there!

I'm working on a brain lesion segmentation problem and I'm trying to implement a Unet with code inspired by: https://github.com/jocicmarko/ultrasound-nerve-segmentation

One of the issues I'm trying to overcome is class balance (lots more non-lesion voxels rather than lesion voxels). I tried using class_balance but that didn't work so now I'm trying to use sample_weight and that's also giving me all sorts of errors.

i) First thing I tried was to set sample_weight_mode to 'temporal' and feed in a weight matrix of the same shape as my target data:
target_data.shape -> (n_samples,512 rows/pixels, 512 cols/pixels, 1 channel )
Weight_map.shape -> (n_samples,512 rows/pixels, 512 cols/pixels, 1 channel )

output:
_ValueError: Found a sample_weight array with shape (100, 512, 512, 1). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.

ii) Second thing I tried was to flatten the sample array so it would be of shape:
Weight_map.shape -> (n_samples,512x512x1).

output:
ValueError: Found a sample_weight array with shape (100, 262144) for an input with shape (100, 512, 512, 1). sample_weight cannot be broadcast.

iii) Next I tried following the advice of uschmidt83 (here) and flattening the output of my model along with the corresponding target data.
last_layer = keras.layers.Flatten()(second_last_layer)

target_data.shape -> (n_samples,512x512x1).
Weight_map.shape -> (n_samples,512x512x1).

output:
ValueError: Found a sample_weight array for an input with shape (100, 262144). Timestep-wise sample weighting (use of sample_weight_mode="temporal") is restricted to outputs that are at least 3D, i.e. that have a time dimension.

Oddly enough, even if I set sample_weight=None I still get the same error as right above.

Any advice on how to fix this sample_weight error? Here is the basic code to reproduce the error
here

Also, if you have advice about how to deal the class imbalance problem, please let me know.

@fedor-chervinskii
Copy link

Got the same error without even any weighting of samples

@fedor-chervinskii
Copy link

Seems like a bug when the traceback says that it found a variable with inappropriate size, but Keras created this variable somewhere inside the call of fit_generator
Found a sample_weight array with shape (16, 10)
when I didn't feed any sample_weight

@fedor-chervinskii
Copy link

I found out that my generator yielded third output that I used as a mask, but Keras used it as a sample_weight, and this is documented in fit_generator so this is just my fault, no bugs here

@stale
Copy link

stale bot commented Nov 6, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

@stale stale bot added the stale label Nov 6, 2017
@stale stale bot closed this as completed Dec 6, 2017
@Anesouadou
Copy link

I also had the same problem and I struggled with for a week. then it clicked with me and I made it work.
This is what I did:

  • I used reshape instead of Flatten. Flatten will give you a fixed 2 dimensional output. say your input is (number of samples,80,80,1), Flatten will give you (number of sample,6400) which is 2 D. instead use reshape to get (number of sample,6400,1). this is your 3-D output which works with "temporal".

  • the other thing you need to do is make sure your label is also 3-D. You probably have changed the shape of your labels to match the 2-D output from your model, so you will have something similar to (number of sample,6400) which will again generate an error. All you have to do is add a third dimension like this (number of sample,6400,1). This should make it work

@wmcnally
Copy link

wmcnally commented Mar 26, 2020

@Anesouadou I have a similar problem. Your suggestion didn't work :(. If I make my label/sample weight 3D i.e., (batch, 3920, 1) and set sample_weight_mode to 'temporal' I get the following error:

ValueError: Found a sample_weight array with shape (None, 3920, 1). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.

When I flatten the labels/sample_weights I then get this error:

ValueError: Found a sample_weight array for an input with shape (None, 3920). Timestep-wise sample weighting (use of sample_weight_mode="temporal") is restricted to outputs that are at least 3D, i.e. that have a time dimension.

It's contradicting itself!

@Anesouadou
Copy link

@wmcnally I think you are confusing the correct format for each parameter.

  • output format is 3D (number of samples, flattened sample, 1) . the last dimension is added just to make your parameter 3D. you can add it using reshape function (don't use flatten).
  • label format the same as output format.
  • sample weight is 2D where each row corresponds to a sample and the number of rows is equal to the number of samples.
    I hope this makes it clearer

@wmcnally
Copy link

@Anesouadou oh, ok. So in that case, the shape of the 2D sample weight should be (number of samples, flattened sample), correct? Thanks in advance for your help.

@Anesouadou
Copy link

@wmcnally yes, it should work just fine. Let me how it goes.

@wmcnally
Copy link

@Anesouadou I got it working. Thanks so much for the help.

@Atul997
Copy link

Atul997 commented Jan 16, 2022

I found out that my generator yielded third output that I used as a mask, but Keras used it as a sample_weight, and this is documented in fit_generator so this is just my fault, no bugs here

How did you resolve this. I am facing same issue with same type of data type. I have embeddings, mask and labels as output from data generator. I really appreciate your help on this.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants