Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions tutorials/eager/eager_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"cell_type": "markdown",
"source": [
"# Eager execution basics"
"# 动态图机制基础"
]
},
{
Expand Down Expand Up @@ -94,12 +94,12 @@
},
"cell_type": "markdown",
"source": [
"This is an introductory tutorial for using TensorFlow. It will cover:\n",
"这是一个使用 TensorFlow 的入门教程。包括以下内容:\n",
"\n",
"* Importing required packages\n",
"* Creating and using Tensors\n",
"* Using GPU acceleration\n",
"* Datasets"
"* 导入需要的包\n",
"* 创建和使用张量\n",
"* 使用 GPU 加速\n",
"* 数据集"
]
},
{
Expand All @@ -109,10 +109,10 @@
},
"cell_type": "markdown",
"source": [
"## Import TensorFlow\n",
"## 导入 TensorFlow\n",
"\n",
"To get started, import the `tensorflow` module and enable eager execution.\n",
"Eager execution enables a more interactive frontend to TensorFlow, the details of which we will discuss much later."
"在开始处,导入`tensorflow`模块,并开启动态图机制。\n",
"动态图机制使 TensorFlow 具有一个更具互动性的前端。具体的细节内容我们将在较后面的章节讨论。"
]
},
{
Expand Down Expand Up @@ -143,9 +143,9 @@
},
"cell_type": "markdown",
"source": [
"## Tensors\n",
"## 张量\n",
"\n",
"A Tensor is a multi-dimensional array. Similar to NumPy `ndarray` objects, `Tensor` objects have a data type and a shape. Additionally, Tensors can reside in accelerator (like GPU) memory. TensorFlow offers a rich library of operations ([tf.add](https://www.tensorflow.org/api_docs/python/tf/add), [tf.matmul](https://www.tensorflow.org/api_docs/python/tf/matmul), [tf.linalg.inv](https://www.tensorflow.org/api_docs/python/tf/linalg/inv) etc.) that consume and produce Tensors. These operations automatically convert native Python types. For example:\n"
"一个张量就是一个多维数组。类似于 NumPy `ndarray`对象,`Tensor`对象也有一个数据类型属性和形状属性。除此之外,张量也可以存在于加速器内存中(比如 GPU )。TensorFlow 提供了丰富的操作库([tf.add](https://www.tensorflow.org/api_docs/python/tf/add), [tf.matmul](https://www.tensorflow.org/api_docs/python/tf/matmul), [tf.linalg.inv](https://www.tensorflow.org/api_docs/python/tf/linalg/inv) 等等)用以使用和获得张量。这些操作会自动的转换原生的 Python 类型。例如:\n"
]
},
{
Expand All @@ -168,7 +168,7 @@
"print(tf.reduce_sum([1, 2, 3]))\n",
"print(tf.encode_base64(\"hello world\"))\n",
"\n",
"# Operator overloading is also supported\n",
"# 运算符重载也支持\n",
"print(tf.square(2) + tf.square(3))"
],
"execution_count": 0,
Expand All @@ -181,7 +181,7 @@
},
"cell_type": "markdown",
"source": [
"Each Tensor has a shape and a datatype"
"每个张量都有一个形状和一个数据类型"
]
},
{
Expand Down Expand Up @@ -211,10 +211,10 @@
},
"cell_type": "markdown",
"source": [
"The most obvious differences between NumPy arrays and TensorFlow Tensors are:\n",
"NumPy 数组和 TensorFlow 张量之间最显著的区别是:\n",
"\n",
"1. Tensors can be backed by accelerator memory (like GPU, TPU).\n",
"2. Tensors are immutable."
"1. 加速器内存(如 GPU, TPU)支持张量的处理。\n",
"2. 张量是不可改变的。"
]
},
{
Expand All @@ -224,14 +224,14 @@
},
"cell_type": "markdown",
"source": [
"### NumPy Compatibility\n",
"### NumPy 的兼容性\n",
"\n",
"Conversion between TensorFlow Tensors and NumPy ndarrays is quite simple as:\n",
"* TensorFlow operations automatically convert NumPy ndarrays to Tensors.\n",
"* NumPy operations automatically convert Tensors to NumPy ndarrays.\n",
"TensorFlow 张量和 NumPy 数组间的转换非常简单,像下面:\n",
"* TensorFlow 的操作会自动地转换 NumPy 数组到张量。\n",
"* NumPy 操作会自动地转换张量到 NumPy 数组。\n",
"\n",
"Tensors can be explicitly converted to NumPy ndarrays by invoking the `.numpy()` method on them.\n",
"These conversions are typically cheap as the array and Tensor share the underlying memory representation if possible. However, sharing the underlying representation isn't always possible since the Tensor may be hosted in GPU memory while NumPy arrays are always backed by host memory, and the conversion will thus involve a copy from GPU to host memory."
"张量可以通过调用 `.numpy()` 方法显式的转换为 NumPy 数组。\n",
"当数组和张量在表达上可以共享底层的内存时,这些转换通常很容易。然而,共享底层内存用做表达并不总是可行。因为张量可能会被托管在 GPU 内存中,而 NumPy 数组则总是由主机内存支持的,两者之间的转换因此将导致一个从 GPU 到主机内存的拷贝过程。"
]
},
{
Expand Down Expand Up @@ -272,9 +272,9 @@
},
"cell_type": "markdown",
"source": [
"## GPU acceleration\n",
"## GPU 加速\n",
"\n",
"Many TensorFlow operations can be accelerated by using the GPU for computation. Without any annotations, TensorFlow automatically decides whether to use the GPU or CPU for an operation (and copies the tensor between CPU and GPU memory if necessary). Tensors produced by an operation are typically backed by the memory of the device on which the operation executed. For example:"
"许多 TensorFlow 操作可以使用 GPU 加速计算过程。在没有任何注释的情况下,TensorFlow 会自动决定使用 GPU 或者 CPU 进行操作的处理(以及是否需要在 CPU GPU 内存间进行拷贝)。由操作产生的张量通常也由执行操作的设备内存进行支持。例如:"
]
},
{
Expand Down Expand Up @@ -309,9 +309,9 @@
},
"cell_type": "markdown",
"source": [
"### Device Names\n",
"### 设备名称\n",
"\n",
"The `Tensor.device` property provides a fully qualified string name of the device hosting the contents of the Tensor. This name encodes a bunch of details, such as an identifier of the network address of the host on which this program is executing and the device within that host. This is required for distributed execution of TensorFlow programs, but we'll skip that for now. The string will end with `GPU:<N>` if the tensor is placed on the `N`-th tensor on the host."
"`Tensor.device` 属性提供托管张量内容设备的绝对名称。此名称对一串详细信息进行了编码,例如,当前正在运行程序的主机网络地址的标识符以及该主机内的设备信息。这些是以分布式的方式执行 TensorFlow 程序所需要的信息,这里我们会先略过。如果某个张量是主机的第 N 个张量,则将用以 `GPU:<N>`为结束的字符串表示。"
]
},
{
Expand All @@ -323,9 +323,9 @@
"source": [
"\n",
"\n",
"### Explicit Device Placement\n",
"### 显式的设备分配\n",
"\n",
"The term \"placement\" in TensorFlow refers to how individual operations are assigned (placed on) a device for execution. As mentioned above, when there is no explicit guidance provided, TensorFlow automatically decides which device to execute an operation, and copies Tensors to that device if needed. However, TensorFlow operations can be explicitly placed on specific devices using the `tf.device` context manager. For example:"
"&quot;placement&quot; 在 TensorFlow 中表示的含义是怎样将独立的操作分配到对映的设备上去运行。就像之前提到的,当没有显式的指示时,TensorFlow 自动的决定哪个设备去执行某个操作,并且在需要的时候拷贝张量到相应的设备。不管怎样,TensorFlow 操作都可以通过使用上下文管理器`tf.device`显式的分配到指定的设配。例如:"
]
},
{
Expand All @@ -344,14 +344,14 @@
"def time_matmul(x):\n",
" %timeit tf.matmul(x, x)\n",
"\n",
"# Force execution on CPU\n",
"# 强制在 CPU 上运行\n",
"print(\"On CPU:\")\n",
"with tf.device(\"CPU:0\"):\n",
" x = tf.random_uniform([1000, 1000])\n",
" assert x.device.endswith(\"CPU:0\")\n",
" time_matmul(x)\n",
"\n",
"# Force execution on GPU #0 if available\n",
"# 如果可以找到 GPU #0,强制在它上面运行\n",
"if tf.test.is_gpu_available():\n",
" with tf.device(\"GPU:0\"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.\n",
" x = tf.random_uniform([1000, 1000])\n",
Expand All @@ -368,18 +368,18 @@
},
"cell_type": "markdown",
"source": [
"## Datasets\n",
"## 数据集\n",
"\n",
"This section demonstrates the use of the [`tf.data.Dataset` API](https://www.tensorflow.org/guide/datasets) to build pipelines to feed data to your model. It covers:\n",
"这部分演示使用 [`tf.data.Dataset` API](https://www.tensorflow.org/guide/datasets) 构建管道以将数据提供给模型。包括:\n",
"\n",
"* Creating a `Dataset`.\n",
"* Iteration over a `Dataset` with eager execution enabled.\n",
"* 创建一个 `Dataset`.\n",
"* 在动态图机制开启下,遍历一个`Dataset`\n",
"\n",
"We recommend using the `Dataset`s API for building performant, complex input pipelines from simple, re-usable pieces that will feed your model's training or evaluation loops.\n",
"我们推荐使用`Dataset` API 构建有利于模型循环训练和评估的管道,该管道通过简单、可重复利用模块的组成,输入虽然复杂,但是高性能。\n",
"\n",
"If you're familiar with TensorFlow graphs, the API for constructing the `Dataset` object remains exactly the same when eager execution is enabled, but the process of iterating over elements of the dataset is slightly simpler.\n",
"You can use Python iteration over the `tf.data.Dataset` object and do not need to explicitly create an `tf.data.Iterator` object.\n",
"As a result, the discussion on iterators in the [TensorFlow Guide](https://www.tensorflow.org/guide/datasets) is not relevant when eager execution is enabled."
"如果你对 TensorFlow 的图熟悉,构建`Dataset`对象的 API 在动态图机制开启的情况下,与之完全相同。但遍历数据集元素的过程更简单些。\n",
"你可以使用 Python 遍历 `tf.data.Dataset` 对象,却不用显式的创建 `tf.data.Iterator` 对象。\n",
"因此,在启用动态图机制时,[TensorFlow 指南](https://www.tensorflow.org/guide/datasets)关于迭代器的讨论与之无关。"
]
},
{
Expand All @@ -389,9 +389,9 @@
},
"cell_type": "markdown",
"source": [
"### Create a source `Dataset`\n",
"### 创建一个源 `Dataset`\n",
"\n",
"Create a _source_ dataset using one of the factory functions like [`Dataset.from_tensors`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensors), [`Dataset.from_tensor_slices`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensor_slices) or using objects that read from files like [`TextLineDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TextLineDataset) or [`TFRecordDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TFRecordDataset). See the [TensorFlow Guide](https://www.tensorflow.org/guide/datasets#reading_input_data) for more information."
"创建一个源数据集,使用以下其中之一的工厂函数,如 [`Dataset.from_tensors`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensors), [`Dataset.from_tensor_slices`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensor_slices) 或者使用从文件中读取的对象,如 [`TextLineDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TextLineDataset) 或者 [`TFRecordDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TFRecordDataset). 更多信息见 [TensorFlow Guide](https://www.tensorflow.org/guide/datasets#reading_input_data) "
]
},
{
Expand All @@ -409,7 +409,7 @@
"source": [
"ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6])\n",
"\n",
"# Create a CSV file\n",
"# 创建一个 CSV 文件\n",
"import tempfile\n",
"_, filename = tempfile.mkstemp()\n",
"\n",
Expand All @@ -431,9 +431,9 @@
},
"cell_type": "markdown",
"source": [
"### Apply transformations\n",
"### 应用转换\n",
"\n",
"Use the transformations functions like [`map`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map), [`batch`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#batch), [`shuffle`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#shuffle) etc. to apply transformations to the records of the dataset. See the [API documentation for `tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) for details."
"使用转换函数,如 [`map`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map), [`batch`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#batch), [`shuffle`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#shuffle) 等等。 应用转换到数据集记录中。更多详细信息见 [`tf.data.Dataset` API 文档](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) "
]
},
{
Expand Down Expand Up @@ -463,10 +463,10 @@
},
"cell_type": "markdown",
"source": [
"### Iterate\n",
"### 迭代\n",
"\n",
"When eager execution is enabled `Dataset` objects support iteration.\n",
"If you're familiar with the use of `Dataset`s in TensorFlow graphs, note that there is no need for calls to `Dataset.make_one_shot_iterator()` or `get_next()` calls."
"当动态图机制允许 `Dataset` 对象支持迭代时,\n",
"如果你熟悉`Dataset`TensorFlow 图中的使用,注意这里将不需要调用 `Dataset.make_one_shot_iterator()` 或者 `get_next()` "
]
},
{
Expand Down