Issue 1:
Please go to Stack Overflow for help and support:

https://stackoverflow.com/questions/tagged/keras

If you open a GitHub issue, here is our policy:

It must be a bug, a feature request, or a significant problem with the

documentation (for small docs fixes please send a PR instead).

The form below must be filled out.

Here's why we have that policy: Keras developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.

System information

Have I written custom code (as opposed to using a stock example script

provided in Keras):

OS Platform and Distribution (e.g., Linux Ubuntu 16.04):

TensorFlow installed from (source or binary):

TensorFlow version (use command below):

Python version:

Bazel version (if compiling from source):

GPU model and memory:

Exact command to reproduce:

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with:

python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the problem

Describe the problem clearly here. Be sure to convey here why it's a bug in Keras or why the requested feature is needed.

Source code / logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

Issue 2:
Currently, Keras builds the computational graph in a top-down fashion. That is, starting from the final output tensor and recursively calling get_output() until reaching the input. This is fine for sequential models, since layers are stacked linearly and the computational flow is a straight line. However, for graph models, the computational flow could have branches, and this top-down fashion will incur redundancy because self.get_output() will "repeat" the downstream computations at the branching point. Consider the following example:

X = T.vector('X')
B = X * 3
Z1 = B + 1
Z2 = B + 2
Y = Z1 + Z2
theano.printing.pydotprint(Y)

The graph looks like this (as one can imagine)

When implemented in Keras:

# helper layer
class LambdaLayer(Layer):
def __init__(self, func):
super(LambdaLayer, self).__init__()
self.func = func

def get_output(self, train=False):
X = self.get_input(train)
return self.func(X)

g = Graph()
g.add_input('X', ndim=1)

mul = LambdaLayer(lambda x: x * 3)
g.add_node(mul, name='mul', input='X')
add1 = LambdaLayer(lambda x: x + 1)
add2 = LambdaLayer(lambda x: x + 2)
add3 = LambdaLayer(lambda x: x['1'] + x['2'])

g.add_node(add1, name='add1', input='mul')
g.add_node(add2, name='add2', input='mul')
# I modified Keras to support multiple inputs, no Merge layer required here
g.add_node(add3, name='add3', inputs={'1': 'add1', '2': 'add2'}, create_output=True)

theano.printing.pydotprint(g.get_output())

The graph looks like this. X * 3 repeats twice.

Of cause we can count on Theano's optimizer to reduce the redundancy, but when it comes to large graph models with complicated wiring. The optimization will take quite a long time (Actually I have a graph model with lots of branching and it takes 8 hours to compile). Does it seem better to build computational graph in bottom-up fashion?