如何使用Functional API模型实施CNN并解决keras层中的“ _keras_shape”错误

时间:2018-12-25 20:46:54

标签: python tensorflow keras

我正在使用Functional API模型实现神经网络,其代码如下所示:

inputTensor = Input(shape=(32, 32,1))
stride = 1

c1 = Conv2D(6, kernel_size=[5,5], strides=(stride,stride), padding="valid", input_shape=(32,32,1), 
                  activation = 'tanh')(inputTensor)
s2 = AveragePooling2D(pool_size=(2, 2), strides=(2, 2))(c1)



c3 = Conv2D(16, kernel_size=[5,5], strides=(stride,stride), padding="valid", activation = 'tanh')(s2)
s4 = AveragePooling2D(pool_size=2, strides=2, padding='valid')(c3)
c5 = Conv2D(120, kernel_size=[5,5], strides=(stride,stride), padding="valid", activation = 'tanh')(s4)

flat_image = Flatten()(c5)
f1 = Dense(84, activation='tanh')(flat_image)
output_layer = Dense(units = 10, activation = 'softmax')(f1)


model = Model(inputTensor,output_layer)

model.compile(loss=tf.losses.softmax_cross_entropy, optimizer='adam', metrics=['accuracy'])
model.fit(train_data, train_labels, epochs= 10 , batch_size=200, 
      validation_split=0.2)

score = model.evaluate(padding_test_data,test_labels, verbose=0)
print ('Test loss:', score[0])
print('Test accuracy:', score[1])

我得到如下所示的错误: AttributeError: 'Tensor' object has no attribute '_keras_shape'

1 个答案:

答案 0 :(得分:1)

1)将张量流更新为最新版本。

2)更改您的import packages,以下o可能会解决该问题:

from tensorflow.python.keras import Input, Model
from tensorflow.python.keras.layers import Conv2D, AveragePooling2D, Dense, Flatten