Table Of Contents

Aggregating Tensors

In [31]:
import tensorflow as tf
import numpy as np
In [32]:
arr = [-7,-10]
tA = tf.constant(arr)
tA
Out [32]:
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([ -7, -10], dtype=int32)>
In [33]:
# 
# get absolute value of tensor values
# 
tf.abs(tA)
Out [33]:
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([ 7, 10], dtype=int32)>

Aggregation Forms

  • minimum
  • maximum
  • mean
  • sum
  • std deviation
  • variance
  • index of minimum value
  • index of maximum value
  • squeezing: removing all single-dimensions
In [34]:
# Create a tensor filled with random vals
howManyVals = 50
minTensorVal = 0
maxTensorVal = 100

randomNums = np.random.randint(minTensorVal, maxTensorVal, size=howManyVals)
tensorRandom = tf.constant(randomNums)
tensorFloat32 = tf.cast(tensorRandom, dtype=tf.float32)

randomNums, tensorRandom
Out [34]:
(array([72, 77, 73, 82, 29, 85, 25, 21, 25, 87, 70, 51, 19, 48, 55,  7, 66,
        46, 79, 55, 28, 10, 31, 34, 52,  4,  3, 57, 66,  2, 39, 20, 81, 99,
        88, 17, 21, 98, 35,  3, 19, 28, 19, 34, 53, 77, 43,  7, 89, 44]),
 <tf.Tensor: shape=(50,), dtype=int64, numpy=
 array([72, 77, 73, 82, 29, 85, 25, 21, 25, 87, 70, 51, 19, 48, 55,  7, 66,
        46, 79, 55, 28, 10, 31, 34, 52,  4,  3, 57, 66,  2, 39, 20, 81, 99,
        88, 17, 21, 98, 35,  3, 19, 28, 19, 34, 53, 77, 43,  7, 89, 44])>)
In [38]:
# 
# minimum
# 
tensorMin = tf.reduce_min(tensorRandom)


# 
# maximum
# 
tensorMax = tf.reduce_max(tensorRandom)

# 
# mean
# 
tensorMean = tf.reduce_mean(tensorRandom)

# 
# sum
# 
tensorSum = tf.reduce_sum(tensorRandom)

# 
# std deviation
# 
tensorStdDev = tf.math.reduce_std(tensorFloat32)

# 
# variance
# 
tensorVariance = tf.math.reduce_variance(tensorFloat32)

# 
# positional min
# 
tensorMinIdx = tf.argmin(tensorRandom)

# 
# positional max
# 
tensorMaxIdx = tf.argmax(tensorRandom)

print(f'min: {tensorMin}')
print(f'max: {tensorMax}')
print(f'mean: {tensorMean}')
print(f'sum: {tensorSum}')
print(f'stdDev: {tensorStdDev}')
print(f'variance: {tensorVariance}')
print(f'min: val:{tensorRandom[tensorMinIdx]} -- idx:{tensorMinIdx}')
print(f'max: val:{tensorRandom[tensorMaxIdx]} -- idx:{tensorMaxIdx}')
min: 2
max: 99
mean: 45
sum: 2273
stdDev: 28.36491584777832
variance: 804.5684204101562
min: val:2 -- idx:29
max: val:99 -- idx:33

Squeezing

In [43]:
# 
# create a tensor filled with random numbers
# 
newRandomNums = tf.random.uniform(shape=[50])
randomTensor = tf.constant(newRandomNums, shape=(1,1,1,1,50))

# 
# 
# 
randomTensor,tf.squeeze(randomTensor)
Out [43]:
(<tf.Tensor: shape=(1, 1, 1, 1, 50), dtype=float32, numpy=
 array([[[[[0.56682   , 0.37258947, 0.7972771 , 0.9847748 , 0.9916148 ,
            0.615888  , 0.06207573, 0.08464444, 0.85881865, 0.2072823 ,
            0.72986317, 0.9594631 , 0.43813002, 0.10439372, 0.41262794,
            0.01618481, 0.21412611, 0.77550805, 0.72469807, 0.8756399 ,
            0.325611  , 0.16181779, 0.7206706 , 0.6479279 , 0.37825286,
            0.7155738 , 0.5807624 , 0.24140692, 0.4662763 , 0.20300198,
            0.5138204 , 0.38832712, 0.37142348, 0.71995485, 0.513836  ,
            0.90567577, 0.558097  , 0.92771995, 0.3303169 , 0.71971726,
            0.59325373, 0.8897488 , 0.8143811 , 0.6244596 , 0.24846935,
            0.11550713, 0.26780748, 0.8932054 , 0.11101353, 0.30304086]]]]],
       dtype=float32)>,
 <tf.Tensor: shape=(50,), dtype=float32, numpy=
 array([0.56682   , 0.37258947, 0.7972771 , 0.9847748 , 0.9916148 ,
        0.615888  , 0.06207573, 0.08464444, 0.85881865, 0.2072823 ,
        0.72986317, 0.9594631 , 0.43813002, 0.10439372, 0.41262794,
        0.01618481, 0.21412611, 0.77550805, 0.72469807, 0.8756399 ,
        0.325611  , 0.16181779, 0.7206706 , 0.6479279 , 0.37825286,
        0.7155738 , 0.5807624 , 0.24140692, 0.4662763 , 0.20300198,
        0.5138204 , 0.38832712, 0.37142348, 0.71995485, 0.513836  ,
        0.90567577, 0.558097  , 0.92771995, 0.3303169 , 0.71971726,
        0.59325373, 0.8897488 , 0.8143811 , 0.6244596 , 0.24846935,
        0.11550713, 0.26780748, 0.8932054 , 0.11101353, 0.30304086],
       dtype=float32)>)