问题
So this problem occurs for in the context of a larger project, but I've assembled a minimal working example. Consider the following:
input_1 = Input((5,))
hidden_a = Dense(2)(input_1)
hidden_b = Dense(2)(input_1)
m1 = Model(input_1, [hidden_a, hidden_b])
input_2 = Input((2,))
output = Dense(1)(input_2)
m2 = Model(input_2, output)
m3 = Model(input_1, m2(m1(input_1)[0]))
print(m3.summary())
m3.compile(optimizer='adam', loss='mse')
x = np.random.random(size=(10,5))
y = np.random.random(size=(10,1))
m3.fit(x,y)
My expectation is that when evaluating this network, the output of hidden_b will simply be discarded and I'll effectively have a simple feed-forward neural network that goes input_1 -> hidden_a -> input_2 -> output. Instead, I get a cryptic error:
Traceback (most recent call last):
File "test.py", line 37, in <module>
m3.fit(x,y)
File "/home/thomas/.local/lib/python3.5/site-packages/keras/engine/training.py", line 1013, in fit
self._make_train_function()
File "/home/thomas/.local/lib/python3.5/site-packages/keras/engine/training.py", line 497, in _make_train_function
loss=self.total_loss)
File "/home/thomas/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/thomas/.local/lib/python3.5/site-packages/keras/optimizers.py", line 445, in get_updates
grads = self.get_gradients(loss, params)
File "/home/thomas/.local/lib/python3.5/site-packages/keras/optimizers.py", line 80, in get_gradients
raise ValueError('An operation has `None` for gradient. '
ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
Any idea what might be causing this? Thanks!
Update: If passing input_1 to m1 is the problem, then why does this work?
input_1 = Input((5,))
hidden_a = Dense(2)(input_1)
hidden_b = Dense(2)(input_1)
def sampling (args):
hidden_a, hidden_b = args
return hidden_a + hidden_b
z = Lambda(sampling)([hidden_a, hidden_b])
m1 = Model(input_1, [hidden_a, hidden_b, z])
input_2 = Input((2,))
output = Dense(1)(input_2)
m2 = Model(input_2, output)
m3 = Model(input_1, m2(m1(input_1)[2]))
m3.compile(optimizer='adam', loss='mse')
x = np.random.random(size=(10,5))
y = np.random.random(size=(10,1))
m3.fit(x,y)
回答1:
You're passing an input to model 1 that is already the input of model 1.
m3 = Model(input_1, m2(m1.outputs[0]))
来源:https://stackoverflow.com/questions/51215838/why-when-using-this-simple-model-with-multiple-outputs-does-keras-complain-about