使用Keras 2.2.0将顺序模型转换为功能模型 [英] Conversion of a sequential model to a functional model with Keras 2.2.0

查看:84
本文介绍了使用Keras 2.2.0将顺序模型转换为功能模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Keras版本2.1.6之前,通过不再可能.

Up to Keras version 2.1.6 one was able to "convert" a sequential model to a functional model by accessing the underlying model.model. Since version 2.2.0 this is no longer possible.

还可以通过其他方式完成吗?

Can it still be done in some other way?

(如果您想知道为什么我想做这样的事情,我将维护一个库依赖于此转换.:wink:)

(In case you wonder why I would like to do something like this, I'm maintaining a library that relies on this conversion. :wink:)

推荐答案

由于我没有安装Keras 2.2.0,因此我目前无法测试此解决方案,但我认为它应该可以工作.假设您的顺序模型存储在seqmodel中:

I can't test this solution right now since I don't have Keras 2.2.0 installed, but I think it should work. Let's assume your sequential model is stored in 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])

这应该给出等效的功能模型.让我知道我是否记错了.

This should give the equivalent functional model. Let me know if I am mistaken.

这篇关于使用Keras 2.2.0将顺序模型转换为功能模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆