Table Of Contents
Aggregating Tensors
In [31]:
import tensorflow as tf
import numpy as npIn [32]:
arr = [-7,-10]
tA = tf.constant(arr)
tAOut [32]:
In [33]:
#
# get absolute value of tensor values
#
tf.abs(tA)Out [33]:
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, tensorRandomOut [34]:
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}')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]: