Tensorflow 根据Keras的规则手册';文档
我找到了关于它的两个主要来源Tensorflow 根据Keras的规则手册';文档,tensorflow,keras,metrics,Tensorflow,Keras,Metrics,我找到了关于它的两个主要来源 ,未按照规则手册进行(我宁愿避免) (我更喜欢避免意外) 我更喜欢遵循Keras的文档,以避免内存泄漏,因为这是一些使用Keras的人的情况 但Keras在文件中展示的是关于分类的。这不是我的情况。 所以我试着看一下Keras的源代码。确切地说是在文件中:/lib/python3.7/site packages/tensorflow\u core/python/keras/metrics.py。这对我一点帮助都没有,因为大多数度量(一些例外是分类度量)都是使用包装器
但Keras在文件中展示的是关于分类的。这不是我的情况。
所以我试着看一下Keras的源代码。确切地说是在文件中:
/lib/python3.7/site packages/tensorflow\u core/python/keras/metrics.py
。这对我一点帮助都没有,因为大多数度量(一些例外是分类度量)都是使用包装器完成的,如下代码所示:
@keras_export('keras.metrics.MeanSquaredError')
class MeanSquaredError(MeanMetricWrapper):
"""Computes the mean squared error between `y_true` and `y_pred`.
For example, if `y_true` is [0., 0., 1., 1.], and `y_pred` is [1., 1., 1., 0.]
the mean squared error is 3/4 (0.75).
Usage:
```python
m = tf.keras.metrics.MeanSquaredError()
m.update_state([0., 0., 1., 1.], [1., 1., 1., 0.])
print('Final result: ', m.result().numpy()) # Final result: 0.75
```
Usage with tf.keras API:
```python
model = tf.keras.Model(inputs, outputs)
model.compile('sgd', metrics=[tf.keras.metrics.MeanSquaredError()])
```
"""
def __init__(self, name='mean_squared_error', dtype=None):
super(MeanSquaredError, self).__init__(
mean_squared_error, name, dtype=dtype)
正如您所看到的,只有构造函数方法,对于我需要的udpate\u state
方法没有很好的灵感我在哪里能找到它
python 3.7.7
tensorflow 2.1.0
keras应用1.0.8
keras preprocessing 1.1.0您可以使用损失函数作为度量,因此可以扩展
keras.loss.loss
。您只需覆盖调用
,如中所示
import tensorflow as tf
class MeanSquaredError(tf.keras.losses.Loss):
def call(self, y_true, y_pred):
y_true = tf.cast(y_true, y_pred.dtype)
return tf.math.reduce_mean(tf.math.square(y_pred - y_true), axis=-1)