我正在尝试在Keras中实现自定义损失功能。它要求我计算每个y in B
这是以下功能的1/epsilon(...)
部分
函子来自this paper-第7页
注意:我绝对可以误解该论文描述的工作。如果我是我,请告诉我
我目前正在尝试使用Keras回调和on_batch_start/end
方法来尝试确定输入批处理的类频率(这意味着访问批处理输入的y_true
,但运气不佳。
谢谢您提供的任何帮助。
编辑:“运气不好”,我是说我找不到在培训期间访问单个批次的y_true
的方法。例如:batch_size = 64
,train_features.shape == (50000, 120, 20)
,我找不到在培训期间访问单个批次的y_true
的方法。我可以从on_batch_start/end
(self.model
)访问keras模型,但无法找到访问实际大小为64的批次的y_true
的方法。
from tensorflow.python.keras.callbacks import Callback class FreqReWeight(Callback): """ Update learning rate by batch label frequency distribution -- for use with LDAM loss """ def __init__(self, C): self.C = C def on_train_begin(self, logs={}): self.model.custom_val = 0 def on_batch_end(self, batch, logs=None): print('batch index', batch) print('Model being trained', self.model) # how can one access the y_true of the batch?
LDAM损失函数
z j
=“第j类的模型的第j输出”EDIT2
损失函数-用于在出现损失时进行测试
def LDAM(C): def loss(y_true, y_pred): print('shape', y_true.shape) # only prints each epoch, not each batch return K.mean(y_pred) + C # NOT LDAM, just dummy for testing purposes return loss
准备数据,编译模型和培训
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
m = 64 # batch_size
model = keras.Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(loss=LDAM(1), optimizer='sgd', metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
model.fit(x_train, y_train,
batch_size=m,
validation_data=(x_test, y_test),
callbacks=[FreqReWeight(1)])
我正在尝试在Keras中实现自定义损失功能。它要求我为B中的每个y计算逆类频率的总和。它是以下...的1 / epsilon(...)部分...
最终问了一个更具体的问题。