提交 492b9dc4 authored 作者: Frederic's avatar Frederic

In gpu tutorial make the timming more precise and small pep8 fix.

上级 346969b3
......@@ -44,9 +44,10 @@ file and run it.
t0 = time.time()
for i in xrange(iters):
r = f()
print 'Looping %d times took'%iters, time.time() - t0, 'seconds'
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
print 'Used the','cpu' if numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]) else 'gpu'
print 'Used the', 'cpu' if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]) else 'gpu'
The program just computes the exp() of a bunch of random numbers.
Note that we use the `shared` function to
......@@ -100,10 +101,11 @@ after the T.exp(x) is replaced by a GPU version of exp().
t0 = time.time()
for i in xrange(iters):
r = f()
print 'Looping %d times took'%iters, time.time() - t0, 'seconds'
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
print 'Numpy result is', numpy.asarray(r)
print 'Used the','cpu' if numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]) else 'gpu'
print 'Used the', 'cpu' if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]) else 'gpu'
The output from this program is
......@@ -155,10 +157,11 @@ that it has the un-wanted side-effect of really slowing things down.
t0 = time.time()
for i in xrange(iters):
r = f()
print 'Looping %d times took'%iters, time.time() - t0, 'seconds'
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
print 'Numpy result is', numpy.asarray(r)
print 'Used the','cpu' if numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]) else 'gpu'
print 'Used the', 'cpu' if numpy.any([isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]) else 'gpu'
Running this version of the code takes just under 0.05 seconds, over 140x faster than
the CPU implementation!
......
......@@ -800,16 +800,17 @@ class T_using_gpu(unittest.TestCase):
t0 = time.time()
for i in xrange(iters):
r = f()
print 'Looping %d times took'%iters, time.time() - t0, 'seconds'
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
if numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print 'Used the cpu'
else:
print 'Used the gpu'
if theano.config.device.find('gpu') > -1:
assert not numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()])
else:
assert numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()])
assert numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()])
......@@ -831,15 +832,16 @@ class T_using_gpu(unittest.TestCase):
t0 = time.time()
for i in xrange(iters):
r = f()
print 'Looping %d times took'%iters, time.time() - t0, 'seconds'
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
print 'Numpy result is', numpy.asarray(r)
if numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print 'Used the cpu'
else:
print 'Used the gpu'
assert not numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()])
assert not numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()])
......@@ -865,15 +867,16 @@ class T_using_gpu(unittest.TestCase):
t0 = time.time()
for i in xrange(iters):
r = f()
print 'Looping %d times took'%iters, time.time() - t0, 'seconds'
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
print 'Numpy result is', numpy.asarray(r)
if numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print 'Used the cpu'
else:
print 'Used the gpu'
assert not numpy.any( [isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()])
assert not numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()])
class T_fibby(unittest.TestCase):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论