Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Numpy] implement np.column_stack #16594

Merged
merged 26 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming', 'blackman', 'flip',
'around', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take',
'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal',
'hsplit', 'rot90', 'einsum', 'true_divide']

'hsplit', 'rot90', 'einsum', 'true_divide', 'column_stack']
haojin2 marked this conversation as resolved.
Show resolved Hide resolved

@set_module('mxnet.ndarray.numpy')
def zeros(shape, dtype=_np.float32, order='C', ctx=None):
Expand Down Expand Up @@ -4761,3 +4760,39 @@ def einsum(*operands, **kwargs):
subscripts = operands[0]
operands = operands[1:]
return _npi.einsum(*operands, subscripts=subscripts, out=out, optimize=int(optimize_arg))

haojin2 marked this conversation as resolved.
Show resolved Hide resolved
@set_module('mxnet.ndarray.numpy')
def column_stack(tup):
""" column_stack(*args, **kwargs)

Stack 1-D arrays as columns into a 2-D array.

Take a sequence of 1-D arrays and stack them as columns
to make a single 2-D array. 2-D arrays are stacked as-is,
just like with `hstack`. 1-D arrays are turned into 2-D columns
first.

Parameters
----------
tup : sequence of 1-D or 2-D arrays.
Arrays to stack. All of them must have the same first dimension.

Returns
-------
stacked : 2-D array
The array formed by stacking the given arrays.

See Also
--------
stack, hstack, vstack, concatenate

Examples
--------
>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.column_stack((a,b))
array([[1, 2],
[2, 3],
haojin2 marked this conversation as resolved.
Show resolved Hide resolved
[3, 4]])
"""
return _npi.column_stack(*tup)
38 changes: 37 additions & 1 deletion python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming',
'blackman', 'flip', 'around', 'arctan2', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril',
'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less',
'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide']
'greater_equal', 'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'column_stack']
haojin2 marked this conversation as resolved.
Show resolved Hide resolved

# Return code for dispatching indexing function call
_NDARRAY_UNSUPPORTED_INDEXING = -1
Expand Down Expand Up @@ -6419,3 +6419,39 @@ def einsum(*operands, **kwargs):
... np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a, optimize=True)
"""
return _mx_nd_np.einsum(*operands, **kwargs)

@set_module('mxnet.numpy')
def column_stack(tup):
""" column_stack(*args, **kwargs)
vexilligera marked this conversation as resolved.
Show resolved Hide resolved

Stack 1-D arrays as columns into a 2-D array.

Take a sequence of 1-D arrays and stack them as columns
to make a single 2-D array. 2-D arrays are stacked as-is,
just like with `hstack`. 1-D arrays are turned into 2-D columns
first.

Parameters
----------
tup : sequence of 1-D or 2-D arrays.
Arrays to stack. All of them must have the same first dimension.

Returns
-------
stacked : 2-D array
The array formed by stacking the given arrays.

See Also
--------
stack, hstack, vstack, concatenate

Examples
--------
>>> a = np.array((1,2,3))
vexilligera marked this conversation as resolved.
Show resolved Hide resolved
>>> b = np.array((2,3,4))
>>> np.column_stack((a,b))
array([[1, 2],
vexilligera marked this conversation as resolved.
Show resolved Hide resolved
[2, 3],
[3, 4]])
"""
return _mx_nd_np.column_stack(tup)
1 change: 1 addition & 0 deletions python/mxnet/numpy_dispatch_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def _run_with_array_ufunc_proto(*args, **kwargs):
'var',
'vdot',
'vstack',
# 'column_stack',
haojin2 marked this conversation as resolved.
Show resolved Hide resolved
'zeros_like',
'linalg.norm',
'trace',
Expand Down
37 changes: 36 additions & 1 deletion python/mxnet/symbol/numpy/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming', 'blackman', 'flip',
'around', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take',
'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal',
'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide']
'less_equal', 'hsplit', 'rot90', 'einsum', 'true_divide', 'column_stack']


def _num_outputs(sym):
Expand Down Expand Up @@ -4554,5 +4554,40 @@ def einsum(*operands, **kwargs):
operands = operands[1:]
return _npi.einsum(*operands, subscripts=subscripts, out=out, optimize=int(optimize_arg))

@set_module('mxnet.symbol.numpy')
def column_stack(tup):
""" column_stack(*args, **kwargs)

Stack 1-D arrays as columns into a 2-D array.

Take a sequence of 1-D arrays and stack them as columns
to make a single 2-D array. 2-D arrays are stacked as-is,
just like with `hstack`. 1-D arrays are turned into 2-D columns
first.

Parameters
----------
tup : sequence of 1-D or 2-D arrays.
Arrays to stack. All of them must have the same first dimension.

Returns
-------
stacked : 2-D array
The array formed by stacking the given arrays.

See Also
--------
stack, hstack, vstack, concatenate

Examples
--------
>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.column_stack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])
"""
return _npi.column_stack(*tup)

_set_np_symbol_class(_Symbol)
82 changes: 82 additions & 0 deletions src/operator/numpy/np_matrix_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ struct NumpyVstackParam : public dmlc::Parameter<NumpyVstackParam> {
}
};

struct NumpyColumnStackParam : public dmlc::Parameter<NumpyColumnStackParam> {
int num_args;
DMLC_DECLARE_PARAMETER(NumpyColumnStackParam) {
DMLC_DECLARE_FIELD(num_args).set_lower_bound(1)
.describe("Number of inputs to be column stacked");
}
};

template<typename xpu>
void NumpyTranspose(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
Expand All @@ -71,6 +79,80 @@ void NumpyTranspose(const nnvm::NodeAttrs& attrs,
}
}

template<typename xpu>
void NumpyColumnStackForward(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
const std::vector<TBlob>& inputs,
const std::vector<OpReqType>& req,
const std::vector<TBlob>& outputs) {
haojin2 marked this conversation as resolved.
Show resolved Hide resolved
using namespace mshadow;
using namespace mshadow_op;

const NumpyColumnStackParam& param = nnvm::get<NumpyColumnStackParam>(attrs.parsed);
CHECK_EQ(inputs.size(), param.num_args);
CHECK_EQ(outputs.size(), 1);
CHECK_EQ(req.size(), 1);

// reshape if necessary
std::vector<TBlob> data(param.num_args);
for (int i = 0; i < param.num_args; i++) {
if (inputs[i].shape_.ndim() == 0 || inputs[i].shape_.ndim() == 1) {
// TShape shape = Shape2(1, inputs[i].shape_.Size());
vexilligera marked this conversation as resolved.
Show resolved Hide resolved
TShape shape = Shape2(inputs[i].shape_.Size(), 1);
data[i] = inputs[i].reshape(shape);
} else {
data[i] = inputs[i];
}
}

// initialize ConcatOp
ConcatParam cparam;
cparam.num_args = param.num_args;
// cparam.dim = 0;
cparam.dim = 1;
MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, DType, {
ConcatOp<xpu, DType> op;
op.Init(cparam);
op.Forward(ctx, data, req, outputs);
});
}

template<typename xpu>
void NumpyColumnStackBackward(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
const std::vector<TBlob>& inputs,
const std::vector<OpReqType>& req,
const std::vector<TBlob>& outputs) {
using namespace mshadow;
using namespace mshadow_op;

const NumpyColumnStackParam& param = nnvm::get<NumpyColumnStackParam>(attrs.parsed);
CHECK_EQ(inputs.size(), 1);
CHECK_EQ(outputs.size(), param.num_args);
CHECK_EQ(req.size(), param.num_args);

// reshape if necessary
std::vector<TBlob> data(param.num_args);
for (int i = 0; i < param.num_args; i++) {
if (outputs[i].shape_.ndim() == 0 || outputs[i].shape_.ndim() == 1) {
TShape shape = Shape2(outputs[i].shape_.Size(), 1);
data[i] = outputs[i].reshape(shape);
} else {
data[i] = outputs[i];
}
}

// initialize ConcatOp
ConcatParam cparam;
cparam.num_args = param.num_args;
cparam.dim = 1;
MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, DType, {
ConcatOp<xpu, DType> op;
op.Init(cparam);
op.Backward(ctx, inputs[0], req, data);
});
}

template<typename xpu>
void NumpyVstackForward(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
Expand Down
Loading