<s> from logging import getLogger <EOL> import time <EOL> from chainer import Link , Chain , ChainList , optimizer , cuda , Function , gradient_check , Variable , optimizers , serializers , utils <EOL> import chainer <EOL> import six <EOL> from sklearn . base import BaseEstimator <EOL> import chainer . functions as F <EOL> import chainer . links as L <EOL> from commonml . sklearn import ChainerEstimator <EOL> import numpy as np <EOL> import math <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> class RnnEstimator ( ChainerEstimator ) : <EOL> def __init__ ( self , bprop_len = <NUM_LIT> , ** params ) : <EOL> super ( RnnEstimator , self ) . __init__ ( ** params ) <EOL> self . bprop_len = bprop_len <EOL> def fit ( self , X , y , report_interval = <NUM_LIT:1000> ) : <EOL> if X is None or y is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> xp = np if self . gpu < <NUM_LIT:0> else cuda . cupy <EOL> data_size = len ( X ) <EOL> jump = data_size // self . batch_size <EOL> cur_log_perp = xp . zeros ( ( ) ) <EOL> start_at = time . time ( ) <EOL> cur_at = start_at <EOL> accum_loss = <NUM_LIT:0> <EOL> batch_idxs = list ( range ( self . batch_size ) ) <EOL> self . model . predictor . reset_state ( ) <EOL> self . model . zerograds ( ) <EOL> for i in six . moves . range ( jump * self . n_epoch ) : <EOL> x = Variable ( xp . asarray ( [ X [ ( jump * j + i ) % data_size ] for j in batch_idxs ] ) ) <EOL> t = Variable ( xp . asarray ( [ y [ ( jump * j + i ) % data_size ] for j in batch_idxs ] ) ) <EOL> loss_i = self . model ( x , t ) <EOL> accum_loss += loss_i <EOL> cur_log_perp += loss_i . data <EOL> if ( i + <NUM_LIT:1> ) % self . bprop_len == <NUM_LIT:0> : <EOL> accum_loss . backward ( ) <EOL> accum_loss . unchain_backward ( ) <EOL> accum_loss = <NUM_LIT:0> <EOL> self . optimizer . update ( ) <EOL> self . model . predictor . reset_state ( ) <EOL> self . model . zerograds ( ) <EOL> if ( i + <NUM_LIT:1> ) % report_interval == <NUM_LIT:0> : <EOL> now = time . time ( ) <EOL> throuput = float ( report_interval ) / ( now - cur_at ) <EOL> perp = math . exp ( float ( cur_log_perp ) / report_interval ) <EOL> logger . info ( '<STR_LIT>' . format ( i + <NUM_LIT:1> , jump * self . n_epoch , perp , throuput ) ) <EOL> cur_at = now <EOL> cur_log_perp . fill ( <NUM_LIT:0> ) <EOL> def predict ( self , X ) : <EOL> xp = np if self . gpu < <NUM_LIT:0> else cuda . cupy <EOL> data_size = len ( X ) <EOL> results = None <EOL> for i in six . moves . range ( <NUM_LIT:0> , data_size , self . batch_size ) : <EOL> end = i + self . batch_size <EOL> x1 = X [ i : end if end < data_size else data_size ] <EOL> x2 = Variable ( xp . asarray ( x1 ) ) <EOL> pred = F . softmax ( self . model . predictor ( x2 , train = False ) ) <EOL> if results is None : <EOL> results = cuda . to_cpu ( pred . data ) <EOL> else : <EOL> results = xp . concatenate ( ( results , cuda . to_cpu ( pred . data ) ) , axis = <NUM_LIT:0> ) <EOL> return results </s>
