6

Like stated in the title, I was wondering as to how to have the custom layer returning multiple tensors: out1, out2,...outn?
I tried

keras.backend.concatenate([out1, out2], axis = 1)

But this does only work for tensors having the same length, and it has to be another solution rather than concatenating two by two tensors every time, is it?

Tassou
  • 439
  • 1
  • 5
  • 16
  • Have you looked at https://keras.io/models/model/. Using the keras Model API rather than Sequential, will allow you to define multiple outputs. If this isn't what you're looking for you probably need to provide more description. – bivouac0 Jan 10 '18 at 19:42

1 Answers1

4

In the call method of your layer, where you perform the layer calculations, you can return a list of tensors:

def call(self, inputTensor):

    #calculations with inputTensor and the weights you defined in "build"
    #inputTensor may be a single tensor or a list of tensors

    #output can also be a single tensor or a list of tensors
    return [output1,output2,output3]

Take care of the output shapes:

def compute_output_shape(self,inputShape):

    #calculate shapes from input shape    
    return [shape1,shape2,shape3]

The result of using the layer is a list of tensors. Naturally, some kinds of keras layers accept lists as inputs, others don't.
You have to manage the outputs properly using a functional API Model. You're probably going to have problems using a Sequential model while having multiple outputs.

I tested this code on my machine (Keras 2.0.8) and it works perfectly:

from keras.layers import *
from keras.models import *
import numpy as np

class Lay(Layer):
    def init(self):
        super(Lay,self).__init__()

    def build(self,inputShape):
        super(Lay,self).build(inputShape)

    def call(self,x):
        return [x[:,:1],x[:,-1:]]

    def compute_output_shape(self,inputShape):
        return [(None,1),(None,1)]


inp = Input((2,))
out = Lay()(inp)
print(type(out))

out = Concatenate()(out)
model = Model(inp,out)
model.summary()

data = np.array([[1,2],[3,4],[5,6]])
print(model.predict(data))

import keras
print(keras.__version__)
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • when I try this, I get : ValueError: Unexpectedly found an instance of type ``. Expected a symbolic tensor instance. – Tassou Jan 10 '18 at 20:14
  • Are you using a `Sequential` model? What is your keras version? What layer you put after this one? – Daniel Möller Jan 10 '18 at 20:16
  • If you'd share more of the stack trace (not only the message, but the function where this message appears and the file), it might be easier to find the issue. – Daniel Möller Jan 10 '18 at 20:25
  • I am using keras version 2.1.2. I just worked around it with some Keras.backend.stack() lines – Tassou Jan 10 '18 at 21:33