一、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