我想在下面的神经网络模型中使用MaxPoo1D作为密集层,但我得到了这个错误:
ValueError: Input 0 of layer max_pooling1d_8 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 128)
为什么我得到这个错误,我怎么能解决这个问题?为什么预期的ndim = 3?
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
features = iris.feature_names
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
stdscr = StandardScaler()
stdscr.fit(X_train)
X_train = stdscr.transform(X_train)
X_test = stdscr.transform(X_test)
one = OneHotEncoder(sparse=False)
one.fit(y_train.reshape(-1, 1))
y_train = one.transform(y_train.reshape(-1, 1))
y_test = one.transform(y_test.reshape(-1, 1))
model = tf.keras.models.Sequential()
model.add(layers.Dense(128, activation="relu"))
model.add(layers.MaxPool1D())
model.add(layers.Dense(32, activation="relu"))
model.add(layers.Dense(3, activation="softmax"))
model.compile(optimizer="Adam", loss="CategoricalCrossentropy", metrics=["accuracy"])
history = model.fit(X_train, y_train, epochs=30, batch_size=16, validation_data=(X_test, y_test))
您需要添加另一个维度来执行卷积。
试题:
X_train = tf.expand_dims(X_train, axis=-1)
那么,你最终会得到3D数据,这将不适用于你的损失函数。因此,您需要将其扁平化,例如tf.keras.layers.Flatten
。
完整运行代码:
import tensorflow as tf
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
iris = load_iris()
X = iris.data
y = iris.target
features = iris.feature_names
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
stdscr = StandardScaler()
stdscr.fit(X_train)
X_train = stdscr.transform(X_train)
X_test = stdscr.transform(X_test)
X_train = tf.expand_dims(X_train, axis=-1)
X_test = tf.expand_dims(X_test, axis=-1)
one = OneHotEncoder(sparse=False)
one.fit(y_train.reshape(-1, 1))
y_train = one.transform(y_train.reshape(-1, 1))
y_test = one.transform(y_test.reshape(-1, 1))
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.MaxPool1D())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(32, activation="relu"))
model.add(tf.keras.layers.Dense(3, activation="softmax"))
model.compile(optimizer="Adam", loss="CategoricalCrossentropy",
metrics=["accuracy"])
history = model.fit(X_train, y_train, epochs=30,
batch_size=16, validation_data=(X_test, y_test))