gpt4 book ai didi

python - model.summary()-AttributeError : 'Tensor' object has no attribute 'summary'

转载 作者:行者123 更新时间:2023-12-03 07:48:31 29 4
gpt4 key购买 nike

这是我的进口:

import tensorflow as tf
import keras
from keras.models import Sequential, Model
from keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from keras.utils import Sequence
我定义了这个模型:
def create_ST_layer(input_shape = (64, 128, 3)):
input_img = Input(shape=input_shape)
model = Conv2D(48, kernel_size=(5, 5), input_shape = input_shape, strides = (1, 1), activation = "relu")(input_img)
model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
model = Conv2D(32, kernel_size=(5, 5), strides = (1, 1), activation = "relu")(model)
model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
model = Dense(50, activation = "relu")(model)
model = Dense(6)(model)

return model
并通过以下方式创建模型:
model = create_ST_layer()
现在,当我尝试获取模型摘要时:
model.summary()
我收到以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-5f15418b3570> in <module>()
----> 1 model.summary()

AttributeError: 'Tensor' object has no attribute 'summary'
我的进口货有问题吗?
非常感谢!

最佳答案

我在Google Colab的tensorflow 2.2.0上对此进行了测试。
我会从几件事做起。使用新的tensorflow版本,而不是导入keras,您应该导入tensorflow.keras。
因此,对于导入,您的代码如下所示:

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from tensorflow.keras.utils import Sequence
另外,您需要调用以下行,以将层分组为具有训练和推理功能的对象。 [模型链接]: https://www.tensorflow.org/api_docs/python/tf/keras/Model
因此,您的完整代码应如下所示:
import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from tensorflow.keras.utils import Sequence

def create_ST_layer(input_shape = (64, 128, 3)):
input_img = Input(shape=input_shape)
model = Conv2D(48, kernel_size=(5, 5), input_shape = input_shape, strides = (1, 1), activation = "relu")(input_img)
model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
model = Conv2D(32, kernel_size=(5, 5), strides = (1, 1), activation = "relu")(model)
model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
model = Dense(50, activation = "relu")(model)
model = Dense(6)(model)
model = tf.keras.Model(inputs=input_img, outputs= model)
return model

model = create_ST_layer()
model.summary()
您的模型得到以下输出:
enter image description here

关于python - model.summary()-AttributeError : 'Tensor' object has no attribute 'summary' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63188879/

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