Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ts export infer #525

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions paddlets/models/anomaly/dl/anomaly_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,8 @@ def save(self,
path: str,
network_model: bool=False,
dygraph_to_static: bool=True,
batch_size: Optional[int]=None) -> None:
batch_size: Optional[int]=None,
data_info: Optional[dict]=None) -> None:
"""
Saves a AnomalyBaseModel instance to a disk file.

Expand Down Expand Up @@ -872,17 +873,27 @@ def save(self,
% (internal_filename_map["network_model"], str(e))))

# 2 save model meta (e.g. classname)
try:
with open(
os.path.join(abs_root_path,
internal_filename_map["model_meta"]),
"w") as f:
json.dump(model_meta, f, ensure_ascii=False)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
internal_filename_map["model_meta"], str(e))))
try:
with open(os.path.join(abs_root_path, 'inference.yml'), "w") as f:
if data_info is not None:
model_meta.update(data_info)
json.dump(model_meta, f, ensure_ascii=False)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
internal_filename_map["model_meta"], str(e))))

if not network_model:
try:
with open(
os.path.join(abs_root_path,
internal_filename_map["model_meta"]),
"w") as f:
json.dump(model_meta, f, ensure_ascii=False)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
internal_filename_map["model_meta"], str(e))))
# 3 save optimizer state dict (currently ignore optimizer logic.)
# optimizer_state_dict = self._optimizer.state_dict()
# try:
Expand All @@ -899,17 +910,18 @@ def save(self,
# )

# 4 save network state dict
network_state_dict = self._network.state_dict()
try:
paddle.save(
obj=network_state_dict,
path=os.path.join(abs_root_path,
internal_filename_map["network_statedict"]))
except Exception as e:
raise_log(
ValueError("error occurred while saving %s: %s, err: %s" %
(internal_filename_map["network_statedict"],
network_state_dict, str(e))))
if not network_model:
network_state_dict = self._network.state_dict()
try:
paddle.save(
obj=network_state_dict,
path=os.path.join(abs_root_path,
internal_filename_map["network_statedict"]))
except Exception as e:
raise_log(
ValueError("error occurred while saving %s: %s, err: %s" %
(internal_filename_map["network_statedict"],
network_state_dict, str(e))))

# 5 save model
optimizer = self._optimizer
Expand All @@ -927,13 +939,14 @@ def save(self,
# loss_fn could possibly contain paddle.Tensor when it is a bound method of a class, thus needs to set to
# None to avoid pickle.dumps failure.
self._loss_fn = None
try:
with open(abs_model_path, "wb") as f:
pickle.dump(self, f)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
abs_model_path, str(e))))
if not network_model:
try:
with open(abs_model_path, "wb") as f:
pickle.dump(self, f)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
abs_model_path, str(e))))

# in order to allow a model instance to be saved multiple times, set attrs back.
self._optimizer = optimizer
Expand Down
13 changes: 7 additions & 6 deletions paddlets/models/base_model/_timesnet/timesblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def FFT_for_Period(x, k=2):
frequency_list[0] = 0
_, top_list = paddle.topk(x=frequency_list, k=k) # 幅度最大的前k个
top_list = top_list.detach().cast('int32')
period = x.shape[1] // top_list.cpu().numpy() # 长度/频率=周期
period = paddle.shape(x)[1] // top_list # 长度/频率=周期
return period, xf.abs().mean(axis=-1).index_select(
index=top_list, axis=1) # 幅值最大的几个频率项

Expand All @@ -26,6 +26,7 @@ def __init__(
super(TimesBlock, self).__init__()
self.seq_len = in_chunk_len
self.pred_len = out_chunk_len
self.seq_pred_len = paddle.to_tensor(self.seq_len + self.pred_len)
self.k = top_k
self.conv = nn.Sequential(
Inception_Block_V1(
Expand All @@ -41,15 +42,15 @@ def forward(self, x):
res = []
for i in range(self.k):
period = period_list[i]
if (self.seq_len + self.pred_len) % period != 0:
length = ((self.seq_len + self.pred_len) // period + 1) * period
if self.seq_pred_len % period != 0:
length = (self.seq_pred_len // period + 1) * period
padding = paddle.zeros(shape=[
x.shape[0], length - (self.seq_len + self.pred_len),
x.shape[0], length - self.seq_pred_len,
x.shape[2]
])
out = paddle.concat(x=[x, padding], axis=1)
out = paddle.concat(x=[x.cast('float32'), padding.cast('float32')], axis=1)
else:
length = self.seq_len + self.pred_len
length = self.seq_pred_len
out = x

out = out.reshape([B, length // period, period, N]).transpose(
Expand Down
5 changes: 4 additions & 1 deletion paddlets/models/classify/dl/adapter/paddle_dataset_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def format_labels(self, labels):
self.classes_ = self.label_encoder.classes_
self.n_classes_ = len(self.classes_)
y = y.reshape(len(y), 1)
self.onehot_encoder = OneHotEncoder(sparse=False, categories="auto")
try:
self.onehot_encoder = OneHotEncoder(sparse=False, categories="auto")
except:
self.onehot_encoder = OneHotEncoder(sparse_output=False, categories="auto")
y = self.onehot_encoder.fit_transform(y).astype(np.float32)
return y

Expand Down
75 changes: 44 additions & 31 deletions paddlets/models/classify/dl/paddle_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ def save(self,
path: str,
network_model: bool=False,
dygraph_to_static=True,
batch_size: Optional[int]=None) -> None:
batch_size: Optional[int]=None,
data_info: Optional[dict]=None) -> None:
"""
Saves a PaddleBaseClassifier instance to a disk file.

Expand Down Expand Up @@ -713,30 +714,40 @@ def save(self,
% (internal_filename_map["network_model"], str(e))))

# 2 save model meta (e.g. classname)
try:
with open(
os.path.join(abs_root_path,
internal_filename_map["model_meta"]),
"w") as f:
json.dump(model_meta, f, ensure_ascii=False)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
internal_filename_map["model_meta"], str(e))))
try:
with open(os.path.join(abs_root_path, 'inference.yml'), "w") as f:
if data_info is not None:
model_meta.update(data_info)
json.dump(model_meta, f, ensure_ascii=False)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
internal_filename_map["model_meta"], str(e))))

if not network_model:
try:
with open(
os.path.join(abs_root_path,
internal_filename_map["model_meta"]),
"w") as f:
json.dump(model_meta, f, ensure_ascii=False)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
internal_filename_map["model_meta"], str(e))))
# 3 save network state dict
network_state_dict = self._network.state_dict()
try:
paddle.save(
obj=network_state_dict,
path=os.path.join(abs_root_path,
internal_filename_map["network_statedict"]),
)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s: %s, err: %s" %
(internal_filename_map["network_statedict"],
network_state_dict, str(e))))
if not network_model:
network_state_dict = self._network.state_dict()
try:
paddle.save(
obj=network_state_dict,
path=os.path.join(abs_root_path,
internal_filename_map["network_statedict"]), )
except Exception as e:
raise_log(
ValueError("error occurred while saving %s: %s, err: %s" %
(internal_filename_map["network_statedict"],
network_state_dict, str(e))))

# 4 save model
optimizer = self._optimizer
Expand All @@ -754,13 +765,14 @@ def save(self,
# loss_fn could possibly contain paddle.Tensor when it is a bound method of a class, thus needs to set to
# None to avoid pickle.dumps failure.
self._loss_fn = None
try:
with open(abs_model_path, "wb") as f:
pickle.dump(self, f)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
abs_model_path, str(e))))
if not network_model:
try:
with open(abs_model_path, "wb") as f:
pickle.dump(self, f)
except Exception as e:
raise_log(
ValueError("error occurred while saving %s, err: %s" % (
abs_model_path, str(e))))

# in order to allow a model instance to be saved multiple times, set attrs back.
self._optimizer = optimizer
Expand Down Expand Up @@ -836,7 +848,8 @@ def _build_meta(self):
"features": [
None, self._fit_params["input_lens"],
self._fit_params["feature_dim"]
]
],
"pad_mask": [None, self._fit_params["input_lens"]]
},
}

Expand Down
7 changes: 7 additions & 0 deletions paddlets/models/forecasting/dl/Nonstationary.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def __init__(self,
DSAttention(
False,
factor,
d_model,
n_heads,
attention_dropout=dropout,
output_attention=output_attention),
d_model,
Expand All @@ -138,6 +140,8 @@ def __init__(self,
DSAttention(
True,
factor,
d_model,
n_heads,
attention_dropout=dropout,
output_attention=False),
d_model,
Expand All @@ -146,6 +150,8 @@ def __init__(self,
DSAttention(
False,
factor,
d_model,
n_heads,
attention_dropout=dropout,
output_attention=False),
d_model,
Expand Down Expand Up @@ -183,6 +189,7 @@ def forward(self,
dec_self_mask=None,
dec_enc_mask=None):
x_enc = x["past_target"]

times_mark = x.get("known_cov_numeric", None)
x_mark_enc = times_mark[:, :self.seq_len, :]
dec_inp = paddle.zeros(shape=[x_enc.shape[0], self.pred_len, self.c_in])
Expand Down
6 changes: 6 additions & 0 deletions paddlets/models/forecasting/dl/TimesNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ def _update_fit_params(
"known_cov_dim": 0,
"observed_cov_dim": 0
}
if train_tsdataset[0].get_known_cov() is not None:
fit_params["known_cov_dim"] = train_tsdataset[0].get_known_cov(
).data.shape[1]
if train_tsdataset[0].get_observed_cov() is not None:
fit_params["observed_cov_dim"] = train_tsdataset[
0].get_observed_cov().data.shape[1]
return fit_params

@revin_norm # aligned? not used seemly
Expand Down
7 changes: 5 additions & 2 deletions paddlets/models/forecasting/dl/_nonstationary/MHSA_Family.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class DSAttention(paddle.nn.Layer):
def __init__(self,
mask_flag=True,
factor=5,
d_model=128,
num_heads=8,
scale=None,
attention_dropout=0.1,
output_attention=False):
super(DSAttention, self).__init__()
self.scale = scale
E = d_model // num_heads
self.scale = scale if scale is not None else 1.0 / sqrt(E)
self.mask_flag = mask_flag
self.output_attention = output_attention
self.dropout = paddle.nn.Dropout(p=attention_dropout)
Expand All @@ -29,7 +32,7 @@ def __init__(self,
def forward(self, queries, keys, values, attn_mask, tau=None, delta=None):
B, L, H, E = queries.shape
_, S, _, D = values.shape
scale = self.scale or 1.0 / sqrt(E)
scale = self.scale
tau = 1.0 if tau is None else tau.unsqueeze(axis=1).unsqueeze(axis=1)
delta = 0.0 if delta is None else delta.unsqueeze(axis=1).unsqueeze(
axis=1)
Expand Down
Loading