Python Keras Multiple outputs returns empty array

我的未来我决定 提交于 2021-01-02 03:47:34

问题


I am trying to create a neural network with 1 input and 2 outputs: This is my code for predicting:

    def predict(self, environment):
        policy, value = self.AI.predict([environment])
        print(type(policy), type(value))
        print(policy, value)

and this is what is printed on the screen:

<class 'numpy.ndarray'> <class 'numpy.ndarray'>
[[0.01186783 0.09048636 0.3038044  0.02231415 0.20798717 0.24917272
  0.06396621 0.02610502 0.02429625]] []

Why is the value array empty? Shouldn't it have 1 float?

This is how I initialised the model (I used my own custom parser):

if __name__ == "__main__":
    dense = [{"type": "flatten"},
             {"type": "dense", "size": 100},
             {"type": "dense", "size": 500},
             {"type": "dense", "size": 250}]

    conv = [{"type": "resize", "shape": (3, 3, 3, 1)},
            {"type": "conv3d", "filters": 32, "filter": (3, 3, 2), "padding": "same"},
            {"type": "flatten"},
            {"type": "dense", "size": 250}]

    policy = [{"type": "dense", "size": 250},
              {"type": "dense", "size": 50},
              {"type": "dense", "size": 9},
              {"type": "softmax"}]

    value = [{"type": "dense", "size": 250},
             {"type": "dense", "size": 75},
             {"type": "dense", "size": 10},
             {"type": "dense", "size": 1, "activation": "tanh"}]

    model = [{"type": "input", "shape": (3, 3, 3)},
             {"type": "duplicate"},
             [conv, dense],
             {"type": "dense", "size": 500},
             {"type": "dropout", "rate": 0.5},
             {"type": "split", "loc": (250, 250), "target_dim": 1},
             [policy, value]]

    def loss_function(true, pred):
        p_true, v_true = true
        p_pred, v_pred = pred
        return tf.reduce_sum(tf.pow(v_true-v_pred,2)) - tf.nn.softmax_cross_entropy_with_logits(p_pred, p_true)

    core = AI(model, loss=loss_function, learning_rate=0.001, ask_verify=True)

By the way this is part of a large project and I can't post all of the code (more than 1000 lines)

来源:https://stackoverflow.com/questions/65375310/python-keras-multiple-outputs-returns-empty-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!