Keras: Graph disconnected error when using merge()

Created on 1 Jul 2016  路  3Comments  路  Source: keras-team/keras

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

a = Input((10,))
b = Input((10,))

a = Dense(10)(a)  # Commenting this line makes it work

c = merge([a, b], mode='sum')

model = Model(input=[a,b], output=c)
model.compile(loss='mse', optimizer='sgd')

x1 = np.random.random((100, 10))
x2 = np.random.random((100, 10))
y = np.random.random((100, 10))

model.fit([x1, x2], y)
C:\Python27\lib\site-packages\keras-1.0.5-py2.7.egg\keras\engine\topology.py:158
9: UserWarning: Model inputs must come from a Keras Input layer, they cannot be
the output of a previous non-Input layer. Here, a tensor specified as input to "
model_1" was not an Input tensor, it was generated by layer dense_1.
Note that input tensors are instantiated via `tensor = Input(shape)`.
The tensor that caused the issue was: None
  str(x.name))
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    model = Model(input=[a,b], output=c)
  File "C:\Python27\lib\site-packages\keras-1.0.5-py2.7.egg\keras\engine\topolog
y.py", line 1758, in __init__
    str(layers_with_complete_input))
Exception: Graph disconnected: cannot obtain value for tensor input_1 at layer "
input_1". The following previous layers were accessed without issue: []

Most helpful comment

Oh!! My bad. I have to get more sleep.

All 3 comments

You have a naming collision there, you're losing the reference to your input tensor a.

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

a = Input((10,))
b = Input((10,))

d = Dense(10)(a)  # do not replace the variable referencing the input tensor

c = merge([d, b], mode='sum')

model = Model(input=[a,b], output=c)
model.compile(loss='mse', optimizer='sgd')

x1 = np.random.random((100, 10))
x2 = np.random.random((100, 10))
y = np.random.random((100, 10))

model.fit([x1, x2], y)

Oh!! My bad. I have to get more sleep.

what finally helped me was to move the 'b' Input right before where it is needed. if I do that, I don't need to rename the other layers:
a = Input((10,))
a = Dense(10)(a)
b = Input((10,))
c = merge([a, b], mode='sum')

Was this page helpful?
0 / 5 - 0 ratings