当我 运行 model.predict(X) 时,我得到 ndim 的 ValueError
I get ValueError with the ndim when I run model.predict(X)
我使用此代码在我的数据上训练我的模型
tf.keras.backend.clear_session()
tf.random.set_seed(50)
np.random.seed(50)
train_set = windowed_dataset(x_train, window_size=30, batch_size=15, shuffle_buffer=shuffle_buffer_size)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=100, kernel_size=5,
strides=1, padding="causal",
activation="relu",
input_shape=[None, 1]),
tf.keras.layers.LSTM(100, return_sequences=True),
tf.keras.layers.LSTM(100, return_sequences=True),
#tf.keras.layers.Dense(30, activation="relu"),
#tf.keras.layers.Dense(30, activation="relu"),
tf.keras.layers.Dense(1),
tf.keras.layers.Lambda(lambda x: x * 400)
])
optimizer = tf.keras.optimizers.Adam(
learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=True,
name='Adam'
)
model.compile(loss=tf.keras.losses.Huber(),
optimizer=optimizer,
metrics=["mae"])
history = model.fit(train_set,epochs=100)
这里是 model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d (Conv1D) (None, 30, 100) 600
_________________________________________________________________
lstm (LSTM) (None, 30, 100) 80400
_________________________________________________________________
lstm_1 (LSTM) (None, 30, 100) 80400
_________________________________________________________________
dense (Dense) (None, 30, 1) 101
_________________________________________________________________
lambda (Lambda) (None, 30, 1) 0
=================================================================
Total params: 161,501
Trainable params: 161,501
Non-trainable params: 0
_________________________________________________________________
None
我正在尝试运行这个代码
model.predict(
x_valid, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10,
workers=1, use_multiprocessing=False
)
并返回此错误消息:
ValueError: Input 0 of layer sequential is incompatible with the
layer: expected ndim=3, found ndim=2. Full shape received: [None, 1]
我尝试使用此函数 np.array(x_valid).reshape(300,1)
重塑 x_valid,但没有成功。
我把ndim扩大三倍解决了这个问题
test_input = x_valid[425]
test_input = np.expand_dims(test_input,axis=0)
test_input = np.expand_dims(test_input,axis=0)
test_input = np.expand_dims(test_input,axis=0)
print(model.predict(test_input))
# OUTPUT [[[71.46894]]]
您的问题来自于您需要添加 batch_dimension 才能预测一个数据点。
这在处理TensorFlow和Keras时是必要的,即使你预测一个单一的样本,你也需要添加batch_size of 1.
您需要做的是:
- 从您的测试集中获取一项(例如,
test_input = x_valid[0]
)
- 构造1的batch_size,即
test_input = np.expand_dims(test_input,axis=0)
- 现在用模型预测,即
prediction = model.predict(test_input)
问题来自不正确的测试数据维度。 x_input 的形状为 (15,30,1),因此由此得出测试数据也必须具有 3 维形状(例如 [1,1,1])。在您的代码中,测试数据是一个 1-dim 数组,因此您应该使用 'test_input = np.expand_dims(test_input,axis=0)'
扩展 dims TWICE 以达到 3-dim 数组
我使用此代码在我的数据上训练我的模型
tf.keras.backend.clear_session()
tf.random.set_seed(50)
np.random.seed(50)
train_set = windowed_dataset(x_train, window_size=30, batch_size=15, shuffle_buffer=shuffle_buffer_size)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=100, kernel_size=5,
strides=1, padding="causal",
activation="relu",
input_shape=[None, 1]),
tf.keras.layers.LSTM(100, return_sequences=True),
tf.keras.layers.LSTM(100, return_sequences=True),
#tf.keras.layers.Dense(30, activation="relu"),
#tf.keras.layers.Dense(30, activation="relu"),
tf.keras.layers.Dense(1),
tf.keras.layers.Lambda(lambda x: x * 400)
])
optimizer = tf.keras.optimizers.Adam(
learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=True,
name='Adam'
)
model.compile(loss=tf.keras.losses.Huber(),
optimizer=optimizer,
metrics=["mae"])
history = model.fit(train_set,epochs=100)
这里是 model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d (Conv1D) (None, 30, 100) 600
_________________________________________________________________
lstm (LSTM) (None, 30, 100) 80400
_________________________________________________________________
lstm_1 (LSTM) (None, 30, 100) 80400
_________________________________________________________________
dense (Dense) (None, 30, 1) 101
_________________________________________________________________
lambda (Lambda) (None, 30, 1) 0
=================================================================
Total params: 161,501
Trainable params: 161,501
Non-trainable params: 0
_________________________________________________________________
None
我正在尝试运行这个代码
model.predict(
x_valid, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10,
workers=1, use_multiprocessing=False
)
并返回此错误消息:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 1]
我尝试使用此函数 np.array(x_valid).reshape(300,1)
重塑 x_valid,但没有成功。
我把ndim扩大三倍解决了这个问题
test_input = x_valid[425]
test_input = np.expand_dims(test_input,axis=0)
test_input = np.expand_dims(test_input,axis=0)
test_input = np.expand_dims(test_input,axis=0)
print(model.predict(test_input))
# OUTPUT [[[71.46894]]]
您的问题来自于您需要添加 batch_dimension 才能预测一个数据点。
这在处理TensorFlow和Keras时是必要的,即使你预测一个单一的样本,你也需要添加batch_size of 1.
您需要做的是:
- 从您的测试集中获取一项(例如,
test_input = x_valid[0]
) - 构造1的batch_size,即
test_input = np.expand_dims(test_input,axis=0)
- 现在用模型预测,即
prediction = model.predict(test_input)
问题来自不正确的测试数据维度。 x_input 的形状为 (15,30,1),因此由此得出测试数据也必须具有 3 维形状(例如 [1,1,1])。在您的代码中,测试数据是一个 1-dim 数组,因此您应该使用 'test_input = np.expand_dims(test_input,axis=0)'
扩展 dims TWICE 以达到 3-dim 数组