forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Numpy compatible multinomial (apache#15219)
* draft of multinomial * rename to more concise name * finish shape * complete the forward function * complete forward without handle 0 dimension & scalar * handle 0 dimension * add new line * fix lint * fix the build error * fix lint * finish unit test * change the registration * make multinomial support pvals as mx.ndarray * delete newline * fix lint error * support input as list, mx.ndarray, np.ndarray & unit test * fix lint * fix the include error * fix lint * refactor & pass the tensor instead of tuple to kernel * fix lint * updata the doc * address the comment
- Loading branch information
Showing
7 changed files
with
434 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2019 by Contributors | ||
* \file np_multinomial_op.h | ||
* \brief Operator for numpy sampling from multinomial distributions | ||
*/ | ||
#include "./np_multinomial_op.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
DMLC_REGISTER_PARAMETER(NumpyMultinomialParam); | ||
|
||
NNVM_REGISTER_OP(_npi_multinomial) | ||
.describe(R"code(Draw samples from a multinomial distribution. " | ||
"The multinomial distribution is a multivariate generalisation of the binomial distribution. " | ||
"Take an experiment with one of p possible outcomes. " | ||
"An example of such an experiment is throwing a dice, where the outcome can be 1 through 6. " | ||
"Each sample drawn from the distribution represents n such experiments. " | ||
"Its values, X_i = [X_0, X_1, ..., X_p], represent the number of times the outcome was i. | ||
)code") | ||
.set_num_inputs( | ||
[](const nnvm::NodeAttrs& attrs) { | ||
const NumpyMultinomialParam& param = nnvm::get<NumpyMultinomialParam>(attrs.parsed); | ||
return param.pvals.has_value() ? 0U : 1U; | ||
} | ||
) | ||
.set_num_outputs(1) | ||
.set_attr_parser(ParamParser<NumpyMultinomialParam>) | ||
.set_attr<mxnet::FInferShape>("FInferShape", NumpyMultinomialOpShape) | ||
.set_attr<nnvm::FInferType>("FInferType", NumpyMultinomialOpType) | ||
.set_attr<FResourceRequest>("FResourceRequest", | ||
[](const nnvm::NodeAttrs& attrs) { | ||
return std::vector<ResourceRequest>{ | ||
ResourceRequest::kRandom, ResourceRequest::kTempSpace}; | ||
}) | ||
.set_attr<FCompute>("FCompute<cpu>", NumpyMultinomialForward<cpu>) | ||
.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) | ||
.add_argument("a", "NDArray-or-Symbol", "Source input") | ||
.add_arguments(NumpyMultinomialParam::__FIELDS__()); | ||
|
||
} // namespace op | ||
} // namespace mxnet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2019 by Contributors | ||
* \file np_multinomial_op.cu | ||
* \brief Operator for numpy sampling from multinomial distributions | ||
*/ | ||
#include "./np_multinomial_op.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
NNVM_REGISTER_OP(_npi_multinomial) | ||
.set_attr<FCompute>("FCompute<gpu>", NumpyMultinomialForward<gpu>); | ||
|
||
} // namespace op | ||
} // namespace mxnet |
Oops, something went wrong.