一、numpy.asarrayhtml
当设置了类型而且类型不一致时,asarray返回一个副本,不然返回一个引用。举例:ide
>>> a = np.array([1, 2], dtype=np.float32) >>> np.asarray(a, dtype=np.float32) is aTrue >>> np.asarray(a, dtype=np.float64) is aFalse
详见:https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html spa
二、theano.shared用于关联Theano内存空间和用户内存空间,使用参数borrow=Ture进行设置。举例:.net
import numpy, theano np_array = numpy.ones(2, dtype='float32') s_default = theano.shared(np_array) s_false = theano.shared(np_array, borrow=False) s_true = theano.shared(np_array, borrow=True)
np_array += 1 # now it is an array of 2.0 s print(s_default.get_value()) print(s_false.get_value()) print(s_true.get_value())
[ 1. 1.] [ 1. 1.] [ 2. 2.]
get_value都会返回副本,当borrow=ture时,会把两个空间进行关联,这种状况下不要对返回值进行更改,不然代码会变成设备相关,致使部分操做没法执行
详见:http://deeplearning.net/software/theano/tutorial/aliasing.html
htm