-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
79 lines (67 loc) · 2.55 KB
/
utils.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
import os
import requests
from tqdm import tqdm
import argparse
def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Model hyperparameters
parser.add_argument(
'--encoder_type',
default='BiLSTM',
type=str,
help='What encoder model to use',
choices=['MeanEmbedding', 'LSTM', 'BiLSTM', 'BiLSTM-maxpool'])
parser.add_argument('--enc_hidden_dim',
default=1028,
type=int,
help='Size of hidden layers of encoder models')
parser.add_argument('--cls_hidden_dim',
default=512,
type=int,
help='Size of hidden layers of classifier model')
# Optimizer hyperparameters
parser.add_argument('--lr',
default=0.001,
type=float,
help='Learning rate to use')
parser.add_argument('--batch_size',
default=128,
type=int,
help='Minibatch size')
# Other hyperparameters
parser.add_argument('--epochs',
default=5,
type=int,
help='Max number of epochs')
parser.add_argument('--seed', default=42, type=int, help='Random seed')
parser.add_argument('--limit_train_batches',
default=1.0,
type=float,
help='Percentage of data to use for training')
parser.add_argument('--refresh_rate',
default=10,
type=int,
help='Progress bar refresh rate')
parser.add_argument('--log_dir', default='./logs', type=str)
return parser.parse_args()
def download_file_from_google_drive(id, destination):
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in tqdm(response.iter_content(CHUNK_SIZE)):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params={'id': id}, stream=True)
token = get_confirm_token(response)
if token:
params = {'id': id, 'confirm': token}
response = session.get(URL, params=params, stream=True)
save_response_content(response, destination)