首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Cloud TPU --未使用TPU

Google Cloud TPU --未使用TPU
EN

Stack Overflow用户
提问于 2021-01-06 02:15:48
回答 1查看 74关注 0票数 1

我正在试着在TPU上运行一个简单的程序:

代码语言:javascript
复制
import tensorflow as tf

tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print("Device:", tpu.master())
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu)

a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])

with strategy.scope():
    c = tf.matmul(a, b)
    print("c device: ", c.device)
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    print(c.eval())

当我运行这个的时候,它看起来像是找到了TPU。但是,所有记录的设备名称中都没有'TPU‘--它们都在CPU上。

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-01-15 09:22:32

strategy.scope()用于模型训练。

如果您想在TPU上运行tf.matmul,您可以使用以下两种方法之一:

代码语言:javascript
复制
with tf.device('/TPU:0'):
  c = tf.matmul(a, b)

代码语言:javascript
复制
@tf.function
def matmul_fn(x, y):
  z = tf.matmul(x, y)
  return z

z = strategy.run(matmul_fn, args=(a, b))
print(z)

详情请参阅here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65584373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档