-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_federated.py
119 lines (90 loc) · 3.72 KB
/
tf_federated.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import tensorflow as tf
tf.compat.v1.enable_v2_behavior()
import tensorflow_federated as tff
import tensorflow.python.keras.backend as K
import numpy as np
import os
import sys
from keras_gpt_2 import load_trained_model_from_checkpoint, get_bpe_from_files, generate
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.callbacks import BaseLogger, History
from collections import defaultdict
import urllib
import gpt_2_simple as gpt2
import requests
from collections import OrderedDict
filenames = ['input_ids.json', 'lm_labels.json', 'mc_labels.json', 'mc_token_ids.json']
url = "https://persona-dataset.s3.amazonaws.com/{}"
data = []
for name in filenames:
full_url = url.format(name)
json_data = requests.get(full_url).json()
data.append(np.array(json_data))
print("Done")
input_ids, lm_labels, mc_labels, mc_token_ids = data
# with open('preprocessed_dataset.json', 'w') as f:
# personachat = json.dump(datasets, f)
print(lm_labels.shape)
print(input_ids.shape)
print(mc_token_ids.shape)
print(mc_labels.shape)
num_clients = 5
batches = [np.array_split(input_ids, num_clients), np.array_split(lm_labels, num_clients), np.array_split(mc_token_ids, num_clients), np.array_split(mc_labels, num_clients)]
assert len(batches) == 4
assert len(batches[0]) == num_clients
datasets = list(zip(*batches))
assert len(datasets) == num_clients
assert len(datasets[0]) == 4
a, b, c, d = datasets[0]
print(a.shape, b.shape, c.shape, d.shape)
tf_datasets = []
# for input_ids, lm_labels, mc_token_ids, mc_labels in datasets:
# tf_datasets.append()
def dataset_map(input_ids, lm_labels, mc_token_ids, mc_labels):
return OrderedDict([
('x', input_ids),
('y', lm_labels)
])
#datasets = [tuple(dataset) for dataset in datasets]
datasets = [tf.data.Dataset.from_tensor_slices(dataset) for dataset in datasets]
datasets = [dataset.map(dataset_map) for dataset in datasets]
train_data = datasets
# Grab a single batch of data so that TFF knows what data looks like.
sample_batch = tf.nest.map_structure(
lambda x: x.numpy(), iter(train_data[0]).next())
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.callbacks import BaseLogger, History
import tensorflow as tf
import numpy as np
from collections import defaultdict
import os
from keras_gpt_2 import load_trained_model_from_checkpoint, get_bpe_from_files, generate
model_folder = 'models/117M'
config_path = os.path.join(model_folder, 'hparams.json')
checkpoint_path = os.path.join(model_folder, 'model.ckpt')
encoder_path = os.path.join(model_folder, 'encoder.json')
vocab_path = os.path.join(model_folder, 'vocab.bpe')
model = load_trained_model_from_checkpoint(config_path, checkpoint_path)
x_val = sample_batch.get('x')
print(x_val.shape)
print("Starting forward pass")
y = model.predict(x_val)
print(sample_batch)
# def model_fn():
# model_folder = 'models/117M'
# config_path = os.path.join(model_folder, 'hparams.json')
# checkpoint_path = os.path.join(model_folder, 'model.ckpt')
# encoder_path = os.path.join(model_folder, 'encoder.json')
# vocab_path = os.path.join(model_folder, 'vocab.bpe')
# if not os.path.isdir(model_folder):
# gpt2.download_gpt2(model_name = '117M')
# print('Load BPE from files...')
# bpe = get_bpe_from_files(encoder_path, vocab_path)
# model = load_trained_model_from_checkpoint(config_path, checkpoint_path)
# return tff.learning.from_compiled_keras_model(model, sample_batch)
# # Simulate a few rounds of training with the selected client devices.
# trainer = tff.learning.build_federated_averaging_process(model_fn)
# state = trainer.initialize()
# for _ in range(5):
# state, metrics = trainer.next(state, train_data)
# print (metrics.loss)