Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple input #31

Closed
HzFu opened this issue Dec 5, 2016 · 7 comments
Closed

Multiple input #31

HzFu opened this issue Dec 5, 2016 · 7 comments

Comments

@HzFu
Copy link

HzFu commented Dec 5, 2016

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

@zsdonghao
Copy link
Member

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
Copy link
Author

HzFu commented Dec 5, 2016

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.

@zsdonghao
Copy link
Member

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

@xjtuljy
Copy link

xjtuljy commented Feb 9, 2017

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

@zsdonghao
Copy link
Member

@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]

@xjtuljy
Copy link

xjtuljy commented Feb 9, 2017

OK I see, thanks!

@changyakun
Copy link

thank you very much!!!

zsdonghao pushed a commit that referenced this issue May 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants