Here is my tensorflow keras model,(you can ignore dropout layer if it makes things tough)
import tensorflow as tf
optimizers = tf.keras.optimizers
Sequential = tf.keras.models.Sequential
Dense = tf.keras.layers.Dense
Dropout = tf.keras.layers.Dropout
to_categorical = tf.keras.utils.to_categorical
model = Sequential()
model.add(Dense(256, input_shape=(20,), activation="relu"))
model.add(Dropout(0.1))
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.1))
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.1))
model.add(Dense(3, activation="softmax"))
adam = optimizers.Adam(lr=1e-3) # I don't mind rmsprop either
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
and I have saved the model structure and weights as
model.save("sim_score.h5", overwrite=True)
model.save_weights('sim_score_weights.h5', overwrite=True)
on doing model.predict(X_test)
I get [0.23, 0.63, 0.14]
which is the prediction probability of the 3 output classes.
How would I visualize how much weight/importance each of my initial 20 features have in this model w.r.t the 3 output softmax?
For example, my 2nd column could have negligible impact on the end result, while the 5th column could have 3x more impact on the output prediction than the 20th column. It does not matter what the absolute effect of the 5th column is, just figuring out the relative importance is good enough, for example 5th column = 0.3, 20th column = 0.1
and so on for a 20 x 3 matrix
.
See this animation for intuition or Tensorflow playground. The visualization doesn't have to show how the weights change during training, but can just show a snapshot image of how it looks at the end of training.
In fact, the solution does not even have to be a visualization, it can even be an array of 20 elements x 3 outputs having the relative importance of each feature w.r.t the 3 output softmax and importance relative to the other features.
Getting the importance of intermediate layers is just an added bonus.
The reason I want to visualize the 20 features is for transparency purposes(currently the model feels like a black box). I am comfortable in matplotlib, pyplot, seaborn. I am aware of Tensorboard as well, but couldn't find any examples for simple Dense Relu network with Softmax.
I feel a time-consuming way to get the 20 x 3 weights would be to do a domain search of 20 features
from 0 - 1
with delta of 0.5
by sending various inputs and trying to deduce the importance of features based off of that(which would have 3 to the power of 20
~= 3.4 billion
possible sample space and gets exponentially worse the more features I add) and then applying conditional probability to reverse engineer the relative weights, but I am not sure if there is a simpler/optimized way via TensorBoard or some custom logic.
Can someone help visualize the model/calculate the 20 x 3 = 60 relative weights of the 20 features w.r.t the 3 outputs with code snippets or provide a reference on how to achieve this?