简体   繁体   中英

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:

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'

1) Update your tensorflow to latest version.

2) Change your import packages the o following will probably fix the issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM