-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmain.go
231 lines (205 loc) · 7.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (c) 2016 Salle, Alexandre <[email protected]>
* Author: Salle, Alexandre <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"runtime/pprof"
"strings"
)
type (
idxUint = uint32
countUint = uint32
real = float64
)
const (
uint32Bytes = 4
float64Bytes = 8
errorLogLevel = 0
infoLogLevel = 1
debugLogLevel = 2
ctxBreakToken = "</s>"
maxSentenceLen = 1000
contextPathSuffix = ".context"
defaultProgressInterval = 10000
vocabCommand = "vocab"
trainCommand = "train"
externalCoocCommand = "cooc"
externalTrainCommand = "trainem"
oovCommand = "embed"
)
// GLOBAL VARS
// general
var verbose int
var randng *rand.Rand
// paths
var vocabPath string
var subwordPath string
var corpusPath, coocPath, coocTotalsPath string
var vectorOutputPath string
var subvecsOutputPath string
// vocab
var corpusSize, rawCorpusSize uint64
var vocab map[string]*word
var vocabSize idxUint
var vocabList []*word
var ctxVocab map[string]*word
var ctxVocabSize idxUint
var ctxVocabList []*word
var minFreq countUint
var maxVocab idxUint
var positionalContexts, periodIsWhitespace bool
var ctxbreakw *word
var ctxbreakbytes []byte
// subword
var subwordMinN, subwordMaxN int
var buckets int
// cooc and sampling
var unigramPower real
var contextDistributionSmoothing, cdsTotal, logCdsTotal, subsample real
var window int
var weightedWindow bool
var negative int
var coocStorage matrix
var lineBufMem real
var noiseSampler sampler
// sgd
var model int
var mVec, mCtx []real
var numThreads int
var initialAlpha real
var iterations int
var dim idxUint
var subwordMatrixRows idxUint
var associationMeasure associationMeasureFunc
var clipPmi real
var processStrategy processStrategyFunc
var processThreshold real
func init() {
ctxbreakbytes = []byte(ctxBreakToken)
}
func main() {
randng = rand.New(rand.NewSource(1))
flags := flag.NewFlagSet("default", flag.ExitOnError)
flags.StringVar(&corpusPath, "corpus", "", "path to corpus")
flags.StringVar(&vocabPath, "vocab", "", "path where to output/load vocab")
flags.IntVar(&subwordMinN, "minn", 3, "mininum ngram length when generating subwords")
flags.IntVar(&subwordMaxN, "maxn", 6, "maximum ngram length when generating subwords")
flags.IntVar(&buckets, "buckets", 2000000, "subword buckets")
flags.StringVar(&subwordPath, "subword", "", "path to subword information")
flags.Float64Var(&initialAlpha, "alpha", 0.025, "learning rate")
flags.Float64Var(&subsample, "subsample", 1e-5, "subsampling threshold")
flags.Float64Var(&contextDistributionSmoothing, "cds", 0.75, "context distribution smoothing")
var dimRaw = flags.Int("dim", 300, "number of dimensions of word vectors")
flags.IntVar(&iterations, "iterations", 5, "how many times to process corpus")
flags.IntVar(&window, "window", 2, "symmetric window of (window, word, window)")
var minFreqRaw = flags.Int("minfreq", 100, "remove from vocab words that occur less that this number of times")
var maxVocabRaw = flags.Int("maxvocab", 0, "max vocab size, 0 for no limit")
flags.IntVar(&negative, "negative", 5, "number of negative samples")
flags.Float64Var(&unigramPower, "unigrampow", 0.75, "raise unigram dist to this power")
flags.BoolVar(&weightedWindow, "weightwindow", false, "use randomized window size from uniform(1, window)")
flags.IntVar(&model, "model", 1, "0 = output W, C; 1 = output W; 2 = output W + C")
flags.IntVar(&numThreads, "threads", 12, "number of threads to use")
var matrixStrings []string
for matrix := range associationMap {
matrixStrings = append(matrixStrings, matrix)
}
var associationMeasureString = flags.String("matrix", "cpmi", "which matrix to factor ("+strings.Join(matrixStrings, ",")+")")
flags.Float64Var(&clipPmi, "clip", 0, "clip value for cPMI (default of 0 gives PPMI) default = 0")
var processFuncStrings []string
for processFunc := range processStrategyMap {
processFuncStrings = append(processFuncStrings, processFunc)
}
var processFuncString = flags.String("process", "all", "which matrix cells to factor ("+strings.Join(processFuncStrings, ",")+")")
flags.Float64Var(&processThreshold, "processthreshold", 0, "threshold for -process flag (ex. 1: -process gt and -processthreshold 0 means only process cells > 0, ex. 2: -process leq and -processthreshold 0 means only process cells <= 0)")
flags.IntVar(&verbose, "verbose", debugLogLevel, "verboseness (0 = errors only, 1 = info, 2 = debug)")
flags.StringVar(&coocTotalsPath, "cooctotalspath", "", "path to cooc totals for each word when using external memory")
flags.StringVar(&coocPath, "coocpath", "", "path to coocs when using external memory")
flags.Float64Var(&lineBufMem, "memory", 4, "GB of memory to use in line buffer")
flags.BoolVar(&positionalContexts, "pos", true, "use positional contexts")
flags.StringVar(&vectorOutputPath, "output", "", "where to save vectors")
flags.StringVar(&subvecsOutputPath, "outputsub", "", "where to save binary subword vectors")
flags.BoolVar(&periodIsWhitespace, "periodiswhitespace", false, "treat period as whitespace")
var cpuprofile = flags.String("cpuprofile", "", "write cpu profile to file")
flags.Usage = func() {
fmt.Printf("Usage: lexvec [command] [options]\n" +
"Commands: vocab, cooc, train, trainem, embed\n" +
"Options:\n")
flags.PrintDefaults()
}
if len(os.Args) < 2 {
flags.Usage()
os.Exit(1)
}
command := os.Args[1]
flags.Parse(os.Args[2:])
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
check(err)
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
dim = idxUint(*dimRaw)
minFreq = countUint(*minFreqRaw)
maxVocab = idxUint(*maxVocabRaw)
var ok bool
associationMeasure, ok = associationMap[*associationMeasureString]
if !ok {
logln(errorLogLevel, "invalid option for -matrix")
}
processStrategy, ok = processStrategyMap[*processFuncString]
if !ok {
logln(errorLogLevel, "invalid option for -process")
}
switch command {
case vocabCommand:
buildVocab()
saveVocab()
case trainCommand:
readVocab()
processSubwords()
buildCoocMatrix()
calculateCdsTotalAndLogs()
initModel()
train(newTrainIteratorIM())
saveVectors()
case externalCoocCommand:
readVocab()
buildCoocFile()
case externalTrainCommand:
readVocab()
processSubwords()
readCoocTotals()
calculateCdsTotalAndLogs()
initModel()
train(newTrainIteratorEM())
saveVectors()
case oovCommand:
calculateOovVectors()
default:
flags.Usage()
os.Exit(1)
}
logln(infoLogLevel, "finished!")
}