博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NumPy arrays and TensorFlow Tensors的区别和联系
阅读量:6880 次
发布时间:2019-06-26

本文共 1540 字,大约阅读时间需要 5 分钟。

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

你可能感兴趣的文章
squid配置及说明文档,很好很详细
查看>>
Trufun UML工具代码生成功能视频演示
查看>>
Log4j按级别输出日志到不同文件配置分析
查看>>
搭建nginx服务器
查看>>
java 运行 jar classpath配置
查看>>
go thrift oprot.Flush() not enough arguments in
查看>>
使用 Tomcat 7 新的连接池 —— Tomcat jdbc pool
查看>>
Spring MVC 介绍
查看>>
博客用途声明---重要
查看>>
linux .la .lo文件以及libtool介绍
查看>>
写python如何组织代码
查看>>
我的友情链接
查看>>
visual studio在浏览器中查看与运行的区别
查看>>
读书清单(2018书单)
查看>>
我的友情链接
查看>>
HTML滚动文字代码
查看>>
c#之旅--第二天
查看>>
vim复制粘贴大全
查看>>
几个Office使用中的小问题解决方法汇总
查看>>
常见硬盘加密解密的4种方法解析
查看>>