用Keras 2.2.0将序列模型转换为功能模型

2024-10-01 15:39:29 发布

您现在位置:Python中文网/ 问答频道 /正文

在Keras版本2.1.6之前,人们可以通过accessing the underlying ^{}将顺序模型“转换”为功能模型。 由于版本2.2.0,这是no longer possible。在

还能用别的方法吗?在

(如果您想知道我为什么要这样做,我将维护依赖于此转换的a library。:传情动漫:)


Tags: the方法no模型功能版本顺序library
2条回答

我现在不能测试这个解决方案,因为我没有安装keras2.2.0,但我认为它应该可以工作。假设序列模型存储在seqmodel

from keras import layers, models

input_layer = layers.Input(batch_shape=seqmodel.layers[0].input_shape)
prev_layer = input_layer
for layer in seqmodel.layers:
    prev_layer = layer(prev_layer)

funcmodel = models.Model([input_layer], [prev_layer])

这应该给出等效的功能模型。如果我弄错了就告诉我。在

不再需要转换,因为Sequential现在是Model的子类,因此它已经是一个模型。在它以前是一个包装,这大概就是你为什么要问的原因。从source code

class Sequential(Model):
  # ...
  @property
  def model(self):
    # Historically, `Sequential` was once
    # implemented as a wrapper for `Model` which maintained
    # its underlying `Model` as the `model` property.
    # We keep it for compatibility reasons.
    warnings.warn('`Sequential.model` is deprecated. '
                  '`Sequential` is a subclass of `Model`, you can '
                  'just use your `Sequential` instance directly.')
    return self

无论您对模型做什么,您也可以使用Sequential,它只添加了额外的功能,如.add函数,以便于使用。你可以忽略那些额外的功能,把对象当作一个函数模型来使用。在

相关问题 更多 >

    热门问题