添加的层必须是使用TF2.10.0和python 3.10.8的类layer的实例



我正在使用TF 2.10.0与python 3.10.8和运行TypeError: The added layer must be an instance of class Layer. Received: layer=<class 'keras.layers.pooling.max_pooling2d.MaxPooling2D'> of type <class 'type'>.

我参考了这篇>https://stackoverflow.com/questions/56089489/how-to-fix-the-added-layer-must-be-an-instance-of-class-layer-while-building-a文章试图修复它,但没有运气

这是我的代码。请告诉我我做错了什么

import tensorflow.keras.layers as layers
from tensorflow.keras.models import Sequential
import numpy as np
import PIL
model = Sequential([
layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPool2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPool2D,
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPool2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes, name='output')
])

请帮忙:)

这是正确的,您没有指定它是一个函数,但有趣的是查看自定义类,并喜欢使用CONV函数,这就是侧填充算法的原因。

示例:侧面填充,工作是特定的或使用tf.pad()

import tensorflow as tf
class MyMaxPoolLayer( tf.keras.layers.MaxPool2D ):
def __init__( self, units ):
super(MyMaxPoolLayer, self).__init__( units )
self.num_units = units
def build(self, input_shape):
self.kernel = self.add_weight("kernel",
shape=[int(input_shape[-1]),
self.num_units])
def call(self, inputs):
max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid')

temp = tf.matmul(inputs, self.kernel)
temp = tf.reshape(temp, [1, 10, 10, 1])
temp = max_pool_2d( temp )
return temp
start = 3
limit = 33
delta = 3
sample = tf.range(start, limit, delta)
sample = tf.cast( sample, dtype=tf.float32 )
sample = tf.constant( sample, shape=( 10, 1, 1, 1 ) )
layer = MyMaxPoolLayer(10)
print( layer(sample) )

输出:我们需要对称,他们称之为对称。

...
[[ 14.819944  ]
[ 10.318433  ]
[ 10.318433  ]
[  2.3505163 ]
[-10.546914  ]
[-11.872433  ]
[ 13.375727  ]
[ 13.375727  ]
[  6.088965  ]]
[[ 16.466604  ]
[ 11.464926  ]
[ 11.464926  ]
[  2.6116848 ]
[-11.865278  ]
[-13.356487  ]
[ 14.861918  ]
[ 14.861918  ]
[  6.7655163 ]]]], shape=(1, 9, 9, 1), dtype=float32)

相关内容

  • 没有找到相关文章

最新更新