如何解决如何获得关于 keras 模型神经网络输入的雅可比矩阵?
我最近开始学习并使用自动微分来确定神经网络相对于给定输入的梯度和雅可比矩阵。 tensorflow 推荐的方法是 tape.gradient
和 tape.jacobian
方法。但是,由于 tensorflow 中的一些错误,我无法使用此方法获取雅可比矩阵。它在我计算 tape.gradient(y_pred,x)
时有效,但在形状应为 (200,3)
的雅可比矩阵时无效。我对计算雅可比矩阵的其他方法持开放态度,但我更倾向于在 Tensorflow 中使用自动微分方法。我使用的当前版本是 Tensorflow 2.1.0。非常感谢任何建议!
import tensorflow as tf
import numpy as np
# The neural network accepts 3 inputs and produces 200 outputs. The actual values of the inputs and outputs are not written in the code as it is too involved.
num_inputs = 3
num_outputs = 200
num_hidden_layers = 5
num_neurons = 50
kernel = 'he_uniform'
activation = tf.keras.layers.LeakyReLU(alpha=0.3)
# Details of model (MLP)
current_model = tf.keras.models.Sequential()
current_model.add(tf.keras.Input(shape=(num_inputs,)))
for i in range(num_hidden_layers):
current_model.add(tf.keras.layers.Dense(units=num_neurons,activation=activation,kernel_initializer=kernel))
current_model.add(tf.keras.layers.Dense(units=num_outputs,activation='linear',kernel_initializer=kernel))
# Finding the Jacobian matrix with respect to a given input of the neural network
# In this case,the inputs are [0.02,0.4 and 0.12] (i.e. 3 inputs)
x = tf.Variable([[0.02,0.4,0.12]],dtype=tf.float32)
with tf.GradientTape() as tape:
y_pred = x
for layer in current_model.layers:
y_pred = layer(y_pred)
jacobian = tape.jacobian(y_pred,x)
print(jacobian)
StagingError: in converted code:
C:\Users\...\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\ops\parallel_for\control_flow_ops.py:183 f *
return _pfor_impl(loop_fn,iters,parallel_iterations=parallel_iterations)
C:\Users\...\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\ops\parallel_for\control_flow_ops.py:256 _pfor_impl
outputs.append(converter.convert(loop_fn_output))
C:\Users\...\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\ops\parallel_for\pfor.py:1280 convert
output = self._convert_helper(y)
C:\Users\...\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\ops\parallel_for\pfor.py:1453 _convert_helper
if flags.FLAGS.op_conversion_fallback_to_while_loop:
C:\Users\...\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\platform\flags.py:84 __getattr__
wrapped(_sys.argv)
C:\Users\...\anaconda3\envs\tf\lib\site-packages\absl\flags\_flagvalues.py:633 __call__
name,value,suggestions=suggestions)
UnrecognizedFlagError: UnkNown command line flag 'f'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。