【问题标题】:How to get Keras activations?如何获得 Keras 激活?
【发布时间】:2020-02-13 20:05:29
【问题描述】:

我不确定如何修改我的代码以激活 keras。我已经看到 K.function() 输入的冲突示例,并且不确定我是否在我们的激活层中获得了输出。

这是我的代码

activity = 'Downstairs'
layer = 1


seg_x = create_segments_and_labels(df[df['ActivityEncoded']==mapping[activity]],TIME_PERIODS,STEP_DISTANCE,LABEL)[0]
get_layer_output = K.function([model_m.layers[0].input],[model_m.layers[layer].output])
layer_output = get_layer_output([seg_x])[0]

try: 
    ax = sns.heatmap(layer_output[0].transpose(),cbar=True,cbar_kws={'label':'Activation'})
except:
    ax = sns.heatmap(layer_output.transpose(),cbar=True,cbar_kws={'label':'Activation','rotate':180})

ax.set_xlabel('Kernel',fontsize=30)
ax.set_yticks(range(0,len(layer_output[0][0])+1,10))
ax.set_yticklabels(range(0,len(layer_output[0][0])+1,10))
ax.set_xticks(range(0,len(layer_output[0])+1,5))
ax.set_xticklabels(range(0,len(layer_output[0])+1,5))
ax.set_ylabel('Filter',fontsize=30)
ax.xaxis.labelpad = 10
ax.set_title('Filter vs. Kernel\n(Layer=' + model_m.layers[layer].name + ')(Activity=' + activity + ')',fontsize=35)

这里关于堆栈溢出的建议就像我一样做: Keras, How to get the output of each layer?

示例 4 将 k 的学习阶段添加到混合中,但我的输出仍然相同。 https://www.programcreek.com/python/example/93732/keras.backend.function

我得到输出或激活了吗?文档暗示我可能需要layers.activations,但我还没有做到这一点。

我的代码,或者在学习阶段传递的代码都得到了这个热图。 https://imgur.com/a/5fI6N0B

【问题讨论】:

    标签: python keras activation


    【解决方案1】:

    对于定义为例如的层Dense(activation='relu')layer.outputs 将获取(relu)激活。要获得层预激活,您需要设置activation=None(即'linear'),然后是Activation 层。下面的例子。

    from keras.layers import Input, Dense, Activation
    from keras.models import Model
    import numpy as np
    import matplotlib.pyplot as plt
    import keras.backend as K
    
    ipt = Input(shape=(8,))
    x   = Dense(10, activation=None)(ipt)
    x   = Activation('relu')(x)
    out = Dense(1, activation='sigmoid')(x)
    
    model = Model(ipt, out)
    model.compile('adam', 'binary_crossentropy')
    
    X = np.random.randn(16, 8)
    outs1 = get_layer_outputs(model, model.layers[1], X, 1)  # Dense
    outs2 = get_layer_outputs(model, model.layers[2], X, 1)  # Activation
    
    plt.hist(np.ndarray.flatten(outs1), bins=200); plt.show()
    plt.hist(np.ndarray.flatten(outs2), bins=200); plt.show()
    



    使用的功能

    def get_layer_outputs(model, layer, input_data, learning_phase=1):
        layer_fn = K.function([model.input, K.learning_phase()], layer.output)
        return layer_fn([input_data, learning_phase])
    

    【讨论】:

    • 这是一个很好的答案,正是我所需要的。如果未提供激活,我正在阅读默认值为 x,线性。是没有指定的情况,因此激活等于输出,还是人们说没有激活时的意思?我想知道为什么我会在 CNN 中将激活用于 DropOut、池化或密集层,在架构上,但所有这些问题都超出了这个问题的范围,记录在案。
    • activation=Noneactivation='linear' 完全相同;在内部,它类似于:if activation is None: activation = 'linear'。激活对于深度神经网络至关重要,原因有很多,其中一个主要原因是,它们启用了 非线性 - 否则,它就像训练一个大层而不是多个堆叠。一些further reading - 很高兴答案很有帮助 - 如果问题得到解决,请考虑投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多