【问题标题】:Train on transformed output训练转换后的输出
【发布时间】:2019-04-12 02:41:30
【问题描述】:

我有一个循环神经网络模型,它将(N,) 序列映射到(N,3) 长度序列。我的目标输出实际上是(N,N) 矩阵。但是,我在 numpy 中实现了一个确定性函数,它以我想要的特定方式将 (N,3) 转换为这些 (N,N) 矩阵。如何在训练中使用此操作? IE。目前我的神经网络正在发出(N,3) 序列,我如何在调用keras.fit 之前执行我的函数将其转换为(N,N)之前

编辑:我还应该注意,从 (N,N)(N,3) 执行反向功能要困难得多,因此将我的目标输出转换为 (N,3) 输出表示不是一个可行的选择。

【问题讨论】:

    标签: python keras tf.keras


    【解决方案1】:

    您可以使用Lambda 层作为模型的最后一层:

    def convert_to_n_times_n(x):
        # transform x from shape (N, 3) to (N, N)
    
    transformation_layer = tf.keras.layers.Lambda(convert_to_n_times_n)
    

    您可能希望尽可能在函数中使用“tf-native 方法”,以避免不必要地将张量转换为 numpy 数组并返回。

    如果您只想在训练期间使用该层,而不是在推理期间,您可以使用函数式 API 来实现:

    # create your original model (N,) -> (N, 3)
    input_ = Input(shape=(N,))
    x = SomeFancyLayer(...)(input_)
    x = ...
    ...
    inference_output = OtherFancyLayer(...)(x)
    
    inference_model = Model(inputs=input_, outputs=inference_output)
    
    # create & fit the training model
    training_output = transformation_layer(inference_output)
    training_model = Model(inputs=input_, outputs=training_output)
    
    training_model.compile(...)
    training_model.fit(X, Y)
    
    # run inference using your original model
    inference_model.predict(...)
    

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 2021-12-30
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2019-08-15
      • 1970-01-01
      相关资源
      最近更新 更多