TensorFlow简介

tensors

'A tensor is a generalization of vectors and matrices to potentially higher dimensions.Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes." (https://www.tensorflow.org/guide/tensor)
lt should’t surprise you that tensors are a fundemental apsect of TensorFlow.They are the main objects that are passed around and
manipluated throughout the program. Each tensor represnts a partialy defined computation that will eventually produce a value. TensorFlowprograms work by buiding a graph of Tensor objects that details how tensors are related. Running dfferent parts of the graph allow results tobe generated.
Each tensor has a data type and a shape.
Data Types Include: float32, int32,string and others.入Shape: Represents the dimension of data.

“张量是向量和矩阵到潜在更高维度的泛化。在内部,TensorFlow 将张量表示为基本数据类型的 n 维数组。”(https://www.tensorflow.org[guide/tensor)
张量是 TensorFlow 的基本组成部分,这应该不会让您感到惊讶。它们是传递的主要对象,并且
在整个程序中进行操作。每个张量代表一个部分定义的计算,最终将产生一个值。 TensorFlow 程序通过构建张量对象图来工作,该图详细说明张量是如何相关的。运行图的不同部分可以生成结果。
每个张量都有一个数据类型和一个形状。
数据类型包括:float32、int32、string等。Shape:表示数据的维度。

creating tensors

string = tf.Variable( "this is a string", tf.string)
number = tf.Variable(324,tf.int16)
floating = tf.Variable(3.567,tf.float64)

rank/degree of tensors

张量的等级/度数

another word for rank is degree,these terms simply mean the number of dimensions involved in the tensors.what we create above is a tensor of rank 0,also known as a scalar

秩的另一个词是度数,这些术语仅表示张量中涉及的维数。我们在上面创建的是秩为 0 的张量,也称为标量

now we will create some tensors of higher degree/ranks

创建更高秩的标量

rank1_tensor = tf.Variable(["Test","ok","yes"], tf.string)
rank2_tensor = tf.Variable([["test", "ok"],["test", "yes"]], tf.string)

to determine the rank of a tensor we can call the following method

要确定张量的等级,我们可以调用以下方法

tf.rank(rank2_tensor)

shape=(),dtype=int32,numpy=2

shape of tensors

Now that weve talked about the rank of tensors it’s time to talk about the shape.

The shape of a tensor is simply the amount of elements that exist in each dimension. TensorFlow will try to determine the shape of a tensor but sometimes it may be unknown.
To get the shape of a tensor we use the shape attribute.

现在我们已经讨论了张量的秩,是时候讨论形状了。张量的形状只是每个维度中存在的元素的数量。 TensorFlow 会尝试确定张量的形状,但有时它可能是未知的。
为了获得张量的形状,我们使用 shape 属性。

rank2_tensor.shape

[2,2]

changing shape

The number of elements of a tensor is the product of the sizes of all its shapes. There are often many shapes that have the same number of elements, making it convient to be able to change the shape of a tensor.
The example below shows how to change the shape of a tensor.

张量的元素数是其所有形状大小的乘积。通常有许多形状具有相同数量的元素,因此可以很方便地改变张量的形状。
下面的例子展示了如何改变张量的形状。

tensor1 = tf.ones([1,2,3]) # tf.ones() creates a shape [1,2,3] tensor full of ohes
tensor2 = tf.reshape(tensor1,[2,3,1]) # reshape existing data to shape [2,3,1]
tensor3 = tf.reshape(tensor2,[3-1]) # -1 tells the tensor to calculate the size of the dimension in that place
# this will reshape the tensor to [3,3]
#The numer of elements in the reshaped tensor MUST match the number in the original

Type of Tensors

before we go to far i will mention that there are different types of tensors.Theses are the most commonly used and we will talk moire in depth about each they are used.

在我们深入之前,我会提到有不同类型的张量。这些是最常用的,我们将更深入地讨论它们的每一个使用。

variable

constant

placeilder

sparestensor

with the execption of Variable all of these tensors are immuttable, meaning their value may not change during execution.For now it is sufficient to understand that we use the Variable tensor when we want to potentially change the value of our tensor.
通过执行变量,所有这些张量都是不可变的,这意味着它们的值在执行期间可能不会改变。现在,当我们想要潜在地改变张量的值时,我们使用变量张量就足够了。

Evaluating Tensors

There will be some times throughout this guide that we need to evaluate a tensor. In other words, get its value. Since tensors represnent apartially complete computation we will sometimes need to run what’s called a session to evaluate the tensor.
There are many different ways to achieve this but I will note the simplest way below.

在本指南中,有时我们需要评估张量。换句话说,获取它的值。由于张量代表部分完整的计算,我们有时需要运行所谓的会话来评估张量。
有许多不同的方法可以实现这一点,但我会在下面说明最简单的方法。

with tf.Session() as sess:
   tensor.eval()

In the code above we evaluated the tensor variable that was stored in the default graph . The default graph holds all operations not specififed toany other graph. It is possible to create our own seperate graphs but for now we will stick with the default.

在上面的代码中,我们评估了存储在默认图中的张量变量。默认图包含未指定给任何其他图的所有操作。可以创建我们自己的单独图表,但现在我们将坚持使用默认值…

http://www.tensorflow.org/guide/tensor

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值