tf.math.reduce_mean和tf.keras.metrics.Mean的区别是什么,区别在于记不记忆之前的状态

今晚在学tensorflow的API的时候,看到reduce_mean,于是心生活意——为何不直接用mean呢?

可能这个和mean有区别?

查询官网后,发现了 tf.math.reduce_mean和tf.keras.metrics.Mean这两种类。

google后,发现stackoverflow中有人已经问了这个问题,第一个回答如下,其实并没有回答到重点。

Functionality of tf.keras.metrics.Mean and tf.math.reduce_mean are slightly different. Look at the example

#tf.keras.metrics.Mean: CASE1

import tensorflow as tf
x = tf.constant([[1, 3, 5, 7],[1, 1, 0, 0]])
m = tf.keras.metrics.Mean()
m.update_state(x)
m.result().numpy()

Output:

1.886

#tf.keras.metrics.Mean: CASE2

m.reset_state()
m.update_state([1, 3, 5, 7], sample_weight=[1, 1, 0, 0])
m.result().numpy()

Output:

2.0

#tf.math.reduce_mean

#CASE1

y = tf.reduce_mean(x)

Output:

tf.Tensor(2, shape=(), dtype=int32)

#CASE2

y = tf.reduce_mean(x1,1)

Output:

tf.Tensor([4 0], shape=(2,), dtype=int32)

#CASE3

y = tf.reduce_mean(x1,0)

Output:

tf.Tensor([1 2 2 3], shape=(4,), dtype=int32)

In case of tf.math.reduce_mean, you see that when axis(numpy) is 1, it computes mean across (1, 3, 5, 7) and (1,1,0,0), so 1 defines across which axis the mean is computed. When it is 0, the mean is computed across(1,1),(3,1),(5,0) and (7,0), and so on.

然后我又做了一份中文版的回答,为啥不用英文去回答呢?因为想让老外多认识一下中华文化。

我可以告诉你。

对于tf.keras.metrics.Mean,如果通过update_state()方法来更新,并且没有用reset_state()重置状态,就会把之前输入的参数包括进去。

比如

m = tf.keras.metrics.Mean()
m.update_state([1, 3, 5, 7])
m.result().numpy()

# m.reset_state()
m.update_state([8])
print(m.result().numpy())
print((1+3+5+7+8) / 5)

输出结果为:

4.8

4.8

但是对于tf.math.reduce_mean,并不存在记忆之前参数的概念。

链接如下:

 python - What's the difference between tf.math.reduce_mean and tf.keras.metrics.Mean? - Stack Overflow

另外拓展一下,在metrics模块下的很多方法和类,计算时都会受到之前状态的影响,

但是对于loss模块或math模块,每一次的计算都是独立不受上一次计算的影响的! 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
During training, Dropout layers are used to randomly drop out some of the neurons in the network, which helps to prevent overfitting and improve generalization performance. However, during prediction, we don't want to randomly drop out neurons because we want to make a deterministic prediction. To use Monte Carlo Dropout for making predictions, we need to modify the model by applying Dropout layers at prediction time. This can be done by setting the Dropout probability to zero during prediction, effectively deactivating the Dropout layer. Then, we can run the model multiple times with different random Dropout masks to obtain a distribution of predictions, which can be used to estimate the uncertainty of the predictions. In TensorFlow, we can achieve Monte Carlo Dropout by creating a new model that is identical to the original model, but with the Dropout layers modified to have a different behavior during prediction. This can be done by creating a custom Dropout layer that overrides the `call()` method to apply the Dropout probability only during training, and to deactivate the Dropout layer during prediction. The modified model can then be used to make predictions by running it multiple times with different random Dropout masks. Here is an example of how to implement Monte Carlo Dropout in TensorFlow: ``` import tensorflow as tf # Define custom Dropout layer for Monte Carlo Dropout class MonteCarloDropout(tf.keras.layers.Dropout): def call(self, inputs): if not self.training: return inputs return super().call(inputs) # Define original model with Dropout layers model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Dropout(0.2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(10) ]) # Create modified model with Monte Carlo Dropout mc_model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D((2, 2)), MonteCarloDropout(0.2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), MonteCarloDropout(0.5), tf.keras.layers.Dense(10) ]) # Train original model with Dropout layers model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) model.fit(train_images, train_labels, epochs=10) # Use Monte Carlo Dropout to make predictions with modified model predictions = [] for i in range(100): predictions.append(mc_model.predict(test_images, training=True)) predictions = tf.stack(predictions) mean_prediction = tf.math.reduce_mean(predictions, axis=0) var_prediction = tf.math.reduce_variance(predictions, axis=0) ``` In this example, we define a custom Dropout layer `MonteCarloDropout` that overrides the `call()` method to deactivate the Dropout layer during prediction. We then create a modified model `mc_model` that is identical to the original model, but with the Dropout layers replaced by `MonteCarloDropout` layers. We train the original model with Dropout layers using the `fit()` method. To make predictions with Monte Carlo Dropout, we run the modified model `mc_model` multiple times with different random Dropout masks by setting the `training` argument to `True`. We then stack the predictions into a tensor and compute the mean and variance of the predictions across the different runs. The mean prediction represents the estimated class probabilities, while the variance represents the uncertainty of the predictions.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值