Python wrapper for damo, a set of fast and robust hash functions.
Project description
Damo-Embedding
Quick Install
pip install damo-embedding
Example
DeepFM
import torch
import torch.nn as nn
from damo_embedding import Embedding
class DeepFM(torch.nn.Module):
def __init__(
self,
emb_size: int,
fea_size: int,
hid_dims=[256, 128],
num_classes=1,
dropout=[0.2, 0.2],
**kwargs,
):
super(DeepFM, self).__init__()
self.emb_size = emb_size
self.fea_size = fea_size
initializer = {
"name": "truncate_normal",
"mean": float(kwargs.get("mean", 0.0)),
"stddev": float(kwargs.get("stddev", 0.0001)),
}
optimizer = {
"name": "adam",
"gamma": float(kwargs.get("gamma", 0.001)),
"beta1": float(kwargs.get("beta1", 0.9)),
"beta2": float(kwargs.get("beta2", 0.999)),
"lambda": float(kwargs.get("lambda", 0.0)),
"epsilon": float(kwargs.get("epsilon", 1e-8)),
}
self.w = Embedding(
1,
initializer=initializer,
optimizer=optimizer,
)
self.v = Embedding(
self.emb_size,
initializer=initializer,
optimizer=optimizer,
)
self.w0 = torch.zeros(1, dtype=torch.float32, requires_grad=True)
self.dims = [fea_size * emb_size] + hid_dims
self.layers = nn.ModuleList()
for i in range(1, len(self.dims)):
self.layers.append(nn.Linear(self.dims[i - 1], self.dims[i]))
self.layers.append(nn.BatchNorm1d(self.dims[i]))
self.layers.append(nn.BatchNorm1d(self.dims[i]))
self.layers.append(nn.ReLU())
self.layers.append(nn.Dropout(dropout[i - 1]))
self.layers.append(nn.Linear(self.dims[-1], num_classes))
self.sigmoid = nn.Sigmoid()
def forward(self, input: torch.Tensor) -> torch.Tensor:
"""forward
Args:
input (torch.Tensor): input tensor
Returns:
tensor.Tensor: deepfm forward values
"""
assert input.shape[1] == self.fea_size
w = self.w.forward(input)
v = self.v.forward(input)
square_of_sum = torch.pow(torch.sum(v, dim=1), 2)
sum_of_square = torch.sum(v * v, dim=1)
fm_out = (
torch.sum((square_of_sum - sum_of_square)
* 0.5, dim=1, keepdim=True)
+ torch.sum(w, dim=1)
+ self.w0
)
dnn_out = torch.flatten(v, 1)
for layer in self.layers:
dnn_out = layer(dnn_out)
out = fm_out + dnn_out
out = self.sigmoid(out)
return out
Save Model
from damo_embedding import save_model
model = DeepFM(8, 39)
save_model(model, "./", training=False)
Document
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
damo-embedding-1.1.9.tar.gz
(224.4 kB
view hashes)
Built Distributions
Close
Hashes for damo_embedding-1.1.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e9bd7aa710d863a9fea978e44c5e70852e5e6ef5e860bda6ac6a59d85af5f43 |
|
MD5 | c4699bd4f697d7e73d231c84e32ccb64 |
|
BLAKE2b-256 | c54fb318ccd94054c33044d5af922e8fe067592e096ebbdd06ceaec733426a31 |
Close
Hashes for damo_embedding-1.1.9-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e364bcae87d54a475c56fa97561b2a42dce3b5868ce817e634e02f4e14d9938 |
|
MD5 | e7720b8abd5f80beb308ede26ff03018 |
|
BLAKE2b-256 | 614bdfe4b617e0016e5e5881426a1acf0e7da0156a1ddf1389784d6848da2010 |
Close
Hashes for damo_embedding-1.1.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdba83c5789715f19de3301df12e063e4ceb0a85a032aa96521a80a1bc546994 |
|
MD5 | 9d52b2e0fde52f7146daa4a2145ea8b8 |
|
BLAKE2b-256 | 30669431fe251fb27903f316a27039f9fb9d5e8fa527705fa92b1b16d48fe0a0 |
Close
Hashes for damo_embedding-1.1.9-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c15960ec971e3c587259a4b96e89ff6e2eea823cbf5aca4f4143f712c4b87d61 |
|
MD5 | 840024b4c06798b9dabde69b172b75be |
|
BLAKE2b-256 | fb245c53247fc2e199864eee9159eaf2e91db3f6fabcebf14deca98032f8e737 |
Close
Hashes for damo_embedding-1.1.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70ab57ccc154a388144ea6c74c3e1401769ca5974ec104a875c80466b964a1f4 |
|
MD5 | 8625500ddcf9e74f36487b853d61242a |
|
BLAKE2b-256 | 3aca749578aa3f7437ce12654edcc963a8243c077328a514592514770a7114f2 |
Close
Hashes for damo_embedding-1.1.9-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c31170357a7bc28f07002889ab6dbe6a34e9d5ab9537150a9b1a8d48515c68dc |
|
MD5 | 7878244aee633807ed79cd9a2930b814 |
|
BLAKE2b-256 | 1897e514e1673626cb4b79199456075eeb974e2d47a0fa47fc533af229e2e8eb |
Close
Hashes for damo_embedding-1.1.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4612c2276a5f04e24fd93251e22c750bf2ae9c2e20a01614a5fd27fe43cabc12 |
|
MD5 | 9dd2bf8ec7826c7d9883a9c649f5bf76 |
|
BLAKE2b-256 | 677d0878a4e8e4979e6b3146c0d3a312bc74f28a41229ab9670e43e4276e2c83 |
Close
Hashes for damo_embedding-1.1.9-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e6dbcccbedbc1e7bea3862cec3e4c9e9b78eea5d234914396cafeb9e1599cfd |
|
MD5 | f5c3bf25d4ad855a4ef8ca4ed974ccd0 |
|
BLAKE2b-256 | d000cf1536e0b2d19be52f09662df4b9ebe5a1e80169f9731d521463492d29d6 |
Close
Hashes for damo_embedding-1.1.9-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a69ae429c987b24f51cfcd776c27646b37bb2db4ad798f989051497dc3452e1 |
|
MD5 | 9b253f519764bf917ca55e6d35220c57 |
|
BLAKE2b-256 | 1f8f9fd05d05414520f217a74c020c5753029ce452bd47b1664feb1102872eb2 |
Close
Hashes for damo_embedding-1.1.9-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 317e007d2a4ea77fa138696ef76e3f19c30d5ef7e56c0aedc4f484cedd48fce0 |
|
MD5 | 5bfc36e1c2609baf8baf8dcead67ff45 |
|
BLAKE2b-256 | a513a42a284278bb45fe1f1426b554b2bdf893951e095295e34dbc27f5afd4a1 |