簡體   English   中英

這個model還沒有建好。 首先通過調用 build() 或對一批數據調用 model 來構建 model

[英]This model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data

增強層代碼

data_augmentation = tf.keras.Sequential([
layers.RandomFlip('horizontal'),
layers.RandomRotation(0.2),
])

Model 塊在 function get_model(model_name, droput_rate) 中:

model = tf.keras.Sequential([
        data_augmentation,
        layers.Conv3D(64, (5, 5, 5), padding='same', activation='relu', input_shape=(22, 64, 64, 1)),
        layers.BatchNormalization(),
        layers.MaxPooling3D(pool_size=(3, 3, 3)),
        layers.Dropout(dropout_rate),
        layers.Conv3D(128, (5, 5, 5), padding='same', activation='relu'),
        layers.Conv3D(128, (5, 5, 5), padding='same', activation='relu'),
        layers.BatchNormalization(),
        layers.MaxPooling3D(pool_size=(3, 3, 3)),
        layers.Dropout(dropout_rate),
        layers.GlobalMaxPool2D(),
        layers.Dense(10, activation='softmax')])

這里model_name會調用上面的model塊

opt = tf.keras.optimizers.SGD(learning_rate=0.001)
model = get_model(model_name, 0.5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model_json = model.to_json()
with open(models_dir + model_name + ".json", "w") as json_file:
    json_file.write(model_json)

# plot the model architecture
model.summary()
plot_model(model, to_file='/content/gdrive/MyDrive/Lip Reading/outputs/architecture_{}.pdf'.format(model_name), show_shapes=True, show_layer_names=False)

執行上面的代碼時,會顯示以下錯誤:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-166-57c956c1a8c7> in <module>()
      4 
      5 # plot the model architecture
----> 6 model.summary()
      7 plot_model(model, to_file='/content/gdrive/MyDrive/Lip Reading/outputs/architecture_{}.pdf'.format(model_name), show_shapes=True, show_layer_names=False)

/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in summary(self, line_length, positions, print_fn, expand_nested, show_trainable)
   2774     if not self.built:
   2775       raise ValueError(
-> 2776           'This model has not yet been built. '
   2777           'Build the model first by calling `build()` or by calling '
   2778           'the model on a batch of data.')

ValueError: This model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.

引發錯誤的是model.summary() 在調用model.summary()之前執行model.build(input_shape=(x1, x2, x3)) ) 。 當然,您需要將input_shape替換為所需的形狀。

將以下行添加到 model 對我有用。

tf.keras.Input(shape=(input_shape,)),

這是代碼片段:

tf.random.set_seed(1234)
model = Sequential(
[               
    tf.keras.Input(shape=(100,)),    #specify input shape
    Dense(25, activation = 'relu'),
    Dense(15, activation = 'linear')        

], name = "model_y" 
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM