tensorflow 基础_TensorFlow-基础

tensorflow 基础_TensorFlow-基础_第1张图片

tensorflow 基础

TensorFlow-基础 (TensorFlow - Basics)

In this chapter, we will learn about the basics of TensorFlow. We will begin by understanding the data structure of tensor.

在本章中,我们将学习TensorFlow的基础知识。 我们将从了解张量的数据结构开始。

张量数据结构 (Tensor Data Structure)

Tensors are used as the basic data structures in TensorFlow language. Tensors represent the connecting edges in any flow diagram called the Data Flow Graph. Tensors are defined as multidimensional array or list.

Tensor用作TensorFlow语言中的基本数据结构。 张量表示任何称为数据流图的流程图中的连接边。 张量定义为多维数组或列表。

Tensors are identified by the following three parameters −

张量由以下三个参数标识-

(Rank)

Unit of dimensionality described within tensor is called rank. It identifies the number of dimensions of the tensor. A rank of a tensor can be described as the order or n-dimensions of a tensor defined.

张量内描述的维数单位称为等级。 它确定张量的维数。 张量的秩可以描述为定义的张量的阶数或n维。

形状 (Shape)

The number of rows and columns together define the shape of Tensor.

行和列的数量共同定义张量的形状。

类型 (Type)

Type describes the data type assigned to Tensor’s elements.

类型描述分配给Tensor元素的数据类型。

A user needs to consider the following activities for building a Tensor −

用户需要考虑以下活动来构建张量-

  • Build an n-dimensional array

    建立一个n维数组
  • Convert the n-dimensional array.

    转换n维数组。
Tensor Data Structure

TensorFlow的各种尺寸 (Various Dimensions of TensorFlow)

TensorFlow includes various dimensions. The dimensions are described in brief below −

TensorFlow包括各种尺寸。 尺寸在下面简要描述-

一维张量 (One dimensional Tensor)

One dimensional tensor is a normal array structure which includes one set of values of the same data type.

一维张量是一种普通的数组结构,其中包含一组相同数据类型的值。

Declaration

宣言


>>> import numpy as np
>>> tensor_1d = np.array([1.3, 1, 4.0, 23.99])
>>> print tensor_1d

The implementation with the output is shown in the screenshot below −

输出的实现显示在下面的屏幕截图中-

One Dimensional Tensor

The indexing of elements is same as Python lists. The first element starts with index of 0; to print the values through index, all you need to do is mention the index number.

元素的索引与Python列表相同。 第一个元素以索引0开头; 要通过索引打印值,您需要做的就是提及索引号。


>>> print tensor_1d[0]
1.3
>>> print tensor_1d[2]
4.0

Declaration

二维张量 (Two dimensional Tensors)

Sequence of arrays are used for creating “two dimensional tensors”.

数组序列用于创建“二维张量”。

The creation of two-dimensional tensors is described below −

二维张量的创建如下所述-

Two Dimensional Tensors

Following is the complete syntax for creating two dimensional arrays −

以下是创建二维数组的完整语法-


>>> import numpy as np
>>> tensor_2d = np.array([(1,2,3,4),(4,5,6,7),(8,9,10,11),(12,13,14,15)])
>>> print(tensor_2d)
[[ 1 2 3 4]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
>>>

The specific elements of two dimensional tensors can be tracked with the help of row number and column number specified as index numbers.

可以借助指定为索引号的行号和列号来跟踪二维张量的特定元素。


>>> tensor_2d[3][2]
14

张量处理和操纵 (Tensor Handling and Manipulations)

In this section, we will learn about Tensor Handling and Manipulations.

在本部分中,我们将学习张量处理和操纵。

To begin with, let us consider the following code −

首先,让我们考虑以下代码-


import tensorflow as tf
import numpy as np

matrix1 = np.array([(2,2,2),(2,2,2),(2,2,2)],dtype = 'int32')
matrix2 = np.array([(1,1,1),(1,1,1),(1,1,1)],dtype = 'int32')

print (matrix1)
print (matrix2)

matrix1 = tf.constant(matrix1)
matrix2 = tf.constant(matrix2)
matrix_product = tf.matmul(matrix1, matrix2)
matrix_sum = tf.add(matrix1,matrix2)
matrix_3 = np.array([(2,7,2),(1,4,2),(9,0,2)],dtype = 'float32')
print (matrix_3)

matrix_det = tf.matrix_determinant(matrix_3)
with tf.Session() as sess:
   result1 = sess.run(matrix_product)
   result2 = sess.run(matrix_sum)
   result3 = sess.run(matrix_det)

print (result1)
print (result2)
print (result3)

Output

输出量

The above code will generate the following output −

上面的代码将生成以下输出-

Tensor Handling and Manipulations

说明 (Explanation)

We have created multidimensional arrays in the above source code. Now, it is important to understand that we created graph and sessions, which manage the Tensors and generate the appropriate output. With the help of graph, we have the output specifying the mathematical calculations between Tensors.

我们在上面的源代码中创建了多维数组。 现在,重要的是要了解我们创建了图和会话,它们管理张量并生成适当的输出。 借助图,我们的输出指定了张量之间的数学计算。

翻译自: https://www.tutorialspoint.com/tensorflow/tensorflow_basics.htm

tensorflow 基础

你可能感兴趣的:(数据结构,列表,python,java,tensorflow)