Cast type

Force tensor to this data type
tf. Cast (tensor name, dtype = data type)

Calculate the maximum and minimum values of elements in the tensor dimension

tf. reduce_ Max (tensor name)
tf. reduce_ Min (tensor name)

import tensorflow as tf
x1 = tf.constant([1, 2, 3], dtype=tf.float64)
print(x1)

x2 = tf.cast(x1, tf.int32)
print(x2)

print(tf.reduce_min(x2), tf.reduce_max(x2))

The results are as follows:
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)

Understand axis

axis can specify the direction of the operation
For a two-dimensional tensor, if axis=0, it means to operate on the first dimension, and axis=1, it means to operate on the second dimension. axis=0 indicates vertical operation, along the longitude direction, axis=0 indicates horizontal operation, along the latitude direction

If axis is not specified, all elements will be operated

Calculates the average value of the tensor along the specified dimension

tf. reduce_ Mean (tensor name, axis = operation axis)

Calculates the sum of tensors along a specified dimension

tf. reduce_ Sum (tensor name, axis = operation axis)

import tensorflow as tf
x = tf.constant([[1, 2, 3],
[2, 2, 3]])
print(x)
print(tf.reduce_mean(x))
print(tf.reduce_sum(x, axis=1))

The results are as follows:
tf.Tensor(
[[1 2 3]
[2 2 3]], shape=(2, 3), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor([6 7], shape=(2,), dtype=int32)

Mark trainable

tf.Variable() marks the variable as "trainable", and the marked variable will record the gradient information in the back propagation. This function is often used to mark the parameters to be trained in neural network training
tf. Variable (initial value)

w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1))

The normal distribution random number is randomly generated, and then the generated random number is marked as trainable, so that the parameter w can be updated through gradient descent in back propagation

Mathematical operation in Tensorflow

Four operations of corresponding elements: TF add, tf. subtract, tf. multiply, tf. divide
Square, power and square: TF square, tf. pow, tf. sqrt
Matrix multiplication: TF matmul
Only tensors with the same dimension can do four operations

import tensorflow as tf
a = tf.ones([1, 3])
b = tf.fill([1, 3], 3.)
print(a)
print(b)
print(tf.subtract(a, b))
print(tf.multiply(a, b))
print(tf.divide(a, b))

a = tf.fill([1, 2], 3.)
print(a)
print(tf.pow(a, 3))
print(tf.square(a))
print(tf.sqrt(a))

a = tf.ones([3, 2])
b = tf.fill([2, 3], 3.)
print(tf.matmul(a, b))

The results are as follows:
tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)
tf.Tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
tf.Tensor([[0.33333334 0.33333334 0.33333334]], shape=(1, 3), dtype=float32)

tf.Tensor([[3. 3.]], shape=(1, 2), dtype=float32)
tf.Tensor([[27. 27.]], shape=(1, 2), dtype=float32)
tf.Tensor([[9. 9.]], shape=(1, 2), dtype=float32)
tf.Tensor([[1.7320508 1.7320508]], shape=(1, 2), dtype=float32)

tf.Tensor(
[[6. 6. 6.]
[6. 6. 6.]
[6. 6. 6.]], shape=(3, 3), dtype=float32)

Pairing features and labels

Segment the first dimension of the incoming tensor, generate the input feature / tag pair, and construct the data set
data = tf. data. Dataset. from_ tensor_ Slices ((input features, labels))
This statement can be used to read data in both Numpy and Tensor formats

import tensorflow as tf
feature = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((feature, labels))
print(dataset)
for element in dataset:
print(element)

The results are as follows:
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
(<tf.Tensor: id=9, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=10, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=11, shape=(), dtype=int32, numpy=23>, <tf.Tensor: id=12, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=13, shape=(), dtype=int32, numpy=10>, <tf.Tensor: id=14, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=15, shape=(), dtype=int32, numpy=17>, <tf.Tensor: id=16, shape=(), dtype=int32, numpy=0>)

Derivation of a function from a specified parameter

import tensorflow as tf
w = tf.Variable(tf.constant(3.0))
loss = tf.pow(w, 2)

The results are as follows:
tf.Tensor(6.0, shape=(), dtype=float32)

Enumerate enumerate

It is a built-in function of python, which can traverse each element (such as list, tuple or string) and match the corresponding index number in front of the element. It is combined into: index element, which is often used in the for loop

seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)

The results are as follows:
0 one
1 two
2 three

Classification problems are often represented by unique hot codes

tf. one_ The hot () function converts the data to be converted into one hot data output
tf. one_ Hot (data to be converted, depth = several categories)

import tensorflow as tf
classes = 3
labels = tf.constant([1, 0, 2])
output = tf.one_hot(labels, depth=classes)
print(output)

The results are as follows:
tf.Tensor(
[[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]], shape=(3, 3), dtype=float32)

Classification problem softmax

Make the output conform to the probability distribution

import tensorflow as tf

y = tf.constant([1.01, 2.01, -0.66])
y_pro = tf.nn.softmax(y)
print("After softmax, y_pro is:", y_pro)  # y_pro conforms to probability distribution
print("The sum of y_pro:", tf.reduce_sum(y_pro))  # After passing softmax, the sum of all probabilities is 1

The results are as follows:
After softmax, y_pro is: tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)
The sum of y_pro: tf.Tensor(1.0, shape=(), dtype=float32)

Self updating of parameters

assign_ The sub function is often used for parameter self updating. The parameter w waiting for self updating must be specified as updatable and trainable first, that is, Variable type
w.assign_sub(w content to be subtracted)

import tensorflow as tf

x = tf.Variable(4)
x.assign_sub(1)
print("x:", x)  # 4-1=3

The results are as follows:
x: <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

Specifies the index of the maximum value in the direction of the operation axis

tf. Argmax (tensor name, axis = operation axis)

import numpy as np
import tensorflow as tf

test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print("test:\n", test)
print("Index of the maximum value of each column:", tf.argmax(test, axis=0))  # Returns the index of the maximum value of each column
print("The index of the maximum value of each row", tf.argmax(test, axis=1))  # Returns the index of the maximum value of each row

The results are as follows:
test:
[[1 2 3]
[2 3 4]
[5 4 3]
[8 7 2]]
Index of the maximum value of each column: tf.Tensor([3 3 1], shape=(3,), dtype=int64)
The index of the maximum value of each row tf.Tensor([2 2 0 0], shape=(4,), dtype=int64)

Posted by PHPycho on Fri, 06 May 2022 13:41:13 +0300