You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemall.apache.org by my...@apache.org on 2021/03/25 08:46:08 UTC
[incubator-hivemall] branch HIVEMALL-301-tfidf created (now 556329d)
This is an automated email from the ASF dual-hosted git repository.
myui pushed a change to branch HIVEMALL-301-tfidf
in repository https://gitbox.apache.org/repos/asf/incubator-hivemall.git.
at 556329d Removed macros
This branch includes the following new commits:
new 1a62e76 Replaced tfidf macro with UDF
new 556329d Removed macros
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[incubator-hivemall] 02/02: Removed macros
Posted by my...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
myui pushed a commit to branch HIVEMALL-301-tfidf
in repository https://gitbox.apache.org/repos/asf/incubator-hivemall.git
commit 556329d7d2f850b4387ca1220e335df040ea51a9
Author: Makoto Yui <my...@apache.org>
AuthorDate: Thu Mar 25 17:45:00 2021 +0900
Removed macros
---
resources/ddl/define-all.hive | 19 ----------------
resources/ddl/define-macros.hive | 49 ----------------------------------------
2 files changed, 68 deletions(-)
diff --git a/resources/ddl/define-all.hive b/resources/ddl/define-all.hive
index b55c59b..8bf36e8 100644
--- a/resources/ddl/define-all.hive
+++ b/resources/ddl/define-all.hive
@@ -893,22 +893,3 @@ create temporary function min_by as 'hivemall.tools.aggr.MinByUDAF';
drop temporary function if exists majority_vote;
create temporary function majority_vote as 'hivemall.tools.aggr.MajorityVoteUDAF';
-
-
---------------------------------------------------------------------------------------------------
--- macros available from hive 0.12.0
--- see https://issues.apache.org/jira/browse/HIVE-2655
-
---------------------
--- General Macros --
---------------------
-
-create temporary macro java_min(x DOUBLE, y DOUBLE)
-reflect("java.lang.Math", "min", x, y);
-
-create temporary macro max2(x DOUBLE, y DOUBLE)
-if(x>y,x,y);
-
-create temporary macro min2(x DOUBLE, y DOUBLE)
-if(x<y,x,y);
-
diff --git a/resources/ddl/define-macros.hive b/resources/ddl/define-macros.hive
deleted file mode 100644
index 84c4e06..0000000
--- a/resources/ddl/define-macros.hive
+++ /dev/null
@@ -1,49 +0,0 @@
------------------------------------------------------------------------------
--- Hivemall: Hive scalable Machine Learning Library
------------------------------------------------------------------------------
-
--- macros available from hive 0.12.0
--- see https://issues.apache.org/jira/browse/HIVE-2655
-
---------------------
--- General Macros --
---------------------
-
-create temporary macro java_min(x DOUBLE, y DOUBLE)
-reflect("java.lang.Math", "min", x, y);
-
-create temporary macro max2(x DOUBLE, y DOUBLE)
-if(x>y,x,y);
-
-create temporary macro min2(x DOUBLE, y DOUBLE)
-if(x<y,x,y);
-
---------------------------
--- Evaluation functions --
---------------------------
-
--- CAUTION: UDAF in macro is not yet support supported in Hive
-
--- Root Mean Squared Error
--- create temporary macro RMSE(predicted FLOAT, actual FLOAT)
--- sqrt(sum(pow(predicted - actual,2.0))/count(1));
-
--- Mean Squared Error
--- create temporary macro MSE(predicted FLOAT, actual FLOAT)
--- sum(pow(predicted - actual,2.0))/count(1);
-
--- Mean Absolute Error
--- create temporary macro MAE(predicted FLOAT, actual FLOAT)
--- sum(abs(predicted - actual))/count(1);
-
--- sum of squared errors
--- create temporary macro SSE(predicted FLOAT, actual FLOAT)
--- sum(pow(actual - predicted,2.0));
-
--- sum of squared total
--- create temporary macro SST(actual FLOAT, mean_actual FLOAT)
--- sum(pow(actual-mean_actual,2.0));
-
--- coefficient of determination (R^2)
--- create temporary macro R2(predicted FLOAT, actual FLOAT, mean_actual FLOAT)
--- 1 - (SSE(predicted, actual) / SST(actual, mean_actual));
\ No newline at end of file
[incubator-hivemall] 01/02: Replaced tfidf macro with UDF
Posted by my...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
myui pushed a commit to branch HIVEMALL-301-tfidf
in repository https://gitbox.apache.org/repos/asf/incubator-hivemall.git
commit 1a62e7645a7b80bd16a15a0e3e567b08940ad2be
Author: Makoto Yui <my...@apache.org>
AuthorDate: Thu Mar 25 17:42:55 2021 +0900
Replaced tfidf macro with UDF
---
.../main/java/hivemall/ftvec/text/TfIdfUDF.java | 104 +++++++++++++++++++++
resources/ddl/define-all-as-permanent.hive | 4 +
resources/ddl/define-all.hive | 12 +--
resources/ddl/define-all.spark | 3 +
resources/ddl/define-macros.hive | 10 --
5 files changed, 114 insertions(+), 19 deletions(-)
diff --git a/core/src/main/java/hivemall/ftvec/text/TfIdfUDF.java b/core/src/main/java/hivemall/ftvec/text/TfIdfUDF.java
new file mode 100644
index 0000000..f7cbb3b
--- /dev/null
+++ b/core/src/main/java/hivemall/ftvec/text/TfIdfUDF.java
@@ -0,0 +1,104 @@
+/*
+ * 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.
+ */
+package hivemall.ftvec.text;
+
+import hivemall.utils.hadoop.HiveUtils;
+import hivemall.utils.lang.StringUtils;
+
+import javax.annotation.Nonnegative;
+import javax.annotation.Nonnull;
+
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.udf.UDFType;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
+
+@Description(name = "tfidf",
+ value = "_FUNC_(double termFrequency, long numDocs, const long totalNumDocs) "
+ + "- Return an TFIDF score in double.")
+@UDFType(deterministic = true, stateful = false)
+public final class TfIdfUDF extends GenericUDF {
+
+ private DoubleObjectInspector tfOI;
+ private LongObjectInspector numDocsOI;
+ private LongObjectInspector totalNumDocsOI;
+
+ @Nonnull
+ private final DoubleWritable result = new DoubleWritable();
+
+ @Override
+ public ObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
+ if (argOIs.length != 3) {
+ throw new UDFArgumentLengthException(
+ "tfidf takes exactly three arguments but got " + argOIs.length);
+ }
+
+ this.tfOI = HiveUtils.asDoubleOI(argOIs[0]);
+ this.numDocsOI = HiveUtils.asLongOI(argOIs[1]);
+ this.totalNumDocsOI = HiveUtils.asLongOI(argOIs[2]);
+
+ return PrimitiveObjectInspectorFactory.writableDoubleObjectInspector;
+ }
+
+
+ @Override
+ public Object evaluate(DeferredObject[] arguments) throws HiveException {
+ Object arg0 = getObject(arguments, 0);
+ Object arg1 = getObject(arguments, 1);
+ Object arg2 = getObject(arguments, 2);
+
+ double tf = PrimitiveObjectInspectorUtils.getDouble(arg0, tfOI);
+ long numDocs = PrimitiveObjectInspectorUtils.getLong(arg1, numDocsOI);
+ long totalNumDocs = PrimitiveObjectInspectorUtils.getLong(arg2, totalNumDocsOI);
+
+ // basic IDF
+ // idf = log(N/n_t)
+ // IDF with smoothing
+ // idf = log(N/(1+n_t))+1
+ // idf = log(N/max(1,n_t))+1 -- about zero division
+ double idf = (Math.log10(totalNumDocs / Math.max(1L, numDocs)) + 1.0d);
+ double tfidf = tf * idf;
+ result.set(tfidf);
+ return result;
+ }
+
+ @Nonnull
+ protected static Object getObject(@Nonnull final DeferredObject[] arguments,
+ @Nonnegative final int index) throws HiveException {
+ Object obj = arguments[index].get();
+ if (obj == null) {
+ throw new UDFArgumentException(String.format("%d-th argument MUST not be null", index));
+ }
+ return obj;
+ }
+
+ @Override
+ public String getDisplayString(String[] children) {
+ return "tfidf(" + StringUtils.join(children, ',') + ")";
+ }
+
+}
diff --git a/resources/ddl/define-all-as-permanent.hive b/resources/ddl/define-all-as-permanent.hive
index f995d55..c5f2669 100644
--- a/resources/ddl/define-all-as-permanent.hive
+++ b/resources/ddl/define-all-as-permanent.hive
@@ -349,6 +349,9 @@ CREATE FUNCTION tf as 'hivemall.ftvec.text.TermFrequencyUDAF' USING JAR '${hivem
DROP FUNCTION IF EXISTS bm25;
CREATE FUNCTION bm25 as 'hivemall.ftvec.text.OkapiBM25UDF' USING JAR '${hivemall_jar}';
+DROP FUNCTION IF EXISTS tfidf;
+CREATE FUNCTION tfidf as 'hivemall.ftvec.text.TfIdfUDF' USING JAR '${hivemall_jar}';
+
--------------------------
-- Regression functions --
--------------------------
@@ -920,3 +923,4 @@ CREATE FUNCTION xgboost_predict_one AS 'hivemall.xgboost.XGBoostPredictOneUDTF'
DROP FUNCTION xgboost_predict_triple;
CREATE FUNCTION xgboost_predict_triple AS 'hivemall.xgboost.XGBoostPredictTripleUDTF' USING JAR '${hivemall_jar}';
+
diff --git a/resources/ddl/define-all.hive b/resources/ddl/define-all.hive
index bf9bc7c..b55c59b 100644
--- a/resources/ddl/define-all.hive
+++ b/resources/ddl/define-all.hive
@@ -345,6 +345,9 @@ create temporary function tf as 'hivemall.ftvec.text.TermFrequencyUDAF';
drop temporary function if exists bm25;
create temporary function bm25 as 'hivemall.ftvec.text.OkapiBM25UDF';
+drop temporary function if exists tfidf;
+create temporary function tfidf as 'hivemall.ftvec.text.TfIdfUDF';
+
--------------------------
-- Regression functions --
--------------------------
@@ -909,12 +912,3 @@ if(x>y,x,y);
create temporary macro min2(x DOUBLE, y DOUBLE)
if(x<y,x,y);
---------------------------
--- Statistics functions --
---------------------------
-
-create temporary macro idf(df_t DOUBLE, n_docs DOUBLE)
-log(10, n_docs / max2(1,df_t)) + 1.0;
-
-create temporary macro tfidf(tf FLOAT, df_t DOUBLE, n_docs DOUBLE)
-tf * (log(10, n_docs / max2(1,df_t)) + 1.0);
diff --git a/resources/ddl/define-all.spark b/resources/ddl/define-all.spark
index 8529134..91c6350 100644
--- a/resources/ddl/define-all.spark
+++ b/resources/ddl/define-all.spark
@@ -348,6 +348,9 @@ sqlContext.sql("CREATE TEMPORARY FUNCTION tf AS 'hivemall.ftvec.text.TermFrequen
sqlContext.sql("DROP TEMPORARY FUNCTION IF EXISTS bm25")
sqlContext.sql("CREATE TEMPORARY FUNCTION bm25 AS 'hivemall.ftvec.text.OkapiBM25UDF'")
+sqlContext.sql("DROP TEMPORARY FUNCTION IF EXISTS tfidf")
+sqlContext.sql("CREATE TEMPORARY FUNCTION tfidf AS 'hivemall.ftvec.text.TfIdfUDF'")
+
/**
* Regression functions
*/
diff --git a/resources/ddl/define-macros.hive b/resources/ddl/define-macros.hive
index ff36a44..84c4e06 100644
--- a/resources/ddl/define-macros.hive
+++ b/resources/ddl/define-macros.hive
@@ -19,16 +19,6 @@ create temporary macro min2(x DOUBLE, y DOUBLE)
if(x<y,x,y);
--------------------------
--- Statistics functions --
---------------------------
-
-create temporary macro idf(df_t DOUBLE, n_docs DOUBLE)
-log(10, n_docs / max2(1,df_t)) + 1.0;
-
-create temporary macro tfidf(tf FLOAT, df_t DOUBLE, n_docs DOUBLE)
-tf * (log(10, n_docs / max2(1,df_t)) + 1.0);
-
---------------------------
-- Evaluation functions --
--------------------------