raghakot / keras-vis

Neural network visualization toolkit for keras
https://raghakot.github.io/keras-vis
MIT License
2.97k stars 664 forks source link

Using a simple MLP to predict 2D matrices : smoothed matrix from keras-vis ? #188

Closed NotaCS closed 4 years ago

NotaCS commented 5 years ago
NotaCS commented 5 years ago

Hi,

I'm quite new to the DL world, so I might be doing idiotic things... If so, my apologies.

I'm trying to predict a continuous variable with a simple MLP by feeding it some 2D square matrices. Then, I'm using visualize_activation on the trained model to see what cells of the matrix increase or decrease the regression result. Each cell is more or less independent from its neighbors. So far so good.

When I use the full 2D matrices (with a flattening layer after the input layer), keras-vis gives me a sharp-looking matrix (no blur), which is great: Capture d’écran 2019-07-11 à 11 10 05 (I masked the upper triangle here, but the matrix given by visualize_activation is full)

But such full matrices contain redundant information, so I usually remove the selected useless cells (the upper triangle of the matrix) and flatten the result before feeding them to the MLP. So now, visualize_activation is supposed to give me a 1D vector, that I then reconstruct into the lower matrix triangle. But here is the result: Capture d’écran 2019-07-08 à 11 59 36

As you can see, the values have been smoothed along the flattened dimension, resulting in the horizontal streaks on the matrix. This looks like the is a 1D conv layer used somewhere, which is not the case...

I also tried to reshape the flattened triangular matrices into a 2D rectangle then train the MLP with these 2D images (so basically: matrix -> remove upper triangle and flatten -> reshape into a 2D image), but again, I obtain a smoothed image (this time in 2D) with visualize_activation! Why would it be different from when I use the 2D square matrices?

It's really strange and my stem from the fact that keras-vis is designed to work with images (and not matrices). But still, why would it work well when I use the full matrices?

Thank you in advance!

keisen commented 4 years ago

That's cool!

How did you generate the smoothed image? If you use matplotlib's imshow with setting color-map, you need to remove cmap option from imshow.

If not, I recommend you that you try to some tweaking such as below:

  1. Please make sure input_range argument of visualize_activation. In default, the argument value is 0 to 255 but your model's input-data seems to be -1 to 1.
  2. Please pass through a random value as seed_input argument to visualize_activation. In default, the random value's standard deviation (std) is 0.05 * input-value-range, and if your data's range is -1 to 1, the std is 0.1. It may be too low value.
  3. Please TotalVariation weight change from 10 to 0.

Despite you try all above If you could not get the result you want, let me know.

NotaCS commented 4 years ago

Hi,

Thanks for your answer. I actually also contacted you on Slack when I posted the question here and you've already perfectly solved my problem ^^

Here is a screenshot of the discussion's resolution, if other people happen to have the same problem: Capture d’écran 2019-12-20 à 09 27 04

Thanks again and have a nice day !

keisen commented 4 years ago

Ah! Sorry for forgetting it 💦 Thank you for reminding me. We'll close this issue, but please feel free to reopen this issue whenever you need. Thanks!