我正在使用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)将您的张量流更新为最新版本。

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

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

关于python - 如何使用Functional API模型实现CNN并解决keras层中的“_keras_shape”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53925527/

10-12 20:19