import numpy as np import tensorflow as tf import tensorflow.python.keras as k import tensorflow.python.keras.layers as kl from tensorflow.python.keras.utils.losses_utils import ReductionV2
x = np.reshape(np.arange(10000), (1000, 10)) y = np.tile(np.array([[1, 2, 3]], np.float32), [1000, 1])
model = k.Sequential([kl.Dense(3, input_shape=[10])])
class test_Loss(k.losses.Loss): def __init__(self, reduction=ReductionV2.SUM_OVER_BATCH_SIZE, name=None): super().__init__(reduction=reduction, name=name)
def call(self, y_true: tf.Tensor, y_pred: tf.Tensor): return y_true + 0 * y_pred
print('AUTO : ') model.compile(k.optimizers.Adam(), [test_Loss(ReductionV2.AUTO)]) model.fit(x, y, 100) print('NONE : ') model.compile(k.optimizers.Adam(), [test_Loss(ReductionV2.NONE)]) model.fit(x, y, 100) print('SUM : ') model.compile(k.optimizers.Adam(), [test_Loss(ReductionV2.SUM)]) model.fit(x, y, 100) print('SUM_OVER_BATCH_SIZE : ') model.compile(k.optimizers.Adam(), [test_Loss(ReductionV2.SUM_OVER_BATCH_SIZE)]) model.fit(x, y, 100)
|