# 07 tensorflow data statistics and tensor comparison

Posted by ericm on Tue, 09 Jun 2020 05:40:29 +0200

## data statistics

### 1. Vector norm

Vector norm is a measure of vector length, which can be extended to tensor.
In neural network, it is often used to express the weight and gradient of tensor. Common vector norms are:

• L1 norm, defined as the sum of the absolute values of all elements of vector 𝒙.
• L2 norm, defined as the sum of squares of all elements of vector 𝒙, and then the root sign.
• ∞ − norm (infinite norm), defined as the maximum absolute value of all elements of vector_.
In TensorFlow, you can use the tf.norm(x, ord) solves L1, L2, ∞ and other norms of tensors, where the parameter ord is specified as 1 and 2, L1 and L2 norms are calculated, and the parameter ord is specified as np.inf The ∞ - norm is calculated.
```import tensorflow as tf
import numpy as np

x = tf.ones([2,2])
L1 = tf.norm(x, ord=1)
L2 = tf.norm(x, ord=2)
inf = tf.norm(x, ord=np.inf)
print(L1)
print(L2)
print(inf)
```

Output results

```tf.Tensor(4.0, shape=(), dtype=float32)
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
```

### 2. Maximum, average, and

In TensorFlow, you can use the tf.reduce_max, tf.reduce_min, tf.reduce_mean, tf.reduce_sum function can solve the maximum, minimum, mean, and sum of tensors in a certain dimension, and can also find the global maximum, minimum, mean, and information.
First, create a tensor whose shape is [4,5].

```x = tf.random.normal([4, 5])
```

The tensor x is

```tf.Tensor(
[[ 0.7654102  -0.5100088   1.6638901  -0.3924462   2.3773618 ]
[-0.953946   -0.56649995 -0.88401294  0.31982812 -0.5179366 ]
[ 0.69028026  0.5659967   0.6052322  -1.5808563  -0.88485676]
[ 0.1018258   0.5862227   0.20678976  1.710149   -0.67545104]], shape=(4, 5), dtype=float32)
```
1. Maximum
```# Maximum
max_w = tf.reduce_max(x, axis=1)  # Count the maximum value on the second dimension (column)
min_w = tf.reduce_min(x, axis=1)  # Count the minimum value on the second dimension (column)
print(max_w)
print(min_w)
```

result

```tf.Tensor([2.3773618  0.31982812 0.69028026 1.710149  ], shape=(4,), dtype=float32)
tf.Tensor([-0.5100088  -0.953946   -1.5808563  -0.67545104], shape=(4,), dtype=float32)
```
1. mean value
```# mean value
mean_h = tf.reduce_mean(x, axis=0)  # Count the mean value of the first dimension (row)
print(mean_h)
```

result

```tf.Tensor([0.15089256 0.01892766 0.3979748  0.01416868 0.07477935], shape=(5,), dtype=float32)
```
1. and
```sum_h = tf.reduce_sum(x, axis=0)  # Count the sum on the first dimension (line)
print(sum_h)
```

result

```tf.Tensor([0.6035702  0.07571065 1.5918992  0.05667472 0.2991174 ], shape=(5,), dtype=float32)
```
1. Global maximum, mean, and
When the axis parameter is not specified, tf.reduce_ *The function solves the maximum, minimum, mean, and other data of the global element.

```max_all = tf.reduce_max(x)
min_all = tf.reduce_min(x)
mean_all = tf.reduce_mean(x)
print(max_all, min_all, mean_all)
```

result

```tf.Tensor(2.3773618, shape=(), dtype=float32)
tf.Tensor(-1.5808563, shape=(), dtype=float32)
tf.Tensor(0.13134858, shape=(), dtype=float32)
```

In addition, in TensorFlow, you can use the tf.argmax(x, axis) and tf.argmin(x, axis) gets the index number of the maximum and minimum value of X on axis.

```t = tf.argmax(x, axis=1)
print(t)
```

result

```tf.Tensor([4 3 0 3], shape=(4,), dtype=int64)
```

## Tensor comparison

In TensorFlow, you can use the tf.equal(a, b) (or tf.math.equal(a,b), they are equivalent) function to compare whether two tensors are equal. tf.equal(a, b) will compare the elements in two tensors, equal to True, not equal to False.

```import tensorflow as tf

a = tf.zeros([2, 2])
b = tf.ones([2, 2])
c = tf.zeros([2, 2])
print(tf.equal(a, b))
print(tf.equal(a, c))
```

result

```tf.Tensor(
[[False False]
[False False]], shape=(2, 2), dtype=bool)
tf.Tensor(
[[ True  True]
[ True  True]], shape=(2, 2), dtype=bool)
```

Topics: network