Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of np.std() in TensorFlow?

Just looking for the equivalent of np.std() in TensorFlow to calculate the standard deviation of a tensor.

like image 438
Kevin Avatar asked Sep 06 '16 17:09

Kevin


People also ask

Can I use NumPy in TensorFlow?

TensorFlow implements a subset of the NumPy API, available as tf. experimental. numpy . This allows running NumPy code, accelerated by TensorFlow, while also allowing access to all of TensorFlow's APIs.

How do you find the standard deviation of a tensor?

The standard deviation of a tensor is computed using torch. std(). It returns the standard deviation of all the elements in the tensor. Like mean, we can also compute the standard deviation, row or column-wise.

How do you convert NP array to TF tensor?

a NumPy array is created by using the np. array() method. The NumPy array is converted to tensor by using tf. convert_to_tensor() method.


2 Answers

To get the mean and variance just use tf.nn.moments.

mean, var = tf.nn.moments(x, axes=[1]) 

For more on tf.nn.moments params see docs

like image 142
Steven Avatar answered Sep 22 '22 07:09

Steven


You can also use reduce_std in the following code adapted from Keras:

#coding=utf-8 import numpy as np import tensorflow as tf  def reduce_var(x, axis=None, keepdims=False):     """Variance of a tensor, alongside the specified axis.      # Arguments         x: A tensor or variable.         axis: An integer, the axis to compute the variance.         keepdims: A boolean, whether to keep the dimensions or not.             If `keepdims` is `False`, the rank of the tensor is reduced             by 1. If `keepdims` is `True`,             the reduced dimension is retained with length 1.      # Returns         A tensor with the variance of elements of `x`.     """     m = tf.reduce_mean(x, axis=axis, keep_dims=True)     devs_squared = tf.square(x - m)     return tf.reduce_mean(devs_squared, axis=axis, keep_dims=keepdims)  def reduce_std(x, axis=None, keepdims=False):     """Standard deviation of a tensor, alongside the specified axis.      # Arguments         x: A tensor or variable.         axis: An integer, the axis to compute the standard deviation.         keepdims: A boolean, whether to keep the dimensions or not.             If `keepdims` is `False`, the rank of the tensor is reduced             by 1. If `keepdims` is `True`,             the reduced dimension is retained with length 1.      # Returns         A tensor with the standard deviation of elements of `x`.     """     return tf.sqrt(reduce_var(x, axis=axis, keepdims=keepdims))  if __name__ == '__main__':     x_np = np.arange(10).reshape(2, 5).astype(np.float32)     x_tf = tf.constant(x_np)     with tf.Session() as sess:         print(sess.run(reduce_std(x_tf, keepdims=True)))         print(sess.run(reduce_std(x_tf, axis=0, keepdims=True)))         print(sess.run(reduce_std(x_tf, axis=1, keepdims=True)))     print(np.std(x_np, keepdims=True))     print(np.std(x_np, axis=0, keepdims=True))     print(np.std(x_np, axis=1, keepdims=True)) 
like image 39
Kai Kang Avatar answered Sep 22 '22 07:09

Kai Kang