python - Keras 序列模型输入形状

标签 python keras

我想训练一个基于 numpy 数组的神经网络,该数组有 4 个条目作为 X 数据,另一个数组有一个条目作为 y 数据。

X_train = [x1, x2, x3, x4]
y_train = [y1]

我认为这是一个相当简单的事情,但我无法让输入形状发挥作用。我还发现有关输入形状如何工作的信息非常少:您必须仅指定 X 数据吗? y 数据呢?

我已经尝试设置 input_dim = 4,因为这是第一个合乎逻辑的事情,但我收到以下错误: 检查输入时出错:预期dense_1_input的形状为(4,),但得到的数组的形状为(1,)

然后我尝试设置 input_dim = (4, 1),因为我认为 y 数据导致了该问题。但我再次收到错误消息: 检查输入时出错:期望dense_1_input具有3维,但得到形状为(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 = 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,请查看此 article

关于python - Keras 序列模型输入形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345443/

相关文章:

python - 使用 pip 和 virtualenv 在 Windows 上安装 psycopg2 时出错

python - 如何使用 NumPy+MKL 避免这种四行内存泄漏?

python - Nginx WebSocket 代理不断获取 HTTP 301 重定向

python - 导入错误 : "No module named tensorflow" (Keras in Anaconda environment)

python - 在 keras 中使用 TFRecords

python - 如何在 tf.data.Dataset 生成器中使用 tf.keras 模型?

python - Keras 教程错误 : NameError: name 'layers' is not defined

Python进程在django db上传脚本中不断增长

python - scrapy 蜘蛛传递参数

python - 将 TensorFlow 张量转换为 Numpy 数组