input_shape
/input_dim
argument to a layer 问题及其解决在使用 Keras/TensorFlow 构建模型时,出现如下警告:
xxx\Lib\site-packages\keras\src\layers\core\dense.py:87: UserWarning: Do not pass an
input_shape
/input_dim
argument to a layer. When using Sequential models, prefer using anInput(shape)
object as the first layer in the model instead.
super().init(activity_regularizer=activity_regularizer, **kwargs)
代码如下:
import matplotlib.pyplot as plt
from keras import layers, models
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
# 生成非线性可分数据(月亮数据集)
X, y = make_moons(n_samples=200, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# 构建过于复杂的神经网络
model = models.Sequential([
layers.Dense(128, activation='relu', input_shape=(2,)),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型(故意不设早停和正则化)
history = model.fit(X_train, y_train, epochs=500, validation_data=(X_test, y_test), verbose=0)
# 绘制训练与验证准确率曲线
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.plot(history.history['accuracy'], label='训练准确率')
plt.plot(history.history['val_accuracy'], label='验证准确率')
plt.legend()
plt.show()
从警告内容可知,这是由于第一层指定了 input_shape
或 input_dim
导致,根据警告建议,在第一层之前,明确 Input(shape)
即可解决问题。
出问题的代码段如下:
model = models.Sequential([
layers.Dense(128, activation='relu', input_shape=(2,)),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
修改后代码如下:
model = models.Sequential([
layers.Input(shape=(2,)),
layers.Dense(128, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
最终执行代码如下:
from keras import layers, models
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
# 生成非线性可分数据(月亮数据集)
X, y = make_moons(n_samples=200, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# 构建过于复杂的神经网络
model = models.Sequential([
layers.Input(shape=(2,)),
layers.Dense(128, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型(故意不设早停和正则化)
history = model.fit(X_train, y_train, epochs=500, validation_data=(X_test, y_test), verbose=0)
# 绘制训练与验证准确率曲线
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.plot(history.history['accuracy'], label='训练准确率')
plt.plot(history.history['val_accuracy'], label='验证准确率')
plt.legend()
plt.show()