自定义损失功能,包括图层输出

时间:2019-04-29 17:37:29

标签: python-3.x keras

我有一个数据集,每个案例包含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,该函数将用于获得损失函数训练网络

谢谢您的时间

1 个答案:

答案 0 :(得分:2)

这些方面的情况如何?

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)