NumPy arrays and TensorFlow Tensors的区别和联系

本文详细介绍了Tensor的主要特点,包括其在加速器内存如GPU、TPU上的支持以及不可变性。同时,深入探讨了Tensor与NumPy数组之间的双向转换机制,以及这种转换可能涉及的内存复制成本。此外,提供了在TensorFlow中检测和选择GPU的方法,以及如何显式指定运行设备的代码示例。

1,tensor的特点

  • Tensors can be backed by accelerator memory (like GPU, TPU).
  • Tensors are immutable

2,双向转换

  • TensorFlow operations automatically convert NumPy ndarrays to Tensors.
  • NumPy operations automatically convert Tensors to NumPy ndarrays

3,转换的代价

Tensors can be explicitly converted to NumPy ndarrays by invoking the .numpy() method on them. 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.

4,使用tensor时如何测定和选择gpu

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'))

print(tf.test.is_built_with_cuda())

5,显式指定运行的xpu

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([900, 900])
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)

转载于:https://www.cnblogs.com/augustone/p/10506893.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值