【问题标题】:Custom loss function including output of layer自定义损失函数,包括层的输出
【发布时间】:2019-04-30 02:29:32
【问题描述】:

我有一个数据集,每个案例包含 1 个 y_true 值。我想构建一个DNN,它输出3个系数,稍后将用于创建y_pred

y_pred = 4*coeff_1 + 5*coeff_2 + 6 *coeff_3

我正在使用keras,当我尝试定义这样的自定义函数时

from keras.callbacks import ModelCheckpoint
from keras.layers import advanced_activations
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error 

import keras.backend as K

def custom_objective(layer):
    return K.sum(layer.output)

NN_model = Sequential()

# The Input Layer :
NN_model.add(Dense(X_train.shape[1], kernel_initializer='normal',input_dim = X_train.shape[1], activation='relu'))

# The Hidden Layers :
NN_model.add(Dense(20, kernel_initializer='normal',activation='elu'))
NN_model.add(Dense(20, kernel_initializer='normal',activation='elu'))

output_layer = Dense(1, kernel_initializer='normal',activation='linear')

# The Output Layer :
NN_model.add(output_layer)

# Compile the network :
NN_model.compile(loss=custom_objective(output_layer), optimizer='Adamax', metrics=['mean_absolute_error'])
NN_model.summary()

NN_model.fit(X_train, y_train, epochs=10,verbose = 1)

print('NN train = ', mean_absolute_error(y_train , NN_model.predict(X_train)))    

predictions = NN_model.predict(X_test)

MAE = mean_absolute_error(y_test , predictions)

print('NN MAE = ', MAE)

我都明白了

TypeError:不允许将tf.Tensor 用作Python bool。利用 if t is not None: 而不是 if t: 来测试是否定义了张量, 并使用 tf.cond 等 TensorFlow 操作来执行子图 以张量的值为条件。

所以我的问题是

我如何定义一个DNN,每个数据需要1个y_true,输出3个值,它将线性组合成一个y_pred,用于获取损失函数和训练网络

感谢您的宝贵时间

【问题讨论】:

  • 你的 y_train 形状是什么?
  • 4071 x 1 as In 1 每个训练示例的值
  • 您是否有理由不希望您的网络预测这一点?
  • 是的,说来话长,但可以说,这三个值是由三个指示性分析模型计算得出的,只有这些模型的组合就足够了

标签: python-3.x keras


【解决方案1】:

按照这些思路怎么样?

from keras.models import Model
from keras.layers import Dense, Input, Add, Lambda


def model(inp_size):
    inp = Input(shape=(inp_size, 1))

    x1 = Dense(20, activation='elu')(inp)
    x1 = Dense(20, activation='elu')(x1)
    x1 = Dense(1, activation = 'linear')(x1)

    x2 = Dense(20, activation='elu')(inp)
    x2 = Dense(20, activation='elu')(x2)
    x2 = Dense(1, activation = 'linear')(x2)

    x3 = Dense(20, activation='elu')(inp)
    x3 = Dense(20, activation='elu')(x3)
    x3 = Dense(1, activation = 'linear')(x3)

    x1 = Lambda(lambda x: x * 4.0)(x1)
    x2 = Lambda(lambda x: x * 5.0)(x2)
    x3 = Lambda(lambda x: x * 6.0)(x3)
    out = Add()([x1, x2, x3])

    return Model(inputs = inp, outputs = out)

【讨论】:

  • 你好,谢谢,但我有点不知所措,假设我有一个大小为 (4000, 46) 的输入 np 数组,我该如何使用代码?如果这是一个菜鸟问题,我很抱歉,我对这些东西很陌生
  • @john 太好了!抱歉,我无法回复您之前的评论。我不在我的电脑附近。
  • 没问题。我可以问另一个问题吗?我对代码进行了一些更改以适应尺寸,现在它有一个非常奇怪和烦人的问题。有没有可能我用错了?
  • dropmefiles.com/l3YDl 如果您仍然愿意提供帮助,这里是文件。无论如何,谢谢,你帮了很大的忙
  • 当然。我去看看。
猜你喜欢
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 2018-07-22
相关资源
最近更新 更多