tensorlayer/TensorLayer

Multiple input

HzFu opened this issue · 7 comments

HzFu commented

Can the tensorlayer support multi-input and multi-output models ?
Is there any multi-input demo or example?

you may want to use ConcatLayer for multi-input, check this.

>>> x = tf.placeholder(tf.float32, shape=[None, 784])
>>> inputs = tl.layers.InputLayer(x, name='input_layer')
>>> x2 = tf.placeholder(tf.float32, shape=[None, 784])
>>> inputs2 = tl.layers.InputLayer(x, name='input_layer2')
>>> net1 = tl.layers.DenseLayer(inputs, n_units=800, act = tf.nn.relu, name='relu1_1')
>>> net2 = tl.layers.DenseLayer(inputs2, n_units=300, act = tf.nn.relu, name='relu2_1')
>>> network = tl.layers.ConcatLayer(layer = [net1, net2], name ='concat_layer')

for multi-output, just simply reuse a layer.

>>> net1 = tl.layers.DenseLayer(inputs, n_units=800, act = tf.nn.relu, name='relu1_1')
>>> net_out1 = tl.layers.DenseLayer(net1, n_units=800, name='out1')
>>> net_out2 = tl.layers.DenseLayer(net1, n_units=800, name='out2')
HzFu commented

I means how to input two different data.
For example, for a RGB-D image classification network, I want to input RGB image and its depth map to the separate layers.

@HzFu you can do exactly like the script I send you.

I wonder can we use tl.iterate.minibatches to generate "feed_dict" to feed multiple inputs into the session? I tried but it seems that minibatches() only takes 4 augments, which mean single input

@xjtuljy in that case, you may need to extend minibatches.

def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
    """Generate a generator that input a group of example in numpy.array and
    their labels, return the examples and labels by the given batchsize.
    Parameters
    ----------
    inputs : numpy.array
        (X) The input features, every row is a example.
    targets : numpy.array
        (y) The labels of inputs, every row is a example.
    batch_size : int
        The batch size.
    shuffle : boolean
        Indicating whether to use a shuffling queue, shuffle the dataset before return.
    Examples
    --------
    >>> X = np.asarray([['a','a'], ['b','b'], ['c','c'], ['d','d'], ['e','e'], ['f','f']])
    >>> y = np.asarray([0,1,2,3,4,5])
    >>> for batch in tl.iterate.minibatches(inputs=X, targets=y, batch_size=2, shuffle=False):
    >>>     print(batch)
    ... (array([['a', 'a'],
    ...        ['b', 'b']],
    ...         dtype='<U1'), array([0, 1]))
    ... (array([['c', 'c'],
    ...        ['d', 'd']],
    ...         dtype='<U1'), array([2, 3]))
    ... (array([['e', 'e'],
    ...        ['f', 'f']],
    ...         dtype='<U1'), array([4, 5]))
    """
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt], targets[excerpt]

OK I see, thanks!

thank you very much!!!