我正在尝试使用以下代码从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'
答案 0 :(得分: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。
中的模块混合使用