-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.py
More file actions
89 lines (63 loc) · 2.79 KB
/
Copy pathTools.py
File metadata and controls
89 lines (63 loc) · 2.79 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
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
import torch
from torch.nn.utils.rnn import pad_sequence
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def create_autoregressive_mask(seq_len):
mask = torch.triu(torch.ones((seq_len, seq_len), dtype=torch.bool), diagonal=1).to(device)
return mask
def left_pad_sequences(sequences, vocab):
max_length = max(len(seq) for seq in sequences)
padded_sequences = []
for seq in sequences:
pad_length = max_length - len(seq)
# padded_seq = [vocab["PAD_TOKEN"]] * pad_length + [vocab["START_TOKEN"]]+ seq + [vocab["END_TOKEN"]]
padded_seq = [vocab["PAD_TOKEN"]] * pad_length + [vocab["START_TOKEN"]]+ seq
padded_sequences.append(padded_seq)
return torch.tensor(padded_sequences)
def left_pad_sequences_inference(sequences, vocab):
max_length = max(len(seq) for seq in sequences)
padded_sequences = []
for seq in sequences:
pad_length = max_length - len(seq)
padded_seq = [vocab["PAD_TOKEN"]] * pad_length + [vocab["START_TOKEN"]]+ seq
padded_sequences.append(padded_seq)
return torch.tensor(padded_sequences)
def collate_fn(batch):
# Add START_TOKEN at the beginning of each sample
# vocab["START_TOKEN"]=2
# vocab["PAD_TOKEN"]=5
device = batch[0].device # Get the device of the first sample
sequences = [torch.cat([
torch.tensor([2], device=device), # Add start token and ensure it's on the correct device
sample.to(device) # Move the sample to the correct device
]) for sample in batch]
# Left padding
reversed_seqs = [torch.flip(seq, [0]) for seq in sequences] # First reversal
padded_reversed = pad_sequence(reversed_seqs,
batch_first=True,
padding_value=5) # Right padding for the reversed sequences
padded_sequences_left = torch.flip(padded_reversed, [1]) # Second reversal
mask = (padded_sequences_left != 5).float()
return padded_sequences_left, mask
def add_new_data_to_df(existing_df, new_data):
"""
Add new data to an existing DataFrame and return the updated DataFrame.
Parameters:
- existing_df: The existing DataFrame to which new data will be added.
- new_data: A dictionary containing the new data to be added.
Returns:
- updated_df: The updated DataFrame.
"""
# Convert new data to DataFrame
new_df = pd.DataFrame(new_data)
# Add new data to existing DataFrame
updated_df = pd.concat([existing_df, new_df], ignore_index=True)
return updated_df
def add_ipv6_colon(ipv6):
# add : to ipv6
ipv6_list = []
for i in range(0, len(ipv6), 4):
ipv6_list.append(ipv6[i:i+4])
ipv6 = ":".join(ipv6_list)
return ipv6
if __name__ == '__main__':
pass