修正iterate = K.function([model.input], [loss, grads])

原始程式碼:
iterate = K.function([model.input], [loss, grads])

# 測試一下
import numpy as np
loss_value, grads_value = iterate([np.zeros((1, 150, 150, 3))])

出現錯誤
ValueError                                Traceback (most recent call last)
Cell In[28], line 1
----> 1 iterate = K.function([model.input], [loss, grads])
      3 # 測試一下
      4 import numpy as np

File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\backend.py:4656, in function(inputs, outputs, updates, name, **kwargs)
   4650     raise ValueError(
   4651         "`updates` argument is not supported during "
   4652         "eager execution. You passed: %s" % (updates,)
   4653     )
   4654 from keras.src import models
-> 4656 model = models.Model(inputs=inputs, outputs=outputs)
   4658 wrap_outputs = isinstance(outputs, list) and len(outputs) == 1
   4660 def func(model_inputs):

File C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\trackable\base.py:204, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
    202 self._self_setattr_tracking = False  # pylint: disable=protected-access
    203 try:
--> 204   result = method(self, *args, **kwargs)
    205 finally:
    206   self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\engine\functional.py:167, in Functional.__init__(self, inputs, outputs, name, trainable, **kwargs)
    158     if not all(
    159         [
    160             functional_utils.is_input_keras_tensor(t)
    161             for t in tf.nest.flatten(inputs)
    162         ]
    163     ):
    164         inputs, outputs = functional_utils.clone_graph_nodes(
    165             inputs, outputs
    166         )
--> 167 self._init_graph_network(inputs, outputs)

File C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\trackable\base.py:204, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
    202 self._self_setattr_tracking = False  # pylint: disable=protected-access
    203 try:
--> 204   result = method(self, *args, **kwargs)
    205 finally:
    206   self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\engine\functional.py:209, in Functional._init_graph_network(self, inputs, outputs)
    204     if any(
    205         not hasattr(tensor, "_keras_history") for tensor in self.outputs
    206     ):
    207         base_layer_utils.create_keras_history(self._nested_outputs)
--> 209 self._validate_graph_inputs_and_outputs()
    211 # A Network does not create weights of its own, thus it is already
    212 # built.
    213 self.built = True

File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\engine\functional.py:872, in Functional._validate_graph_inputs_and_outputs(self)
    870 if not hasattr(x, "_keras_history"):
    871     cls_name = self.__class__.__name__
--> 872     raise ValueError(
    873         f"Output tensors of a {cls_name} model must be "
    874         "the output of a TensorFlow `Layer` "
    875         f"(thus holding past layer metadata). Found: {x}"
    876     )

ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: 0.11414399743080139

修正成這樣就好了
import numpy as np
import tensorflow as tf
from tensorflow.keras import optimizers

# The rest of your code remains the same

# Compile your model before using it in the gradient computation
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(learning_rate=1e-5),
              metrics=['acc'])

# Create a custom function to compute loss and gradients
@tf.function
def compute_loss_and_gradients(inputs):
    with tf.GradientTape() as tape:
        predictions = model(inputs)
        loss = tf.reduce_mean(predictions)
    gradients = tape.gradient(loss, inputs)
    return loss, gradients

# Dummy input for example gradient computation (replace with actual input)
input_height = 128  # Replace with the height of your input images
input_width = 128   # Replace with the width of your input images
input_channels = 3  # Replace with the number of channels in your input images
dummy_input = np.random.random((1, input_height, input_width, input_channels))

# Compute the loss and gradients using the custom function
loss_value, grads_value = compute_loss_and_gradients(tf.convert_to_tensor(dummy_input))

# Now you can use the loss_value and grads_value for further processing or analysis

评论

此博客中的热门博文

修正input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128.

緣起