![](/img/trans.png)
[英]How to create tensor with shape(?,) and dtype=string from image with tensorflow in python
[英]How do I create an arbitrary theano tensor given a dtype and a shape?
如何创建给定dtype和形状的任意theano张量? 我宁愿不对形状的长度和dtype的种类做大的改变。
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)
使用theano.tensor.TensorType(dtype, broadcastable)
dtype是一个numpy dtype字符串,broadcastable是一个布尔值列表,用于指定维是否可广播。
您的函数示例为:
def arbitrary_tensor(dtype, shape, name=None):
# create the type.
var_type = theano.tensor.TensorType(
dtype=dtype,
broadcastable=[False]*len(shape))
# create the variable from the type.
return var_type(name)
除了dtype
这里应该像一个字符串'float32'
,而不是一个numpy的对象像np.float32
。 如果您绝对必须使用numpy对象,则必须将它们映射到字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.