roywei commented on a change in pull request #14346: [MXNet-1334][Fit API]base class for estimator and eventhandler URL: https://github.com/apache/incubator-mxnet/pull/14346#discussion_r266105279
########## File path: python/mxnet/gluon/estimator/estimator.py ########## @@ -0,0 +1,203 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# coding: utf-8 +# pylint: disable=wildcard-import +"""Gluon Estimator""" + + +import warnings + +from .event_handler import LoggingHandler +from ... import * +from ... import gluon, autograd +from ...context import cpu, gpu, num_gpus +from ...metric import EvalMetric, Loss + +__all__ = ['Estimator'] + + +class Estimator(object): + """ + Estimator Class for easy model training + TODO: update doc + """ + + def __init__(self, net, + loss=None, + metrics=None, + initializer=None, + trainers=None, + context=None): + + self.net = net + if isinstance(loss, gluon.loss.Loss): + self.loss = [loss] + else: + self.loss = loss or [] + if isinstance(metrics, EvalMetric): + self.metrics = [metrics] + else: + self.metrics = metrics or [] + + self.initializer = initializer + # store training statistics + self.train_stats = {} + self.train_stats['epochs'] = [] + self.train_stats['learning_rate'] = [] + # time used for each epoch + self.train_stats['step'] = '' + for metric in self.metrics: + # record a history of metrics over each epoch + self.train_stats['train_' + metric.name] = [] + # only record the latest metric numbers after each batch + self.train_stats['batch_' + metric.name] = 0. + self.loss_metrics = [] + # using the metric wrapper for loss to record loss value + for loss in self.loss: + self.loss_metrics.append(Loss(loss.name)) + self.train_stats['train_' + loss.name] = [] + # only record the latest loss numbers after each batch + self.train_stats['batch_' + loss.name] = 0. + + # handle context + if isinstance(context, Context): + self.context = [context] + if not context: + if num_gpus() > 0: + # only use 1 GPU by default + if num_gpus() > 1: + warnings.warn("You have multiple GPUs, gpu(0) will be used by default." Review comment: @piyushghai Agree that using gpus will be simpler. But the context arg is exposed many places in MXNet/Gluon, from inference to traninig. right now there is no good way to only allow gpu as there are too many api accepting context argument. User may get confused and error if we have both gpus and context. e.g. ``` net.initialize(..., ctx=[mx.gpu(0)]) est = Estimator( net, ..., gpus=2) ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
