Keras:计算模型输出与输入返回值的导数[无] [英] Keras: calculating derivatives of model output wrt input returns [None]

查看:426
本文介绍了Keras:计算模型输出与输入返回值的导数[无]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助为Keras中的模型输出wrt输入计算导数.

I need help with calculating derivatives for model output wrt inputs in Keras.

我想向损失函数添加一个正则化函数.正则化器包含分类器函数的导数.因此,我尝试采用模型输出的导数.该模型是具有一个隐藏层的MLP.数据集是MNIST.当我编译模型并采用导数时,得到的结果为[None],而不是导数函数.

I want to add a regularization functional to the loss function. The regularizer contains the derivative of the classifier function. So I tried to take the derivative of model output. The model is a MLP with one hidden layer. The dataset is MNIST. When I compile the model and take the derivative, I get [None] as the result instead of the derivative function.

我也看到过类似的帖子,但是那里也没有得到答案: 将Keras模型wrt的导数作为输入返回全零

I have seen a similar post, but didn't get answer there either: Taking derivative of Keras model wrt to inputs is returning all zeros

这是我的代码.请帮助我解决问题.

Here is my code. Please help me to solve the problem.

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as K

num_hiddenNodes = 1024
num_classes = 10

(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28 * 28)
X_train = X_train.astype('float32')
X_train /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)

model = Sequential()
model.add(Dense(num_hiddenNodes, activation='softplus', input_shape=(784,)))
model.add(Dense(num_classes, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
logits = model.output
# logits = model.layers[-1].output
print(logits)
X = K.identity(X_train)
# X = tf.placeholder(dtype=tf.float32, shape=(None, 784))
print(X)
print(K.gradients(logits, X))

这是代码的输出.这两个参数是张量.渐变函数返回无.

Here is the output for the code. The two parameters are Tensors. The gradients function returns None.

Tensor("dense_2/Softmax:0", shape=(?, 10), dtype=float32)
Tensor("Identity:0", shape=(60000, 784), dtype=float32)
[None]

推荐答案

您正在计算相对于X_train的坡度,X_train不是计算图的输入变量.相反,您需要获取模型的符号输入张量,因此请尝试执行以下操作:

You are computing the gradients respect to X_train, which is not an input variable to the computation graph. Instead you need to get the symbolic input tensor to the model, so try something like:

grads = K.gradients(model.output, model.input)

这篇关于Keras:计算模型输出与输入返回值的导数[无]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆