| 39 | |
| 40 | @classmethod |
| 41 | def load(cls, model_dir=None): |
| 42 | if model_dir is None: |
| 43 | model_dir = config.modelDir |
| 44 | model_path = os.path.join(model_dir, "weights.npz") |
| 45 | if os.path.exists(model_path): |
| 46 | npz = np.load(model_path) |
| 47 | sizes = npz["sizes"] |
| 48 | w = npz["w"] |
| 49 | model = cls.__new__(cls) |
| 50 | model.n_tag = int(sizes[0]) |
| 51 | model.n_feature = int(sizes[1]) |
| 52 | model.n_transition_feature = model.n_tag * ( |
| 53 | model.n_feature + model.n_tag |
| 54 | ) |
| 55 | model.w = w |
| 56 | assert model.w.shape[0] == model.n_transition_feature |
| 57 | return model |
| 58 | |
| 59 | print( |
| 60 | "WARNING: weights.npz does not exist, try loading using old format", |
| 61 | file=sys.stderr, |
| 62 | ) |
| 63 | |
| 64 | model_path = os.path.join(model_dir, "model.txt") |
| 65 | with open(model_path, encoding="utf-8") as f: |
| 66 | ary = f.readlines() |
| 67 | |
| 68 | model = cls.__new__(cls) |
| 69 | model.n_tag = int(ary[0].strip()) |
| 70 | wsize = int(ary[1].strip()) |
| 71 | w = np.zeros(wsize) |
| 72 | for i in range(2, wsize): |
| 73 | w[i - 2] = float(ary[i].strip()) |
| 74 | model.w = w |
| 75 | model.n_feature = wsize // model.n_tag - model.n_tag |
| 76 | model.n_transition_feature = wsize |
| 77 | |
| 78 | model.save(model_dir) |
| 79 | return model |
| 80 | |
| 81 | @classmethod |
| 82 | def new(cls, model, copy_weight=True): |