Deep Learning/Tensorflow

reduce_sum, cast, argmax, image_dataset_from_directory, one_hot, reduce_mean, assign_sub, boolean_mask, random.normal, zeros

Naranjito 2023. 12. 12. 13:38
  • reduce_sum

Computes the sum of elements across dimensions of a tensor.

 

- keepdims=False

The rank of the tensor is reduced by 1 for each of the entries in axis

A = tf.constant([[[1, 2, 3]], [[4, 5, 6]]])

tf.reduce_sum(A)=21
tf.reduce_sum(A, 0)=[[5 7 9]]
tf.reduce_sum(A, 1)=[[1 2 3]
	  	     [4 5 6]]
tf.reduce_sum(A, 2)=[[ 6]
	  	     [15]]

 

- keepdims=True

The reduced dimensions are retained with length 1.

tf.reduce_sum(A,keepdims=True))=[[[21]]]
tf.reduce_sum(A, 0,keepdims=True)=[[[5 7 9]]]
tf.reduce_sum(A, 1,keepdims=True)=[[[1 2 3]]
	  	     	           [[4 5 6]]]
tf.reduce_sum(A, 2,keepdims=True)=[[[ 6]]
	  	   	           [[15]]]

 

  • cast

Casts a tensor to a new type.

tf.cast(x, dtype, name=None)

 

  • argmax

Returns the index with the largest value.

tf.math.argmax(input,axis=None,output_type=tf.dtypes.int64,name=None))

 

  • image_dataset_from_directory

Generates a tf.data.Dataset from image files in a directory.

tf.keras.utils.image_dataset_from_directory(
    directory,
    labels='inferred',
    label_mode='int',
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=None,
    validation_split=None,
    subset=None,
    interpolation='bilinear',
    follow_links=False,
    crop_to_aspect_ratio=False,
    **kwargs)

 

  • one_hot

Returns a one-hot tensor.

tf.one_hot(indices,depth,on_value=None,off_value=None,axis=None,dtype=None,name=None)

 

  • reduce_mean

Computes the mean of elements across dimensions of a tensor.

tf.math.reduce_mean(input_tensor, axis=None, keepdims=False, name=None)

 

  • assign_sub

It used for updating the value of a TensorFlow variable by subtracting another value from it.

tf.assign_sub(ref, value, use_locking=None, name=None)
  • ref: This is a TensorFlow variable. The value of this variable will be updated by subtracting the value specified in the next argument.
  • value: This is the value that will be subtracted from the variable referenced by ref.
  • use_locking: (Optional) If True, use locking during the operation to prevent concurrent updates. If None, the system decides.
  • name: (Optional) A name for the operation.

For example,

 A.assign_sub(B)

meaning

A = A - B
A -= B

 

  • boolean_mask

Apply boolean mask to tensor.

tf.boolean_mask(tensor, mask, axis=None, name='boolean_mask')

 

tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example
mask = np.array([True, False, True])
tf.boolean_mask(tensor, mask)
>>>
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [5, 6]], dtype=int32)>

 

  • random.normal

Outputs random values from a normal distribution.

stddev : A Tensor or Python value of type dtype, broadcastable with mean. The standard deviation of the normal distribution.

tf.random.normal(
    shape,
    mean=0.0,
    stddev=1.0,
    dtype=tf.dtypes.float32,
    seed=None,
    name=None
)

 

  • zeros

Creates a tensor with all elements set to zero.

tf.zeros(
    shape,
    dtype=tf.dtypes.float32,
    name=None,
    layout=None
)

 

'Deep Learning > Tensorflow' 카테고리의 다른 글

Layers-Sequential, input_shape, Flatten, Dense  (0) 2023.12.19
GradientTape  (0) 2023.12.12
LSTM  (0) 2022.09.15
Keras-Preprocessing, One-hot encoding, Word Embedding , Modeling, Compile  (0) 2021.04.09
keras-Tokenizer  (0) 2021.03.08