【问题标题】:Keras Model Not Being Created未创建 Keras 模型
【发布时间】:2025-12-21 22:10:11
【问题描述】:

我正在复制 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()

但是,模型总结如下:

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

预期模型总结:

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()

【问题讨论】:

  • 您应该添加一个独立的可重现示例,包括所有导入。
  • 我已经添加了导入。不太清楚你所说的独立的可重现示例是什么意思。你能告诉我更多吗?
  • 一个我们可以运行的例子。查看您的导入,您正在混合使用 keras 和 tf.keras,它们不是同一个库并且不兼容。
  • 是的,我做了一些阅读,我注释掉了 keras 部分并仅在 tensorflow 导入活动的情况下重新启动了内核。结果还是一样。
  • 您的“可重现示例”不可重现!您有一个从未定义过的变量“输入”。

标签: python tensorflow keras deep-learning neural-network


【解决方案1】:

您正在将 3D 输入传递给 Dense 层,而没有展平它。我认为这会导致警告。 但是为了获得预期的模型摘要, 先import tensorflow,把输入层改成inputs = tensorflow.keras.Input(shape=(784,))

这是完整的sn-p代码。

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()

【讨论】: