简体   繁体   English

如何使用 keras 后端将可训练的标量参数添加到张量流图中

[英]How do I add trainable scalar parameters to tensorflow graph with keras backend

I want to approximate the function g(x) = exp(x) with a linear combination of functions, h(x) = sum_i(a_i * f_i(x)) using a neural network with TF2.我想用函数的线性组合 h(x) = sum_i(a_i * f_i(x)) 使用带有 TF2 的神经网络来近似函数 g(x) = exp(x)。

Now, the network input is just x and the outputs are the functions f_i.现在,网络输入只是 x,输出是函数 f_i。

The custom loss function is simple: the mean squared difference |g(x) - h(x)|^2.自定义损失函数很简单:均方差|g(x) - h(x)|^2。

My problem is that I don't understand how to define/use a_i?我的问题是我不明白如何定义/使用 a_i?

First, all a_i are just scalars, In addition they don't depend on x.首先,所有 a_i 都只是标量,此外它们不依赖于 x。

Defining a_i as inputs doesn't make sense since I want them to be optimized.将 a_i 定义为输入没有意义,因为我希望它们得到优化。

Defining as outputs makes them depend on x which I don't want.定义为输出使它们依赖于我不想要的 x。

How can I add these variables as scalars to the network and make them optimized by the optimization process ?.如何将这些变量作为标量添加到网络中并通过优化过程使它们优化?。

During training, Tensorflow tracks all tf.Variable objects defined in the model.在训练期间,Tensorflow 会跟踪模型中定义的所有tf.Variable对象。 If you define your constant as a tf.Variable , it will be ajusted via backpropagation.如果您将常量定义为tf.Variable ,它将通过反向传播进行调整。

Let's say we have a dataset wi X , and y which is y = X * 2 :假设我们有一个数据集 wi Xy ,即y = X * 2

import tensorflow as tf

x = tf.random.uniform((10_000, 1))
y = x * 2

We will create a model which has a constant inside, which will need to replicate the relationship between X and y .我们将创建一个内部有常量的模型,它需要复制Xy之间的关系。 We will of course initialize this value as something other than 2. The relationship between X and y is 2 so the training should make the constant converge towards 2. So let's define a model that is nothing but a constant.我们当然会将这个值初始化为 2 以外的值Xy之间的关系是 2,因此训练应该使常数收敛到 2。所以让我们定义一个模型,它只是一个常数。

class CustomModel(tf.keras.models.Model):
    def __init__(self):
        super(CustomModel, self).__init__()
        self.constant = tf.Variable(initial_value=0.1, dtype=tf.float32, trainable=True)

    def call(self, x, **kwargs):
        x = self.constant * x
        return x


model = CustomModel()

Now just compile and train the model:现在只需编译和训练模型:

model.compile(loss='mae')

history = model.fit(x, y, epochs=25, verbose=0)

Now look at the weights.现在看看权重。 The constant, which was initialized with value 0.1, is now 2. It was optimized, as it understood the relationship between X and y , which is 2.常量,初始化为 0.1,现在是 2。它被优化,因为它理解Xy之间的关系,即 2。

model.weights
[<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM