Tensorflow学习笔记8(研究和实验之Eager Execution)

    xiaoxiao2022-07-07  200

    前言

    Eager Execution 提供了由运行定义的命令式高级操作接口。通过自动微分编写自定义层、前向传播和训练循环。请先查看下面的笔记本,然后阅读 Eager Execution 指南。

    Eager execution basics

    这是一个介绍如何使用tensorflow的教程。它包括了以下的几个主题:

    导入必备的包

    生成和使用张量

    使用GPU加速

     数据集

    导入Tensorflow

    开始导入tensorflow模块,并且使eager executation能。

    eager execution能够使得更方便的与tensorflow交互,更多的细节在下面陈述

    import tensorflow as tf tf.enable_eager_execution()

    张量

    张量是一个多维数组,类似于Numpy的ndarray对象。Tensor对象拥有一个data数据类型和shape数据类型 。

    此外,张量可以被制定在GPU上。Tensorflow提供了丰富的库操作和产生张量。

    这些函数能够自动的转换为Python自带的数据类型。

    print(tf.add(1, 2)) print(tf.add([1, 2], [3, 4])) print(tf.square(5)) print(tf.reduce_sum([1, 2, 3])) print(tf.encode_base64("hello world")) # Operator overloading is also supported print(tf.square(2) + tf.square(3))

     

    每个张量都有一个形状和数据类型的成员函数

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

    Tensorflow张量和NumPy的区别在于:

    1.张量能够被GPU/TPU加速

    2.张量是不可改变的

    Numpy的兼容性

    Tensorflow张量和Numpy ndarrays之间的转换非常简单

    1.Tensorflow会自动的把ndarrays转换为张量

    2.NumPy会自动的把张量转换为NumPy ndarrays

    通过调用.numpy()成员函数,张量被显式的转换为numpy()。转换的代价很小,因为array和Tensor尽可能的共享内存

    不过并不是总是共享内存,因为有的时候Tensor被托管在GPU,而numpy则是在CPU上,此时的转换涉及到复制一个副本到GPU

    import numpy as np ndarray = np.ones([3, 3]) print("TensorFlow operations convert numpy arrays to Tensors automatically") tensor = tf.multiply(ndarray, 42) print(tensor) print("And NumPy operations convert Tensors to numpy arrays automatically") print(np.add(tensor, 1)) print("The .numpy() method explicitly converts a Tensor to a numpy array") print(tensor.numpy())

    GPU加速

    很多的张量操作可以由GPU加速计算。如果没有指定的话,那么Tensorflow会自动决定采用GPU或者CPU计算。在那上面计算产生的张量,那么它的内存就分配在那个设备上。

    x = tf.random_uniform([3, 3]) print("Is there a GPU available: "), print(tf.test.is_gpu_available()) print("Is the Tensor on GPU #0:  "), print(x.device.endswith('GPU:0'))

    设备名称

    Tensor.device()指定了在那上面进行计算。如GPU:N代表了在第N个GPU上面托管

    显式的确定设备放置

    术语“placement”在tensorflow中代表了单个操作被指派在那个设备上进行。

    就如上面提到的,当没有显式指定的话,Tensorflow会自动确定在CPU还是GPU上进行计算。

    不过通过使用tf.devices可以指定设备

    import time def time_matmul(x):   start = time.time()   for loop in range(10):     tf.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.test.is_gpu_available():   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)

    数据集

    这部分展示了如何使用tf.data.Dataset API 建立一个pipelines以喂数据给你的模型;包括了以下的这些主题:

    1.建立一个数据集

     2.在数据集上迭代且使能eager execution

    创建一个数据集

    这里利用 Dataset.from_tensors, Dataset.from_tensor_slices建立一个数据集

    或者使用其他的读取文件的函数:TextLineDataset or TFRecordDataset

    参考TensorFlow Guide获取更多的信息

    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)

    应用变换

    使用map、 batch、 shuffle等转换功能将转换应用于数据集的记录。有关详细信息, 参考tf.data.Dataset 和 tf.data.Dataset。

    ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2) ds_file = ds_file.batch(2)

    迭代

    When eager execution is enabled Dataset objects support iteration.

    如果您熟悉在Tensorflow 图中使用Datasets, 请注意不需要调用Dataset.make_one_shot_iterator()或get_next()调用。

    print('Elements of ds_tensors:') for x in ds_tensors:   print(x) print('\nElements in ds_file:') for x in ds_file:   print(x)
    最新回复(0)