webleaf.Leaf

 1import torch
 2from torch.nn.functional import cosine_similarity
 3
 4
 5class Leaf(torch.Tensor):
 6    """
 7    The Leaf class is a custom subclass of PyTorch's torch.Tensor, designed to represent an HTML element
 8    with an associated feature embedding. It includes additional methods to calculate similarity and distance
 9    between elements, as well as a boolean check for the existence of the tensor.
10
11    Methods:
12    --------
13    similarity(other):
14        Calculates the cosine similarity between this Leaf object and another.
15
16    mdist(other):
17        Calculates the Manhattan distance (L1 distance) between this Leaf object and another.
18
19    __bool__():
20        Returns whether the Leaf tensor is non-empty (contains elements).
21    """
22    def similarity(self, other):
23        """
24        Computes the cosine similarity between the current Leaf tensor and another Leaf tensor.
25
26        Cosine similarity measures the cosine of the angle between two vectors, giving a value between -1 and 1,
27        where 1 indicates perfect similarity, 0 indicates orthogonality, and -1 indicates perfect dissimilarity.
28
29        Parameters:
30        -----------
31        other : Leaf
32            The other Leaf tensor to compare with.
33
34        Returns:
35        --------
36        float
37            A similarity score between -1 and 1, where 1 indicates high similarity.
38        """
39        return cosine_similarity(self.unsqueeze(0), other.unsqueeze(0)).detach().item()
40
41    def mdist(self, other):
42        """
43        Computes the Manhattan distance (L1 distance) between the current Leaf tensor and another Leaf tensor.
44
45        Manhattan distance is the sum of the absolute differences between corresponding elements of two vectors.
46        It is a measure of how different two vectors are, with higher values indicating greater dissimilarity.
47
48        Parameters:
49        -----------
50        other : Leaf
51            The other Leaf tensor to compare with.
52
53        Returns:
54        --------
55        float
56            The Manhattan distance between the two tensors.
57        """
58        return torch.sum(torch.abs(self - other)).detach().item()
59
60    def __bool__(self):
61        """
62        Checks whether the Leaf tensor is non-empty (i.e., contains elements).
63
64        This method allows Leaf objects to be used in boolean contexts such as conditions.
65
66        Returns:
67        --------
68        bool
69            True if the Leaf tensor has more than zero elements, False otherwise.
70        """
71        return self.size().numel() > 0
class Leaf(torch.Tensor):
 6class Leaf(torch.Tensor):
 7    """
 8    The Leaf class is a custom subclass of PyTorch's torch.Tensor, designed to represent an HTML element
 9    with an associated feature embedding. It includes additional methods to calculate similarity and distance
10    between elements, as well as a boolean check for the existence of the tensor.
11
12    Methods:
13    --------
14    similarity(other):
15        Calculates the cosine similarity between this Leaf object and another.
16
17    mdist(other):
18        Calculates the Manhattan distance (L1 distance) between this Leaf object and another.
19
20    __bool__():
21        Returns whether the Leaf tensor is non-empty (contains elements).
22    """
23    def similarity(self, other):
24        """
25        Computes the cosine similarity between the current Leaf tensor and another Leaf tensor.
26
27        Cosine similarity measures the cosine of the angle between two vectors, giving a value between -1 and 1,
28        where 1 indicates perfect similarity, 0 indicates orthogonality, and -1 indicates perfect dissimilarity.
29
30        Parameters:
31        -----------
32        other : Leaf
33            The other Leaf tensor to compare with.
34
35        Returns:
36        --------
37        float
38            A similarity score between -1 and 1, where 1 indicates high similarity.
39        """
40        return cosine_similarity(self.unsqueeze(0), other.unsqueeze(0)).detach().item()
41
42    def mdist(self, other):
43        """
44        Computes the Manhattan distance (L1 distance) between the current Leaf tensor and another Leaf tensor.
45
46        Manhattan distance is the sum of the absolute differences between corresponding elements of two vectors.
47        It is a measure of how different two vectors are, with higher values indicating greater dissimilarity.
48
49        Parameters:
50        -----------
51        other : Leaf
52            The other Leaf tensor to compare with.
53
54        Returns:
55        --------
56        float
57            The Manhattan distance between the two tensors.
58        """
59        return torch.sum(torch.abs(self - other)).detach().item()
60
61    def __bool__(self):
62        """
63        Checks whether the Leaf tensor is non-empty (i.e., contains elements).
64
65        This method allows Leaf objects to be used in boolean contexts such as conditions.
66
67        Returns:
68        --------
69        bool
70            True if the Leaf tensor has more than zero elements, False otherwise.
71        """
72        return self.size().numel() > 0

The Leaf class is a custom subclass of PyTorch's torch.Tensor, designed to represent an HTML element with an associated feature embedding. It includes additional methods to calculate similarity and distance between elements, as well as a boolean check for the existence of the tensor.

Methods:

similarity(other): Calculates the cosine similarity between this Leaf object and another.

mdist(other): Calculates the Manhattan distance (L1 distance) between this Leaf object and another.

__bool__(): Returns whether the Leaf tensor is non-empty (contains elements).

def similarity(self, other):
23    def similarity(self, other):
24        """
25        Computes the cosine similarity between the current Leaf tensor and another Leaf tensor.
26
27        Cosine similarity measures the cosine of the angle between two vectors, giving a value between -1 and 1,
28        where 1 indicates perfect similarity, 0 indicates orthogonality, and -1 indicates perfect dissimilarity.
29
30        Parameters:
31        -----------
32        other : Leaf
33            The other Leaf tensor to compare with.
34
35        Returns:
36        --------
37        float
38            A similarity score between -1 and 1, where 1 indicates high similarity.
39        """
40        return cosine_similarity(self.unsqueeze(0), other.unsqueeze(0)).detach().item()

Computes the cosine similarity between the current Leaf tensor and another Leaf tensor.

Cosine similarity measures the cosine of the angle between two vectors, giving a value between -1 and 1, where 1 indicates perfect similarity, 0 indicates orthogonality, and -1 indicates perfect dissimilarity.

Parameters:

other : Leaf The other Leaf tensor to compare with.

Returns:

float A similarity score between -1 and 1, where 1 indicates high similarity.

def mdist(self, other):
42    def mdist(self, other):
43        """
44        Computes the Manhattan distance (L1 distance) between the current Leaf tensor and another Leaf tensor.
45
46        Manhattan distance is the sum of the absolute differences between corresponding elements of two vectors.
47        It is a measure of how different two vectors are, with higher values indicating greater dissimilarity.
48
49        Parameters:
50        -----------
51        other : Leaf
52            The other Leaf tensor to compare with.
53
54        Returns:
55        --------
56        float
57            The Manhattan distance between the two tensors.
58        """
59        return torch.sum(torch.abs(self - other)).detach().item()

Computes the Manhattan distance (L1 distance) between the current Leaf tensor and another Leaf tensor.

Manhattan distance is the sum of the absolute differences between corresponding elements of two vectors. It is a measure of how different two vectors are, with higher values indicating greater dissimilarity.

Parameters:

other : Leaf The other Leaf tensor to compare with.

Returns:

float The Manhattan distance between the two tensors.

Inherited Members
torch.Tensor
storage
backward
register_hook
register_post_accumulate_grad_hook
reinforce
detach
detach_
is_shared
share_memory_
norm
solve
lstsq
eig
symeig
lu
stft
istft
resize
resize_as
split
unique
unique_consecutive
storage_type
refine_names
align_to
unflatten
rename_
rename
to_sparse_coo
dim_order
torch._C.TensorBase
apply_
bfloat16
byte
char
contiguous
copy_
cpu
cuda
xpu
ipu
data_ptr
dim
has_names
double
cdouble
element_size
float
cfloat
get_device
bool
half
int
is_contiguous
item
long
map_
map2_
ndimension
nelement
new
new_tensor
nonzero
numel
numpy
requires_grad_
set_
short
size
untyped_storage
storage_offset
stride
to
tolist
type
abs
abs_
absolute
absolute_
acos
acos_
acosh
acosh_
add
add_
addbmm
addbmm_
addcdiv
addcdiv_
addcmul
addcmul_
addmm
addmm_
addmv
addmv_
addr
addr_
adjoint
align_as
all
allclose
amax
amin
aminmax
angle
any
arccos
arccos_
arccosh
arccosh_
arcsin
arcsin_
arcsinh
arcsinh_
arctan
arctan2
arctan2_
arctan_
arctanh
arctanh_
argmax
argmin
argsort
argwhere
as_strided
as_strided_
as_strided_scatter
asin
asin_
asinh
asinh_
atan
atan2
atan2_
atan_
atanh
atanh_
baddbmm
baddbmm_
bernoulli
bernoulli_
bincount
bitwise_and
bitwise_and_
bitwise_left_shift
bitwise_left_shift_
bitwise_not
bitwise_not_
bitwise_or
bitwise_or_
bitwise_right_shift
bitwise_right_shift_
bitwise_xor
bitwise_xor_
bmm
broadcast_to
cauchy_
ccol_indices
ceil
ceil_
chalf
cholesky
cholesky_inverse
cholesky_solve
chunk
clamp
clamp_
clamp_max
clamp_max_
clamp_min
clamp_min_
clip
clip_
clone
coalesce
col_indices
conj
conj_physical
conj_physical_
copysign
copysign_
corrcoef
cos
cos_
cosh
cosh_
count_nonzero
cov
cross
crow_indices
cummax
cummin
cumprod
cumprod_
cumsum
cumsum_
deg2rad
deg2rad_
dense_dim
dequantize
det
diag
diag_embed
diagflat
diagonal
diagonal_scatter
diff
digamma
digamma_
dist
div
div_
divide
divide_
dot
dsplit
eq
eq_
equal
erf
erf_
erfc
erfc_
erfinv
erfinv_
exp
exp2
exp2_
exp_
expand
expand_as
expm1
expm1_
exponential_
fill_
fill_diagonal_
fix
fix_
flatten
flip
fliplr
flipud
float_power
float_power_
floor
floor_
floor_divide
floor_divide_
fmax
fmin
fmod
fmod_
frac
frac_
frexp
gather
gcd
gcd_
ge
ge_
geometric_
geqrf
ger
greater
greater_
greater_equal
greater_equal_
gt
gt_
hardshrink
heaviside
heaviside_
histc
histogram
hsplit
hypot
hypot_
i0
i0_
igamma
igamma_
igammac
igammac_
index_add
index_add_
index_copy
index_copy_
index_fill
index_fill_
index_put
index_put_
index_reduce
index_reduce_
index_select
indices
inner
int_repr
inverse
is_coalesced
is_complex
is_conj
is_distributed
is_floating_point
is_inference
is_neg
is_nonzero
is_pinned
is_same_size
is_set_to
is_signed
isclose
isfinite
isinf
isnan
isneginf
isposinf
isreal
kron
kthvalue
lcm
lcm_
ldexp
ldexp_
le
le_
lerp
lerp_
less
less_
less_equal
less_equal_
lgamma
lgamma_
log
log10
log10_
log1p
log1p_
log2
log2_
log_
log_normal_
log_softmax
logaddexp
logaddexp2
logcumsumexp
logdet
logical_and
logical_and_
logical_not
logical_not_
logical_or
logical_or_
logical_xor
logical_xor_
logit
logit_
logsumexp
lt
lt_
lu_solve
masked_fill
masked_fill_
masked_scatter
masked_scatter_
masked_select
matmul
matrix_exp
matrix_power
max
maximum
mean
median
min
minimum
mm
mode
moveaxis
movedim
msort
mul
mul_
multinomial
multiply
multiply_
mv
mvlgamma
mvlgamma_
nan_to_num
nan_to_num_
nanmean
nanmedian
nanquantile
nansum
narrow
narrow_copy
ne
ne_
neg
neg_
negative
negative_
new_empty
new_empty_strided
new_full
new_ones
new_zeros
nextafter
nextafter_
nonzero_static
normal_
not_equal
not_equal_
orgqr
ormqr
outer
permute
pin_memory
pinverse
polygamma
polygamma_
positive
pow
pow_
prelu
prod
put
put_
q_per_channel_axis
q_per_channel_scales
q_per_channel_zero_points
q_scale
q_zero_point
qr
qscheme
quantile
rad2deg
rad2deg_
random_
ravel
reciprocal
reciprocal_
record_stream
relu
relu_
remainder
remainder_
renorm
renorm_
repeat
repeat_interleave
reshape
reshape_as
resize_
resize_as_
resize_as_sparse_
resolve_conj
resolve_neg
retain_grad
roll
rot90
round
round_
row_indices
rsqrt
rsqrt_
scatter
scatter_
scatter_add
scatter_add_
scatter_reduce
scatter_reduce_
select
select_scatter
sgn
sgn_
sigmoid
sigmoid_
sign
sign_
signbit
sin
sin_
sinc
sinc_
sinh
sinh_
slice_scatter
slogdet
smm
softmax
sort
sparse_dim
sparse_mask
sparse_resize_
sparse_resize_and_clear_
split_with_sizes
sqrt
sqrt_
square
square_
squeeze
squeeze_
sspaddmm
std
sub
sub_
subtract
subtract_
sum
sum_to_size
svd
swapaxes
swapaxes_
swapdims
swapdims_
t
t_
take
take_along_dim
tan
tan_
tanh
tanh_
tensor_split
tile
to_dense
to_mkldnn
to_padded_tensor
to_sparse
to_sparse_bsc
to_sparse_bsr
to_sparse_csc
to_sparse_csr
topk
trace
transpose
transpose_
triangular_solve
tril
tril_
triu
triu_
true_divide
true_divide_
trunc
trunc_
type_as
unbind
unfold
uniform_
unsafe_chunk
unsafe_split
unsafe_split_with_sizes
unsqueeze
unsqueeze_
values
var
vdot
view
view_as
vsplit
where
xlogy
xlogy_
zero_
as_subclass
T
H
mT
mH
grad_fn
is_leaf
retains_grad
data
grad
volatile
output_nr
requires_grad
name
shape
is_cuda
is_mtia
is_cpu
is_xla
is_xpu
is_ipu
is_sparse
is_sparse_csr
is_mkldnn
is_mps
is_ort
is_vulkan
is_quantized
is_meta
is_nested
dtype
layout
device
ndim
nbytes
itemsize
names
real
imag