簡體   English   中英

Keras Model 未創建

[英]Keras Model Not Being Created

我正在從 Keras 文檔中復制代碼:

img_inputs = keras.Input(shape=(32, 32, 3))
dense = Dense(64, activation="relu")
x = dense(inputs)
x = Dense(64, activation="relu")(x)
outputs = Dense(10)(x)
model = Model(inputs=inputs, outputs=outputs, name="mnist_model")
model.summary()

但是,model 總結如下:

______________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

預期Model 總結:

Model: "mnist_model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 784)]             0         
_________________________________________________________________
dense (Dense)                (None, 64)                50240     
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650       
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________

我收到的錯誤是:

WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.tensordot_11), but
are not present in its tracked objects:
  <tf.Variable 'dense_6/kernel:0' shape=(50, 64) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.nn.bias_add_24), but
are not present in its tracked objects:
  <tf.Variable 'dense_6/bias:0' shape=(64,) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.tensordot_12), but
are not present in its tracked objects:
  <tf.Variable 'dense_7/kernel:0' shape=(64, 64) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.nn.bias_add_25), but
are not present in its tracked objects:
  <tf.Variable 'dense_7/bias:0' shape=(64,) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.tensordot_13), but
are not present in its tracked objects:
  <tf.Variable 'dense_8/kernel:0' shape=(64, 10) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.nn.bias_add_26), but
are not present in its tracked objects:
  <tf.Variable 'dense_8/bias:0' shape=(10,) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
Model: "mnist_model"

我無法弄清楚是什么導致了這個問題。 我之前已經實現了相同的代碼,那時我沒有遇到這個問題。 我目前正在使用 TensorFlow 2.5。

編輯1:文檔鏈接- https://keras.io/guides/functional_api/

編輯 2:進口

# !pip install tensorflow-gpu==2.5.0
import keras
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Conv3D, MaxPooling2D, MaxPooling3D, Softmax, Multiply, Dense, concatenate, Dropout, Flatten, LSTM, BatchNormalization
from tensorflow.keras.activations import softmax
import numpy as np
from keras import backend as K
from keras.callbacks import ModelCheckpoint, History
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

編輯 3:可重現的示例

# !pip install tensorflow-gpu==2.5.0
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Conv3D, MaxPooling2D, MaxPooling3D, Softmax, Multiply, Dense, concatenate, Dropout, Flatten, LSTM, BatchNormalization
from tensorflow.keras.activations import softmax
import numpy as np
from tensorflow.keras import backend as K
from tensorflow.keras.callbacks import ModelCheckpoint, History
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

inputs = tensorflow.keras.Input(shape=(32, 32, 3))
dense = Dense(64, activation="relu")
x = dense(inputs)
x = Dense(64, activation="relu")(x)
outputs = Dense(10)(x)
model = Model(inputs=inputs, outputs=outputs, name="mnist_model")
model.summary()

您正在將 3D 輸入傳遞給 Dense 層,而不會將其展平。 我認為這會導致警告。 However for getting the expected model summary, first import tensorflow and change the input layer to inputs = tensorflow.keras.Input(shape=(784,))

這是完整的代碼片段。

import tensorflow
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Conv3D, MaxPooling2D, MaxPooling3D, Softmax, Multiply, Dense, concatenate, Dropout, Flatten, LSTM, BatchNormalization
from tensorflow.keras.activations import softmax
import numpy as np
from tensorflow.keras import backend as K
from tensorflow.keras.callbacks import ModelCheckpoint, History
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

inputs = tensorflow.keras.Input(shape=(784,))
dense = Dense(64, activation="relu")
x = dense(inputs)
x = Dense(64, activation="relu")(x)
outputs = Dense(10)(x)
model = Model(inputs=inputs, outputs=outputs, name="mnist_model")
model.summary()

暫無
暫無

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

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