相关疑难解决方法(0)

Keras自定义丢失功能:访问当前输入模式

在Keras(使用Tensorflow后端),我的自定义丢失功能可以使用当前输入模式吗?

当前输入模式被定义为用于产生预测的输入向量.例如,请考虑以下事项:X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42, shuffle=False).然后将当前输入模式是与y_train(在损耗函数称为y_true)相关联的当前X_train矢量.

在设计自定义损失函数时,我打算优化/最小化需要访问当前输入模式的值,而不仅仅是当前预测.

我已经浏览了https://github.com/fchollet/keras/blob/master/keras/losses.py

我还查看了" 不只是y_pred的成本函数,y_true? "

我也熟悉以前的例子来产生一个定制的损失函数:

import keras.backend as K

def customLoss(y_true,y_pred):
    return K.sum(K.log(y_true) - K.log(y_pred))
Run Code Online (Sandbox Code Playgroud)

据推测(y_true,y_pred),其他地方也有定义.我已经看了一下源代码没有成功,我想知道我是否需要自己定义当前的输入模式,或者我的丢失函数是否已经可以访问它.

python loss keras tensorflow

13
推荐指数
1
解决办法
4633
查看次数

如何修复tensorflow“InvalidArgumentError:所有输入的形状必须匹配”

我正在尝试通过顺序 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)

python machine-learning keras tensorflow loss-function

3
推荐指数
1
解决办法
2万
查看次数