forked from PaddlePaddle/PaddleHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
57 lines (42 loc) · 1.33 KB
/
util.py
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
import time
import yaml
import numpy as np
from paddlehub.common.logger import logger
from slda_weibo.config import ModelType
def load_prototxt(config_file, config):
"""
Args:
config_file: model configuration file.
config: ModelConfig class
"""
logger.info("Loading SLDA config.")
with open(config_file, 'r', encoding='utf-8') as f:
yaml_dict = yaml.load(f, Loader=yaml.FullLoader)
# Assignment.
if yaml_dict["type"] == "LDA":
config.type = ModelType.LDA
else:
config.type = ModelType.SLDA
config.num_topics = yaml_dict["num_topics"]
config.alpha = yaml_dict["alpha"]
config.beta = yaml_dict["beta"]
config.word_topic_file = yaml_dict["word_topic_file"]
config.vocab_file = yaml_dict["vocab_file"]
def fix_random_seed(seed=2147483647):
np.random.seed(seed)
def rand(min_=0, max_=1):
return np.random.uniform(low=min_, high=max_)
def rand_k(k):
"""Returns an integer float number between [0, k - 1].
"""
return int(rand() * k)
def timeit(f):
"""Return time cost of function f.
"""
def timed(*args, **kwargs):
start_time = time.time()
result = f(*args, **kwargs)
end_time = time.time()
print(" [-] %s : %2.5f sec" % (f.__name__, end_time - start_time))
return result
return timed