- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我对 Google TPU 还很陌生。根据我已经研究过的内容,它专门针对训练在 TensorFlow 上编写的机器学习模型进行了优化。目前,我正在尝试了解 TPU 如何执行其他类型的功能。这些功能与机器学习无关。我一直在尝试调整我的代码,以便它可以在 Google Colab 的 TPU 上运行,但我不确定它是否有效,或者这是否是最佳方法。这是我的 O(n<sup>3</sup>)
代码矩阵乘法算法:
import os
import numpy as np
from random import seed
from random import random
import tensorflow as tf
import time;
#check that this is running on the TPU
try:
tpu = tf.contrib.cluster_resolver.TPUClusterResolver() # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
print("Running on GPU or CPU")
tpu = None
#TPU details
if 'COLAB_TPU_ADDR' not in os.environ:
print('ERROR: Not connected to a TPU runtime; please see the first cell in this notebook for instructions!')
else:
tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']
print ('TPU address is', tpu_address)
def multiplicationComputation():
#size of matrix
row_size = 128
col_size = 128
N = row_size*col_size
#class for matrix
class MatrixMultiplication:
matrix1 = np.empty(N) #DO NOT USE np.arange(N)
matrix2 = np.empty(N)
product = np.empty(N) #product size is the matrix1.columns x matrix2.rows
#create MatrixMultiplication object
m = MatrixMultiplication()
#fill objects's data structures
#seed for matrix 1
seed(1)
for x in range(N):
value = random()
m.matrix1[x] = value
#seed for matrix 2
seed(7)
for x in range(N):
value = random()
m.matrix2[x] = value
#multiply matrix1 and matrix2
start = time.time()
qtySaves = 0;
for i in range(row_size):
for j in range(col_size):
i_col = i * col_size
sum = 0
for k in range(row_size):
k_col = k * col_size
multiplication = m.matrix1[i_col + k] * m.matrix2[k_col + j]
sum = sum + multiplication
m.product[i_col + j] = sum #The result of the multiplication is saved on the product matrix
qtySaves = qtySaves + 1
end = time.time()
#print result
print()
print("Result O(n^3): ")
for i in range(N):
if i % row_size == 0 and i > 0:
print()
print(str(m.product[i]), end =" ")
print()
print("For n = " + str(N) + ", time is " + str(end - start))
#rewrite computation so it can be executed on the TPU
#tpuOperation = tf.contrib.tpu.rewrite(multiplicationComputation)
tpuOperation = tf.contrib.tpu.batch_parallel(multiplicationComputation, [], num_shards=8)
#run
session = tf.Session(tpu_address, config=tf.ConfigProto(isolate_session_state=True, log_device_placement=True)) #isolate session state = True for distributed runtime
try:
session.run(tf.contrib.tpu.initialize_system()) #initializes a distributed TPU system
session.run(tpuOperation)
finally:
#TPU sessions must be shutdown separately from closing the session
session.run(tf.contrib.tpu.shutdown_system())
session.close()
我担心这不是在 TPU 上运行。调用session.list_devices()
时我看到列出了一个 CPU,我担心我的代码可能实际上是在 CPU 而不是 TPU 上运行。这是上述命令的输出:
TPU devices:
[_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:CPU:0, CPU, -1, 10448234186946304259),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 2088593175391423031),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 1681908406791603718),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 2618396797726491975),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:2, TPU, 17179869184, 14243051360425930068),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:3, TPU, 17179869184, 15491507241115490455),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:4, TPU, 17179869184, 9239156557030772892),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:5, TPU, 17179869184, 16970377907446102335),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:6, TPU, 17179869184, 6145936732121669294),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:7, TPU, 17179869184, 11372860691871753999),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU_SYSTEM:0, TPU_SYSTEM, 17179869184, 12653526146081894211)]
目前,我不寻求有关使用哪种加速器的建议。我想测试 TPU 并确保我的代码在上面运行。请帮忙!
最佳答案
恐怕 tensorflow 的存在与否对 np
操作的执行方式没有影响。
在上面的示例中,当您指定
tpuOperation = tf.contrib.tpu.batch_parallel(multiplicationComputation, [], num_shards=8)
其中 multiplicationComputation
没有要并行化的 TPU 特定代码,它将按照在 CPU 上调用 multiplicationComputation
时正常运行的方式运行。
您将不得不使用 TF 操作重写您的代码,以允许它在 GPU 上运行。 Tensorflow 会将您的操作转换为 TPU 特定代码。
关于python - 是否可以在 Google TPU 上运行常规的 python 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572787/
我有一个 if 语句,如下所示 if (not(fullpath.lower().endswith(".pdf")) or not (fullpath.lower().endswith(tup
然而,在 PHP 中,可以: only appears if $foo is true. only appears if $foo is false. 在 Javascript 中,能否在一个脚
XML有很多好处。它既是机器可读的,也是人类可读的,它具有标准化的格式,并且用途广泛。 它也有一些缺点。它是冗长的,不是传输大量数据的非常有效的方法。 XML最有用的方面之一是模式语言。使用模式,您可
由于长期使用 SQL2000,我并没有真正深入了解公用表表达式。 我给出的答案here (#4025380)和 here (#4018793)违背了潮流,因为他们没有使用 CTE。 我很欣赏它们对于递
我有一个应用程序: void deleteObj(id){ MyObj obj = getObjById(id); if (obj == null) { throw n
我的代码如下。可能我以类似的方式多次使用它,即简单地说,我正在以这种方式管理 session 和事务: List users= null; try{ sess
在开发J2EE Web应用程序时,我通常会按以下方式组织我的包结构 com.jameselsey.. 控制器-控制器/操作转到此处 服务-事务服务类,由控制器调用 域-应用程序使用的我的域类/对象 D
这更多是出于好奇而不是任何重要问题,但我只是想知道 memmove 中的以下片段文档: Copying takes place as if an intermediate buffer were us
路径压缩涉及将根指定为路径上每个节点的新父节点——这可能会降低根的等级,并可能降低路径上所有节点的等级。有办法解决这个问题吗?有必要处理这个吗?或者,也许可以将等级视为树高的上限而不是确切的高度? 谢
我有两个类,A 和 B。A 是 B 的父类,我有一个函数接收指向 A 类型类的指针,检查它是否也是 B 类型,如果是将调用另一个函数,该函数接受一个指向类型 B 的类的指针。当函数调用另一个函数时,我
有没有办法让 valgrind 使用多个处理器? 我正在使用 valgrind 的 callgrind 进行一些瓶颈分析,并注意到我的应用程序中的资源使用行为与在 valgrind/callgrind
假设我们要使用 ReaderT [(a,b)]超过 Maybe monad,然后我们想在列表中进行查找。 现在,一个简单且不常见的方法是: 第一种可能性 find a = ReaderT (looku
我的代码似乎有问题。我需要说的是: if ( $('html').attr('lang').val() == 'fr-FR' ) { // do this } else { // do
根据this文章(2018 年 4 月)AKS 在可用性集中运行时能够跨故障域智能放置 Pod,但尚不考虑更新域。很快就会使用更新域将 Pod 放入 AKS 中吗? 最佳答案 当您设置集群时,它已经自
course | section | type comart2 : bsit201 : lec comart2 :
我正在开发自己的 SDK,而这又依赖于某些第 3 方 SDK。例如 - OkHttp。 我应该将 OkHttp 添加到我的 build.gradle 中,还是让我的 SDK 用户包含它?在这种情况下,
随着 Rust 越来越充实,我对它的兴趣开始激起。我喜欢它支持代数数据类型,尤其是那些匹配的事实,但是对其他功能习语有什么想法吗? 例如标准库中是否有标准过滤器/映射/归约函数的集合,更重要的是,您能
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我一直在研究 PHP 中的对象。我见过的所有示例甚至在它们自己的对象上都使用了对象构造函数。 PHP 会强制您这样做吗?如果是,为什么? 例如: firstname = $firstname;
...比关联数组? 关联数组会占用更多内存吗? $arr = array(1, 1, 1); $arr[10] = 1; $arr[] = 1; // <- index is 11; does the
我是一名优秀的程序员,十分优秀!