代码是这样的。变量“bneck”是按顺序的。我想得到中间层的输出
...
x = bneck(x)
x = CBNModule(960, 1, 1, activation=HSwish())(x) # 7 * 960
s32 = CBNModule(320, 1, 1, activation=HSwish())(x) # 7 * 960 -> 7 * 320
s32 = CBNModule(24, 1, 1, activation=HSwish())(s32) # 7 * 320 -> 7 * 24
s16 = k.layers.Add()([
CBNModule(24, 1, 1, activation=HSwish())(bneck.layers[12].output),
UpModule(24, 2)(s32)
]) # (14 * 160 -> 14 * 24) + (7 * 24 -> 14 * 24)
...
return keras.Model(inputs=[...], outputs=[...])
当我运行model.summary()
时,我得到了这样的错误ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor
错误发生在第6行bneck.layers[12].output
。但是当我用代码替换第1行时
for layer in bneck.layers:
x = layers(x)
没有错误。为什么呢?和他们有什么区别
首先,您必须根据所需的输出层创建一个特征提取器。您的图形在此处断开连接
bneck.layers[12].output
。假设你有model A
和model B
。您希望从model A
获得某个层的输出(比如说2层),并在model B
中使用它们来完成其架构。为此,首先从model A
创建2特征提取器,如下所示在这里,我将引导您完成一个简单的代码示例。有一种更灵活、更聪明的方法可以做到这一点,但这里有一种。我将构建一个序列模型并在
CIFAR10
上对其进行训练,接下来,我将尝试构建一个功能模型,在其中我将利用一些序列模型层(仅其中的^{2),并在CIFAR100
上训练整个模型在
CIFAR10
数据集上执行Trian现在,假设我们希望从这个顺序模型中得到一些输出,比如说下面两层
为了得到它们,我们首先从序列模型中创建两个特征提取器
或者,如果我们想冻结它们,我们现在也可以这样做。冻结,因为我们在这里选择了相同类型的数据集。(
CIFAR 10-100
)现在,让我们使用函数式API创建一个新模型,并使用上述两个功能提取器
列车在
CIFAR100
参考:Feature extraction with a Sequential model
根据你对我的第一篇文章的第一次评论,我正在添加一篇新的文章,而不是编辑我现有的答案,因为它已经有一篇太长的文章了。无论如何,你的担心是合理的。甚至我也在为子类API here的某些问题而挣扎。但我似乎在我的问题中写得不太好,因为人们并不觉得这是一个值得关注的问题
无论如何,这里有另一个更简洁和精确的答案,因为我们构建了一个带有期望输出的单一模型。一个单独的提取器,而不是以前的两个单独的提取器,这会带来额外的计算开销。比如说,我们的顺序模型
我们需要一个模型中的
conv3
和conv5
。我们可以很容易地做到这一点,如下所示很好,预期层的两个特征输出。现在,让我们使用这两个层(就像我的第一篇文章)来构建一个功能API模型
相关问题 更多 >
编程相关推荐