File size: 1,144 Bytes
efa0ad4 c9fbd14 efa0ad4 c9fbd14 efa0ad4 63d6e05 efa0ad4 a721924 efa0ad4 53184fe efa0ad4 a721924 |
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 |
import torch
from torch import nn
from sentence_transformers import SentenceTransformer
from regressor import *
import numpy as np
import os
ENCODER = os.getenv("ENCODER")
class NextUsRegressor(nn.Module):
def __init__(self):
super(NextUsRegressor, self).__init__()
self.embedder = SentenceTransformer(ENCODER)
self.regressor = WRegressor()
return
def forward(self, txts):
if type(txts) == str:
txts = [txts]
embedded = self.embedder.encode(np.array(txts))
# embedded_tensor = self.embedder(np.array(txts))
embedded_tensor = torch.tensor(embedded, dtype=torch.float32)
regressed = self.regressor(embedded_tensor)
# return regressed.tolist()
# TODO: actually handle list of strings
vals = regressed.flatten().tolist()
# must return the whole thing, not just the 0-th element
strs = list()
for t, v in list(zip(txts, vals)):
strs.append(str(round(v, 4)) + "\t" + t[:20])
return "\n".join(strs)
# return torch.tensor(val).unsqueeze(1)
|