ValueError:检查输入时出错:预期input_1具有形状(28,1,1),但数组的形状为(1,1,1)

时间:2019-04-28 07:26:55

标签: python keras reinforcement-learning openai-gym

我正在尝试构建一个自动编码器,使其适合观察数据,并使用这种损失作为对代理的奖励。但是,我真的不知道Gym的观测数据是如何工作的,因此当我尝试将数据适合自动编码器时会显示此错误。

试图通过展平,瓶颈化和重塑形式将自动编码器的输出更改为与输入完全相同。还尝试了更改输入的维数,同样的错误。

体育馆环境名称:RoboschoolAnt-v1

自动编码器拟合代码:

rew = fitandgetloss(curious_autoencoder,
                    add_batch(old_obs, 4, False),
                    add_batch(obs, 4, False),
                    rew)           

fitandgetloss()的代码:

def fitandgetloss(compiled_ae, state1, state2, rew=0.0):
    his = compiled_ae.fit(state1, state2)
    rew += his.history['loss'][-1]
    return rew

add_batch()的代码:

# Adds batch dimension (or one extra dimension) so that the autoencoder below
# can interpret the data.
def add_batch(arr, ndim, thing):
    if debug:
        print(arr.shape)
    arr_shape = (1,) * (ndim - arr.ndim)
    if thing:
        arr_shape += arr.shape
        arr.shape = arr_shape
    else:
        arr.shape += arr_shape
    if debug:
        print(arr.shape)
    return arr

自动编码器的代码:

def build_network(input_shape):
    input_input = Input(shape=input_shape)
    x = input_input
    shape_one = K.int_shape(x)
    for i in range(3):
        x = Conv2D(filters=n_filt,
                   kernel_size=k_size,
                   strides=2,
                   activation='relu',
                   padding='same')(x)
    x = Flatten()(x)
    x = Dense(256)(x)
    x = Dense(shape_one[1] * shape_one[2] * shape_one[3])(x)
    output = Reshape((shape_one[1], shape_one[2], shape_one[3]))(x)
    #output = Conv2D(1, 1, 2)(x)
    autoencoder = Model(input_input, output)
    autoencoder.summary()
    return autoencoder

完整追溯:

Traceback (most recent call last):
  File "tensorforcedemo.py", line 110, in <module>
    rew)
  File "/home/ai/expeditionRL/examples/expedition.py", line 29, in curiosity
    his = compiled_ae.fit(state1, state2)
  File "/home/ai/anaconda3/envs/drl/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit
    batch_size=batch_size)
  File "/home/ai/anaconda3/envs/drl/lib/python3.6/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
    exception_prefix='input')
  File "/home/ai/anaconda3/envs/drl/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected input_1 to have shape (28, 1, 1) but got array with shape (1, 1, 1)

0 个答案:

没有答案