示例#1
0
    def forward(self, data, train=False):
        features = np.asarray([d.features for d in data])
        max_len = max(len(d.demonstration) for d in data)
        if self.categorical:
            n_targets = N_CLASSES
            targets = np.zeros((len(data), max_len))
        else:
            n_targets = len(d.demonstration[0])
            targets = np.zeros((len(data), max_len, n_targets))
        masks = np.zeros((len(data), max_len, n_targets))
        for i_datum in range(len(data)):
            demo_len = len(data[i_datum].demonstration)
            targets[i_datum, :demo_len, ...] = data[i_datum].demonstration
            masks[i_datum, :demo_len, ...] = 1

        l_features = "features"
        l_ip_repr = "ip_repr"
        l_relu_repr = "relu_repr"
        lt_mask = "mask_%d"
        lt_target = "target_%d"

        self.net.clear_forward()
        self.net.f(NumpyData(l_features, features))
        self.net.f(InnerProduct(l_ip_repr, N_HIDDEN, bottoms=[l_features]))
        self.net.f(ReLU(l_relu_repr, bottoms=[l_ip_repr]))

        l_plan = self.think(l_relu_repr, randomize=train)

        if train:
            ll_targets = []
            ll_masks = []
            for i_target in range(1, max_len):
                l_target = lt_target % i_target
                l_mask = lt_mask % i_target
                self.net.f(NumpyData(l_target, targets[:, i_target]))
                self.net.f(NumpyData(l_mask, masks[:, i_target]))
                ll_targets.append(l_target)
                ll_masks.append(l_mask)
        else:
            ll_targets = None
            ll_masks = None

        loss, ll_predictions = self.act(l_plan, max_len, data, ll_targets,
                ll_masks, self_init=not train)

        if train:
            self.net.backward()
            adadelta.update(self.net, self.opt_state, OPT_PARAMS)

        return loss, ll_predictions
示例#2
0
    def forward(self, data, train=False):
        features = np.asarray([d.features for d in data])
        max_len = max(len(d.demonstration) for d in data)

        n_targets = len(d.demonstration[0])
        targets = np.zeros((len(data), max_len, n_targets))
        masks = np.zeros((len(data), max_len, n_targets))

        for i_datum in range(len(data)):
            demo_len = len(data[i_datum].demonstration)
            targets[i_datum, :demo_len, ...] = data[i_datum].demonstration
            masks[i_datum, :demo_len, ...] = 1

        l_features = "features"
        l_ip_repr = "ip_repr"
        l_relu_repr = "relu_repr"
        lt_mask = "mask_%d"
        lt_target = "target_%d"

        self.net.clear_forward()
        self.net.f(NumpyData(l_features, features))
        self.net.f(InnerProduct(l_ip_repr, N_HIDDEN, bottoms=[l_features]))
        self.net.f(ReLU(l_relu_repr, bottoms=[l_ip_repr]))

        ll_pred1 = self.initialize(l_relu_repr, max_len, n_targets, data,
                self_init=not train)
        ll_pred2 = self.refine(ll_pred1, n_targets)
        #ll_pred2 = ll_pred1

        if train:
            ll_targets = []
            ll_masks = []
            for i_target in range(1, max_len):
                l_target = lt_target % i_target
                l_mask = lt_mask % i_target
                self.net.f(NumpyData(l_target, targets[:, i_target]))
                self.net.f(NumpyData(l_mask, masks[:, i_target]))
                ll_targets.append(l_target)
                ll_masks.append(l_mask)

            loss1 = self.loss("pred1", ll_pred1, ll_targets, ll_masks)
            loss2 = self.loss("pred2", ll_pred2, ll_targets, ll_masks)
            loss = np.asarray([loss1, loss2])
            self.net.backward()
            adadelta.update(self.net, self.opt_state, OPT_PARAMS)
        else:
            loss = None

        return loss, ll_pred2
示例#3
0
    def forward(self, features, targets, masks, train=False):
        features = np.asarray(features)
        #positions = np.asarray(positions)
        target = np.asarray(targets)
        mask = np.asarray(masks)

        l_features = "features"
        l_positions = "positions"
        l_concat = "concat"
        lt_ip = "ip_%d"
        lt_relu = "relu_%d"
        l_target = "targets"
        l_mask = "mask"
        l_mul_mask = "mul_mask"
        l_loss = "loss"

        self.net.clear_forward()
        self.net.f(NumpyData(l_features, features))
        #self.net.f(NumpyData(l_positions, positions))
        #self.net.f(Concat(l_concat, bottoms=[l_features, l_positions]))

        l_prev = l_features
        for i_layer in range(N_LAYERS - 1):
            l_ip = lt_ip % i_layer
            l_relu = lt_relu % i_layer
            self.net.f(InnerProduct(l_ip, N_HIDDEN, bottoms=[l_prev]))
            self.net.f(ReLU(l_relu, bottoms=[l_ip]))
            l_prev = l_relu

        l_ip = lt_ip % (N_LAYERS - 1)
        self.net.f(InnerProduct(l_ip, self.n_targets, bottoms=[l_prev]))
        self.l_predict = l_ip

        if train:
            self.net.f(NumpyData(l_target, target))
            if self.categorical:
                loss = self.net.f(SoftmaxWithLoss(l_loss, 
                    bottoms=[self.l_predict, l_target]))
            else:
                self.net.f(NumpyData(l_mask, mask))
                self.net.f(Eltwise(l_mul_mask, "PROD", bottoms=[l_mask, self.l_predict]))
                loss = self.net.f(EuclideanLoss(l_loss, bottoms=[l_target, l_mul_mask]))
            self.net.backward()
            adadelta.update(self.net, self.opt_state, OPT_PARAMS)
            return np.asarray([loss])
示例#4
0
文件: nmn.py 项目: zxsted/nmn2
 def train(self):
     self.reinforce_layout(self.cumulative_datum_losses)
     self.apollo_net.backward()
     adadelta.update(self.apollo_net, self.opt_state, self.opt_config)
示例#5
0
文件: att.py 项目: BinbinBian/nmn2
 def train(self):
     self.apollo_net.backward()
     adadelta.update(self.apollo_net, self.opt_state, self.opt_config)
示例#6
0
文件: nmn.py 项目: jacobandreas/nmn2
 def train(self):
     self.reinforce_layout(self.cumulative_datum_losses)
     self.apollo_net.backward()
     adadelta.update(self.apollo_net, self.opt_state, self.opt_config)
示例#7
0
文件: att.py 项目: zxsted/nmn2
 def train(self):
     self.apollo_net.backward()
     adadelta.update(self.apollo_net, self.opt_state, self.opt_config)