keisen / tf-keras-vis

Neural network visualization toolkit for tf.keras
https://keisen.github.io/tf-keras-vis-docs/
MIT License
311 stars 45 forks source link

loss function for different labels #3

Closed betterze closed 4 years ago

betterze commented 4 years ago

Dear Keisen,

Thank you for sharing with us this nice project. I really like it.

In attentions.ipynb, cell 4,

# Define loss function. 20 is the imagenet index corresponding to ouzel.
loss = lambda output: K.mean(output[:, 20])

This loss function is only for images with the same class (20). If I want to use images with different classes (for example, the first image is class 20, the second image is class 21), how should I modify the code?

Thank you for your help.

Best Wishes,

Alex

keisen commented 4 years ago

Hi, @betterze . I’m happy that you like it.

for example, the first image is class 20, the second image is class 21

When you know the number of images (TWO in above example) and each class (20 and 21 in above example) , you can implement as follows:

loss = lambda output: (output[0, 20] + output[1, 21]) / 2

Thanks!

betterze commented 4 years ago

thx a lot, I understand it now.