0
如何創建一個給定dtype和shape的任意theano張量?我寧願不爲dtype的形狀和種類做長時間的轉換。如何根據dtype和形狀創建任意theano張量?
import numpy as np
from theano import tensor
def arbitrary_tensor(dtype, shape, name=None):
function = {
np.float32: 'f',
np.float64: 'd',
np.int8: 'b',
np.int16: 'w',
np.int32: 'i',
np.int64: 'l',
np.complex64: 'c',
np.complex128: 'z',
}[dtype]
function += {
0: 'scalar',
1: 'vector',
2: 'matrix',
3: 'tensor3',
4: 'tensor4'}[len(shape)]
return getattr(tensor, function)(name=name)