【发布时间】:2021-07-23 00:45:41
【问题描述】:
我正在使用由多个 Keras 层构建的 Keras 自定义层。我试图让内层的 output_shape 形成一个回调(on_train_batch_end)并得到以下错误: “AttributeError:该层从未被调用,因此没有定义的输入形状。”
我不明白如果调用自定义层中的调用函数会发生这种情况,因为我已经为单个批次训练了模型。
代码示例:
from tensorflow.keras.layers import ReLU, MaxPooling2D, Input, Dense, Conv2D, Flatten
from tensorflow.keras.layers import Layer
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import Callback
import tensorflow as tf
import numpy as np
class MyLayer(Layer):
def __init__(self):
super(MyLayer, self).__init__()
self.conv = None
self.m_max = None
self.relu = None
def call(self, inputs, **kwargs):
x = self.conv(inputs)
x = self.m_max(x)
return self.relu(x)
def build(self, input_shape):
self.conv = Conv2D(input_shape=input_shape, filters=128, kernel_size=(2,2))
self.m_max = MaxPooling2D()
self.relu = ReLU()
class ModelCallback(Callback):
def on_batch_end(self, batch, logs=None):
print(self.model.layers[1].conv.output_shape)
inp = Input((32,32,3))
x = MyLayer()(inp)
x = Flatten()(x)
out = Dense(1)(x)
model = Model(inputs=inp, outputs=out)
model.compile(optimizer='adam', loss='categorical_crossentropy' )
x_train = np.random.rand(5000,32,32,3)
y_train = np.random.randint(2, size=(5000,1))
model.fit(x_train, y_train,epochs=5, callbacks=ModelCallback())
【问题讨论】:
-
尝试复制您的代码,但遇到错误,
NameError: name 'ds_train' is not defined。您能否提供完整的代码(如果它不是机密的),以便我们为您提供帮助。请找到 Colab Gist:colab.research.google.com/gist/rmothukuru/… -
我更新了完整的代码,这只是一个我可以分享的虚拟代码,但仍然出现同样的错误。
-
@TFer2 ,我用正确的代码创建了一个新的 github gist colab.research.google.com/gist/arielAmsel/…
标签: tensorflow keras