-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
121 lines (105 loc) · 3.53 KB
/
Copy pathconfig.py
File metadata and controls
121 lines (105 loc) · 3.53 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
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
120
121
import io
import zipfile
import numpy as np
import requests
import torch
class Config:
def __init__(self, download=True):
self.mode = False # true if you want sequential output
if download: UCI_HAR_download()
x, y = UCI_HAR_read(
x_path='UCI_HAR_Dataset/UCI HAR Dataset/train/X_train.txt',
y_path='UCI_HAR_Dataset/UCI HAR Dataset/train/y_train.txt',
) # UCI_HAR_read
self.x, self.y = list(map(str_to_tensor, x)), list(map(lambda val: torch.tensor(float(val)), y))
print('str_to_tensor is applied to self.x\nint_to_hv is applied to self.y')
self.episode_length = len(self.x[0])
self.max_var_len = get_max_x_len(self.x)
# FIX
#self.x = list(map(lambda val: zero_padding(val, self.max_var_len), self.x))
feature = self.x[0]
print(f'feature_len: {feature.__len__()}')
for var in feature: print(var.shape, end='')
# COMPLETE!!
_ = list(map(lambda val: val.item(), self.y))
min_y, max_y = min(_), max(_)
print(f'min_y: {min_y} max_y: {max_y}')
self.y_bins = torch.from_numpy(np.linspace(min_y, max_y, num=int(max_y - min_y + 1)))
self.y = list(map(lambda val: int_to_hv(val, self.y_bins), self.y))
return
# create bins for y
# hippo init
in_features, hid_features, out_features= self.x[0][0].shape[0], self.x[0][0].shape[0], self.y_bins.shape[0]
self.delta = {
"k": self.episode_length,
"in_features": in_features,
"hid_features": hid_features,
"out_features": out_features
} # self.delta
self.A = {
"in_features": in_features,
"out_features": hid_features,
} # self.A
self.B = {
"in_features": in_features,
"out_features": hid_features,
} # self.B
self.C = {
"in_features": in_features,
"out_features": out_features,
} # self.C
self.D = {
"in_features": in_features,
"out_features": out_features,
} # self.D
self.bias = True
self.dummy = torch.zeros_like(self.x[0][0])
# __init__
# Config
def UCI_HAR_download(url='https://archive.ics.uci.edu/ml/machine-learning-databases/00240/UCI%20HAR%20Dataset.zip'):
response = requests.get(url)
if response.status_code == 200:
print("Download successful!")
with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
zip_ref.extractall("UCI_HAR_Dataset")
print("Extraction complete.")
else: print("Failed to download. Status code:", response.status_code)
return response
# get_UCI_HAR_dataset
def UCI_HAR_read(x_path, y_path):
x, y = None, None
with open(x_path, 'r') as file:
content = file.read()
x = content.strip().split('\n')
with open(y_path, 'r') as file:
content = file.read()
y = content.strip().split('\n')
return x, y
# UCI_HAR_txt_to_tensor
def str_to_tensor(val):
val = val.strip()
val = val.split(' ')
val = [torch.tensor([float(feature) for feature in seq.split(' ')]) for seq in val]
return val
# str_to_tensor
def int_to_hv(val, bins):
index = torch.argmin(torch.abs(bins - val))
returned_val = torch.zeros(bins.__len__())
returned_val[index] = 1.0
return returned_val
# int_to_hv
def zero_padding(sequence, target_var_len):
for var in sequence:
var_len = len(var)
if var_len < target_var_len:
# _zero_padding
def get_max_x_len(dataset):
sequence = dataset[0]
length_list = list()
for feature in sequence: length_list.append(feature.shape[0])
return max(length_list)
# get_max_x_len
if __name__ == "__main__":
config = Config(download=False)
print(f'max_var_len: {config.max_var_len}')
# if __name__ == "__main__":