【问题标题】:Unable to build example model neither under Linux nor under Windows with different errors无法在 Linux 和 Windows 下构建示例模型并出现不同的错误
【发布时间】:2017-07-12 14:32:47
【问题描述】:

我正在尝试使用以下代码从Keras "first example" 构建模型:

from keras.models import Model
from tensorflow.contrib.keras.api.keras.layers import Dense, Input

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)

不训练不跑步,只是建立一个模型。

在 Linux(Ubuntu 14.04LTS、Python 3.5、Tensorflow 1.2.1、Keras 2.0.6)下它会报错

TypeError:Model 的输入层必须是 InputLayer 对象。 接收到的输入:Tensor("input_1:0", shape=(?, 784), dtype=float32)。 输入 0(从 0 开始)源自图层类型InputLayer

在 Windows 下(Windows 10、Anaconda Python 3.5、Tensorflow-gpu 1.1.0、Keras 2.0.4)它会抱怨

AttributeError: 'Tensor' 对象没有属性 '_keras_shape'

这些是错误,还是过时的文档,或两者兼而有之,还是什么?

如何从文档站点运行这个简单的示例?

更新

如果我将第一行更改为

 inputs = InputLayer(input_shape=(784,))

然后两个操作系统开始抱怨:

 AttributeError: 'InputLayer' object has no attribute 'get_shape'

【问题讨论】:

  • 为什么要将 keras 模型与从 tensorflow 模块导入的图层混合在一起?
  • @Harwee 好问题 :)

标签: python linux windows keras


【解决方案1】:
from keras.models import Model
from keras.layers import Dense, Input

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)

model.summary()

不要将 Keras 独立模块与 contrib 中的模块混用。

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 2013-07-14
    • 2017-06-15
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2021-05-30
    相关资源
    最近更新 更多