我的类似这样的代码:
class sub_sub_Block(tf.keras.Model):
def __init__(self, kernel_size, filters):
super(sub_sub_Block, self).__init__(name='')
if req==True:
self.layer1 = tf.keras.layers.Conv2D(filters1, (1, 1))
if inf==True:
self.layer2 = tf.keras.layers.Conv2D(filters, (1, 1))
def call(self, input_tensor, training=False):
x = self.layer1(input_tensor)
x = self.layer2(x)
return tf.nn.relu(x)
class sub_Block(tf.keras.Model):
def __init__(self, kernel_size, filters):
super(sub_Block, self).__init__(name='')
if req==True:
self.layer1 = sub_sub_Block(filters1, (1, 1))
if inf==True:
self.layer2 = tf.keras.layers.Conv2D(filters, (1, 1))
def call(self, input_tensor, training=False):
x = self.layer1(input_tensor)
x = self.layer2(x)
return tf.nn.relu(x)
class Block(tf.keras.Model):
def __init__(self, kernel_size, filters):
super(Block, self).__init__(name='')
if req==True:
self.layer1 = subBlock(filters1, (1, 1))
if inf==True:
self.layer2 = tf.keras.layers.Conv2D(filters, (1, 1))
def call(self, input_tensor, training=False):
x = self.layer1(input_tensor)
x = self.layer2(x)
return tf.nn.relu(x)
block = Block(1, [1, 2, 3])
print(block(tf.zeros([1, 2, 3, 3])))
print(block.summary())
错误消息是:
ValueError:尚未构建此模型。首先通过调用
build()
或使用某些数据调用fit()
来构建模型,或指定一个 第一层中的input_shape
个参数用于自动构建。
tf.keras.Model
不能在其他tf.keras.Model
区块中吗?