我试图在keras前馈神经网络中获得单个神经元的衍生物w.r.t.网络的权重。
为此,我使用子网和keras的内置渐变功能。例如,对于层 l 中的神经元 k ,我将返回一个子网络,其中层 l 与层中的其余神经元一起 l 已删除。然后,我采用该子网的梯度w.r.t.子网中的权重。 (我对网络中的所有神经元都这样做。)
这个问题是渐变函数实际上是关于网络的错误或成本,而不是输出本身,即它类似于错误* grad(neuron_lk),并且您需要数据点的标签来计算该错误。我不想要那个错误术语。
此外,我觉得可能有更简单或更有效的方法来完成所有这些。有什么建议吗?
这是我的代码:
渐变:
def gradient(model,x):
weights = model.trainable_weights # weight tensors
##weights = [weight for weight in weights if model.get_layer(weight.name[:-2]).trainable] # filter down weights tensors to only ones which are trainable
gradients = model.optimizer.get_gradients(model.total_loss, weights) # gradient tensors
input_tensors = [model.inputs[0], # input data
model.sample_weights[0], # how much to weight each sample by
model.targets[0], # labels
K.learning_phase(), # train or test mode
]
get_gradients = K.function(inputs=input_tensors, outputs=gradients)
inputs = [[list(x)], # X
[1], # sample weights
[[model.predict(np.array([x]))[0][0]+1]], # y
0 # learning phase in TEST mode
]
return get_gradients(inputs)
对于子模型:
def get_submodel(model, layer_l, neuron_k):
#creating submodel and pruning layers:
submodel = deepcopy(model)
for i in range(len(model.layers)-layer_l):
submodel.pop()
#getting [pruned] weight matrix:
W = model.layers[layer_l].get_weights()[0]
if layer_l != len(model.layers) - 1:
Wk = W[neuron_k,:]
elif layer_l == len(model.layers) - 1:
Wk = W
#adding new layer back:
if layer_l != 0:
submodel.add(Dense(1, activation='relu', use_bias=False))
elif layer_l == 0:
in_shape = model.layers[0].input_shape[1]
submodel.add(Dense(1, activation='relu', use_bias=False, input_shape=(in_shape,)))
#setting [pruned] weight matrix:
submodel.layers[layer_l].set_weights([Wk.reshape([-1,1])])
submodel.compile(optimizer='sgd', loss='mean_squared_error')
return submodel
对单个神经元的最终调用(图层 l ,神经元 k ):
gradient(get_submodel(model, l, k),x)
提前感谢任何输入!