如何在 Tensorflow / Keras 中为具有字典形式的多个标签的数据集定义损失?

我有一个具有多个标签的数据集,我想定义取决于标签的损失。数据集中的标签存储为字典,例如:


y = tf.data.Dataset.from_tensor_slices({'values': [1, 2, 3], 'symbols': [4, 5, 6]})

然后我想为每个标签定义一个损失,以便稍后对损失进行某种组合。我尝试这样定义损失:


def model_loss(y, y_):

    return tf.losses.SparseCategoricalCrossentropy(from_logits=False, name='values_xent')(y['values'], y_)


然而,当我拟合模型时,它给了我以下错误:


TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got 'values'

所以看来我不能这样做y['values']。我怎样才能在损失中获取这个值?提前致谢。


编辑


我想要实现的是这样的:


import tensorflow as tf

import numpy as np


# samples

ds_x = tf.data.Dataset.from_tensor_slices(np.random.randn(5, 5))


# labels

ds_y = tf.data.Dataset.from_tensor_slices({'l1': np.arange(5), 'l2':np.arange(5)})


# samples + labels

ds = tf.data.Dataset.zip((ds_x, ds_y))


# model

input_ = tf.keras.Input(shape=(5,))

x = tf.keras.layers.Dense(30, activation='relu')(input_)

x1 = tf.keras.layers.Dense(5, activation='softmax')(x)

x2 = tf.keras.layers.Dense(5, activation='softmax')(x)

model = tf.keras.Model(inputs=input_, outputs={'l1':x1, 'l2':x2})


# loss

def model_loss(y, y_):

    res = 3 * tf.losses.SparseCategoricalCrossentropy()(y['l1'], y_['l1'])

    res += tf.losses.SparseCategoricalCrossentropy()(y['l2'], y_['l2'])

    return res


# compile and train

model.compile(optimizer='adam', loss=model_loss)

model.fit(ds.batch(5), epochs=5)


慕容3067478
浏览 129回答 1
1回答

ABOUTYOU

一旦你做了一些对 Keras 来说不完全正常的事情,我建议使用自定义训练循环。然后您可以控制训练过程的每一步。我这样做了,我不需要改变你的损失函数。import tensorflow as tfimport numpy as npds_x = tf.data.Dataset.from_tensor_slices(np.random.randn(5, 5).astype(np.float32))ds_y = tf.data.Dataset.from_tensor_slices({'l1': np.arange(5), 'l2':np.arange(5)})ds = tf.data.Dataset.zip((ds_x, ds_y)).batch(2)input_ = tf.keras.Input(shape=[5])x = tf.keras.layers.Dense(30, activation='relu')(input_)x1 = tf.keras.layers.Dense(5, activation='softmax')(x)x2 = tf.keras.layers.Dense(5, activation='softmax')(x)model = tf.keras.Model(inputs=input_, outputs={'l1':x1, 'l2':x2})def model_loss(y, y_):    res = 3 * tf.losses.SparseCategoricalCrossentropy()(y['l1'], y_['l1'])    res += tf.losses.SparseCategoricalCrossentropy()(y['l2'], y_['l2'])    return restrain_loss = tf.keras.metrics.Mean()optimizer = tf.keras.optimizers.Adam()for i in range(25):    for x, y in ds:        with tf.GradientTape() as tape:            out = model(x)            loss = model_loss(y, out)                    gradients = tape.gradient(loss, model.trainable_variables)        optimizer.apply_gradients(zip(gradients, model.trainable_variables))        train_loss(loss)    print(f'Epoch {i} Loss: {train_loss.result():=4.4f}')    train_loss.reset_states()Epoch 0 Loss: 6.4170Epoch 1 Loss: 6.3396Epoch 2 Loss: 6.2737Epoch 11 Loss: 5.7191Epoch 12 Loss: 5.6608Epoch 19 Loss: 5.2646Epoch 24 Loss: 4.9896
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python