【问题标题】:Build a Keras model that takes a structured array as an input构建一个将结构化数组作为输入的 Keras 模型
【发布时间】:2021-03-02 03:15:54
【问题描述】:

我正在做强化学习来教代理在二维世界中完成任务。其中很大一部分是弄清楚如何将他们的环境表示为神经元。

到目前为止,我已经表示世界具有形状 (10, 10, 7) 的 3-d 网格。前两个 10 是因为网格的大小在每个方向上都是 10,而 7 是因为我对每个空间有 7 种不同的东西要说(是否有食物、敌人、墙壁......)

然后我在 Keras 中使用卷积层来处理这些信息并从中学习。它起作用了,这些生物是successfully walking towards the food

现在我还想添加更多信息,神经网络可能会弄清楚如何使用。例如,我想对代理执行的最后一个操作进行编码。我还可以编码到最近食物的距离或角度。显然,这不是 3-d 数据,这是 1-d 数据的序列。

我希望 Keras 能够将其与 3-d 输入一起用作输入,并从中学习。我已经在 NumPy 中将组合数据表示为结构化数组:

observation = np.zeros((1,), dtype=[('grid', np.float64, (10, 10, 7,)), ('sequential', np.float64, (7,))])

这样可以以observation['grid'] 访问网格数据,以observation['sequential'] 访问顺序数据。

不幸的是,我不知道如何让 Keras 使用这种结构化数组。我的理由是我应该使用功能 API 构建一个模型,并且该模型将有两个“插脚”用于输入,它们将在某个时候连接到 concatenate 并合并到最终输出层。

但是,我不知道如何让 Keras 弄清楚 NumPy 结构化数组应该分解为组成它的子数组。 这可能吗?

如果我做错了,请指教。

【问题讨论】:

    标签: numpy machine-learning keras deep-learning reinforcement-learning


    【解决方案1】:

    在 keras 中,您可以提供不同的输入,如下所示

    from keras.models import Input, Model
    from keras.layers import Conv2D, Dense, Flatten, concatenate
    
    first_input = Input(shape=(10, 10, 7))
    second_input = Input(shape=(7))
    
    c1 = Conv2D(32, (3,3), padding='same') (first_input)
    c1 = Flatten() (c1)
    d1 = Dense(10) (second_input)
    
    m = concatenate([c1,d1])
    m = Dense(5) (m)
    
    model = Model(inputs=[first_input,second_input], outputs=m)
    model.compile(optimizer='adam' loss='categorical_crossentropy')
    
    model.fit([observation['grid'], observation['sequential']], Y_train)
    

    这是粗略的设计,但它会接受您的输入并连接,然后产生结果

    【讨论】:

      猜你喜欢
      • 2018-12-14
      • 2021-03-04
      • 1970-01-01
      • 2018-05-20
      • 2019-08-15
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多