我正在尝试通过顺序 Keras 神经网络运行小波重建数据集。为了从训练中获得更好的结果,我尝试构建一个仅关注波形的某些索引的自定义损失函数。我打算创建一个对剪切波形进行插值的神经网络,因此我只想让神经网络通过将波形的剪切片段与实际输出进行比较来计算损失。
我已经尝试为我的自定义损失函数创建一个包装器,以便我可以传递额外的输入参数。然后,我使用此输入参数来查找剪辑数据点的索引,并尝试从 y_pred 和 y_true 收集这些索引。
这是模型实例化和训练的地方:
x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.7)
_dim = len(x_train[0])
# define the keras model
model = Sequential()
# tanh activation allows for vals between -1 and 1 unlike relu
model.add(Dense(_dim*2, input_dim=_dim, activation=_activation))
model.add(Dense(_dim*2, activation=_activation))
model.add(Dense(_dim, activation=_activation))
# model.compile(loss=_loss, optimizer=_optimizer)
model.compile(loss=_loss, optimizer=_optimizer, metrics=[custom_loss_wrapper_2(x_train)])
print(model.summary())
# The patience parameter is the amount of epochs to check for improvement
early_stop = EarlyStopping(monitor='val_loss', patience=5)
# fit the model
history = model.fit(x_train, y_train, validation_data=(x_test, …Run Code Online (Sandbox Code Playgroud)