theano 编程细节(二)

type/dtype

dtype:data type

>> x = T.matrix()
>> x.type
TensorType(float64, matrix)

>> x.dtype
'float64'

numpy 下的多维数组到 theano.shared

Place the data into shared variables. This allows theano to copy the data to the GPU, if one is available.

def load_data_shared(filename='./data/mnist.pkl.gz'):
    with gzip.open(filename) as fp:
        training_data, valid_data, test_data = pickle.load(fp)
    def shared_data(data):
        shared_x = theano.shared(
                np.asarray(data[0], dtype=theano.config.floatX), borrow=True)
        shared_y = theano.shared(
                np.asarray(data[1], dtype=theano.config.floatX), borrow=True)
        return shared_x, share_y
    return shared_data(training_data), shared_data(valid_data), shared_data(test_data)

theano.function 的参数

def function(inputs, outputs=None, mode=None, updates=None, givens=None, no_default_updates=False, accept_inplace=False, name=None, rebuild_strict=True, allow_input_downcast=None, profile=None, on_unused_input=None):
                         # 常用的一般为 inputs,outputs,updates,givens

theano.tensor.var.TensorVariable

>> import theano.tensor as T
>> inpt = T.tensor4(name='inpt')

>> type(inpt)
theano.tensor.var.TensorVariable

>> inpt.ndim
4

>> inpt.dtype
'float64'

所谓 theano.config.floatX

>> type(theano.config.floatX)
str
>> theano.config.floatX
'float64'

你可能感兴趣的:(theano 编程细节(二))