daveliepmann commented on a change in pull request #14769: [Clojure] Add Fine 
Tuning Sentence Pair Classification BERT Example
URL: https://github.com/apache/incubator-mxnet/pull/14769#discussion_r280027757
 
 

 ##########
 File path: 
contrib/clojure-package/examples/bert/src/bert/bert_sentence_classification.clj
 ##########
 @@ -0,0 +1,161 @@
+;;
+;; 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.
+;;
+
+(ns bert.bert-sentence-classification
+  (:require [bert.util :as bert-util]
+            [clojure-csv.core :as csv]
+            [clojure.string :as string]
+            [org.apache.clojure-mxnet.callback :as callback]
+            [org.apache.clojure-mxnet.context :as context]
+            [org.apache.clojure-mxnet.dtype :as dtype]
+            [org.apache.clojure-mxnet.io :as mx-io]
+            [org.apache.clojure-mxnet.layout :as layout]
+            [org.apache.clojure-mxnet.module :as m]
+            [org.apache.clojure-mxnet.ndarray :as ndarray]
+            [org.apache.clojure-mxnet.optimizer :as optimizer]
+            [org.apache.clojure-mxnet.symbol :as sym]))
+
+(def model-path-prefix "data/static_bert_base_net")
+;; epoch number of the model
+(def epoch 0)
+;; the vocabulary used in the model
+(def model-vocab "data/vocab.json")
+;; the input question
+;; the maximum length of the sequence
+(def seq-length 128)
+
+(defn pre-processing
+  "Preprocesses the sentences in the format that BERT is expecting"
+  [ctx idx->token token->idx train-item]
+  (let [[sentence-a sentence-b label] train-item
+       ;;; pre-processing tokenize sentence
+        token-1 (bert-util/tokenize (string/lower-case sentence-a))
+        token-2 (bert-util/tokenize (string/lower-case sentence-b))
+        valid-length (+ (count token-1) (count token-2))
+        ;;; generate token types [0000...1111...0000]
+        qa-embedded (into (bert-util/pad [] 0 (count token-1))
+
+                          (bert-util/pad [] 1 (count token-2)))
+        token-types (bert-util/pad qa-embedded 0 seq-length)
+        ;;; make BERT pre-processing standard
+        token-2 (conj token-2 "[SEP]")
+        token-1 (into [] (concat ["[CLS]"] token-1 ["[SEP]"] token-2))
+        tokens (bert-util/pad token-1 "[PAD]" seq-length)
+        ;;; pre-processing - token to index translation
+        indexes (bert-util/tokens->idxs token->idx tokens)]
+    {:input-batch [indexes
+                   token-types
+                   [valid-length]]
+     :label (if (= "0" label)
+              [0]
+              [1])
+     :tokens tokens
+     :train-item train-item}))
+
+(defn fine-tune-model
+  "msymbol: the pretrained network symbol
+   num-classes: the number of classes for the fine-tune datasets
+   dropout: The dropout rate amount"
+  [msymbol {:keys [num-classes dropout]}]
+  (as-> msymbol data
+    (sym/dropout {:data data :p dropout})
+    (sym/fully-connected "fc-finetune" {:data data :num-hidden num-classes})
+    (sym/softmax-output "softmax" {:data data})))
+
+(defn slice-inputs-data
+  "Each sentence pair had to be processed as a row. This breaks all
+  the rows up into a column for creating a NDArray"
+  [processed-datas n]
+  (->> processed-datas
+       (mapv #(nth (:input-batch %) n))
+       (flatten)
+       (into [])))
+
+(defn get-raw-data []
+  (csv/parse-csv (slurp "data/dev.tsv") :delimiter \tab))
+
+(defn prepare-data
+  "This prepares the senetence pairs into NDArrays for use in NDArrayIterator"
+  [dev]
 
 Review comment:
   `prepare-data` only uses its parameter `dev` to pass to the `pre-processing` 
fn, which doesn't use it, so I think `prepare-data` could take 0 arguments.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to