What is the difference between numpy.log and theano.tensor.log? Do they perform the same?
It is likely that numpy.log
will be faster. You can compare them both on your CPU and your data.
import theano
import theano.tensor as T
import numpy as
x = T.vector('x')
theano_log = theano.function([x], T.log(x))
a = np.random.rand(1000).astype(np.float32) # test data
assert np.allclose(theano_log(a), np.log(a)) # optional correctness check
Then measure with:
In [6]: %timeit np.log(a)
100000 loops, best of 3: 7.89 µs per loop
In [7]: %timeit theano_log(a)
10000 loops, best of 3: 44.1 µs per loop
So for a vector of size 1000 numpy
is about 5 times faster. This results might vary if you switch to run the calculations in a GPU which you can do with theano
and not with numpy
.
The main difference is the way you use each library. In theano if you want to do several operations to an array (ex: log->square->mean) you would declare first a computation graph and then evaluate the whole graph at once which might result in some optimizations. With numpy
you would evaluate each intermediate step, creating many intermediate variables on the process that can in some cases be avoided in theano
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.