svjack commited on
Commit
429e22e
·
1 Parent(s): fe334a4

Upload train_t5_en_simple.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_t5_en_simple.py +176 -0
train_t5_en_simple.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import pickle as pkl
3
+ import numpy as np
4
+
5
+ def read_file_to_lines(path):
6
+ with open(path, "r") as f:
7
+ return pd.Series(f.readlines()).map(lambda x: x.replace("\n", ""))
8
+
9
+ peil_en = read_file_to_lines("../peil_ja en-US.txt")
10
+
11
+ with open("peil_ja.pkl", "rb") as f:
12
+ peil = pkl.load(f)
13
+
14
+ assert len(peil) == len(peil_en)
15
+ d = dict(zip(peil, peil_en))
16
+
17
+ '''
18
+ NEED_PREFIX = '次の出来事に必要な前提条件は何ですか: '
19
+ EFFECT_PREFIX = '次の出来事の後に起こりうることは何ですか: '
20
+ INTENT_PREFIX = '次の出来事が起こった動機は何ですか: '
21
+ REACT_PREFIX = '次の出来事の後に感じることは何ですか: '
22
+
23
+ NEED_PREFIX = '以下事件有哪些必要的先决条件:'
24
+ EFFECT_PREFIX = '下面的事件发生后可能会发生什么:'
25
+ INTENT_PREFIX = '以下事件的动机是什么:'
26
+ REACT_PREFIX = '以下事件发生后,你有什么感觉:'
27
+ '''
28
+
29
+ NEED_PREFIX = 'What are the necessary preconditions for the next event?'
30
+ EFFECT_PREFIX = 'What could happen after the next event?'
31
+ INTENT_PREFIX = 'What is the motivation for the next event?'
32
+ REACT_PREFIX = 'What are your feelings after the following event?'
33
+
34
+ with open("graph_ja_zh_df.pkl", "rb") as f:
35
+ df = pkl.load(f)
36
+
37
+ df["en_event"] = df["event"].map(lambda x: d[x])
38
+
39
+ def one_infe_ele_map(inf_d):
40
+ req = {}
41
+ for k, v in inf_d.items():
42
+ req[k] = {}
43
+ for kk, vv in v.items():
44
+ assert type(vv) == type([])
45
+ req[k][kk] = list(map(lambda x: d[x], vv))
46
+ return req
47
+
48
+ df["en_inference"] = df["inference"].map(one_infe_ele_map)
49
+
50
+ with open("graph_ja_zh_en_df.pkl", "wb") as f:
51
+ pkl.dump(df, f)
52
+
53
+ from huggingface_hub import HfApi
54
+ api = HfApi()
55
+
56
+ api.upload_file(
57
+ path_or_fileobj="graph_ja_zh_en_df.pkl",
58
+ path_in_repo="graph_ja_zh_en_df.pkl",
59
+ repo_id="svjack/comet-atomic-ja-zh",
60
+ repo_type="dataset",
61
+ )
62
+
63
+ examples = {
64
+ "event": df["en_event"].values.tolist(),
65
+ "inference": df["en_inference"].values.tolist(),
66
+ }
67
+
68
+ def preprocess_function_chunked(examples):
69
+ inputs = []
70
+ targets = []
71
+
72
+ for head_text, inf_type_dict in zip(examples['event'], examples['inference']):
73
+ for inf_type, inf_dirs in inf_type_dict.items():
74
+ if inf_dirs is None:
75
+ continue
76
+
77
+ for inf_dir, tail_texts in inf_dirs.items():
78
+ if tail_texts is None:
79
+ continue
80
+
81
+ if inf_type == 'event':
82
+ if inf_dir == 'before':
83
+ prefix = NEED_PREFIX
84
+ else:
85
+ prefix = EFFECT_PREFIX
86
+ else:
87
+ if inf_dir == 'before':
88
+ prefix = INTENT_PREFIX
89
+ else:
90
+ prefix = REACT_PREFIX
91
+
92
+ for tail_text in tail_texts:
93
+ inputs.append(prefix + head_text)
94
+ targets.append(tail_text)
95
+ return inputs, targets
96
+
97
+ src_tgt_df = pd.DataFrame(list(zip(*preprocess_function_chunked(examples))))
98
+ src_tgt_df.columns = ["source_text", "target_text"]
99
+ src_tgt_df.dropna().drop_duplicates().to_csv("comet-atomic-ja-en-simple.csv", index = False)
100
+
101
+ from huggingface_hub import HfApi
102
+ api = HfApi()
103
+
104
+ api.upload_file(
105
+ path_or_fileobj="comet-atomic-ja-en-simple.csv",
106
+ path_in_repo="comet-atomic-ja-en-simple.csv",
107
+ repo_id="svjack/comet-atomic-ja-zh",
108
+ repo_type="dataset",
109
+ )
110
+
111
+ df = src_tgt_df.dropna().drop_duplicates().sample(frac = 1.0)
112
+ df.iloc[:35300].to_csv("train_en.csv", index = False)
113
+ df.iloc[35300:].to_csv("valid_en.csv", index = False)
114
+
115
+ api.upload_file(
116
+ path_or_fileobj="train_en.csv",
117
+ path_in_repo="train_en.csv",
118
+ repo_id="svjack/comet-atomic-ja-zh",
119
+ repo_type="dataset",
120
+ )
121
+
122
+ api.upload_file(
123
+ path_or_fileobj="valid_en.csv",
124
+ path_in_repo="valid_en.csv",
125
+ repo_id="svjack/comet-atomic-ja-zh",
126
+ repo_type="dataset",
127
+ )
128
+
129
+ !tar -zcvf tt_en.tar.gz train_en.csv valid_en.csv
130
+
131
+ from simplet5 import SimpleT5
132
+ import pandas as pd
133
+ model = SimpleT5()
134
+ model.from_pretrained(model_type="t5",
135
+ model_name="google/flan-t5-base",)
136
+
137
+ train_df = pd.read_csv("train_en.csv")
138
+ valid_df = pd.read_csv("valid_en.csv")
139
+
140
+ train_df = train_df.dropna()
141
+ valid_df = valid_df.dropna()
142
+
143
+ model.train(train_df=train_df,
144
+ eval_df=valid_df,
145
+ source_max_token_len=128,
146
+ target_max_token_len=128,
147
+ batch_size=4, max_epochs=10, use_gpu=True)
148
+
149
+ '''
150
+ %matplotlib inline
151
+ from time import sleep
152
+ import matplotlib.pyplot as plt
153
+ from IPython import display
154
+ while True:
155
+ from tensorboard.backend.event_processing import event_accumulator
156
+ import pandas as pd
157
+ path = "lightning_logs/version_0/events.out.tfevents.1672423867.featurize.22967.0"
158
+ path = "lightning_logs/version_0/events.out.tfevents.1675325602.featurize.19734.0"
159
+ ea = event_accumulator.EventAccumulator(
160
+ path,
161
+ size_guidance={event_accumulator.SCALARS: 0},
162
+ )
163
+ _absorb_print = ea.Reload()
164
+ ea.Tags()
165
+ #pd.DataFrame(ea.Scalars("train_loss_step"))["value"].rolling(1000).mean().plot()
166
+ s = pd.DataFrame(ea.Scalars("train_loss_step"))["value"].rolling(100).mean()
167
+ plt.plot(s)
168
+ display.display(plt.gcf())
169
+ display.clear_output(wait=True)
170
+ sleep(5)
171
+ '''
172
+
173
+ model = SimpleT5()
174
+ model.load_model(
175
+ model_dir = "outputs/simplet5-epoch-0-train-loss-0.1042-val-loss-0.0814",
176
+ use_gpu = False)