简体   繁体   English

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

[英]How do I implement CNN using Functional API model and resolve '_keras_shape' error in keras layers

I am implementing a neural network using Functional API Model and the code is as shown below:我正在使用功能 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])

And I get error as shown below: AttributeError: 'Tensor' object has no attribute '_keras_shape'我得到如下所示的错误: AttributeError: 'Tensor' object has no attribute '_keras_shape'

1) Update your tensorflow to latest version. 1) 将您的 tensorflow 更新到最新版本。

2) Change your import packages the o following will probably fix the issue: 2) 更改您的import packages ,以下 o 可能会解决问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Keras 中使用自定义步骤激活 function 会导致“'tuple' object has no attribute '_keras_shape'”错误。 如何解决这个问题? - Using a custom step activation function in Keras results in “'tuple' object has no attribute '_keras_shape'” error. How to resolve this? Keras:如何为 CNN 和 LSTM 层塑造输入? - Keras: How to shape inputs for CNN and LSTM layers? 如何在 Keras 功能 API 中创建具有多个共享层的 model? - How to create a model with multiple shared layers in Keras Functional API? 如何在keras的功能性api中向预训练模型的底部添加图层? - How to add layers to the bottom of a pretrained model in keras' functional api? 如何使用 Keras 的功能 api 优化 model - How to optimize a model using the functional api of Keras 如何重用keras函数模型的层 - How to reuse the layers of keras functional model 输出层形状中的Keras Functional API错误 - Keras Functional API error in output layer shape 如何使用功能 keras API 在预训练的非序列 model 中的激活层之后插入丢失层? - How to insert dropout layers after activation layers in a pre-trained non-sequential model using functional keras API? 在功能 API 模型上从 Keras CNN predict() 获取概率 - Get probabilities from Keras CNN predict() on Functional API model 将层列表添加到 keras 模型计算图(功能 API) - Adding a list of layers to a keras model computation graph (functional API)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM