简体   繁体   English

Keras神经网络输出函数参数/如何构造损失函数?

[英]Keras neural network outputting function parameters / how to construct loss function?

I'm working on a Keras/TensorFlow based neural network. 我正在研究基于Keras / TensorFlow的神经网络。 I am trying to do something a bit different. 我想做一些不同的事情。

Normally, the output layer of the network produces an output tensor (ie a list of numbers). 通常,网络的输出层产生输出张量(即数字列表)。 Then those numbers are compared directly to a target list of training data (the labels) using a loss function such as mean squared error. 然后使用诸如均方误差的损失函数将这些数字直接与训练数据(标签)的目标列表进行比较。

However, I would instead like the output layer of the network to be a list of numbers that serve as function parameters . 但是,我希望网络的输出层是一个用作函数参数的数字列表。 The function operates on these parameters to produce a new list of numbers. 该函数对这些参数进行操作以生成新的数字列表。 The loss function then becomes the MSE between the function output and the labels (instead of, as would normally be the case, the MSE between the output layer and the labels). 然后,损失函数变为函数输出和标签之间的MSE(而不是通常情况下,输出层和标签之间的MSE)。

I understand that I need to write a Keras custom loss function that computes the values of the target function from the output layer values, then computes and returns the MSE between the target function output and the labels. 我知道我需要编写一个Keras自定义丢失函数,它从输出层值计算目标函数的值,然后计算并返回目标函数输出和标签之间的MSE。 I also realize that all of this needs to be done within the TensorFlow graph, and that the target function needs to be differentiable so that gradients can be computed. 我也意识到所有这些都需要在TensorFlow图中完成,并且目标函数需要是可微分的,以便可以计算梯度。 I believe I understand all of this well enough. 我相信我对这一切都很了解。

Here's what I can't wrap my head around. 这是我无法解决的问题。 Let's say there are four neurons in the output layer - call them a, b, c, d. 假设输出层中有四个神经元 - 称为a,b,c,d。 Each of them is a separate parameter to the target function F(a, b, c, d). 它们中的每一个都是目标函数F(a,b,c,d)的单独参数。 Let's say I iterate F(a, b, c, d) 20 times and get a set of 20 values. 假设我迭代F(a,b,c,d)20次并得到一组20个值。 That is, F(a, b, c, d, 1); 即,F(a,b,c,d,1); F(a, b, c, d, 2); F(a,b,c,d,2); etc. Then I just want to take the MSE between these 20 values and the 20 values in the corresponding label tensor. 然后我只想在这20个值和相应标签张量中的20个值之间取MSE。 That will be the loss function. 这将是损失函数。

I just don't understand the Keras/Tensorflow backend well enough to know how to obtain the individual elements of the output tensor. 我只是不了解Keras / Tensorflow后端,知道如何获得输出张量的各个元素。 How do I address the zeroth, first, second, etc elements in this tensor so that I can use them to compute the function values? 如何解决此张量中的第0,第1,第2等元素,以便我可以使用它们来计算函数值? I know how to perform operations on whole tensors, but I don't understand how to address individual tensor elements. 我知道如何在整个张量上执行操作,但我不明白如何处理单个张量元素。

I hope I've explained the issue sufficiently clearly. 我希望我已经足够清楚地解释了这个问题。

Thanks for your help! 谢谢你的帮助!

Since the predicted result and the labels must have the same shape, we should create an entire model, containing the function you want (not leaving the function to the loss function). 由于预测结果和标签必须具有相同的形状,我们应该创建一个完整的模型,包含您想要的功能(不将功能留给损失功能)。

Later we can take the output of a previous layer, which will be the desired parameters. 稍后我们可以获取前一层的输出,这将是所需的参数。

So, suppose you've got your model prepared up to the layer that outputs the parameters (A Dense(4) most likely, which will output 4 parameters for each input sample ). 因此,假设您已将模型准备到输出参数的层Dense(4)最有可能是A Dense(4) ,这将为每个输入样本输出4个参数)。

Let's add two lambda layers after it. 让我们在它之后添加两个lambda图层。

  • One to output the 4 unique parameters, independent from samples, because you will want to retrieve them later 一个输出独立于样本的4个唯一参数,因为您将在以后检索它们
  • One to be the actual function a*sin(bx + c) + d 一个是实际函数a*sin(bx + c) + d

So: 所以:

#add them to your model the usual way you do
model.add(Lambda(getParameters,output_shape=(4,),name='paramLayer'))
model.add(Lambda(yourFunction,output_shape=(1,),name='valueLayer'))

Where: 哪里:

import keras.backend as K

def getParameters(x):

    #since x comes in as a batch with shape (20,4) -- (or any other batch size different from 20)

    #let's condense X in one sample only, because we want only 4 elements, not 20*4 elements
    xCondensed = K.mean(x,axis=0,keepdims=True)
        #I'm using keepdims because we will need that x end up with the same number of samples for compatibility purposes (keras rules)

    #let's expand x again (for compatibility purposes), now repeating the 4 values 20 (or more) times
    return K.ones_like(x) * xCondensed



def yourFunction(x):


    #now x has 4 parameters (assuming you had a Dense(4) before these lambda layers)
    a = x[:,0]
    b = x[:,1]
    c = x[:,2]
    d = x[:,3]

    #creating the 20 (or more) iterations
    ones = K.ones_like(x[:,0])
    iterationsStartingAt1= K.cumsum(ones)
    iterationsStartingAt0= iterationsStartingAt1 - 1
    iterations = #choose one of the above


    return (a * K.sin((b*iterations) + c)) + d

Now you can train this model passing the labels. 现在您可以通过标签训练此模型。

When you want to retrieve the four parameters, you need another model, that ends earlier: 当您想要检索这四个参数时,您需要另一个模型,它在前面结束:

from keras.models import Model

paramModel = Model(model.inputs,model.get_layer('paramLayer').output)
params = paramModel.predict(testOrTrainData)

The result will be shaped like (20,4), but all the 20 lines will be repeated. 结果将形如(20,4),但所有20行都将重复。

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

相关问题 通过使用 Gan 神经网络的判别器使用 Keras 自定义 Keras 损失函数 - Custom Keras loss function with Keras by discriminator with Gan neural network 自定义损失函数,它依赖于keras中的另一个神经网络 - Custom loss function which depends on another neural network in keras relu function 神经网络输出0或1 - relu function neural network outputting 0 or 1 在 function 中使用 keras 神经网络 - Using keras neural network in function 如何在损失中执行快速傅立叶变换 function 用于神经网络训练 - how to execute a fast fourier transform in a loss function for neural network training 如何在带有MLPClassifier Sklearn的神经网络中使用自定义损失函数? - How to use a custom loss function in a Neural Network with MLPClassifier Sklearn? 在Keras中,为什么必须基于神经网络的输出来计算损失函数? - In Keras, why must the loss function be computed based upon the output of the neural network? 使用 yolo 自定义损失 function 训练神经网络时损失等于 nan? - Loss equals nan in training neural network with yolo custom loss function? 如何在 Tensoflow.Keras 中将可训练参数转换为损失 function - How to take the trainable parameters into a loss function in Tensoflow.Keras 我可以使用阶跃函数作为损失函数来训练神经网络吗? - Can I use step function as loss function to train Neural Network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM