gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:30:27 24 4
gpt4 key购买 nike

我正在使用 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”对象没有属性“_keras_shape”

最佳答案

1) 将您的 tensorflow 更新到最新版本。

2) 更改您的导入包以下内容可能会解决该问题:

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com