我正在构建一个自定义层,然后在添加密集层时遇到输出形状问题。即使我明确地这样做,该层的输出形状似乎也没有定义。这是重现该问题的最小代码:
import tensorflow as tf
from tensorflow import keras
class fakeLayer(keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def compute_output_shape(self, input_shape):
return ((input_shape[0], input_shape[1]* input_shape[2], input_shape[3]))
def build( self, input_shape):
super().build(input_shape)
def call(self, inputs):
return(tf.reshape(inputs , self.compute_output_shape(tf.shape(inputs))))
inp = keras.layers.Input((32,32,3))
x = keras.layers.Conv2D(16, (3,3))(inp)
x = fakeLayer()(x)
# x = keras.layers.Flatten()(x)
# x = keras.layers.Dense(1)(x)
model = keras.models.Model(inputs= inp, outputs = x)
print(model.summary())
Run Code Online (Sandbox Code Playgroud)
输出这个:
WARNING:tensorflow:Entity <bound method fakeLayer.call of <__main__.fakeLayer object at 0x0000021A7370E470>> could not be transformed and will be executed as-is. Please …Run Code Online (Sandbox Code Playgroud)