# 自定义基础知识：张量和运算

## 导入 TensorFlow

``````import tensorflow as tf
``````

## 张量

``````print(tf.math.add(1, 2))
print(tf.math.square(5))
print(tf.math.reduce_sum([1, 2, 3]))

print(tf.math.square(2) + tf.math.square(3))
``````

``````x = tf.linalg.matmul([[1]], [[2, 3]])
print(x)
print(x.shape)
print(x.dtype)
``````

NumPy 数组与 `tf.Tensor` 之间最明显的区别是：

1. 张量可以驻留在加速器内存（例如 GPU、TPU）中。
2. 张量不可变。

### NumPy 兼容性

• TensorFlow 运算会自动将 NumPy ndarray 转换为张量。
• NumPy 运算会自动将张量转换为 NumPy ndarray。

``````import numpy as np

ndarray = np.ones([3, 3])

print("TensorFlow operations convert numpy arrays to Tensors automatically")
tensor = tf.math.multiply(ndarray, 42)
print(tensor)

print("And NumPy operations convert Tensors to NumPy arrays automatically")

print("The .numpy() method explicitly converts a Tensor to a numpy array")
print(tensor.numpy())
``````

## GPU 加速

``````x = tf.random.uniform([3, 3])

print("Is there a GPU available: "),
print(tf.config.list_physical_devices("GPU"))

print("Is the Tensor on GPU #0:  "),
print(x.device.endswith('GPU:0'))
``````

### 设备名称

`Tensor.device` 属性提供存放张量内容的设备的完全限定字符串名称。此名称会编码许多详细信息，例如执行此程序的主机的网络地址标识符以及该主机内设备的标识符。这是分布式执行 TensorFlow 程序所必需的信息。如果将张量放置在主机的第 `N` 个 GPU 上，则该字符串将以 `GPU:<N>` 结尾。

### 显式设备放置

``````import time

def time_matmul(x):
start = time.time()
for loop in range(10):
tf.linalg.matmul(x, x)

result = time.time()-start

print("10 loops: {:0.2f}ms".format(1000*result))

# Force execution on CPU
print("On CPU:")
with tf.device("CPU:0"):
x = tf.random.uniform([1000, 1000])
assert x.device.endswith("CPU:0")
time_matmul(x)

# Force execution on GPU #0 if available
if tf.config.list_physical_devices("GPU"):
print("On GPU:")
with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.
x = tf.random.uniform([1000, 1000])
assert x.device.endswith("GPU:0")
time_matmul(x)
``````

## 数据集

### 创建源 `Dataset`

``````ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6])

# Create a CSV file
import tempfile
_, filename = tempfile.mkstemp()

with open(filename, 'w') as f:
f.write("""Line 1
Line 2
Line 3
""")

ds_file = tf.data.TextLineDataset(filename)
``````

### 应用转换

``````ds_tensors = ds_tensors.map(tf.math.square).shuffle(2).batch(2)

ds_file = ds_file.batch(2)
``````

### 迭代

`tf.data.Dataset` 对象支持迭代以循环记录：

``````print('Elements of ds_tensors:')
for x in ds_tensors:
print(x)

print('\nElements in ds_file:')
for x in ds_file:
print(x)
``````
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"没有我需要的信息" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"太复杂/步骤太多" },{ "type": "thumb-down", "id": "outOfDate", "label":"内容需要更新" },{ "type": "thumb-down", "id": "translationIssue", "label":"翻译问题" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"示例/代码问题" },{ "type": "thumb-down", "id": "otherDown", "label":"其他" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"易于理解" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"解决了我的问题" },{ "type": "thumb-up", "id": "otherUp", "label":"其他" }]