Я нахожусь на tenorflow 2.1 и хочу создать модель, которая выводит как ее прогноз, так и градиенты с постоянным значением.
Input => Model => [Score, Gradients]
Мой код следующий, с base_model - простой моделью, делающей прогнозирование с плавающей точкой из изображения 128x128x3:
def GradNet(base_model):
input_shape = (128,128,3)
inputs = tf.keras.Input(input_shape)
with tf.GradientTape() as tape:
tape.watch(scoring_model.variables)
score = base_model(inputs)
loss = tf.keras.losses.mean_squared_error(123., score)
gradients = tape.gradient(loss, inputs)
model = tf.keras.Model(inputs=inputs, outputs=[score, gradients], name='GradNet')
return model
Однако я получаю следующую ошибку:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-46-566ab9eaafd1> in <module>
----> 1 grad_net = GradNet(model)
<ipython-input-45-adb556fb740b> in GradNet(scoring_model)
8 loss = tf.keras.losses.mean_squared_error(666., score)
9
---> 10 gradients = tape.gradient(loss, inputs)
11
12 model = tf.keras.Model(inputs=inputs, outputs=[score, gradients], name='GradNet')
~/.local/lib/python3.7/site-packages/tensorflow/python/eager/backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
1046 output_gradients=output_gradients,
1047 sources_raw=flat_sources_raw,
-> 1048 unconnected_gradients=unconnected_gradients)
1049
1050 if not self._persistent:
~/.local/lib/python3.7/site-packages/tensorflow/python/eager/imperative_grad.py in imperative_grad(tape, target, sources, output_gradients, sources_raw, unconnected_gradients)
75 output_gradients,
76 sources_raw,
---> 77 compat.as_str(unconnected_gradients.value))
~/.local/lib/python3.7/site-packages/tensorflow/python/eager/backprop.py in _gradient_function(op_name, attr_tuple, num_inputs, inputs, outputs, out_grads, skip_input_indices, forward_pass_name_scope)
155 gradient_name_scope = "gradient_tape/"
156 with ops.name_scope(gradient_name_scope):
--> 157 return grad_fn(mock_op, *out_grads)
158 else:
159 return grad_fn(mock_op, *out_grads)
~/.local/lib/python3.7/site-packages/tensorflow/python/ops/math_grad.py in _MeanGrad(op, grad)
249 def _MeanGrad(op, grad):
250 """Gradient for Mean."""
--> 251 sum_grad = _SumGrad(op, grad)[0]
252 input_shape = op.inputs[0]._shape_tuple() # pylint: disable=protected-access
253 output_shape = op.outputs[0]._shape_tuple() # pylint: disable=protected-access
~/.local/lib/python3.7/site-packages/tensorflow/python/ops/math_grad.py in _SumGrad(op, grad)
209 # more sense.
210 output_shape_kept_dims = math_ops.reduced_shape(input_shape,
--> 211 op.inputs[1])
212 grad = array_ops.reshape(grad, output_shape_kept_dims)
213 return [array_ops.broadcast_to(grad, input_shape), None]
~/.local/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in reduced_shape(input_shape, axes)
3754 """
3755 if context.executing_eagerly():
-> 3756 input_shape = input_shape.numpy()
3757 axes = axes.numpy()
3758 input_shape[axes] = 1
AttributeError: 'Tensor' object has no attribute 'numpy'
Не понимаю, почему это невозможно,
Заранее спасибо