gpt4 book ai didi

python - 无法在 Linux 和 Windows 下构建示例模型并出现不同的错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:02 25 4
gpt4 key购买 nike

我正在尝试从 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: Input layers to a Model must be InputLayer objects. Received inputs: Tensor("input_1:0", shape=(?, 784), dtype=float32). Input 0 (0-based) originates from layer type InputLayer.

在 Windows 下(Windows 10、Anaconda Python 3.5、Tensorflow-gpu 1.1.0、Keras 2.0.4)它提示

AttributeError: 'Tensor' object has no attribute '_keras_shape'

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

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

更新

如果我把第一行改成

 inputs = InputLayer(input_shape=(784,))

然后两个操作系统开始提示:

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

最佳答案

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 中的模块混合使用。

关于python - 无法在 Linux 和 Windows 下构建示例模型并出现不同的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45060624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com