[英]Keras Sequential model input shape
我想训练一个基于 numpy 数组的神经网络,该数组有 4 个条目作为 X 数据,另一个数组有一个条目作为 y 数据。
X_train = [x1, x2, x3, x4]
y_train = [y1]
我认为这是一件相当简单的事情,但我无法使输入形状起作用。 我还发现关于输入形状如何工作的信息很少:您是否必须仅指定 X 数据? y 数据呢?
我已经尝试设置 input_dim = 4,因为这是第一个合乎逻辑的事情,但我收到以下错误: Error when checking input: expected dense_1_input to have shape (4,) but got array with shape (1,)
然后我尝试设置 input_dim = (4, 1),因为我认为 y 数据导致了这个问题。 但我再次收到一条错误消息: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (4, 1)
代码如下:
# importing the packages
import gym
import numpy as np
from collections import deque
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.wrappers.scikit_learn import KerasRegressor
from joblib import Parallel
# creating the environment
env = gym.make('CartPole-v1')
#defining global variables
lr=0.0001
decay=0.001
batch_size=None
# creating a deep learning model with keras
def model():
model = Sequential()
model.add(Dense(64, input_dim=4, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(Adam(lr=lr, decay=decay), loss='mse')
model.summary()
return model
# running the game
for i_episodes in range(200):
env.reset()
for i in range(100):
env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
# observation = ndarray float64
# reward = float
# done = bool
# action = int
# info = empty
observation = np.asarray(observation)
reward = np.asarray(reward)
action = np.asarray(action)
# print(observation.dtype, reward.dtype, action.dtype)
# print(observation.shape, action.shape)
estimator = KerasRegressor(build_fn=model, epochs=30, batch_size=3, verbose=1)
estimator.fit(observation, action)
if done:
break
env.close()
如果有人能解释输入形状的工作原理,我们将不胜感激。
输入形状总是期望批量大小作为第一维。
例如,在您的情况下,以下图层不期望形状为 (4,) 的数组
Dense(64, input_dim=4, activation='relu')
这个密集层的输入形状是一个形状为 (n, 4) 的张量,其中 n 是批量大小。
要将您的observation
传递给模型,您首先需要按如下方式扩展其暗度:
observation = np.asarray(observation)
observation = np.expand_dims(observation, axis=0) # From shape (4,) to (1, 4)
estimator.fit(observation, action)
您的代码应如下所示。
# creating a deep learning model with keras
def build_model():
model = Sequential()
model.add(Dense(64, input_dim=4, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(Adam(lr=lr, decay=decay), loss='mse')
model.summary()
return model
model = build_model()
# running the game
for i_episodes in range(200):
env.reset()
for i in range(100):
env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
# observation = ndarray float64
# reward = float
# done = bool
# action = int
# info = empty
observation = np.asarray(observation)
reward = np.asarray(reward)
action = np.asarray(action)
model.fit(np.expand_dims(observation, axis=0), np.expand_dims(action, axis=0))
另外,如果您正在学习 DQN,请查看这篇文章
试试这段代码。 当您要使用神经网络解决任何回归问题时,您必须指定输入维度。 因此,在输入维度中,您必须将要提供给网络的列数传递给网络。
def baseline_model():
model = Sequential()
model.add(Dense(64, input_dim=4, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(Adam(lr=lr, decay=decay), loss='mse')
model.summary()
return model
现在您必须将其包装在 keras regressor 类中,以便 keras 知道这是您要解决的回归问题。
estimator = KerasRegressor(build_fn=baseline_model, epochs=30, batch_size=3, verbose=1)
如果您需要更多关于如何使用 keras 解决回归问题而不是查看下面的我的笔记本,您将从中获得帮助。
在调用 Fit fucntion 之前也使用这一行
(observation=observation.reshape(1,4))
兄弟! 对于第二个错误,请使用此代码。 现在它对我来说运行良好。
X=[]
y=[]
# creating the environment
env = gym.make('CartPole-v1')
#defining global variables
lr=0.0001
decay=0.001
batch_size=None
# creating a deep learning model with keras
def model():
model = Sequential()
model.add(Dense(64, input_dim=4, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(Adam(lr=lr, decay=decay), loss='mse')
model.summary()
return model
# running the game
for i_episodes in range(200):
env.reset()
for i in range(100):
#env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
observation = np.asarray(observation)
reward = np.asarray(reward)
action = np.asarray(action)
X.append( observation)
y.append(action)
if done:
break
env.close()
X=np.asarray(X)
y=np.asarray(y)
estimator = KerasRegressor(build_fn=model, epochs=30, batch_size=3, verbose=1)
estimator.fit(X, y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.