zoukankan      html  css  js  c++  java
  • NumPy arrays and TensorFlow Tensors的区别和联系

    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)

  • 相关阅读:
    c语言:猴子吃桃问题
    c语言:输入任意数求该数的阶乘
    (整理三)高并发架构思路,附十万定时任务执行解决方案
    (整理4)RPC服务和HTTP服务简单说明
    .NET Core和Swagger 生成 Api 文档转
    (整理二)读取大日志文件
    (整理一)理解分布式事务,高并发下分布式事务的解决方案-附索引的利弊
    2016年结
    2013年结
    2017年结
  • 原文地址:https://www.cnblogs.com/augustone/p/10506893.html
Copyright © 2011-2022 走看看