微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获得关于 keras 模型神经网络输入的雅可比矩阵?

如何解决如何获得关于 keras 模型神经网络输入的雅可比矩阵?

我最近开始学习并使用自动微分来确定神经网络相对于给定输入的梯度和雅可比矩阵。 tensorflow 推荐的方法tape.gradienttape.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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?