【问题标题】:Add custom output layer to built-in (functional) keras model in R将自定义输出层添加到 R 中的内置(功能)keras 模型
【发布时间】:2021-12-04 00:39:20
【问题描述】:

我正在尝试使用 R keras 将内置网络架构与自定义输出层相结合。具体来说,我想要一个最初为分类而构建的架构的回归输出。

这是我想要的一个简单示例:

inlayer <- layer_input(shape = c(75, 75, 1))
N1 <- application_inception_v3(weights = NULL,
                               input_tensor = inlayer,
                               include_top = FALSE)
outlayer <- layer_dense(units = 2, activation = 'sigmoid')

fullnet <- N1 %>% outlayer

但是,最后一行代码不起作用 - 我收到以下错误:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  AttributeError: 'Model' object has no attribute 'shape' 

我认为部分问题在于内置网络 (N1) 是使用功能 API 定义的,因此无法使用 %&gt;% 运算符按顺序添加额外的层。

我还尝试使用功能 API 将额外的输出层定义为单独的架构,但我找不到合并这两个模型的方法:

N2_in <-  layer_input(shape = c(2048)) #note: output shape of N1
N2_out <- N2_in %>% layer_dense(units = 2, activation = 'sigmoid')
N2 <- keras_model(N2_in, N2_out)

#try to merge with pipe again:
N1 %>% N2

如果我尝试与管道运算符合并,则会出现以下错误:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Attempt to convert a value (<tensorflow.python.keras.engine.training.Model object at 0x7f88950ed748>) with an unsupported type (<class 'tensorflow.python.keras.engine.training.Model'>) to a Tensor. 

非常感谢任何关于如何将 N1outlayerN2 结合使用的想法 - 并感谢您的阅读!

【问题讨论】:

    标签: r keras


    【解决方案1】:

    使用函数式 api 时,您可以使用以下方式调用层/模型 像下面的input 这样的张量,而不是层本身。 这是一个可以满足您要求的工作 sn-p:

    library(keras)
    input <- layer_input(shape = c(75, 75, 1))
    #> Loaded Tensorflow version 2.6.0
    input
    #> KerasTensor(type_spec=TensorSpec(shape=(None, 75, 75, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'")
    
    N1 <- application_inception_v3(weights = NULL,
                                   input_tensor = input,
                                   include_top = FALSE)
    
    output_layer_instance <- layer_dense(units = 2, activation = 'sigmoid')
    output <- input %>% N1() %>% output_layer_instance()
    output
    #> KerasTensor(type_spec=TensorSpec(shape=(None, 1, 1, 2), dtype=tf.float32, name=None), name='dense/Sigmoid:0', description="created by layer 'dense'")
    
    model <- keras_model(input, output)
    model
    #> Model
    #> Model: "model"
    #> ________________________________________________________________________________
    #> Layer (type)                        Output Shape                    Param #     
    #> ================================================================================
    #> input_1 (InputLayer)                [(None, 75, 75, 1)]             0           
    #> ________________________________________________________________________________
    #> inception_v3 (Functional)           (None, 1, 1, 2048)              21802208    
    #> ________________________________________________________________________________
    #> dense (Dense)                       (None, 1, 1, 2)                 4098        
    #> ================================================================================
    #> Total params: 21,806,306
    #> Trainable params: 21,771,874
    #> Non-trainable params: 34,432
    #> ________________________________________________________________________________
    

    reprex package (v2.0.1) 于 2021 年 10 月 15 日创建

    【讨论】:

    • 非常感谢,这正是我想要的!
    • 很高兴听到这个消息!请不要忘记接受答案。
    猜你喜欢
    • 2021-04-08
    • 2018-08-29
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2022-06-13
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多