Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ExpandDims(inputs, axis=-1, **kwargs):
if not isinstance(inputs, Tensor):
raise RuntimeError('ExpandDims Operator accepts a Tensor as inputs')
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
output = Tensor.CreateOperator(nout=1, op_type='ExpandDims', **kwargs)
if inputs.shape is not None:
output.shape = inputs.shape[:]
output.shape.insert(axis, 1)
return output
def Reshape(inputs, shape, **kwargs):
if not isinstance(inputs, Tensor):
raise RuntimeError('Reshape Operator accepts a Tensor as inputs')
if not isinstance(shape, tuple) and not isinstance(shape, list):
raise TypeError('Reshape dims must be a tuple or list')
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
output = Tensor.CreateOperator(nout=1, op_type='Reshape', **kwargs)
if inputs.shape is not None:
output.shape = [1] * len(shape)
for i, s in enumerate(shape):
if s == -1: output.shape[i] = 1
else: output.shape[i] = s
return output
def Reduce(inputs, axis=-1, operation='NONE', keep_dims=False, **kwargs):
if not isinstance(inputs, Tensor):
raise RuntimeError('Reduce Operator accepts a Tensor as inputs')
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
output = Tensor.CreateOperator(nout=1, op_type='Reduce', **kwargs)
if inputs.shape is not None:
if axis == -1: output.shape = [1]
else:
output.shape = inputs.shape[:]
if keep_dims: output.shape[axis] = 1
else: del output.shape[axis]
return output
def Proposal(inputs, ratio, scale,
base_size=16, min_size=16, feat_stride=16,
pre_nms_topn=12000, post_nms_topn=2000,
nms_thresh=0.7, **kwargs):
if not isinstance(inputs, list) or len(inputs) is not 3:
raise RuntimeError('Proposal Operator accpets a list of 3 Tensors')
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
output = Tensor.CreateOperator(nout=1, op_type='Proposal', **kwargs)
return output
def Run(inputs, module, op, param_str='', nout=1, **kwargs):
"""
:param inputs: a list of Tensor contains inputs
:param module: a str of the python module
:param op: a str of the operator class
:param param_str: a str of param_str to be used in operator class
:param nout: a int of returned tensors
:return:
"""
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
return Tensor.CreateOperator(op_type='Run', **kwargs)
def Slice(inputs, axis=1, num_output=1, **kwargs):
if not isinstance(inputs, Tensor):
raise RuntimeError('Slice Operator accepts a Tensor as inputs')
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
outputs = Tensor.CreateOperator(op_type='Slice', nout=num_output, **kwargs)
if inputs.shape is not None:
if inputs.shape[axis] is not None:
for i in xrange(len(outputs)):
outputs[i].shape = inputs.shape[:]
outputs[i].shape[axis] /= num_output
return outputs
'inputs': [x, self.weights] +
([hx] if hx else []) +
([cx] if cx else []),
'hidden_size': self.hidden_size,
'num_layers': self.num_layers,
'bidirectional': self.bidirectional,
'rnn_mode': self.mode,
'rnn_input_mode': 'linear',
'dropout_ratio': self.dropout,
}
if required_cell: num_outputs = 3
elif required_hidden: num_outputs = 2
else: num_outputs = 1
return _Tensor.CreateOperator(
num_outputs=num_outputs, **arguments)
def Tile(inputs, multiples, **kwargs):
if not isinstance(inputs, Tensor):
raise RuntimeError('Tile Operator accepts a Tensor as inputs')
args = locals(); kwargs = args['kwargs']
del args['kwargs']; kwargs = dict(args, **kwargs)
output = Tensor.CreateOperator(nout=1, op_type='Tile', **kwargs)
if inputs.shape is not None:
if len(inputs.shape) != len(multiples):
raise ValueError('input ndim is {}, but multiples provide {}'. \
format(len(inputs.shape), len(multiples)))
output.shape = inputs.shape[:]
for i, multiple in enumerate(multiples):
output.shape[i] *= multiple
return output