我正在尝试使用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定义的,因此不能使用%>%
操作符顺序添加额外的层。
我还尝试使用功能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.
关于如何将N1
与outlayer
或N2
结合的任何想法都非常感谢-感谢阅读!
在使用函数式api时,使用像input
这样的张量,而不是层本身。下面是您想要的工作代码片段:
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包(v2.0.1)在2018-10-15上创建