-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfeature_tensor.py
39 lines (35 loc) · 1.41 KB
/
feature_tensor.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
import numpy as np
import math
import tensornetwork as tn
from typing import Any, List, Optional, Text, Type, Union, Dict, Sequence, Tuple
class FeatureTensor:
def __init__(self, input: np.ndarray, feature_map=None):
"""
Represents a "feature tensor" obtained from applying {feature_map}
uniformly to every component of the vector {input}.
:param input: input vector of shape (d, )
:param feature_map: R -> R^n
:param idx: index of {input} in the original data matrix
"""
self.input = input
self.feature_map = feature_map
@classmethod
def apply_feature_map(cls, vec, feature_map):
feature = []
for i in range(len(vec)):
feature.append(feature_map(vec[i]))
return np.array(feature)
def get_nodes(self) -> List[tn.Node]:
if self.feature_map is not None:
feature_vector = FeatureTensor.apply_feature_map(self.input, self.feature_map)
else:
feature_vector = self.input
if len(feature_vector.shape) != 1:
raise ValueError('Feature vector does not produce a vector.')
d = feature_vector.shape[0]
input_tn = []
# Loop through features
for i in range(int(d / 2)):
feature = np.array([feature_vector[2 * i], feature_vector[2 * i + 1]])
input_tn.append(tn.Node(feature, axis_names=['in']))
return input_tn