0

我正在尝试向我的 keras 模型(y1,y2)的输出添加一个函数(有趣)以创建另一个 keras 模型

这是导致上述错误的代码:

    i = tf.constant(0)
    output2 = tf.Variable([])

    def loop_body(i, output2):
        x = fun(y1[i],y2[i])
        output2 = tf.concat([output2, x], 0)
        return i + 1, output2

    c = lambda i, output2: i < 4

    index2, output2 = tf.while_loop(c, loop_body, [i, output2], shape_invariants=[i.get_shape(), tf.TensorShape([None])])

    return tf.keras.Model(inputs, output2)


模型已经 y1,y2 对应这个函数中的 y1,y2 :https ://github.com/qqwweee/keras-yolo3/blob/master/yolo3/model.py#L89

假设在此模型上运行 predict_on_batch 时我的输入批量大小中有 4 个样本,我循环了 4 次。我仍然必须想办法为 y1.shape[0] 或 y2.shape[0] 放置占位符

以下是完整的错误日志:

    return tf.keras.Model(inputs, output2)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 147, in __init__
    super(Model, self).__init__(*args, **kwargs)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 164, in __init__
    self._init_graph_network(*args, **kwargs)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 267, in _init_graph_network
    base_layer_utils.create_keras_history(self._nested_outputs)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer_utils.py", line 184, in create_keras_history
    _, created_layers = _create_keras_history_helper(tensors, set(), [])
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer_utils.py", line 231, in _create_keras_history_helper
    layer_inputs, processed_ops, created_layers)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer_utils.py", line 231, in _create_keras_history_helper
    layer_inputs, processed_ops, created_layers)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer_utils.py", line 231, in _create_keras_history_helper
    layer_inputs, processed_ops, created_layers)
  [Previous line repeated 946 more times]
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer_utils.py", line 229, in _create_keras_history_helper
    constants[i] = backend.function([], op_input)([])
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3678, in function
    return GraphExecutionFunction(inputs, outputs, updates=updates, **kwargs)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3339, in __init__
    self.updates_op = control_flow_ops.group(*updates_ops)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\ops\control_flow_ops.py", line 2878, in group
    return no_op(name=name)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\ops\gen_control_flow_ops.py", line 584, in no_op
    "NoOp", name=name)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 480, in new_func
    named_args = tf_inspect.getcallargs(func, *args, **kwargs)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\util\tf_inspect.py", line 278, in getcallargs
    argspec = getfullargspec(func)
  File "my_path\.venv\lib\site-packages\tensorflow_core\python\util\tf_inspect.py", line 257, in getfullargspec
    return _getfullargspec(target)
  File "my_user\.pyenv\pyenv-win\versions\3.7.8\lib\inspect.py", line 1132, in getfullargspec
    raise TypeError('unsupported callable') from ex


4

0 回答 0