简体   繁体   English

计算/可视化Tensorflow Keras Dense模型层相对连接权重wrt输出类

[英]Calculate/Visualize Tensorflow Keras Dense model layer relative connection weights w.r.t output classes

Here is my tensorflow keras model,(you can ignore dropout layer if it makes things tough) 这是我的tensorflow keras模型(如果让事情变得艰难,您可以忽略辍学层)

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. 执行model.predict(X_test)我得到[0.23, 0.63, 0.14] model.predict(X_test) [0.23, 0.63, 0.14] ,这是3个输出类的预测概率。

How would I visualize how much weight/importance each of my initial 20 features have in this model wrt the 3 output softmax? 在此模型中,使用3个输出softmax,我如何可视化我最初的20个功能中每个功能的重要性/重要性?

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 . 不管第5列的绝对影响是什么,只要弄清楚相对重要性就足够了,例如对于20 x 3 matrix 5th column = 0.3, 20th column = 0.1等等。

See this animation for intuition or Tensorflow playground . 有关直觉或Tensorflow游乐场请参见此 动画 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 wrt the 3 output softmax and importance relative to the other features. 实际上,该解决方案甚至不必是可视化的,它甚至可以是20个元素x 3个输出的数组,这些数组具有每个功能的相对重要性以及3个输出softmax和相对于其他功能的重要性。

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). 我之所以要可视化这20个功能,是出于透明目的(当前该模型感觉像一个黑匣子)。 I am comfortable in matplotlib, pyplot, seaborn. 我对matplotlib,pyplot和seaborn感到满意。 I am aware of Tensorboard as well, but couldn't find any examples for simple Dense Relu network with Softmax. 我也知道Tensorboard,但是找不到带有Softmax的简单Dense Relu网络的任何示例。

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. 我觉得一个耗时的方式来获得20×3点的权重是做一个域搜索的20 features0 - 1与增量0.5通过发送各种输入,并试图推断的基础断的是功能的重要性(这将具有3 to the power of 20 20〜= 3.4 billion可能的样本空间的幂,并且随着我添加的更多功能而呈指数级变差),然后应用条件概率对相对权重进行逆向工程,但是我不确定是否有更简单/优化的方法通过TensorBoard或一些自定义逻辑。

Can someone help visualize the model/calculate the 20 x 3 = 60 relative weights of the 20 features wrt the 3 outputs with code snippets or provide a reference on how to achieve this? 有人可以帮助可视化模型/计算带有代码段的3个输出的20个功能的20 x 3 = 60的相对权重,或者提供有关如何实现此目的的参考吗?

I had come across a similar problem, but i was concerned more about the visualization of the model parameters(weights and biases) rather than the model features [ since i wanted to explore and view the black box as well ]. 我遇到了一个类似的问题,但是我更关心模型参数(权重和偏差)的可视化,而不是模型特征( 因为我也想探索和查看黑匣子 )。

For example, the following is a snippet of a shallow neural network with 2 hidden layers. 例如,以下是具有2个隐藏层的浅层神经网络的代码段。

model = Sequential()
model.add(Dense(128, input_dim=13, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(8, kernel_initializer='uniform', activation='softmax'))

# Compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Using TensorBoard to visualise the Model
ks=TensorBoard(log_dir="/your_full_path/logs/{}".format(time()), histogram_freq=1, write_graph=True, write_grads=True, batch_size=10)

# Fit the model   
model.fit(X, Y, epochs = 64, shuffle = True, batch_size=10, verbose = 2, validation_split=0.2, callbacks=[ks])

For one to be able to visualize the parameters, there are few important things to be kept in mind: 为了使参数可视化,需要牢记一些重要的事情:

  1. Always ensure to have a validation_split in the model.fit() function[else histograms cannot be visualised]. 始终确保在model.fit()函数中有一个validation_split [无法直观显示其他直方图]。

  2. Make sure the value of histogram_freq > 0 always!![histograms won't be computed otherwise]. 确保histogram_freq的值始终> 0 !! [否则将不会计算直方图]。

  3. The callbacks to TensorBoard have to be specified in the model.fit() as a list. TensorBoard的回调必须在model.fit()中指定为列表。

Once, this is done; 一旦完成, goto cmd and type the folllowing command: goto cmd并输入以下命令:

tensorboard --logdir=logs/ 张量板--logdir = logs /

This gives you a local address with which you can access TensorBoard on your web browser. 这为您提供了一个本地地址,您可以使用该地址在Web浏览器上访问TensorBoard。 All histograms, distributions, loss and accuracy functions will be available as plots and can be selected from the menu bar at the top. 所有直方图,分布,损失和准确性功能都将以图表形式提供,并且可以从顶部的菜单栏中进行选择。

Hope this answer gives a hint about the procedure to visualize model parameters(I myself had a bit of a struggle as the above points weren't available together). 希望这个答案能给可视化模型参数的过程一个提示(由于以上几点无法同时使用,我本人也有些挣扎)。

Do let me know if it helped. 让我知道是否有帮助。

Below is the keras documentation link for your reference: 以下是keras文档链接供您参考:

https://keras.io/callbacks/#tensorboard https://keras.io/callbacks/#tensorboard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM