【问题标题】:Keras regression | Get a single prediction from a model fitted with multiple y parametersKeras 回归 |从具有多个 y 参数的模型中获取单个预测
【发布时间】:2020-12-09 03:46:00
【问题描述】:

我有以下代码来创建自定义损失函数,使用数据集中的附加特征(不是输入特征)作为参数。代码运行良好,没有错误,但我想确保它对正确的“Y”做出预测。

    def custom_loss(data, y_pred):

        y_true = data[:, 0]
        feature = data[:, 1]
        return K.mean(K.square((y_pred - y_true) + K.std(y__pred - feature)))

    def create_model():
        # create model
        model = Sequential()
        model.add(Dense(5, input_dim=1, activation="relu"))
        model.add(Dense(1, activation="linear"))

    (train, test) = train_test_split(df, test_size=0.3, random_state=42)

    model = models.create_model(train["X"].shape[1])
    opt = Adam(learning_rate=1e-2, decay=1e-3/200)
    model.compile(loss=custom_loss, optimizer=opt)


    model.fit(x=train["X"], y=train[["Y", "feature"]], validation_data=(test["X"], test[["Y", "feature"]]), batch_size = 8, epochs=90)

    predY = model.predict(test["X"]) # what does the model predict here?

当我使用此模型进行预测时,它是仅对“Y”还是对 Y 和附加特征的组合进行预测,因为 model.fit() 将“Y”和“特征”作为 y 参数进行训练但是 model.predict( ) 只给出一个输出。如果预测是 Y 和附加特征的组合,我怎样才能只提取 Y?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    模型仅预测“Y”。预测时没有调用损失函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2014-07-30
      • 1970-01-01
      相关资源
      最近更新 更多