-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add random_seed
to LdaMallet
#2153
Changes from 1 commit
75c25ee
1bfaf48
2ca085c
645863c
d8e0f54
53182a0
6b2d52b
a5db1df
3ef309f
a6af553
53136a1
0b3d7aa
7fa5069
b001cd3
f3bd927
a1e73b6
86ede21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ class LdaMallet(utils.SaveLoad, basemodel.BaseTopicModel): | |
|
||
""" | ||
def __init__(self, mallet_path, corpus=None, num_topics=100, alpha=50, id2word=None, workers=4, prefix=None, | ||
optimize_interval=0, iterations=1000, topic_threshold=0.0): | ||
optimize_interval=0, iterations=1000, topic_threshold=0.0, random_seed=None): | ||
""" | ||
|
||
Parameters | ||
|
@@ -100,6 +100,8 @@ def __init__(self, mallet_path, corpus=None, num_topics=100, alpha=50, id2word=N | |
Number of training iterations. | ||
topic_threshold : float, optional | ||
Threshold of the probability above which we consider a topic. | ||
random_seed: int, optional | ||
Random seed to ensure consistent results, default is None | ||
|
||
""" | ||
self.mallet_path = mallet_path | ||
|
@@ -122,6 +124,7 @@ def __init__(self, mallet_path, corpus=None, num_topics=100, alpha=50, id2word=N | |
self.workers = workers | ||
self.optimize_interval = optimize_interval | ||
self.iterations = iterations | ||
self.random_seed = random_seed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. define custom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check I have coded and placed this correctly... Should I include logging as in the example? Training with random_seed - what is required there? |
||
if corpus is not None: | ||
self.train(corpus) | ||
|
||
|
@@ -268,11 +271,16 @@ def train(self, corpus): | |
cmd = self.mallet_path + ' train-topics --input %s --num-topics %s --alpha %s --optimize-interval %s '\ | ||
'--num-threads %s --output-state %s --output-doc-topics %s --output-topic-keys %s '\ | ||
'--num-iterations %s --inferencer-filename %s --doc-topics-threshold %s' | ||
|
||
if self.random_seed != None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reason for None is so that random seed is not invoked unless a value is explicitly passed, which can be zero. If I test just for if self.random_seed and zero is passed, will it enter this if - a zero is a valid random_seed value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, in this case, should be |
||
cmd += ' --random-seed ' + str(self.random_seed) | ||
|
||
cmd = cmd % ( | ||
self.fcorpusmallet(), self.num_topics, self.alpha, self.optimize_interval, | ||
self.workers, self.fstate(), self.fdoctopics(), self.ftopickeys(), self.iterations, | ||
self.finferencer(), self.topic_threshold | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I just inserted a blank line after the cmd definition just for readability - it is not required |
||
# NOTE "--keep-sequence-bigrams" / "--use-ngrams true" poorer results + runs out of memory | ||
logger.info("training MALLET LDA with %s", cmd) | ||
check_output(args=cmd, shell=True) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to write default value in docstring description +
.
at the end of sentenceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spelled out the default so that users know that they need not enter a random_seed parameter at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default parameters automatically showed in documentation, for this reason, no need to duplicate it in docstring.