1、numpy.asarray
当设置了类型并且类型不一致时,asarray返回一个副本,否则返回一个引用。举例:
>>> a = np.array([1, 2], dtype=np.float32)>>> np.asarray(a, dtype=np.float32) is aTrue>>> np.asarray(a, dtype=np.float64) is aFalse
详见:
2、theano.shared用于关联Theano内存空间和用户内存空间,使用参数borrow=Ture进行设置。举例:
import numpy, theanonp_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 sprint(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