You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/01/14 15:30:18 UTC

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic

xidulu commented on a change in pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r366404838
 
 

 ##########
 File path: src/operator/numpy/random/np_logistic_op.h
 ##########
 @@ -0,0 +1,331 @@
+/*
+ * 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_logistic_op.h
+ * \brief Operator for numpy sampling from logistic distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLogisticParam : public dmlc::Parameter<NumpyLogisticParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+        .set_default(dmlc::optional<mxnet::Tuple<int>>())
+        .describe(
+            "Output shape. If the given shape is, "
+            "e.g., (m, n, k), then m * n * k samples are drawn. "
+            "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLogisticOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyLogisticParam &param = nnvm::get<NumpyLogisticParam>(attrs.parsed);
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logical_kernel {
 
 Review comment:
   Better be `logistic_kernel {...`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services