【问题标题】:ValueError: Error when checking : expected dense_1_input to have shape (9,) but got array with shape (1,)ValueError:检查时出错:预期dense_1_input的形状为(9,)但得到的数组形状为(1,)
【发布时间】:2018-09-30 23:51:02
【问题描述】:

我有这个数据集

step    pos_x   pos_y   vel_x   vel_y   ship_lander_angle   ship_lander_angular_vel leg_1_ground_contact    leg_2_ground_contact    action
0   0   -0.004053   0.937387    -0.410560   -0.215127   0.004703    0.092998    0.0 0.0 3
1   1   -0.008040   0.933774    -0.401600   -0.240878   0.007613    0.058204    0.0 0.0 3
2   2   -0.011951   0.929763    -0.392188   -0.267401   0.008632    0.020372    0.0 0.0 3
3   3   -0.015796   0.925359    -0.383742   -0.293582   0.007955    -0.013536   0.0 0.0 3
4   4   -0.019576   0.920563    -0.375744   -0.319748   0.005674    -0.045625   0.0 0.0 3

我是这样拆分的:

X = dataset[dataset.columns.difference(["action"])]
Y = dataset["action"]
    # Use a range scaling to scale all variables to between 0 and 1
min_max_scaler = preprocessing.MinMaxScaler()
cols = X.columns

X = pd.DataFrame(min_max_scaler.fit_transform(X), columns = cols) # Watch out for putting back in columns here
# Perfrom split to train, validation, test
x_train_plus_valid, x_test, y_train_plus_valid, y_test = train_test_split(X, Y, random_state=0, test_size = 0.30, train_size = 0.7)
x_train, x_valid, y_train, y_valid = train_test_split(x_train_plus_valid, y_train_plus_valid, random_state=0, test_size = 0.199/0.7, train_size = 0.5/0.7)


# convert to numpy arrays
y_train_wide = keras.utils.to_categorical(np.asarray(y_train)) # convert the target classes to binary 
y_train_plus_valid_wide = keras.utils.to_categorical(np.asarray(y_train_plus_valid))
y_valid_wide = keras.utils.to_categorical(np.asarray(y_valid))

我使用神经网络来训练我的数据

   model_mlp = Sequential()
    model_mlp.add(Dense(input_dim=9, units=32))
    model_mlp.add(Activation('relu'))
    model_mlp.add(Dropout(0.2))
    model_mlp.add(Dense(32))
    model_mlp.add(Activation('relu'))
    model_mlp.add(Dropout(0.2))
    model_mlp.add(Dense(4))
    model_mlp.add(Activation('softmax'))
    #model.add(Dense(num_classes, activation='softmax'))

    model_mlp.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model_mlp.fit(np.asfarray(x_train), np.asfarray(y_train_wide), \
              epochs=20, batch_size=32, verbose=1, \
              validation_data=(np.asfarray(x_valid), np.asfarray(y_valid_wide)))

我几乎得到了 93% 的准确率。我保存模型如下

 filepath = "first_model.mod"
 model_mlp.save(filepath)

在另一个文件中,我需要加载模型并计算上面提到的错误奖励

 if __name__=="__main__":
    # Load the Lunar Lander environment
    env = LunarLander()
    s = env.reset()

    # Load and initialise the contrll model
    ROWS = 64
    COLS = 64
    CHANNELS = 1
    model = keras.models.load_model("first_model.mod")

    # Run the game loop
    total_reward = 0
    steps = 0
    while True:

        # Get the model to make a prediction
        a = model.predict_classes(s)
        a = a[0]

        # Step on the game
        s, r, done, info = env.step(a)
        env.render()
        total_reward += r
        if steps % 20 == 0 or done:
            print(["{:+0.2f}".format(x) for x in s])
            print("step {} total_reward {:+0.2f}".format(steps, total_reward))
        steps += 1


        if done: break

错误在以下行:a = model.predict_classes(s)

【问题讨论】:

  • 看起来 env.reset() 没有返回您期望它返回的内容。您的顺序 Keras 模型期望输入的形状为(9,示例数)。 env.reset() 正在返回一个以 1 作为起始维度的数组。
  • en.reset() 返回的值是这样的数组 [-0.0017684 0.93765326 -0.17912539 -0.1973398 0.00205582 0.04057457 0. 0. ]。我该如何纠正它?
  • 你在那里写了 8 个值,但应该有 9 个。你需要检查 env.reset() 返回的形状是什么。如果是 (1,9),只需转置即可。如果是 (9,) (我怀疑你的错误信息)它应该是 env.reset()[:, np.newaxis]
  • 形状返回为 s.shape= (8,)
  • 那么您训练 NN 所用的数据列与 env.reset() 返回的数据不匹配。我的猜测是您在训练时将 step 作为列包含在内,但这不是 env.reset() 返回的内容。请仔细查看您的代码以及您正在尝试执行的操作。

标签: python-3.x pandas machine-learning neural-network conv-neural-network


【解决方案1】:

问题出在这一行:

X = dataset[dataset.columns.difference(["action"])]
  • 首先,它包含9列,而不是8,这使得网络与env.step返回的gym状态不兼容。这会导致形状不匹配错误。
  • 接下来,columns.difference 还会对输入列进行洗牌(它们按名称排序)。列因此变为:

    Index(['leg_1_ground_contact', 'leg_2_ground_contact', 'pos_x', 'pos_y',
       'ship_lander_angle', 'ship_lander_angular_vel', 'step', 'vel_x',
       'vel_y'],
      dtype='object')
    

拆分Xy的正确方法是这样的:

X = dataset.iloc[:,1:-1]
Y = dataset.iloc[:,-1]

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 2018-10-24
    • 2020-11-22
    • 1970-01-01
    • 2020-05-30
    • 2019-10-22
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    相关资源
    最近更新 更多