当前位置: 首页>>代码示例>>Python>>正文


Python Solver.set_iter_start_callback方法代码示例

本文整理汇总了Python中solver.Solver.set_iter_start_callback方法的典型用法代码示例。如果您正苦于以下问题:Python Solver.set_iter_start_callback方法的具体用法?Python Solver.set_iter_start_callback怎么用?Python Solver.set_iter_start_callback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在solver.Solver的用法示例。


在下文中一共展示了Solver.set_iter_start_callback方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: cluster

# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import set_iter_start_callback [as 别名]
    def cluster(self, X, y=None, update_interval=None):
        N = X.shape[0]
        if not update_interval:
            update_interval = N
        batch_size = 256
        test_iter = mx.io.NDArrayIter({"data": X}, batch_size=batch_size, shuffle=False, last_batch_handle="pad")
        args = {k: mx.nd.array(v.asnumpy(), ctx=self.xpu) for k, v in self.args.items()}
        z = model.extract_feature(self.feature, args, None, test_iter, N, self.xpu).values()[0]
        kmeans = KMeans(self.num_centers, n_init=20)
        kmeans.fit(z)
        args["dec_mu"][:] = kmeans.cluster_centers_
        solver = Solver("sgd", momentum=0.9, wd=0.0, learning_rate=0.01)

        def ce(label, pred):
            return np.sum(label * np.log(label / (pred + 0.000001))) / label.shape[0]

        solver.set_metric(mx.metric.CustomMetric(ce))

        label_buff = np.zeros((X.shape[0], self.num_centers))
        train_iter = mx.io.NDArrayIter(
            {"data": X}, {"label": label_buff}, batch_size=batch_size, shuffle=False, last_batch_handle="roll_over"
        )
        self.y_pred = np.zeros((X.shape[0]))

        def refresh(i):
            if i % update_interval == 0:
                z = model.extract_feature(self.feature, args, None, test_iter, N, self.xpu).values()[0]
                p = np.zeros((z.shape[0], self.num_centers))
                self.dec_op.forward([z, args["dec_mu"].asnumpy()], [p])
                y_pred = p.argmax(axis=1)
                print np.std(np.bincount(y_pred)), np.bincount(y_pred)
                print np.std(np.bincount(y.astype(np.int))), np.bincount(y.astype(np.int))
                if y is not None:
                    print (cluster_acc(y_pred, y)[0])
                weight = 1.0 / p.sum(axis=0)
                weight *= self.num_centers / weight.sum()
                p = (p ** 2) * weight
                train_iter.data_list[1][:] = (p.T / p.sum(axis=1)).T
                print np.sum(y_pred != self.y_pred), 0.001 * y_pred.shape[0]
                if np.sum(y_pred != self.y_pred) < 0.001 * y_pred.shape[0]:
                    self.y_pred = y_pred
                    return True
                self.y_pred = y_pred

        solver.set_iter_start_callback(refresh)
        solver.set_monitor(Monitor(50))

        solver.solve(self.xpu, self.loss, args, self.args_grad, None, train_iter, 0, 1000000000, {}, False)
        self.end_args = args
        if y is not None:
            return cluster_acc(self.y_pred, y)[0]
        else:
            return -1
开发者ID:253681319,项目名称:mxnet,代码行数:55,代码来源:dec.py


注:本文中的solver.Solver.set_iter_start_callback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。