-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_tensor.py
More file actions
46 lines (34 loc) · 2.08 KB
/
Copy pathcompute_tensor.py
File metadata and controls
46 lines (34 loc) · 2.08 KB
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
# Copyright (c) 2026 CyberAgent, Inc.
# Author: Ayuto Tsutsumi (@Atotti) <aya172957@ayutaso.com>
import argparse
from pathlib import Path
from src.matrix import Matrix
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument("--input_path", type=str, default="data/processed/matrix/blocks.json")
# parser.add_argument("--output_path", type=str, default="data/processed/matrix/optimized_matrix.csv")
parser.add_argument("--master", type=str, default="data/external/character-situation-master.json")
args = parser.parse_args()
input_path = args.input_path
# output_path = args.output_path
master_path = Path(args.master)
# Load matrix data
blocks_json = Path(input_path)
id_map_path = Path("data/interim") / f"{master_path.stem}_id_map.json"
matrix = Matrix.load(blocks_json=str(blocks_json), master_data_path=str(master_path), id_map_path=str(id_map_path))
# Generate a tensor of shape (num_characters, num_situations, num_samples, embedding_dim) in numpy format and cache it
tensor, situation_embeddings, character_embeddings = matrix.to_numpy()
tensor_cache_path = Path("{input_path}_dialogue_embeddings.npy".format(input_path=input_path.replace(".json", "")))
np.save(tensor_cache_path, tensor)
print(f"Cached tensor to {tensor_cache_path} with shape {tensor.shape}")
situation_cache_path = Path("{input_path}_situation_embeddings.npy".format(input_path=input_path.replace(".json", "")))
np.save(situation_cache_path, situation_embeddings)
print(f"Cached situation embeddings to {situation_cache_path} with shape {situation_embeddings.shape}")
character_cache_path = Path("{input_path}_character_embeddings.npy".format(input_path=input_path.replace(".json", "")))
np.save(character_cache_path, character_embeddings)
print(f"Cached character embeddings to {character_cache_path} with shape {character_embeddings.shape}")
# Generate a csv file containing the dialogue in texts
texts = matrix.get_texts()
texts_cache_path = Path("{input_path}_texts.csv".format(input_path=input_path.replace(".json", "")))
texts.to_csv(texts_cache_path)
print(f"Cached texts to {texts_cache_path} with shape {texts.shape}")