Keras:使用模型的一阶和二阶导数之和作为最终输出 [英] Keras: using the sum of the first and second derivatives of a model as final output

查看:145
本文介绍了Keras:使用模型的一阶和二阶导数之和作为最终输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用Keras创建了此模型:

Suppose that I created this model using Keras:

model = Sequential()
model.add(Dense(32, activation='tanh', input_dim=1))
model.add(Dense(10, activation='tanh'))
model.add(Dense(1, activation='linear'))

此模型的输入和输出维度均为1.现在,假设我想再增加一层,即上面模型的一阶导数和二阶导数的总和(相对于输入),并且将其用作我的新输出层.在Keras可能吗?我做了很多谷歌搜索,但找不到任何东西.

The input and output dimensions of this model are both 1. Now, suppose I want to add one more layer, which is the sum of the first derivative and second derivative (with respect to the input) of the model above, and use it as my new output layer. Is that possible in Keras? I did quite a lot of Googling but couldn't find anything.

推荐答案

您可以使用 tf.gradients ,并在Keras中使用K.gradients:

first = K.gradients(model.outputs, model.inputs)
second = K.gradients(first, model.inputs)

以下是用于计算模型中的梯度的代码:

Here is the code that calculates the gradient in your model:

from tensorflow.python.keras import Model, Input
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.layers import Dense, Lambda

def deriative(inps):
    i, o = inps[0], inps[1]
    grad1 = K.gradients(o, i)[0]
    grad2 = K.gradients(grad1, i)[0]
    return K.concatenate([grad1, grad2])


inps = Input(shape=(1,))
dense1 = Dense(32, activation='tanh')(inps)
dense2 = Dense(10, activation='tanh')(dense1)
dense3 = Dense(1, activation='linear')(dense2)

output = Lambda(deriative)([inps, dense3])

new_model = Model(inputs=inps, outputs=output)
new_model.compile('adam', 'mse')
print(new_model.summary())

这篇关于Keras:使用模型的一阶和二阶导数之和作为最终输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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