解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘

简介: 解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘

2022-01-02 18:41:16.826148: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Traceback (most recent call last):
  File "E:/Code/PyCharm/TensorFlow学习/Keras/自定义评估指标.py", line 59, in <module>
    validation_split=0.2
  File "D:\Anaconda\lib\site-packages\keras\engine\training.py", line 1184, in fit
    tmp_logs = self.train_function(iterator)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 885, in __call__
    result = self._call(*args, **kwds)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 760, in _initialize
    *args, **kwds))
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 3066, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 3463, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 3308, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "D:\Anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1007, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 668, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py", line 994, in wrapper
    raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
    D:\Anaconda\lib\site-packages\keras\engine\training.py:853 train_function  *
        return step_function(self, iterator)
    TypeError: tf__update_state() got an unexpected keyword argument 'sample_weight'

问题原因:

使用TensorFlow实现一些自定义层或者指标等,在一些需要实现的函数参数没有加上默认参数,导致模型训练时传递参数出现问题

解决办法:

在指定地方添加上默认参数

def update_state(self, y_true, y_pred, sample_weight=None):
        y_pred = tf.reshape(tf.argmax(y_pred, axis=1), shape=(-1, 1))
        values = tf.cast(y_true, 'int32') == tf.cast(y_pred, 'int32')
        values = tf.cast(values, 'float32')
        self.true_positives.assign_add(tf.reduce_sum(values))


目录
相关文章
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
2982 0
|
5月前
|
机器学习/深度学习 Python
【Python】已解决TypeError: init() got an unexpected keyword argument ‘threshold’
【Python】已解决TypeError: init() got an unexpected keyword argument ‘threshold’
233 0
|
Python
Python报错ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Python报错ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
1708 1
|
算法框架/工具
Keras报错:TypeError: (‘Keyword argument not understood:‘, ‘offset‘)
Keras报错:TypeError: (‘Keyword argument not understood:‘, ‘offset‘)
202 0
成功解决ValueError: Found input variables with inconsistent numbers of samples: [86, 891]
成功解决ValueError: Found input variables with inconsistent numbers of samples: [86, 891]
|
机器学习/深度学习 Windows
raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}‘.format( RuntimeError: Error(s)..报错
即load_state_dict(fsd,strict=False) 属性strict;当strict=True,要求预训练练权重层数的键值与新构建的模型中的权重层数名称完全吻合;
1568 0
ValueError: not enough values to unpack (expected 3, got 2)
这个错误通常是因为在解包(unpacking)元组(tuple)时,元组中的元素数量与期望不符,导致无法将所有元素正确解包。 例如,在以下代码中,元组中只有两个元素,但我们尝试将其解包为三个变量:
653 0
解决办法:RuntimeError: dictionary changed size during iteration
解决办法:RuntimeError: dictionary changed size during iteration
153 0