web-dev-qa-db-ja.com

このモデルは、model.summary()でまだビルドされていません

私は次のように定義されたケラスモデルを持っています

class ConvLayer(Layer) :
    def __init__(self, nf, ks=3, s=2, **kwargs):
        self.nf = nf
        self.grelu = GeneralReLU(leak=0.01)
        self.conv = (Conv2D(filters     = nf,
                            kernel_size = ks,
                            strides     = s,
                            padding     = "same",
                            use_bias    = False,
                            activation  = "linear"))
        super(ConvLayer, self).__init__(**kwargs)

    def rsub(self): return -self.grelu.sub
    def set_sub(self, v): self.grelu.sub = -v
    def conv_weights(self): return self.conv.weight[0]

    def build(self, input_shape):
        # No weight to train.
        super(ConvLayer, self).build(input_shape)  # Be sure to call this at the end

    def compute_output_shape(self, input_shape):
        output_shape = (input_shape[0],
                        input_shape[1]/2,
                        input_shape[2]/2,
                        self.nf)
        return output_shape

    def call(self, x):
        return self.grelu(self.conv(x))

    def __repr__(self):
        return f'ConvLayer(nf={self.nf}, activation={self.grelu})'
class ConvModel(tf.keras.Model):
    def __init__(self, nfs, input_shape, output_shape, use_bn=False, use_dp=False):
        super(ConvModel, self).__init__(name='mlp')
        self.use_bn = use_bn
        self.use_dp = use_dp
        self.num_classes = num_classes

        # backbone layers
        self.convs = [ConvLayer(nfs[0], s=1, input_shape=input_shape)]
        self.convs += [ConvLayer(nf) for nf in nfs[1:]]
        # classification layers
        self.convs.append(AveragePooling2D())
        self.convs.append(Dense(output_shape, activation='softmax'))

    def call(self, inputs):
        for layer in self.convs: inputs = layer(inputs)
        return inputs

問題なくこのモデルをコンパイルできます

>>> model.compile(optimizer=tf.keras.optimizers.Adam(lr=lr), 
              loss='categorical_crossentropy',
              metrics=['accuracy'])

しかし、このモデルの概要をクエリすると、このエラーが表示されます

>>> model = ConvModel(nfs, input_shape=(32, 32, 3), output_shape=num_classes)
>>> model.summary()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-220-5f15418b3570> in <module>()
----> 1 model.summary()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in summary(self, line_length, positions, print_fn)
   1575     """
   1576     if not self.built:
-> 1577       raise ValueError('This model has not yet been built. '
   1578                        'Build the model first by calling `build()` or calling '
   1579                        '`fit()` with some data, or specify '

ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

モデルの最初のレイヤーにinput_shapeを提供していますが、なぜこのエラーがスローされるのですか?

11
bachr
# X is a train dataset with features excluding a target variable

input_shape = X.shape  
model.build(input_shape) 
model.summary()
0
B. Kanani

ケラスサブクラスモデルと他のケラスモデル(順次および機能)の間には非常に大きな違いがあります。

シーケンシャルモデルと機能モデルは、レイヤーのDAGを表すデータ構造です。簡単に言うと、機能モデルまたはシーケンシャルモデルは、レゴのように1つを互いに積み重ねることによって構築されたレイヤーの静的なグラフです。したがって、最初のレイヤーにinput_shapeを指定すると、これらの(機能および順次)モデルは他のすべてのレイヤーの形状を推測してモデルを構築できます。次に、model.summary()を使用して入力/出力形状を印刷できます。

一方、サブクラス化モデルは、Pythonコードの本体(呼び出しメソッド)を介して定義されます。サブクラス化モデルの場合、ここにはレイヤーのグラフがありません。レイヤーがどのように接続されているかはわかりません互いに(明示的なデータ構造としてではなく、呼び出しの本文で定義されているため)、入力/出力形状を推測することはできません。サブクラスモデルの場合、最初にテストされるまで、入力/出力形状は不明です。適切なデータ。compile()メソッドでは、遅延コンパイルを実行して適切なデータを待ちます。中間層の形状を推測するには、適切なデータで実行してからmodel.summary()を使用する必要があります。データを使用してモデルを実行しないと、気づいたようにエラーがスローされます。完全なコードについては、 GitHub Gist を確認してください。

以下はTensorflowウェブサイトの例です。

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

class ThreeLayerMLP(keras.Model):

  def __init__(self, name=None):
    super(ThreeLayerMLP, self).__init__(name=name)
    self.dense_1 = layers.Dense(64, activation='relu', name='dense_1')
    self.dense_2 = layers.Dense(64, activation='relu', name='dense_2')
    self.pred_layer = layers.Dense(10, name='predictions')

  def call(self, inputs):
    x = self.dense_1(inputs)
    x = self.dense_2(x)
    return self.pred_layer(x)

def get_model():
  return ThreeLayerMLP(name='3_layer_mlp')

model = get_model()

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(60000, 784).astype('float32') / 255
x_test = x_test.reshape(10000, 784).astype('float32') / 255

model.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              optimizer=keras.optimizers.RMSprop())

model.summary() # This will throw an error as follows
# ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

# Need to run with real data to infer shape of different layers
history = model.fit(x_train, y_train,
                    batch_size=64,
                    epochs=1)

model.summary()

お役に立てれば。ありがとう!