Handling variable output units in MLP? I am using Keras to build my architecture.

The regression problem I am trying to solve has outputs different for different training samples.

Suppose that I have very first two rows of y_train = [[16, 3], [6], ... ] corresponding to my very first two rows of X_train (input data). I would like to assign the output of the dense layer's unit to length of these y_train rows.

For example, for 1st training sample, I would like model.add(dense(2)) as y_train[0] has length 2, for 2nd training sample, I would like model.add(dense(1)) as y_train[0] has length 1, and so on.

I also think that I can call Lambda Layer instead of output dense layer and in that Lambda Layer I can wrap length of every row of y_train and assign it as output units by using output_shape argument of Lambda Layer. But, I don't know how can I access my y_train at training time of my model so that I can use y_train inside lambda layer to make this scenario happen either, nonetheless.

Can anybody help me on this variable length output problem?
If you want to use arbitrary length input and outputs, there are two common options: train a single model for each length or use a recurrent neural network (RNN) architecture. A non-RNN neural network cannot handle arbitrary length because the shape of the weights depend on the length of the input. From your example, it seems like you want to keep different copies of weights based on the length. You could do that, but in theory, it is the same as training separate models. The weights from one length will only be updated by inputs of the same length, so why not save yourself the hassle and have separate models? 

Although RNNs are typically used for natural language processing and understanding, it does not mean they cannot be used to solve other deep learning problems. Specifically for your problem, you should use a many-to-many architecture.



Source: http://karpathy.github.io/2015/05/21/rnn-effectiveness/

In Keras, you can use the LSTM layer and set the parameter return_sequences to True, which will return an output the same length as your input sequence. Documentation here: https://keras.io/layers/recurrent/#lstm.