tf.cast() is used to implement forced type conversion
Use tf.reduce_min() to find the minimum value
in a tensor. Use tf.reduce_max() to find the maximum value in a tensor.
We construct a tensor x1 and turn it into a 32-bit integer. Its minimum value is 1 and its maximum value is 3.
Axis can specify the direction of operation. For a two-dimensional tensor, if axis=0, it means to operate on the first dimension, axis=1, which means to operate on the second dimension. axis=0, which means vertical operation, along longitude Direction axis=1, which means horizontal operation, along the latitude direction
For example, we can control the direction of
averaging by adjusting axis=0 or 1. tf.reduce_mean() is to average all the elements in a two-row and three-column vector
tf.reduce_sum(x, axis=1) is along axis= 1, which is the horizontal and latitude direction. So the sum of the first row is 6, and the sum of the second row is 7.
The Variable() function can be marked as "trainable" as a variable, and the variable marked by it will record gradient information in backpropagation. In neural network training, this function is often used to mark the parameters to be trained.
This example is the code of the neural network initialization parameter w.
First, randomly generate a normal distribution random number, and then mark the generated random number as trainable, so that it can be used in back propagation Update parameter w through gradient descent
TensorFlow provides some commonly used calculation functions
such as: addition, subtraction, multiplication, division, square, power, square root, matrix multiplication
code examples:
import tensorflow as tf
a = tf.ones([1, 3])
b = tf.fill([1, 3], 3.)
print("a:", a)
print("b:", b)
print("a+b:", tf.add(a, b))
print("a-b:", tf.subtract(a, b))
print("a*b:", tf.multiply(a, b))
print("b/a:", tf.divide(b, a))
First create a tensor a with one row and three columns, and all elements have the value 1. Create a tensor b with one row and three columns. The value of all elements is 3 The
result of adding the corresponding elements of a and b is [[4. 4. 4.]] The
result of subtracting the corresponding elements of a and b is [[-2 . -2. -2.]] The
result of multiplying a by b is [[3. 3. 3.]] The
result of dividing b by a is [[3. 3. 3.]]
import tensorflow as tf
a = tf.fill([1, 2], 3.)
print("a:", a)
print("a的平方:", tf.pow(a, 3))
print("a的平方:", tf.square(a))
print("a的开方:", tf.sqrt(a))
square() squares the tensor a, pow() squares the tensor, sqrt() squares the tensor
For example, to construct a two-dimensional tensor a with one row and two columns, the filling value is 3
to the power of 3, which is a two-dimensional tensor [[27. 27.]]
squares a, which is a two-dimensional tensor [[ 9. 9.]]
Square root of a is a two-dimensional tensor [[1.7320508 1.7320508]]
Use tf.matmul() function to multiply matrix 1 and matrix 2
import tensorflow as tf
a = tf.ones([3, 2])
b = tf.fill([2, 3], 3.)
print("a:", a)
print("b:", b)
print("a*b:", tf.matmul(a, b))
For example, perform matrix multiplication on a matrix a with three rows and two columns and a matrix b with two rows and three columns and a matrix b with
three rows and three columns. The result is a matrix with six rows and three columns
[[6. 6. 6.]
[6. 6. 6.]
[6. 6. 6.]]
When the neural network is trained, the input features and labels are paired and then fed into the network. TensorFlow provides a function to pair features and labels from_tensor_slices()
from_tensor_slices() This function is applicable to both numpy format and tensor format
import tensorflow as tf
features = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
print(dataset)
for element in dataset:
print(element)
The features we collected are 12 23 10 17, and the label corresponding to each feature is 0 1 1 0. You can use tf.data.Dataset.from_tensor_slices((features, labels)) to pair the features and labels to
see the running results of the program:
Input feature 12 corresponds to label 0. Here pair 12 and 0.
Input feature 23 corresponds to label 1. Here, pair 23 and 1.
Input feature 10 corresponds to label 1. Here, pair 10 and 1 and
input feature 17 corresponds to label 0. Here, 17 and 0 are paired