在 Keras 中实现批量相关损失

Implementing a batch dependent loss in Keras

我在 Keras 中设置了一个自动编码器。我希望能够根据预先确定的 'precision' 向量对输入向量的特征进行加权。这个连续值向量与输入长度相同,每个元素都在[0, 1]范围内,对应对应输入元素的置信度,1为完全置信度,0为无置信度。

每个例子都有一个精度向量。

我定义了一个考虑到这个精度向量的损失。在这里,低置信度特征的重建被降低了权重。

def MAEpw_wrapper(y_prec):
    def MAEpw(y_true, y_pred):
        return K.mean(K.square(y_prec * (y_pred - y_true)))
    return MAEpw

我的问题是精度张量 y_prec 取决于批次。我希望能够根据当前批次更新 y_prec,以便每个精度向量与其观察值正确关联。

我已经完成了以下操作:

global y_prec
y_prec = K.variable(P[:32])

这里 P 是一个 numpy 数组,包含所有具有与示例对应的索引的精度向量。我初始化 y_prec 以获得批量大小为 32 的正确形状。然后我定义以下内容 DataGenerator:

class DataGenerator(Sequence):

    def __init__(self, batch_size, y, shuffle=True):

        self.batch_size = batch_size

        self.y = y

        self.shuffle = shuffle
        self.on_epoch_end()

    def on_epoch_end(self):
        self.indexes = np.arange(len(self.y))

        if self.shuffle == True:
            np.random.shuffle(self.indexes)

    def __len__(self):
        return int(np.floor(len(self.y) / self.batch_size))

    def __getitem__(self, index):
        indexes = self.indexes[index * self.batch_size: (index+1) * self.batch_size]

        # Set precision vector.
        global y_prec
        new_y_prec = K.variable(P[indexes])
        y_prec = K.update(y_prec, new_y_prec)

        # Get training examples.
        y = self.y[indexes]

        return y, y

这里我的目标是在生成批处理的同一个函数中更新 y_prec。这似乎正在按预期更新 y_prec。然后我定义我的模型架构:

dims = [40, 20, 2]

model2 = Sequential()
model2.add(Dense(dims[0], input_dim=64, activation='relu'))
model2.add(Dense(dims[1], input_dim=dims[0], activation='relu'))
model2.add(Dense(dims[2], input_dim=dims[1], activation='relu', name='bottleneck'))
model2.add(Dense(dims[1], input_dim=dims[2], activation='relu'))
model2.add(Dense(dims[0], input_dim=dims[1], activation='relu'))
model2.add(Dense(64, input_dim=dims[0], activation='linear'))

最后,我编译 运行:

model2.compile(optimizer='adam', loss=MAEpw_wrapper(y_prec))
model2.fit_generator(DataGenerator(32, digits.data), epochs=100)

其中 digits.data 是观察值的 numpy 数组。

然而,这最终定义了单独的图:

StopIteration: Tensor("Variable:0", shape=(32, 64), dtype=float32_ref) must be from the same graph as Tensor("Variable_4:0", shape=(32, 64), dtype=float32_ref).

我已经搜索了 SO 来解决我的问题,但没有找到任何有效的方法。感谢任何有关如何正确执行此操作的帮助。

尝试在调用 fit_generator 时使用 worker=0 测试您的代码,如果它正常工作,那么线程就是您的问题。

如果线程是原因,试试这个:

# In the code that executes on the main thread
graph = tf.get_default_graph()

# In code that executes in other threads(e.g. your generator)
with graph.as_default():
    ...
    ...
    new_y_prec = K.variable(P[indexes])
    y_prec = K.update(y_prec, new_y_prec)

可以使用 Keras functional API. This will allow to have an additional input placeholder y_prec_input, which will be fed with the "precision" vector. The full source code can be found here.

轻松实现此自动编码器

数据生成器

首先,让我们按如下方式重新实现您的数据生成器:

class DataGenerator(Sequence):
    def __init__(self, batch_size, y, prec, shuffle=True):
        self.batch_size = batch_size
        self.y = y
        self.shuffle = shuffle
        self.prec = prec
        self.on_epoch_end()

    def on_epoch_end(self):
        self.indexes = np.arange(len(self.y))
        if self.shuffle:
            np.random.shuffle(self.indexes)

    def __len__(self):
        return int(np.floor(len(self.y) / self.batch_size))

    def __getitem__(self, index):
        indexes = self.indexes[index * self.batch_size: (index + 1) * self.batch_size]
        y = self.y[indexes]
        y_prec = self.prec[indexes]
        return [y, y_prec], y

请注意,我去掉了全局变量。现在,精度向量 P 作为输入参数 (prec) 提供,并且生成器会产生一个额外的输入,该输入将被提供给精度占位符 y_prec_input(参见模型定义) .


型号

最后,您的模型可以按如下方式定义和训练:

y_input = Input(shape=(input_dim,))
y_prec_input = Input(shape=(1,))
h_enc = Dense(dims[0], activation='relu')(y_input)
h_enc = Dense(dims[1], activation='relu')(h_enc)
h_enc = Dense(dims[2], activation='relu', name='bottleneck')(h_enc)
h_dec = Dense(dims[1], activation='relu')(h_enc)
h_dec = Dense(input_dim, activation='relu')(h_dec)
model2 = Model(inputs=[y_input, y_prec_input], outputs=h_dec)
model2.compile(optimizer='adam', loss=MAEpw_wrapper(y_prec_input))

# Train model
model2.fit_generator(DataGenerator(32, digits.data, P), epochs=100)

其中 input_dim = digits.data.shape[1]。请注意,我还将解码器的输出维度更改为 input_dim,因为它必须与输入维度匹配。