ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (5, 1)
ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (5, 1)
我知道之前有人问过这个问题,但我无法从他们那里得到答案。
所以
state is [[ 0.2]
[ 10. ]
[ 1. ]
[-10.5]
[ 41.1]]
和
(5, 1) # np.shape(state)
当model.predict(状态)它抛出
ValueError: Error when checking input: expected dense_1_input to have
3 dimensions, but got array with shape (5, 1)
但是……
model = Sequential()
model.add(Dense(5,activation='relu',input_shape=(5,1)))
我的第一层模型有 input_shape=(5,1) 等于我正在传递的状态形状。
在此之后我还有 2 个更密集的层。
并且
print(model.summary())
// output
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 5, 5) 10
_________________________________________________________________
dropout_1 (Dropout) (None, 5, 5) 0
_________________________________________________________________
dense_2 (Dense) (None, 5, 5) 30
_________________________________________________________________
dropout_2 (Dropout) (None, 5, 5) 0
_________________________________________________________________
dense_3 (Dense) (None, 5, 3) 18
=================================================================
模型定义是 (!!noob alert )
model = Sequential()
model.add(Dense(5,activation='relu',input_shape=(5,1)))
model.add(Dropout(0.2))
# model.add(Flatten())
model.add(Dense(5,activation='relu'))
model.add(Dropout(0.2))
# model.add(Flatten())
model.add(Dense(3,activation='softmax'))
model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
几件事。首先,predict
函数假定输入张量的第一维是批量大小(即使您只预测一个样本),但顺序模型中第一层的 input_shape
属性不包括批量大小,如 here 所示。其次,密集层应用于最后一个维度,这不会给你你想要的,因为我假设你的输入向量有 5 个特征,但你添加了最后一个维度,这使得你的模型输出错误的大小。试试下面的代码:
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
model = Sequential()
model.add(Dense(5, activation='relu', input_shape=(5,)))
model.add(Dropout(0.2))
model.add(Dense(5,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(3,activation='softmax'))
model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
print(model.summary())
state = np.array([0.2, 10., 1., -10.5, 41.1]) # shape (5,)
print("Prediction:", model.predict(np.expand_dims(state, 0))) # expand_dims adds batch dimension
您应该会看到此模型摘要输出,并且还能看到预测向量:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 5) 30
_________________________________________________________________
dropout (Dropout) (None, 5) 0
_________________________________________________________________
dense_1 (Dense) (None, 5) 30
_________________________________________________________________
dropout_1 (Dropout) (None, 5) 0
_________________________________________________________________
dense_2 (Dense) (None, 3) 18
=================================================================
Total params: 78
Trainable params: 78
Non-trainable params: 0
我知道之前有人问过这个问题,但我无法从他们那里得到答案。
所以
state is [[ 0.2]
[ 10. ]
[ 1. ]
[-10.5]
[ 41.1]]
和
(5, 1) # np.shape(state)
当model.predict(状态)它抛出
ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (5, 1)
但是……
model = Sequential()
model.add(Dense(5,activation='relu',input_shape=(5,1)))
我的第一层模型有 input_shape=(5,1) 等于我正在传递的状态形状。
在此之后我还有 2 个更密集的层。
并且
print(model.summary())
// output
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 5, 5) 10
_________________________________________________________________
dropout_1 (Dropout) (None, 5, 5) 0
_________________________________________________________________
dense_2 (Dense) (None, 5, 5) 30
_________________________________________________________________
dropout_2 (Dropout) (None, 5, 5) 0
_________________________________________________________________
dense_3 (Dense) (None, 5, 3) 18
=================================================================
模型定义是 (!!noob alert )
model = Sequential()
model.add(Dense(5,activation='relu',input_shape=(5,1)))
model.add(Dropout(0.2))
# model.add(Flatten())
model.add(Dense(5,activation='relu'))
model.add(Dropout(0.2))
# model.add(Flatten())
model.add(Dense(3,activation='softmax'))
model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
几件事。首先,predict
函数假定输入张量的第一维是批量大小(即使您只预测一个样本),但顺序模型中第一层的 input_shape
属性不包括批量大小,如 here 所示。其次,密集层应用于最后一个维度,这不会给你你想要的,因为我假设你的输入向量有 5 个特征,但你添加了最后一个维度,这使得你的模型输出错误的大小。试试下面的代码:
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
model = Sequential()
model.add(Dense(5, activation='relu', input_shape=(5,)))
model.add(Dropout(0.2))
model.add(Dense(5,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(3,activation='softmax'))
model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
print(model.summary())
state = np.array([0.2, 10., 1., -10.5, 41.1]) # shape (5,)
print("Prediction:", model.predict(np.expand_dims(state, 0))) # expand_dims adds batch dimension
您应该会看到此模型摘要输出,并且还能看到预测向量:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 5) 30
_________________________________________________________________
dropout (Dropout) (None, 5) 0
_________________________________________________________________
dense_1 (Dense) (None, 5) 30
_________________________________________________________________
dropout_1 (Dropout) (None, 5) 0
_________________________________________________________________
dense_2 (Dense) (None, 3) 18
=================================================================
Total params: 78
Trainable params: 78
Non-trainable params: 0