Keras顺序模型输入形状 [英] Keras Sequential model input shape

查看:72
本文介绍了Keras顺序模型输入形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to train a neural net based on a numpy array with 4 entries as the X-data and another array with one entry as the y-data.

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

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

我认为这很简单,但是我无法使输入形状起作用.我还发现关于输入形状工作原理的信息很少:您是否仅需要指定X数据? y数据呢?

A rather simple thing I thought, but I can't get the input shape to work. I also found very little information about how the input shape works: Do you have to specify only the X data? What about the y data?

我已经尝试设置input_dim = 4,因为这是要做的第一件事,但出现以下错误: Error when checking input: expected dense_1_input to have shape (4,) but got array with shape (1,)

I already tried setting input_dim = 4, since that was the first logical thing to do but I got the following Error: 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)

I then tried setting input_dim = (4, 1), since I thought the y data was causing that issue. But again I got an error message: 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()

如果有人可以解释输入形状的工作原理,将不胜感激.

If someone could explain how the input shapes work would be much appreciated.

推荐答案

输入形状始终希望将批次大小作为第一维.

Input shape always expect the batch size as first dimention.

例如,在您的情况下,下一层不希望包含形状(4,)的数组

For example in your case, the following layer does not expect an array of shape (4,)

Dense(64, input_dim=4, activation='relu')

此致密层的输入形状是形状(n,4)的张量,其中n是批大小.

The input shape of this dense layer is a tensor of shape (n, 4) where n is the batch size.

要将您的observation传递给模型,您首先需要按以下方式扩展其暗淡度:

To pass your observation to the model you first need to expand its dims as follows:

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,请查看此文章

Also if you are learning DQN check out this article

这篇关于Keras顺序模型输入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆