来自 Theano 表达式的梯度,用于 Keras 中的过滤器可视化

Gradient from Theano expression for filter visualization in Keras

对于 ConvNet,找到最大化单个 conv activity 的范数有界输入可能很有趣。过滤 visualize the filters. I'd like to do this in the deep learning package Keras. This could be done using a black box optimization algorithm with the code from the FAQ.

# with a Sequential model
get_3rd_layer_output = theano.function([model.layers[0].input],
                                       model.layers[3].get_output(train=False))
layer_output = get_3rd_layer_output(X)

但是,如果我有梯度,这将是一个非常容易的优化任务。如何从 Theano 表达式中提取梯度并将其输入 Python 优化库,例如 Scipy?

您可以按照 here and hand-code it into Scipy. You can also do the optimization in Theano - see this question 所述打印渐变。

然而,最直接的方法可能是创建一个函数 get_gradients(),它使用 theano.grad() 到 return 过滤器相对于输入的梯度,然后调用scipy.optimize.minimizejac=get_gradients。根据 documentation:

jac : bool or callable, optional Jacobian (gradient) of objective function. [...] jac can also be a callable returning the gradient of the objective. In this case, it must accept the same arguments as fun.