繁体   English   中英

在 keras model 中对输入进行微分以用于损失

[英]Performing Differentiation wrt input within a keras model for use in loss

keras 中是否有任何层计算导数 wrt 输入? 例如,如果输入x ,第一层是f(x) ,那么下一层的 output 应该是f'(x) 关于这个主题有多个问题,但所有问题都涉及 model 之外的导数计算。本质上,我想创建一个神经网络,其损失 function 涉及输入的雅可比矩阵和粗麻布矩阵。

我试过以下

import keras.backend as K

def create_model():

    x = keras.Input(shape = (10,))
    layer = Dense(1, activation = "sigmoid")
    output = layer(x)

    jac = K.gradients(output, x)
    
    model = keras.Model(inputs=x, outputs=jac)
    
    return model

model = create_model()
X = np.random.uniform(size = (3, 10))

这给出了错误tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead. tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

所以我试着用那个

def create_model2():
    with tf.GradientTape() as tape:
        x = keras.Input(shape = (10,))
        layer = Dense(1, activation = "sigmoid")
        output = layer(x)

    jac = tape.gradient(output, x)
    
    model = keras.Model(inputs=x, outputs=jac)
    
    return model

model = create_model2()
X = np.random.uniform(size = (3, 10))

但这告诉我'KerasTensor' object has no attribute '_id'

这两种方法在 model 之外都可以正常工作。我的最终目标是在损失 function 中使用 Jacobian 和 Hessian,因此也欢迎使用其他方法

不确定你到底想做什么,但可以尝试使用tf.gradients自定义Keras层:

import tensorflow as tf
tf.random.set_seed(111)

class GradientLayer(tf.keras.layers.Layer):
  def __init__(self):
    super(GradientLayer, self).__init__()
    self.dense = tf.keras.layers.Dense(1, activation = "sigmoid")
  
  @tf.function
  def call(self, inputs):
    outputs = self.dense(inputs)
    return tf.gradients(outputs, inputs)


def create_model2():
    gradient_layer = GradientLayer()
    inputs = tf.keras.layers.Input(shape = (10,))
    outputs = gradient_layer(inputs)    
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    
    return model

model = create_model2()
X = tf.random.uniform((3, 10))
print(model(X))
tf.Tensor(
[[-0.07935508 -0.12471244 -0.0702782  -0.06729251  0.14465885 -0.0818079
  -0.08996294  0.07622238  0.11422144 -0.08126545]
 [-0.08666676 -0.13620329 -0.07675356 -0.07349276  0.15798753 -0.08934557
  -0.09825202  0.08324542  0.12474566 -0.08875315]
 [-0.08661086 -0.13611545 -0.07670406 -0.07344536  0.15788564 -0.08928795
  -0.09818865  0.08319173  0.12466521 -0.08869591]], shape=(3, 10), dtype=float32)

暂无
暂无

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

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