|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import paddle |
| 16 | +import paddle.nn as nn |
| 17 | +import paddle.nn.functional as F |
| 18 | +import math |
| 19 | +import numpy as np |
| 20 | +import pickle |
| 21 | +import net |
| 22 | + |
| 23 | + |
| 24 | +class DygraphModel(): |
| 25 | + # define model |
| 26 | + def create_model(self, config): |
| 27 | + max_idxs = config.get("hyper_parameters.max_idxs") |
| 28 | + embed_dim = config.get("hyper_parameters.embed_dim") |
| 29 | + mlp_dims = config.get("hyper_parameters.mlp_dims") |
| 30 | + |
| 31 | + num_expert = config.get("hyper_parameters.num_expert") |
| 32 | + num_output = config.get("hyper_parameters.num_output") |
| 33 | + |
| 34 | + meta_model = net.WideAndDeepModel(max_idxs, embed_dim, mlp_dims, |
| 35 | + num_expert, num_output) |
| 36 | + # model_state_dict = paddle.load('paddle.pkl') |
| 37 | + # meta_model.set_dict(model_state_dict) |
| 38 | + |
| 39 | + return meta_model |
| 40 | + |
| 41 | + # define feeds which convert numpy of batch data to paddle.tensor |
| 42 | + def create_feeds(self, batch_data, config): |
| 43 | + x_spt = batch_data[0] |
| 44 | + y_spt = batch_data[1] |
| 45 | + |
| 46 | + x_qry = batch_data[2] |
| 47 | + y_qry = batch_data[3] |
| 48 | + return x_spt, y_spt, x_qry, y_qry |
| 49 | + |
| 50 | + # define loss function by predicts and label |
| 51 | + def create_loss(self, pred, y_label): |
| 52 | + |
| 53 | + loss_ctr = paddle.nn.functional.log_loss( |
| 54 | + input=pred, label=paddle.cast( |
| 55 | + y_label, dtype="float32")) |
| 56 | + return loss_ctr |
| 57 | + |
| 58 | + # define optimizer |
| 59 | + def create_optimizer(self, dy_model, config, mode="train"): |
| 60 | + if mode == "train": |
| 61 | + lr = config.get("hyper_parameters.optimizer.global_learning_rate", |
| 62 | + 0.001) |
| 63 | + optimizer = paddle.optimizer.Adam( |
| 64 | + learning_rate=lr, parameters=dy_model.parameters()) |
| 65 | + else: |
| 66 | + lr = config.get( |
| 67 | + "hyper_parameters.optimizer.local_test_learning_rate", 0.001) |
| 68 | + optimizer = paddle.optimizer.Adam( |
| 69 | + learning_rate=lr, parameters=dy_model.parameters()) |
| 70 | + return optimizer |
| 71 | + |
| 72 | + # define metrics such as auc/acc |
| 73 | + # multi-task need to define multi metric |
| 74 | + def create_metrics(self): |
| 75 | + metrics_list_name = ["AUC"] |
| 76 | + auc_ctr_metric = paddle.metric.Auc("ROC") |
| 77 | + metrics_list = [auc_ctr_metric] |
| 78 | + return metrics_list, metrics_list_name |
| 79 | + |
| 80 | + # construct train forward phase |
| 81 | + def train_forward(self, dy_model, metric_list, batch, config): |
| 82 | + # print(len(batch)) |
| 83 | + # exit(0) |
| 84 | + x_spt, y_spt, x_qry, y_qry = self.create_feeds(batch, config) |
| 85 | + |
| 86 | + task_count = config.get("hyper_parameters.task_count", 5) |
| 87 | + local_lr = config.get("hyper_parameters.local_lr", 0.0002) |
| 88 | + criterion = paddle.nn.BCELoss() |
| 89 | + |
| 90 | + losses_q = [] |
| 91 | + dy_model.clear_gradients() |
| 92 | + for i in range(task_count): |
| 93 | + ## local update -------------- |
| 94 | + fast_parameters = list(dy_model.parameters()) |
| 95 | + for weight in fast_parameters: |
| 96 | + weight.fast = None |
| 97 | + |
| 98 | + support_set_y_pred = dy_model(x_spt[i]) |
| 99 | + label = paddle.squeeze(y_spt[i].astype('float32')) |
| 100 | + |
| 101 | + loss = criterion(support_set_y_pred, label) |
| 102 | + dy_model.clear_gradients() |
| 103 | + loss.backward() |
| 104 | + |
| 105 | + fast_parameters = list(dy_model.parameters()) |
| 106 | + for weight in fast_parameters: |
| 107 | + if weight.grad is None: |
| 108 | + continue |
| 109 | + if weight.fast is None: |
| 110 | + weight.fast = weight - local_lr * weight.grad # create weight.fast |
| 111 | + else: |
| 112 | + weight.fast = weight.fast - local_lr * weight.grad |
| 113 | + dy_model.clear_gradients() |
| 114 | + ## local update -------------- |
| 115 | + |
| 116 | + query_set_y_pred = dy_model(x_qry[i]) |
| 117 | + label = paddle.squeeze(y_qry[i].astype('float32')) |
| 118 | + loss_q = criterion(query_set_y_pred, label) |
| 119 | + losses_q.append(loss_q) |
| 120 | + |
| 121 | + loss_average = paddle.stack(losses_q).mean(0) |
| 122 | + print_dict = {'loss': loss_average} |
| 123 | + |
| 124 | + return loss_average, metric_list, print_dict |
| 125 | + |
| 126 | + def infer_train_forward(self, dy_model, batch, config): |
| 127 | + batch_x, batch_y = batch[0], batch[1] |
| 128 | + criterion = paddle.nn.BCELoss() |
| 129 | + |
| 130 | + pred = dy_model.forward(batch_x) |
| 131 | + |
| 132 | + label = paddle.squeeze(batch_y.astype('float32')) |
| 133 | + loss_q = criterion(pred, label) |
| 134 | + |
| 135 | + return loss_q |
| 136 | + |
| 137 | + def infer_forward(self, dy_model, metric_list, metric_list_local, batch, |
| 138 | + config): |
| 139 | + batch_x, batch_y = batch[0], batch[1] |
| 140 | + pred = dy_model.forward(batch_x) |
| 141 | + label = paddle.squeeze(batch_y.astype('float32')) |
| 142 | + |
| 143 | + pred = paddle.unsqueeze(pred, 1) |
| 144 | + pred = paddle.concat([1 - pred, pred], 1) |
| 145 | + |
| 146 | + metric_list[0].update(preds=pred.numpy(), labels=label.numpy()) |
| 147 | + metric_list_local[0].update(preds=pred.numpy(), labels=label.numpy()) |
| 148 | + |
| 149 | + return metric_list, metric_list_local |
0 commit comments