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 2019/05/18 08:11:23 UTC

[GitHub] [incubator-mxnet] canerturkmen commented on a change in pull request #14683: Adding loss operator of a Hawkes self-exciting process

canerturkmen commented on a change in pull request #14683: Adding loss operator of a Hawkes self-exciting process
URL: https://github.com/apache/incubator-mxnet/pull/14683#discussion_r285333621
 
 

 ##########
 File path: src/operator/contrib/hawkes_ll-inl.h
 ##########
 @@ -0,0 +1,493 @@
+/*
+ * 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) 2018 by Contributors
+ * \file hawkes_ll-inl.h
+ * \brief Log likelihood of a marked self-exciting Hawkes process
+ * \author Caner Turkmen <tu...@gmail.com>
+ */
+#ifndef MXNET_OPERATOR_CONTRIB_HAWKES_LL_INL_H_
+#define MXNET_OPERATOR_CONTRIB_HAWKES_LL_INL_H_
+
+#include <mxnet/operator.h>
+#include <vector>
+
+#include "../operator_common.h"
+#include "../mshadow_op.h"
+#include "../mxnet_op.h"
+
+namespace mxnet {
+namespace op {
+
+namespace hawkesll {
+  enum HawkesLLOpInputs {kMu, kAlpha, kBeta, kState, kIATimes, kMarks,
+                         kValidLength, kMaxTime};
+  enum HawkesLLGradInputs {kOutGradLL, kOutGradStates, kGradMu, kGradAlpha,
+                           kGradBeta, kGradState, kGradIATimes, kGradMarks,
+                           kGradValidLength, kGradMaxTime};
+  enum HawkesLLOpOutputs {kOutLL, kOutStates};
+  enum HawkesLLOpResource {kTempSpace};
+}  // namespace hawkesll
+
+inline bool HawkesLLOpType(const nnvm::NodeAttrs& attrs,
+                            std::vector<int>* in_attrs,
+                            std::vector<int>* out_attrs) {
+  // check dimensions of the type vectors
+  CHECK_EQ(in_attrs->size(), 8U);
+  CHECK_EQ(out_attrs->size(), 2U);
+
+  TYPE_ASSIGN_CHECK(*out_attrs, hawkesll::kOutLL, in_attrs->at(0))
+  TYPE_ASSIGN_CHECK(*out_attrs, hawkesll::kOutStates, in_attrs->at(0))
+
+  for (index_t j = 0; j < 8; ++j) {
+    if (j != hawkesll::kMarks) {
+      TYPE_ASSIGN_CHECK(*in_attrs, j, out_attrs->at(0))
+    }
+  }
+  TYPE_ASSIGN_CHECK(*in_attrs, hawkesll::kMarks, 4)  // int32
+
+  return out_attrs->at(hawkesll::kOutLL) != -1;
+}
+
+inline bool HawkesLLOpShape(const nnvm::NodeAttrs& attrs,
+                             std::vector<TShape>* in_attrs,
+                             std::vector<TShape>* out_attrs) {
+  using namespace mshadow;
+  int N, T, K;
+
+  CHECK_EQ(in_attrs->size(), 8U);
+  CHECK_EQ(out_attrs->size(), 2U);
+
+  // check ndims
+  CHECK_EQ(in_attrs->at(hawkesll::kMu).ndim(), 2);  // mu (N, K)
+  CHECK_EQ(in_attrs->at(hawkesll::kAlpha).ndim(), 1);  // branching ratio (K,)
+  CHECK_EQ(in_attrs->at(hawkesll::kBeta).ndim(), 1);  // decay exponent (K,)
+  CHECK_EQ(in_attrs->at(hawkesll::kState).ndim(), 2);  // Hawkes states (N, K)
+  CHECK_EQ(in_attrs->at(hawkesll::kIATimes).ndim(), 2);  // i.a. times  (N, T)
+  CHECK_EQ(in_attrs->at(hawkesll::kMarks).ndim(), 2);  // marks (N, T)
+  CHECK_EQ(in_attrs->at(hawkesll::kValidLength).ndim(), 1);  // valid len (N,)
+  CHECK_EQ(in_attrs->at(hawkesll::kMaxTime).ndim(), 1);  // max_time (N,)
+
+  N = in_attrs->at(hawkesll::kIATimes)[0];  // number of samples in batch
+  T = in_attrs->at(hawkesll::kIATimes)[1];  // time length
+  K = in_attrs->at(hawkesll::kMu)[1];  // number of marks
+
+  // check inputs consistent
+  CHECK_EQ(in_attrs->at(hawkesll::kMu)[0], N);
+  CHECK_EQ(in_attrs->at(hawkesll::kMu)[1], K);
+  CHECK_EQ(in_attrs->at(hawkesll::kAlpha)[0], K);
+  CHECK_EQ(in_attrs->at(hawkesll::kBeta)[0], K);
+  CHECK_EQ(in_attrs->at(hawkesll::kState)[0], N);
+  CHECK_EQ(in_attrs->at(hawkesll::kState)[1], K);
+  CHECK_EQ(in_attrs->at(hawkesll::kMarks)[0], N);
+  CHECK_EQ(in_attrs->at(hawkesll::kMarks)[1], T);
+  CHECK_EQ(in_attrs->at(hawkesll::kValidLength)[0], N);
+  CHECK_EQ(in_attrs->at(hawkesll::kMaxTime)[0], N);
+
+  // infer output type
+  SHAPE_ASSIGN_CHECK(*out_attrs, hawkesll::kOutLL, Shape1(N))
+  SHAPE_ASSIGN_CHECK(*out_attrs, hawkesll::kOutStates, Shape2(N, K))
+
+  return out_attrs->at(hawkesll::kOutLL).ndim() != 0U &&
+    out_attrs->at(hawkesll::kOutStates).Size() != 0U;
+}
+
+template<int req>
+struct hawkesll_forward {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i,
+                                  DType* out_loglike,
+                                  DType* out_state,
+                                  const DType* mu,
+                                  const DType* alpha,
+                                  const DType* beta,
+                                  DType* state,
+                                  const DType* lags,
+                                  const int32_t* marks,
+                                  DType* valid_length,
+                                  DType* max_time,
+                                  int K,
+                                  int T,
+                                  DType* temp_register
+                                  ) {
+    int32_t ci;  // current mark
+    DType ll = 0;  // log likelihood
+    DType t = 0;  // current time
+    DType d, lda, comp;  //
+    DType *last_ = &temp_register[i * K];
+    const DType *lag_ = &lags[i * T];
+    const int32_t *mark_ = &marks[i * T];
+    DType *state_ = &out_state[i * K];
+
+    // iterate over points in sequence
+    for (index_t j = 0; j < valid_length[i]; ++j) {
+      ci = mark_[j];
+      t += lag_[j];
+      d = t - last_[ci];
+
 
 Review comment:
   Thank you for pointing them out! I think this current version gets rid of those redundancies.

----------------------------------------------------------------
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