python - Keras 模型输入形状错误
问题描述
我有一个 keras 模型,其布局如下
def keras_model(x_train, y_train, x_test, y_test):
model = Sequential()
model.add(Dense(128, input_dim=x_train.shape[1], activation='relu'))
model.add(Dense(256,activation='relu'))
model.add(Dense(512,activation='relu'))
model.add(Dense(256,activation='relu'))
model.add(Dense(128,activation='relu'))
#model.add(Dense(10,activation='relu'))
model.add(Dense(y_train.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto')
checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) # save best model
model.fit(x_train ,y_train, validation_data=(x_test, y_test),callbacks=[monitor,checkpointer], verbose=2,epochs=1000)
model.load_weights('best_weights.hdf5') # load weights from best model
return model
使用来自 open-ai 健身房手推车游戏的数据进行训练,并保存模型。下一步是使用训练好的模型进行预测
from keras.models import load_model
model = load_model('data/model-v0.h5')
action = random.randrange(0,2)
import gym
env = gym.make("CartPole-v0")
env.reset()
>>> array([ 0.02429215, -0.00674185, -0.03713565, -0.0046836 ])
import random
action = random.randrange(0,2)
observation, reward, done, info = env.step(action)
observation.shape
>>> (4,)
new_observation, reward, done, info = env.step(action)
prev_obs = new_observation
prev_obs
>>> array([-0.00229585, 0.15330146, 0.02160273, -0.30723955])
prev_obs.shape
>>> (4,)
model.predict(prev_obs)
>>>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-943f2f44ed64> in <module>()
----> 1 model.predict(prev_obs)
c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\keras\engine\training.py in predict(self, x, batch_size, verbose, steps)
1145 'argument.')
1146 # Validate user data.
-> 1147 x, _, _ = self._standardize_user_data(x)
1148 if self.stateful:
1149 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
747 feed_input_shapes,
748 check_batch_axis=False, # Don't enforce the batch size.
--> 749 exception_prefix='input')
750
751 if y is not None:
c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
135 ': expected ' + names[i] + ' to have shape ' +
136 str(shape) + ' but got array with shape ' +
--> 137 str(data_shape))
138 return data
139
ValueError: Error when checking input: expected dense_1_input to have shape (4,) but got array with shape (1,)
观察的形状与所使用的训练数据的形状相似,问题甚至正如您所看到的那样,observation
并且prev_observation
形状(4,)
为(1,)
我什至尝试过重塑它
prev_obs.shape = (4,)
prev_obs.reshape((4,))
但它仍然会引发同样的错误。
解决方案
的 APIkeras
始终假定您以批量或以数组的形式提供数据,它可以从中提取批量。因此,即使模型的第一层需要输入形状(4,)
,您也必须重塑数据以具有形状(1,4)
。
model.predict(prev_obs.reshape((1, -1)
这告诉模型对 1 个数据样本进行预测,该样本由 4 维向量(观察值)组成。
推荐阅读
- php - 为什么这个 php 代码给了我一个空白屏幕?
- python - Pandas 使用 df[[]] 选择错误的列
- paypal - 如何在braintree中使用SEPA而不要求贝宝帐户?
- reactjs - 模块/组件执行顺序如何确定?
- python - 如何腌制引用cytpes库的python对象
- visual-studio-code - 将文件从 VS Code 拖动到其他应用程序将复制文件路径而不是文件本身
- android - 使用 Dagger2 创建 ActionBarDrawerToggle 时导航抽屉不起作用
- javascript - 我添加了javascript的按钮但不起作用
- javascript - 即使我在元标记中提到它,也找不到 CSRF_TOKEN
- angular - 角度设置对组件的唯一引用#name