How to train network with 2D output? (python,Keras)

无人久伴 提交于 2019-12-08 03:33:26

问题


I want to train a regression network which its outputs are two coordinates (x1,y1) and (x2,y2). my question is: if I want to train network should my output be separated? I mean should my output like this:

 [x1,y1,x2,y2] 

or is there a way to stack them like:

[(x1,y1),(x2,y2)]

Thanks in advance


回答1:


The RepeatVector is there for this purpose (see Keras documentation).

You want your output shape to be (2, 2), or an array of two coordinates with two entries each.

num_outputs = 2
num_elements = 2

After you have added layers for processing input to your network, add a RepeatVector. This will make it possible to get an array of data as output. Your final layer needs to output num_elements so for example:

model.add(RepeatVector(num_outputs))
# Optional layers can be inserted here
model.add(Dense(num_elements))

In a nutshell, repeat vectors copy the content of the previous layer to be sent to further nodes, and each path will output an array of num_elements, giving you the desired output shape. Note that your training data (labels) has to consist of shape (2, 2) arrays also.



来源:https://stackoverflow.com/questions/41006159/how-to-train-network-with-2d-output-python-keras

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