【发布时间】:2020-06-01 15:51:07
【问题描述】:
tensorflow 2.x 代码如下:
将张量流导入为 tf
导入操作系统
将 tensorflow_datasets 导入为 tfds
解析器=tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://'+ os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_cluster(解析器)
tf.tpu.experimental.initialize_tpu_system(解析器)
策略 = tf.distribute.experimental.TPUStrategy(resolver)
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 tf.device('/TPU:0'):
c = tf.matmul(a, b)
print("c 设备:", c.device)
打印(c)
@tf.function
def matmul_fn(x, y):
z = tf.matmul(x, y)
返回 z
z = strategy.run(matmul_fn, args=(a, b))
打印(z)
我的 1.x 代码如下:
%tensorflow_version 1.x
将张量流导入为 tf
导入操作系统
将 tensorflow_datasets 导入为 tfds
tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']
解析器 = tf.distribute.cluster_resolver.TPUClusterResolver(tpu_address)
tf.config.experimental_connect_to_cluster(解析器)
tf.tpu.experimental.initialize_tpu_system(解析器)
策略 = tf.distribute.experimental.TPUStrategy(resolver)
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 tf.device('/TPU:0'):
c = tf.matmul(a, b)
打印(c)
def matmul_fn(x, y):
z = tf.matmul(x, y)
返回 z
使用 tf.Session() 作为 sess:
使用 strategy.scope():
z = strategy.experimental_run_v2(matmul_fn, args=(a, b))
打印(sess.run(z))
最后,我对在 colab 上的 tensorlfow 1.x 中使用 TPU 感到很困惑。
【问题讨论】:
-
我看不懂。使用 ``` 代码格式并编辑您的帖子。
标签: tensorflow google-colaboratory tpu google-cloud-tpu