partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
discriminator1
First part of the discriminator which takes a 32x32 image as input and output a convolutional feature map, this is required to calculate the layer loss
example/vae-gan/vaegan_mxnet.py
def discriminator1(ndf, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12): '''First part of the discriminator which takes a 32x32 image as input and output a convolutional feature map, this is required to calculate the layer loss''' BatchNorm = mx.sym.BatchNorm data = mx.sym.Variable('data') d1 = mx.sym.Convolution(data, name='d1', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf, no_bias=no_bias) dact1 = mx.sym.LeakyReLU(d1, name='dact1', act_type='leaky', slope=0.2) d2 = mx.sym.Convolution(dact1, name='d2', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf*2, no_bias=no_bias) dbn2 = BatchNorm(d2, name='dbn2', fix_gamma=fix_gamma, eps=eps) dact2 = mx.sym.LeakyReLU(dbn2, name='dact2', act_type='leaky', slope=0.2) d3 = mx.sym.Convolution(dact2, name='d3', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf*4, no_bias=no_bias) dbn3 = BatchNorm(d3, name='dbn3', fix_gamma=fix_gamma, eps=eps) dact3 = mx.sym.LeakyReLU(dbn3, name='dact3', act_type='leaky', slope=0.2) return dact3
def discriminator1(ndf, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12): '''First part of the discriminator which takes a 32x32 image as input and output a convolutional feature map, this is required to calculate the layer loss''' BatchNorm = mx.sym.BatchNorm data = mx.sym.Variable('data') d1 = mx.sym.Convolution(data, name='d1', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf, no_bias=no_bias) dact1 = mx.sym.LeakyReLU(d1, name='dact1', act_type='leaky', slope=0.2) d2 = mx.sym.Convolution(dact1, name='d2', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf*2, no_bias=no_bias) dbn2 = BatchNorm(d2, name='dbn2', fix_gamma=fix_gamma, eps=eps) dact2 = mx.sym.LeakyReLU(dbn2, name='dact2', act_type='leaky', slope=0.2) d3 = mx.sym.Convolution(dact2, name='d3', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf*4, no_bias=no_bias) dbn3 = BatchNorm(d3, name='dbn3', fix_gamma=fix_gamma, eps=eps) dact3 = mx.sym.LeakyReLU(dbn3, name='dact3', act_type='leaky', slope=0.2) return dact3
[ "First", "part", "of", "the", "discriminator", "which", "takes", "a", "32x32", "image", "as", "input", "and", "output", "a", "convolutional", "feature", "map", "this", "is", "required", "to", "calculate", "the", "layer", "loss" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L118-L137
[ "def", "discriminator1", "(", "ndf", ",", "no_bias", "=", "True", ",", "fix_gamma", "=", "True", ",", "eps", "=", "1e-5", "+", "1e-12", ")", ":", "BatchNorm", "=", "mx", ".", "sym", ".", "BatchNorm", "data", "=", "mx", ".", "sym", ".", "Variable", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
discriminator2
Second part of the discriminator which takes a 256x8x8 feature map as input and generates the loss based on whether the input image was a real one or fake one
example/vae-gan/vaegan_mxnet.py
def discriminator2(ndf, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12): '''Second part of the discriminator which takes a 256x8x8 feature map as input and generates the loss based on whether the input image was a real one or fake one''' BatchNorm = mx.sym.BatchNorm data = mx.sym.Variable('data') label = mx.sym.Variable('label') d4 = mx.sym.Convolution(data, name='d4', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf*8, no_bias=no_bias) dbn4 = BatchNorm(d4, name='dbn4', fix_gamma=fix_gamma, eps=eps) dact4 = mx.sym.LeakyReLU(dbn4, name='dact4', act_type='leaky', slope=0.2) h = mx.sym.Flatten(dact4) d5 = mx.sym.FullyConnected(h, num_hidden=1, name="d5") dloss = mx.sym.LogisticRegressionOutput(data=d5, label=label, name='dloss') return dloss
def discriminator2(ndf, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12): '''Second part of the discriminator which takes a 256x8x8 feature map as input and generates the loss based on whether the input image was a real one or fake one''' BatchNorm = mx.sym.BatchNorm data = mx.sym.Variable('data') label = mx.sym.Variable('label') d4 = mx.sym.Convolution(data, name='d4', kernel=(5,5), stride=(2,2), pad=(2,2), num_filter=ndf*8, no_bias=no_bias) dbn4 = BatchNorm(d4, name='dbn4', fix_gamma=fix_gamma, eps=eps) dact4 = mx.sym.LeakyReLU(dbn4, name='dact4', act_type='leaky', slope=0.2) h = mx.sym.Flatten(dact4) d5 = mx.sym.FullyConnected(h, num_hidden=1, name="d5") dloss = mx.sym.LogisticRegressionOutput(data=d5, label=label, name='dloss') return dloss
[ "Second", "part", "of", "the", "discriminator", "which", "takes", "a", "256x8x8", "feature", "map", "as", "input", "and", "generates", "the", "loss", "based", "on", "whether", "the", "input", "image", "was", "a", "real", "one", "or", "fake", "one" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L139-L159
[ "def", "discriminator2", "(", "ndf", ",", "no_bias", "=", "True", ",", "fix_gamma", "=", "True", ",", "eps", "=", "1e-5", "+", "1e-12", ")", ":", "BatchNorm", "=", "mx", ".", "sym", ".", "BatchNorm", "data", "=", "mx", ".", "sym", ".", "Variable", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
GaussianLogDensity
GaussianLogDensity loss calculation for layer wise loss
example/vae-gan/vaegan_mxnet.py
def GaussianLogDensity(x, mu, log_var, name='GaussianLogDensity', EPSILON = 1e-6): '''GaussianLogDensity loss calculation for layer wise loss ''' c = mx.sym.ones_like(log_var)*2.0 * 3.1416 c = mx.symbol.log(c) var = mx.sym.exp(log_var) x_mu2 = mx.symbol.square(x - mu) # [Issue] not sure the dim works or not? x_mu2_over_var = mx.symbol.broadcast_div(x_mu2, var + EPSILON) log_prob = -0.5 * (c + log_var + x_mu2_over_var) log_prob = mx.symbol.sum(log_prob, axis=1, name=name) # keep_dims=True, return log_prob
def GaussianLogDensity(x, mu, log_var, name='GaussianLogDensity', EPSILON = 1e-6): '''GaussianLogDensity loss calculation for layer wise loss ''' c = mx.sym.ones_like(log_var)*2.0 * 3.1416 c = mx.symbol.log(c) var = mx.sym.exp(log_var) x_mu2 = mx.symbol.square(x - mu) # [Issue] not sure the dim works or not? x_mu2_over_var = mx.symbol.broadcast_div(x_mu2, var + EPSILON) log_prob = -0.5 * (c + log_var + x_mu2_over_var) log_prob = mx.symbol.sum(log_prob, axis=1, name=name) # keep_dims=True, return log_prob
[ "GaussianLogDensity", "loss", "calculation", "for", "layer", "wise", "loss" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L161-L171
[ "def", "GaussianLogDensity", "(", "x", ",", "mu", ",", "log_var", ",", "name", "=", "'GaussianLogDensity'", ",", "EPSILON", "=", "1e-6", ")", ":", "c", "=", "mx", ".", "sym", ".", "ones_like", "(", "log_var", ")", "*", "2.0", "*", "3.1416", "c", "=",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
DiscriminatorLayerLoss
Calculate the discriminator layer loss
example/vae-gan/vaegan_mxnet.py
def DiscriminatorLayerLoss(): '''Calculate the discriminator layer loss ''' data = mx.sym.Variable('data') label = mx.sym.Variable('label') data = mx.sym.Flatten(data) label = mx.sym.Flatten(label) label = mx.sym.BlockGrad(label) zeros = mx.sym.zeros_like(data) output = -GaussianLogDensity(label, data, zeros) dloss = mx.symbol.MakeLoss(mx.symbol.mean(output),name='lloss') return dloss
def DiscriminatorLayerLoss(): '''Calculate the discriminator layer loss ''' data = mx.sym.Variable('data') label = mx.sym.Variable('label') data = mx.sym.Flatten(data) label = mx.sym.Flatten(label) label = mx.sym.BlockGrad(label) zeros = mx.sym.zeros_like(data) output = -GaussianLogDensity(label, data, zeros) dloss = mx.symbol.MakeLoss(mx.symbol.mean(output),name='lloss') return dloss
[ "Calculate", "the", "discriminator", "layer", "loss" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L173-L192
[ "def", "DiscriminatorLayerLoss", "(", ")", ":", "data", "=", "mx", ".", "sym", ".", "Variable", "(", "'data'", ")", "label", "=", "mx", ".", "sym", ".", "Variable", "(", "'label'", ")", "data", "=", "mx", ".", "sym", ".", "Flatten", "(", "data", ")...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
KLDivergenceLoss
KLDivergenceLoss loss
example/vae-gan/vaegan_mxnet.py
def KLDivergenceLoss(): '''KLDivergenceLoss loss ''' data = mx.sym.Variable('data') mu1, lv1 = mx.sym.split(data, num_outputs=2, axis=0) mu2 = mx.sym.zeros_like(mu1) lv2 = mx.sym.zeros_like(lv1) v1 = mx.sym.exp(lv1) v2 = mx.sym.exp(lv2) mu_diff_sq = mx.sym.square(mu1 - mu2) dimwise_kld = .5 * ( (lv2 - lv1) + mx.symbol.broadcast_div(v1, v2) + mx.symbol.broadcast_div(mu_diff_sq, v2) - 1.) KL = mx.symbol.sum(dimwise_kld, axis=1) KLloss = mx.symbol.MakeLoss(mx.symbol.mean(KL),name='KLloss') return KLloss
def KLDivergenceLoss(): '''KLDivergenceLoss loss ''' data = mx.sym.Variable('data') mu1, lv1 = mx.sym.split(data, num_outputs=2, axis=0) mu2 = mx.sym.zeros_like(mu1) lv2 = mx.sym.zeros_like(lv1) v1 = mx.sym.exp(lv1) v2 = mx.sym.exp(lv2) mu_diff_sq = mx.sym.square(mu1 - mu2) dimwise_kld = .5 * ( (lv2 - lv1) + mx.symbol.broadcast_div(v1, v2) + mx.symbol.broadcast_div(mu_diff_sq, v2) - 1.) KL = mx.symbol.sum(dimwise_kld, axis=1) KLloss = mx.symbol.MakeLoss(mx.symbol.mean(KL),name='KLloss') return KLloss
[ "KLDivergenceLoss", "loss" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L194-L211
[ "def", "KLDivergenceLoss", "(", ")", ":", "data", "=", "mx", ".", "sym", ".", "Variable", "(", "'data'", ")", "mu1", ",", "lv1", "=", "mx", ".", "sym", ".", "split", "(", "data", ",", "num_outputs", "=", "2", ",", "axis", "=", "0", ")", "mu2", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_data
Get the dataset
example/vae-gan/vaegan_mxnet.py
def get_data(path, activation): '''Get the dataset ''' data = [] image_names = [] for filename in os.listdir(path): img = cv2.imread(os.path.join(path,filename), cv2.IMREAD_GRAYSCALE) image_names.append(filename) if img is not None: data.append(img) data = np.asarray(data) if activation == 'sigmoid': data = data.astype(np.float32)/(255.0) elif activation == 'tanh': data = data.astype(np.float32)/(255.0/2) - 1.0 data = data.reshape((data.shape[0], 1, data.shape[1], data.shape[2])) np.random.seed(1234) p = np.random.permutation(data.shape[0]) X = data[p] return X, image_names
def get_data(path, activation): '''Get the dataset ''' data = [] image_names = [] for filename in os.listdir(path): img = cv2.imread(os.path.join(path,filename), cv2.IMREAD_GRAYSCALE) image_names.append(filename) if img is not None: data.append(img) data = np.asarray(data) if activation == 'sigmoid': data = data.astype(np.float32)/(255.0) elif activation == 'tanh': data = data.astype(np.float32)/(255.0/2) - 1.0 data = data.reshape((data.shape[0], 1, data.shape[1], data.shape[2])) np.random.seed(1234) p = np.random.permutation(data.shape[0]) X = data[p] return X, image_names
[ "Get", "the", "dataset" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L213-L237
[ "def", "get_data", "(", "path", ",", "activation", ")", ":", "data", "=", "[", "]", "image_names", "=", "[", "]", "for", "filename", "in", "os", ".", "listdir", "(", "path", ")", ":", "img", "=", "cv2", ".", "imread", "(", "os", ".", "path", ".",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
fill_buf
fill the ith grid of the buffer matrix with the values from the img buf : buffer matrix i : serial of the image in the 2D grid img : image data shape : ( height width depth ) of image
example/vae-gan/vaegan_mxnet.py
def fill_buf(buf, i, img, shape): '''fill the ith grid of the buffer matrix with the values from the img buf : buffer matrix i : serial of the image in the 2D grid img : image data shape : ( height width depth ) of image''' # grid height is a multiple of individual image height m = buf.shape[0]/shape[0] sx = (i%m)*shape[1] sy = (i//m)*shape[0] sx = int(sx) sy = int(sy) buf[sy:sy+shape[0], sx:sx+shape[1], :] = img
def fill_buf(buf, i, img, shape): '''fill the ith grid of the buffer matrix with the values from the img buf : buffer matrix i : serial of the image in the 2D grid img : image data shape : ( height width depth ) of image''' # grid height is a multiple of individual image height m = buf.shape[0]/shape[0] sx = (i%m)*shape[1] sy = (i//m)*shape[0] sx = int(sx) sy = int(sy) buf[sy:sy+shape[0], sx:sx+shape[1], :] = img
[ "fill", "the", "ith", "grid", "of", "the", "buffer", "matrix", "with", "the", "values", "from", "the", "img", "buf", ":", "buffer", "matrix", "i", ":", "serial", "of", "the", "image", "in", "the", "2D", "grid", "img", ":", "image", "data", "shape", "...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L254-L268
[ "def", "fill_buf", "(", "buf", ",", "i", ",", "img", ",", "shape", ")", ":", "# grid height is a multiple of individual image height", "m", "=", "buf", ".", "shape", "[", "0", "]", "/", "shape", "[", "0", "]", "sx", "=", "(", "i", "%", "m", ")", "*",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
visual
create a grid of images and save it as a final image title : grid image name X : array of images
example/vae-gan/vaegan_mxnet.py
def visual(title, X, activation): '''create a grid of images and save it as a final image title : grid image name X : array of images ''' assert len(X.shape) == 4 X = X.transpose((0, 2, 3, 1)) if activation == 'sigmoid': X = np.clip((X)*(255.0), 0, 255).astype(np.uint8) elif activation == 'tanh': X = np.clip((X+1.0)*(255.0/2.0), 0, 255).astype(np.uint8) n = np.ceil(np.sqrt(X.shape[0])) buff = np.zeros((int(n*X.shape[1]), int(n*X.shape[2]), int(X.shape[3])), dtype=np.uint8) for i, img in enumerate(X): fill_buf(buff, i, img, X.shape[1:3]) cv2.imwrite('%s.jpg' % (title), buff)
def visual(title, X, activation): '''create a grid of images and save it as a final image title : grid image name X : array of images ''' assert len(X.shape) == 4 X = X.transpose((0, 2, 3, 1)) if activation == 'sigmoid': X = np.clip((X)*(255.0), 0, 255).astype(np.uint8) elif activation == 'tanh': X = np.clip((X+1.0)*(255.0/2.0), 0, 255).astype(np.uint8) n = np.ceil(np.sqrt(X.shape[0])) buff = np.zeros((int(n*X.shape[1]), int(n*X.shape[2]), int(X.shape[3])), dtype=np.uint8) for i, img in enumerate(X): fill_buf(buff, i, img, X.shape[1:3]) cv2.imwrite('%s.jpg' % (title), buff)
[ "create", "a", "grid", "of", "images", "and", "save", "it", "as", "a", "final", "image", "title", ":", "grid", "image", "name", "X", ":", "array", "of", "images" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L270-L286
[ "def", "visual", "(", "title", ",", "X", ",", "activation", ")", ":", "assert", "len", "(", "X", ".", "shape", ")", "==", "4", "X", "=", "X", ".", "transpose", "(", "(", "0", ",", "2", ",", "3", ",", "1", ")", ")", "if", "activation", "==", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
train
adversarial training of the VAE
example/vae-gan/vaegan_mxnet.py
def train(dataset, nef, ndf, ngf, nc, batch_size, Z, lr, beta1, epsilon, ctx, check_point, g_dl_weight, output_path, checkpoint_path, data_path, activation,num_epoch, save_after_every, visualize_after_every, show_after_every): '''adversarial training of the VAE ''' #encoder z_mu, z_lv, z = encoder(nef, Z, batch_size) symE = mx.sym.Group([z_mu, z_lv, z]) #generator symG = generator(ngf, nc, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12, z_dim = Z, activation=activation ) #discriminator h = discriminator1(ndf) dloss = discriminator2(ndf) symD1 = h symD2 = dloss # ==============data============== X_train, _ = get_data(data_path, activation) train_iter = mx.io.NDArrayIter(X_train, batch_size=batch_size, shuffle=True) rand_iter = RandIter(batch_size, Z) label = mx.nd.zeros((batch_size,), ctx=ctx) # =============module E============= modE = mx.mod.Module(symbol=symE, data_names=('data',), label_names=None, context=ctx) modE.bind(data_shapes=train_iter.provide_data) modE.init_params(initializer=mx.init.Normal(0.02)) modE.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 1e-6, 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) mods = [modE] # =============module G============= modG = mx.mod.Module(symbol=symG, data_names=('rand',), label_names=None, context=ctx) modG.bind(data_shapes=rand_iter.provide_data, inputs_need_grad=True) modG.init_params(initializer=mx.init.Normal(0.02)) modG.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 1e-6, 'beta1': beta1, 'epsilon': epsilon, }) mods.append(modG) # =============module D============= modD1 = mx.mod.Module(symD1, label_names=[], context=ctx) modD2 = mx.mod.Module(symD2, label_names=('label',), context=ctx) modD = mx.mod.SequentialModule() modD.add(modD1).add(modD2, take_labels=True, auto_wiring=True) modD.bind(data_shapes=train_iter.provide_data, label_shapes=[('label', (batch_size,))], inputs_need_grad=True) modD.init_params(initializer=mx.init.Normal(0.02)) modD.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 1e-3, 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) mods.append(modD) # =============module DL============= symDL = DiscriminatorLayerLoss() modDL = mx.mod.Module(symbol=symDL, data_names=('data',), label_names=('label',), context=ctx) modDL.bind(data_shapes=[('data', (batch_size,nef * 4,4,4))], ################################################################################################################################ fix 512 here label_shapes=[('label', (batch_size,nef * 4,4,4))], inputs_need_grad=True) modDL.init_params(initializer=mx.init.Normal(0.02)) modDL.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 0., 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) # =============module KL============= symKL = KLDivergenceLoss() modKL = mx.mod.Module(symbol=symKL, data_names=('data',), label_names=None, context=ctx) modKL.bind(data_shapes=[('data', (batch_size*2,Z))], inputs_need_grad=True) modKL.init_params(initializer=mx.init.Normal(0.02)) modKL.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 0., 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) mods.append(modKL) def norm_stat(d): return mx.nd.norm(d)/np.sqrt(d.size) mon = mx.mon.Monitor(10, norm_stat, pattern=".*output|d1_backward_data", sort=True) mon = None if mon is not None: for mod in mods: pass def facc(label, pred): '''calculating prediction accuracy ''' pred = pred.ravel() label = label.ravel() return ((pred > 0.5) == label).mean() def fentropy(label, pred): '''calculating binary cross-entropy loss ''' pred = pred.ravel() label = label.ravel() return -(label*np.log(pred+1e-12) + (1.-label)*np.log(1.-pred+1e-12)).mean() def kldivergence(label, pred): '''calculating KL divergence loss ''' mean, log_var = np.split(pred, 2, axis=0) var = np.exp(log_var) KLLoss = -0.5 * np.sum(1 + log_var - np.power(mean, 2) - var) KLLoss = KLLoss / nElements return KLLoss mG = mx.metric.CustomMetric(fentropy) mD = mx.metric.CustomMetric(fentropy) mE = mx.metric.CustomMetric(kldivergence) mACC = mx.metric.CustomMetric(facc) print('Training...') stamp = datetime.now().strftime('%Y_%m_%d-%H_%M') # =============train=============== for epoch in range(num_epoch): train_iter.reset() for t, batch in enumerate(train_iter): rbatch = rand_iter.next() if mon is not None: mon.tic() modG.forward(rbatch, is_train=True) outG = modG.get_outputs() # update discriminator on fake label[:] = 0 modD.forward(mx.io.DataBatch(outG, [label]), is_train=True) modD.backward() gradD11 = [[grad.copyto(grad.context) for grad in grads] for grads in modD1._exec_group.grad_arrays] gradD12 = [[grad.copyto(grad.context) for grad in grads] for grads in modD2._exec_group.grad_arrays] modD.update_metric(mD, [label]) modD.update_metric(mACC, [label]) #update discriminator on decoded modE.forward(batch, is_train=True) mu, lv, z = modE.get_outputs() z = z.reshape((batch_size, Z, 1, 1)) sample = mx.io.DataBatch([z], label=None, provide_data = [('rand', (batch_size, Z, 1, 1))]) modG.forward(sample, is_train=True) xz = modG.get_outputs() label[:] = 0 modD.forward(mx.io.DataBatch(xz, [label]), is_train=True) modD.backward() #modD.update() gradD21 = [[grad.copyto(grad.context) for grad in grads] for grads in modD1._exec_group.grad_arrays] gradD22 = [[grad.copyto(grad.context) for grad in grads] for grads in modD2._exec_group.grad_arrays] modD.update_metric(mD, [label]) modD.update_metric(mACC, [label]) # update discriminator on real label[:] = 1 batch.label = [label] modD.forward(batch, is_train=True) lx = [out.copyto(out.context) for out in modD1.get_outputs()] modD.backward() for gradsr, gradsf, gradsd in zip(modD1._exec_group.grad_arrays, gradD11, gradD21): for gradr, gradf, gradd in zip(gradsr, gradsf, gradsd): gradr += 0.5 * (gradf + gradd) for gradsr, gradsf, gradsd in zip(modD2._exec_group.grad_arrays, gradD12, gradD22): for gradr, gradf, gradd in zip(gradsr, gradsf, gradsd): gradr += 0.5 * (gradf + gradd) modD.update() modD.update_metric(mD, [label]) modD.update_metric(mACC, [label]) modG.forward(rbatch, is_train=True) outG = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(outG, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG1 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(xz, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG2 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() modD1.forward(mx.io.DataBatch(xz, []), is_train=True) outD1 = modD1.get_outputs() modDL.forward(mx.io.DataBatch(outD1, lx), is_train=True) modDL.backward() dlGrad = modDL.get_input_grads() modD1.backward(dlGrad) diffD = modD1.get_input_grads() modG.backward(diffD) for grads, gradsG1, gradsG2 in zip(modG._exec_group.grad_arrays, gradG1, gradG2): for grad, gradg1, gradg2 in zip(grads, gradsG1, gradsG2): grad = g_dl_weight * grad + 0.5 * (gradg1 + gradg2) modG.update() mG.update([label], modD.get_outputs()) modG.forward(rbatch, is_train=True) outG = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(outG, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG1 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(xz, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG2 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() modD1.forward(mx.io.DataBatch(xz, []), is_train=True) outD1 = modD1.get_outputs() modDL.forward(mx.io.DataBatch(outD1, lx), is_train=True) modDL.backward() dlGrad = modDL.get_input_grads() modD1.backward(dlGrad) diffD = modD1.get_input_grads() modG.backward(diffD) for grads, gradsG1, gradsG2 in zip(modG._exec_group.grad_arrays, gradG1, gradG2): for grad, gradg1, gradg2 in zip(grads, gradsG1, gradsG2): grad = g_dl_weight * grad + 0.5 * (gradg1 + gradg2) modG.update() mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() #update generator modD1.forward(mx.io.DataBatch(xz, []), is_train=True) outD1 = modD1.get_outputs() modDL.forward(mx.io.DataBatch(outD1, lx), is_train=True) DLloss = modDL.get_outputs() modDL.backward() dlGrad = modDL.get_input_grads() modD1.backward(dlGrad) diffD = modD1.get_input_grads() modG.backward(diffD) #update encoder nElements = batch_size modKL.forward(mx.io.DataBatch([mx.ndarray.concat(mu,lv, dim=0)]), is_train=True) KLloss = modKL.get_outputs() modKL.backward() gradKLLoss = modKL.get_input_grads() diffG = modG.get_input_grads() diffG = diffG[0].reshape((batch_size, Z)) modE.backward(mx.ndarray.split(gradKLLoss[0], num_outputs=2, axis=0) + [diffG]) modE.update() pred = mx.ndarray.concat(mu,lv, dim=0) mE.update([pred], [pred]) if mon is not None: mon.toc_print() t += 1 if t % show_after_every == 0: print('epoch:', epoch, 'iter:', t, 'metric:', mACC.get(), mG.get(), mD.get(), mE.get(), KLloss[0].asnumpy(), DLloss[0].asnumpy()) mACC.reset() mG.reset() mD.reset() mE.reset() if epoch % visualize_after_every == 0: visual(output_path +'gout'+str(epoch), outG[0].asnumpy(), activation) visual(output_path + 'data'+str(epoch), batch.data[0].asnumpy(), activation) if check_point and epoch % save_after_every == 0: print('Saving...') modG.save_params(checkpoint_path + '/%s_G-%04d.params'%(dataset, epoch)) modD.save_params(checkpoint_path + '/%s_D-%04d.params'%(dataset, epoch)) modE.save_params(checkpoint_path + '/%s_E-%04d.params'%(dataset, epoch))
def train(dataset, nef, ndf, ngf, nc, batch_size, Z, lr, beta1, epsilon, ctx, check_point, g_dl_weight, output_path, checkpoint_path, data_path, activation,num_epoch, save_after_every, visualize_after_every, show_after_every): '''adversarial training of the VAE ''' #encoder z_mu, z_lv, z = encoder(nef, Z, batch_size) symE = mx.sym.Group([z_mu, z_lv, z]) #generator symG = generator(ngf, nc, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12, z_dim = Z, activation=activation ) #discriminator h = discriminator1(ndf) dloss = discriminator2(ndf) symD1 = h symD2 = dloss # ==============data============== X_train, _ = get_data(data_path, activation) train_iter = mx.io.NDArrayIter(X_train, batch_size=batch_size, shuffle=True) rand_iter = RandIter(batch_size, Z) label = mx.nd.zeros((batch_size,), ctx=ctx) # =============module E============= modE = mx.mod.Module(symbol=symE, data_names=('data',), label_names=None, context=ctx) modE.bind(data_shapes=train_iter.provide_data) modE.init_params(initializer=mx.init.Normal(0.02)) modE.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 1e-6, 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) mods = [modE] # =============module G============= modG = mx.mod.Module(symbol=symG, data_names=('rand',), label_names=None, context=ctx) modG.bind(data_shapes=rand_iter.provide_data, inputs_need_grad=True) modG.init_params(initializer=mx.init.Normal(0.02)) modG.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 1e-6, 'beta1': beta1, 'epsilon': epsilon, }) mods.append(modG) # =============module D============= modD1 = mx.mod.Module(symD1, label_names=[], context=ctx) modD2 = mx.mod.Module(symD2, label_names=('label',), context=ctx) modD = mx.mod.SequentialModule() modD.add(modD1).add(modD2, take_labels=True, auto_wiring=True) modD.bind(data_shapes=train_iter.provide_data, label_shapes=[('label', (batch_size,))], inputs_need_grad=True) modD.init_params(initializer=mx.init.Normal(0.02)) modD.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 1e-3, 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) mods.append(modD) # =============module DL============= symDL = DiscriminatorLayerLoss() modDL = mx.mod.Module(symbol=symDL, data_names=('data',), label_names=('label',), context=ctx) modDL.bind(data_shapes=[('data', (batch_size,nef * 4,4,4))], ################################################################################################################################ fix 512 here label_shapes=[('label', (batch_size,nef * 4,4,4))], inputs_need_grad=True) modDL.init_params(initializer=mx.init.Normal(0.02)) modDL.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 0., 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) # =============module KL============= symKL = KLDivergenceLoss() modKL = mx.mod.Module(symbol=symKL, data_names=('data',), label_names=None, context=ctx) modKL.bind(data_shapes=[('data', (batch_size*2,Z))], inputs_need_grad=True) modKL.init_params(initializer=mx.init.Normal(0.02)) modKL.init_optimizer( optimizer='adam', optimizer_params={ 'learning_rate': lr, 'wd': 0., 'beta1': beta1, 'epsilon': epsilon, 'rescale_grad': (1.0/batch_size) }) mods.append(modKL) def norm_stat(d): return mx.nd.norm(d)/np.sqrt(d.size) mon = mx.mon.Monitor(10, norm_stat, pattern=".*output|d1_backward_data", sort=True) mon = None if mon is not None: for mod in mods: pass def facc(label, pred): '''calculating prediction accuracy ''' pred = pred.ravel() label = label.ravel() return ((pred > 0.5) == label).mean() def fentropy(label, pred): '''calculating binary cross-entropy loss ''' pred = pred.ravel() label = label.ravel() return -(label*np.log(pred+1e-12) + (1.-label)*np.log(1.-pred+1e-12)).mean() def kldivergence(label, pred): '''calculating KL divergence loss ''' mean, log_var = np.split(pred, 2, axis=0) var = np.exp(log_var) KLLoss = -0.5 * np.sum(1 + log_var - np.power(mean, 2) - var) KLLoss = KLLoss / nElements return KLLoss mG = mx.metric.CustomMetric(fentropy) mD = mx.metric.CustomMetric(fentropy) mE = mx.metric.CustomMetric(kldivergence) mACC = mx.metric.CustomMetric(facc) print('Training...') stamp = datetime.now().strftime('%Y_%m_%d-%H_%M') # =============train=============== for epoch in range(num_epoch): train_iter.reset() for t, batch in enumerate(train_iter): rbatch = rand_iter.next() if mon is not None: mon.tic() modG.forward(rbatch, is_train=True) outG = modG.get_outputs() # update discriminator on fake label[:] = 0 modD.forward(mx.io.DataBatch(outG, [label]), is_train=True) modD.backward() gradD11 = [[grad.copyto(grad.context) for grad in grads] for grads in modD1._exec_group.grad_arrays] gradD12 = [[grad.copyto(grad.context) for grad in grads] for grads in modD2._exec_group.grad_arrays] modD.update_metric(mD, [label]) modD.update_metric(mACC, [label]) #update discriminator on decoded modE.forward(batch, is_train=True) mu, lv, z = modE.get_outputs() z = z.reshape((batch_size, Z, 1, 1)) sample = mx.io.DataBatch([z], label=None, provide_data = [('rand', (batch_size, Z, 1, 1))]) modG.forward(sample, is_train=True) xz = modG.get_outputs() label[:] = 0 modD.forward(mx.io.DataBatch(xz, [label]), is_train=True) modD.backward() #modD.update() gradD21 = [[grad.copyto(grad.context) for grad in grads] for grads in modD1._exec_group.grad_arrays] gradD22 = [[grad.copyto(grad.context) for grad in grads] for grads in modD2._exec_group.grad_arrays] modD.update_metric(mD, [label]) modD.update_metric(mACC, [label]) # update discriminator on real label[:] = 1 batch.label = [label] modD.forward(batch, is_train=True) lx = [out.copyto(out.context) for out in modD1.get_outputs()] modD.backward() for gradsr, gradsf, gradsd in zip(modD1._exec_group.grad_arrays, gradD11, gradD21): for gradr, gradf, gradd in zip(gradsr, gradsf, gradsd): gradr += 0.5 * (gradf + gradd) for gradsr, gradsf, gradsd in zip(modD2._exec_group.grad_arrays, gradD12, gradD22): for gradr, gradf, gradd in zip(gradsr, gradsf, gradsd): gradr += 0.5 * (gradf + gradd) modD.update() modD.update_metric(mD, [label]) modD.update_metric(mACC, [label]) modG.forward(rbatch, is_train=True) outG = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(outG, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG1 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(xz, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG2 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() modD1.forward(mx.io.DataBatch(xz, []), is_train=True) outD1 = modD1.get_outputs() modDL.forward(mx.io.DataBatch(outD1, lx), is_train=True) modDL.backward() dlGrad = modDL.get_input_grads() modD1.backward(dlGrad) diffD = modD1.get_input_grads() modG.backward(diffD) for grads, gradsG1, gradsG2 in zip(modG._exec_group.grad_arrays, gradG1, gradG2): for grad, gradg1, gradg2 in zip(grads, gradsG1, gradsG2): grad = g_dl_weight * grad + 0.5 * (gradg1 + gradg2) modG.update() mG.update([label], modD.get_outputs()) modG.forward(rbatch, is_train=True) outG = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(outG, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG1 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() label[:] = 1 modD.forward(mx.io.DataBatch(xz, [label]), is_train=True) modD.backward() diffD = modD1.get_input_grads() modG.backward(diffD) gradG2 = [[grad.copyto(grad.context) for grad in grads] for grads in modG._exec_group.grad_arrays] mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() modD1.forward(mx.io.DataBatch(xz, []), is_train=True) outD1 = modD1.get_outputs() modDL.forward(mx.io.DataBatch(outD1, lx), is_train=True) modDL.backward() dlGrad = modDL.get_input_grads() modD1.backward(dlGrad) diffD = modD1.get_input_grads() modG.backward(diffD) for grads, gradsG1, gradsG2 in zip(modG._exec_group.grad_arrays, gradG1, gradG2): for grad, gradg1, gradg2 in zip(grads, gradsG1, gradsG2): grad = g_dl_weight * grad + 0.5 * (gradg1 + gradg2) modG.update() mG.update([label], modD.get_outputs()) modG.forward(sample, is_train=True) xz = modG.get_outputs() #update generator modD1.forward(mx.io.DataBatch(xz, []), is_train=True) outD1 = modD1.get_outputs() modDL.forward(mx.io.DataBatch(outD1, lx), is_train=True) DLloss = modDL.get_outputs() modDL.backward() dlGrad = modDL.get_input_grads() modD1.backward(dlGrad) diffD = modD1.get_input_grads() modG.backward(diffD) #update encoder nElements = batch_size modKL.forward(mx.io.DataBatch([mx.ndarray.concat(mu,lv, dim=0)]), is_train=True) KLloss = modKL.get_outputs() modKL.backward() gradKLLoss = modKL.get_input_grads() diffG = modG.get_input_grads() diffG = diffG[0].reshape((batch_size, Z)) modE.backward(mx.ndarray.split(gradKLLoss[0], num_outputs=2, axis=0) + [diffG]) modE.update() pred = mx.ndarray.concat(mu,lv, dim=0) mE.update([pred], [pred]) if mon is not None: mon.toc_print() t += 1 if t % show_after_every == 0: print('epoch:', epoch, 'iter:', t, 'metric:', mACC.get(), mG.get(), mD.get(), mE.get(), KLloss[0].asnumpy(), DLloss[0].asnumpy()) mACC.reset() mG.reset() mD.reset() mE.reset() if epoch % visualize_after_every == 0: visual(output_path +'gout'+str(epoch), outG[0].asnumpy(), activation) visual(output_path + 'data'+str(epoch), batch.data[0].asnumpy(), activation) if check_point and epoch % save_after_every == 0: print('Saving...') modG.save_params(checkpoint_path + '/%s_G-%04d.params'%(dataset, epoch)) modD.save_params(checkpoint_path + '/%s_D-%04d.params'%(dataset, epoch)) modE.save_params(checkpoint_path + '/%s_E-%04d.params'%(dataset, epoch))
[ "adversarial", "training", "of", "the", "VAE" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L288-L613
[ "def", "train", "(", "dataset", ",", "nef", ",", "ndf", ",", "ngf", ",", "nc", ",", "batch_size", ",", "Z", ",", "lr", ",", "beta1", ",", "epsilon", ",", "ctx", ",", "check_point", ",", "g_dl_weight", ",", "output_path", ",", "checkpoint_path", ",", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
create_and_validate_dir
Creates/Validates dir
example/vae-gan/vaegan_mxnet.py
def create_and_validate_dir(data_dir): '''Creates/Validates dir ''' if data_dir != "": if not os.path.exists(data_dir): try: logging.info('create directory %s', data_dir) os.makedirs(data_dir) except OSError as exc: if exc.errno != errno.EEXIST: raise OSError('failed to create ' + data_dir)
def create_and_validate_dir(data_dir): '''Creates/Validates dir ''' if data_dir != "": if not os.path.exists(data_dir): try: logging.info('create directory %s', data_dir) os.makedirs(data_dir) except OSError as exc: if exc.errno != errno.EEXIST: raise OSError('failed to create ' + data_dir)
[ "Creates", "/", "Validates", "dir" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L660-L670
[ "def", "create_and_validate_dir", "(", "data_dir", ")", ":", "if", "data_dir", "!=", "\"\"", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "data_dir", ")", ":", "try", ":", "logging", ".", "info", "(", "'create directory %s'", ",", "data_dir", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
parse_args
Parse args
example/vae-gan/vaegan_mxnet.py
def parse_args(): '''Parse args ''' parser = argparse.ArgumentParser(description='Train and Test an Adversarial Variatiional Encoder') parser.add_argument('--train', help='train the network', action='store_true') parser.add_argument('--test', help='test the network', action='store_true') parser.add_argument('--save_embedding', help='saves the shape embedding of each input image', action='store_true') parser.add_argument('--dataset', help='dataset name', default='caltech', type=str) parser.add_argument('--activation', help='activation i.e. sigmoid or tanh', default='sigmoid', type=str) parser.add_argument('--training_data_path', help='training data path', default='datasets/caltech101/data/images32x32', type=str) parser.add_argument('--testing_data_path', help='testing data path', default='datasets/caltech101/test_data', type=str) parser.add_argument('--pretrained_encoder_path', help='pretrained encoder model path', default='checkpoints32x32_sigmoid/caltech_E-0045.params', type=str) parser.add_argument('--pretrained_generator_path', help='pretrained generator model path', default='checkpoints32x32_sigmoid/caltech_G-0045.params', type=str) parser.add_argument('--output_path', help='output path for the generated images', default='outputs32x32_sigmoid', type=str) parser.add_argument('--embedding_path', help='output path for the generated embeddings', default='outputs32x32_sigmoid', type=str) parser.add_argument('--checkpoint_path', help='checkpoint saving path ', default='checkpoints32x32_sigmoid', type=str) parser.add_argument('--nef', help='encoder filter count in the first layer', default=64, type=int) parser.add_argument('--ndf', help='discriminator filter count in the first layer', default=64, type=int) parser.add_argument('--ngf', help='generator filter count in the second last layer', default=64, type=int) parser.add_argument('--nc', help='generator filter count in the last layer i.e. 1 for grayscale image, 3 for RGB image', default=1, type=int) parser.add_argument('--batch_size', help='batch size, keep it 1 during testing', default=64, type=int) parser.add_argument('--Z', help='embedding size', default=100, type=int) parser.add_argument('--lr', help='learning rate', default=0.0002, type=float) parser.add_argument('--beta1', help='beta1 for adam optimizer', default=0.5, type=float) parser.add_argument('--epsilon', help='epsilon for adam optimizer', default=1e-5, type=float) parser.add_argument('--g_dl_weight', help='discriminator layer loss weight', default=1e-1, type=float) parser.add_argument('--gpu', help='gpu index', default=0, type=int) parser.add_argument('--use_cpu', help='use cpu', action='store_true') parser.add_argument('--num_epoch', help='number of maximum epochs ', default=45, type=int) parser.add_argument('--save_after_every', help='save checkpoint after every this number of epochs ', default=5, type=int) parser.add_argument('--visualize_after_every', help='save output images after every this number of epochs', default=5, type=int) parser.add_argument('--show_after_every', help='show metrics after this number of iterations', default=10, type=int) args = parser.parse_args() return args
def parse_args(): '''Parse args ''' parser = argparse.ArgumentParser(description='Train and Test an Adversarial Variatiional Encoder') parser.add_argument('--train', help='train the network', action='store_true') parser.add_argument('--test', help='test the network', action='store_true') parser.add_argument('--save_embedding', help='saves the shape embedding of each input image', action='store_true') parser.add_argument('--dataset', help='dataset name', default='caltech', type=str) parser.add_argument('--activation', help='activation i.e. sigmoid or tanh', default='sigmoid', type=str) parser.add_argument('--training_data_path', help='training data path', default='datasets/caltech101/data/images32x32', type=str) parser.add_argument('--testing_data_path', help='testing data path', default='datasets/caltech101/test_data', type=str) parser.add_argument('--pretrained_encoder_path', help='pretrained encoder model path', default='checkpoints32x32_sigmoid/caltech_E-0045.params', type=str) parser.add_argument('--pretrained_generator_path', help='pretrained generator model path', default='checkpoints32x32_sigmoid/caltech_G-0045.params', type=str) parser.add_argument('--output_path', help='output path for the generated images', default='outputs32x32_sigmoid', type=str) parser.add_argument('--embedding_path', help='output path for the generated embeddings', default='outputs32x32_sigmoid', type=str) parser.add_argument('--checkpoint_path', help='checkpoint saving path ', default='checkpoints32x32_sigmoid', type=str) parser.add_argument('--nef', help='encoder filter count in the first layer', default=64, type=int) parser.add_argument('--ndf', help='discriminator filter count in the first layer', default=64, type=int) parser.add_argument('--ngf', help='generator filter count in the second last layer', default=64, type=int) parser.add_argument('--nc', help='generator filter count in the last layer i.e. 1 for grayscale image, 3 for RGB image', default=1, type=int) parser.add_argument('--batch_size', help='batch size, keep it 1 during testing', default=64, type=int) parser.add_argument('--Z', help='embedding size', default=100, type=int) parser.add_argument('--lr', help='learning rate', default=0.0002, type=float) parser.add_argument('--beta1', help='beta1 for adam optimizer', default=0.5, type=float) parser.add_argument('--epsilon', help='epsilon for adam optimizer', default=1e-5, type=float) parser.add_argument('--g_dl_weight', help='discriminator layer loss weight', default=1e-1, type=float) parser.add_argument('--gpu', help='gpu index', default=0, type=int) parser.add_argument('--use_cpu', help='use cpu', action='store_true') parser.add_argument('--num_epoch', help='number of maximum epochs ', default=45, type=int) parser.add_argument('--save_after_every', help='save checkpoint after every this number of epochs ', default=5, type=int) parser.add_argument('--visualize_after_every', help='save output images after every this number of epochs', default=5, type=int) parser.add_argument('--show_after_every', help='show metrics after this number of iterations', default=10, type=int) args = parser.parse_args() return args
[ "Parse", "args" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/vaegan_mxnet.py#L673-L708
[ "def", "parse_args", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "'Train and Test an Adversarial Variatiional Encoder'", ")", "parser", ".", "add_argument", "(", "'--train'", ",", "help", "=", "'train the network'", ",", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_rmse_log
Gets root mse between the logarithms of the prediction and the truth.
example/gluon/house_prices/kaggle_k_fold_cross_validation.py
def get_rmse_log(net, X_train, y_train): """Gets root mse between the logarithms of the prediction and the truth.""" num_train = X_train.shape[0] clipped_preds = nd.clip(net(X_train), 1, float('inf')) return np.sqrt(2 * nd.sum(square_loss( nd.log(clipped_preds), nd.log(y_train))).asscalar() / num_train)
def get_rmse_log(net, X_train, y_train): """Gets root mse between the logarithms of the prediction and the truth.""" num_train = X_train.shape[0] clipped_preds = nd.clip(net(X_train), 1, float('inf')) return np.sqrt(2 * nd.sum(square_loss( nd.log(clipped_preds), nd.log(y_train))).asscalar() / num_train)
[ "Gets", "root", "mse", "between", "the", "logarithms", "of", "the", "prediction", "and", "the", "truth", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/house_prices/kaggle_k_fold_cross_validation.py#L66-L71
[ "def", "get_rmse_log", "(", "net", ",", "X_train", ",", "y_train", ")", ":", "num_train", "=", "X_train", ".", "shape", "[", "0", "]", "clipped_preds", "=", "nd", ".", "clip", "(", "net", "(", "X_train", ")", ",", "1", ",", "float", "(", "'inf'", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_net
Gets a neural network. Better results are obtained with modifications.
example/gluon/house_prices/kaggle_k_fold_cross_validation.py
def get_net(): """Gets a neural network. Better results are obtained with modifications.""" net = gluon.nn.Sequential() with net.name_scope(): net.add(gluon.nn.Dense(50, activation="relu")) net.add(gluon.nn.Dense(1)) net.initialize() return net
def get_net(): """Gets a neural network. Better results are obtained with modifications.""" net = gluon.nn.Sequential() with net.name_scope(): net.add(gluon.nn.Dense(50, activation="relu")) net.add(gluon.nn.Dense(1)) net.initialize() return net
[ "Gets", "a", "neural", "network", ".", "Better", "results", "are", "obtained", "with", "modifications", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/house_prices/kaggle_k_fold_cross_validation.py#L73-L80
[ "def", "get_net", "(", ")", ":", "net", "=", "gluon", ".", "nn", ".", "Sequential", "(", ")", "with", "net", ".", "name_scope", "(", ")", ":", "net", ".", "add", "(", "gluon", ".", "nn", ".", "Dense", "(", "50", ",", "activation", "=", "\"relu\""...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
train
Trains the model.
example/gluon/house_prices/kaggle_k_fold_cross_validation.py
def train(net, X_train, y_train, epochs, verbose_epoch, learning_rate, weight_decay, batch_size): """Trains the model.""" dataset_train = gluon.data.ArrayDataset(X_train, y_train) data_iter_train = gluon.data.DataLoader(dataset_train, batch_size, shuffle=True) trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': learning_rate, 'wd': weight_decay}) net.initialize(force_reinit=True) for epoch in range(epochs): for data, label in data_iter_train: with autograd.record(): output = net(data) loss = square_loss(output, label) loss.backward() trainer.step(batch_size) avg_loss = get_rmse_log(net, X_train, y_train) if epoch > verbose_epoch: print("Epoch %d, train loss: %f" % (epoch, avg_loss)) return avg_loss
def train(net, X_train, y_train, epochs, verbose_epoch, learning_rate, weight_decay, batch_size): """Trains the model.""" dataset_train = gluon.data.ArrayDataset(X_train, y_train) data_iter_train = gluon.data.DataLoader(dataset_train, batch_size, shuffle=True) trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': learning_rate, 'wd': weight_decay}) net.initialize(force_reinit=True) for epoch in range(epochs): for data, label in data_iter_train: with autograd.record(): output = net(data) loss = square_loss(output, label) loss.backward() trainer.step(batch_size) avg_loss = get_rmse_log(net, X_train, y_train) if epoch > verbose_epoch: print("Epoch %d, train loss: %f" % (epoch, avg_loss)) return avg_loss
[ "Trains", "the", "model", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/house_prices/kaggle_k_fold_cross_validation.py#L82-L102
[ "def", "train", "(", "net", ",", "X_train", ",", "y_train", ",", "epochs", ",", "verbose_epoch", ",", "learning_rate", ",", "weight_decay", ",", "batch_size", ")", ":", "dataset_train", "=", "gluon", ".", "data", ".", "ArrayDataset", "(", "X_train", ",", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
k_fold_cross_valid
Conducts k-fold cross validation for the model.
example/gluon/house_prices/kaggle_k_fold_cross_validation.py
def k_fold_cross_valid(k, epochs, verbose_epoch, X_train, y_train, learning_rate, weight_decay, batch_size): """Conducts k-fold cross validation for the model.""" assert k > 1 fold_size = X_train.shape[0] // k train_loss_sum = 0.0 test_loss_sum = 0.0 for test_idx in range(k): X_val_test = X_train[test_idx * fold_size: (test_idx + 1) * fold_size, :] y_val_test = y_train[test_idx * fold_size: (test_idx + 1) * fold_size] val_train_defined = False for i in range(k): if i != test_idx: X_cur_fold = X_train[i * fold_size: (i + 1) * fold_size, :] y_cur_fold = y_train[i * fold_size: (i + 1) * fold_size] if not val_train_defined: X_val_train = X_cur_fold y_val_train = y_cur_fold val_train_defined = True else: X_val_train = nd.concat(X_val_train, X_cur_fold, dim=0) y_val_train = nd.concat(y_val_train, y_cur_fold, dim=0) net = get_net() train_loss = train(net, X_val_train, y_val_train, epochs, verbose_epoch, learning_rate, weight_decay, batch_size) train_loss_sum += train_loss test_loss = get_rmse_log(net, X_val_test, y_val_test) print("Test loss: %f" % test_loss) test_loss_sum += test_loss return train_loss_sum / k, test_loss_sum / k
def k_fold_cross_valid(k, epochs, verbose_epoch, X_train, y_train, learning_rate, weight_decay, batch_size): """Conducts k-fold cross validation for the model.""" assert k > 1 fold_size = X_train.shape[0] // k train_loss_sum = 0.0 test_loss_sum = 0.0 for test_idx in range(k): X_val_test = X_train[test_idx * fold_size: (test_idx + 1) * fold_size, :] y_val_test = y_train[test_idx * fold_size: (test_idx + 1) * fold_size] val_train_defined = False for i in range(k): if i != test_idx: X_cur_fold = X_train[i * fold_size: (i + 1) * fold_size, :] y_cur_fold = y_train[i * fold_size: (i + 1) * fold_size] if not val_train_defined: X_val_train = X_cur_fold y_val_train = y_cur_fold val_train_defined = True else: X_val_train = nd.concat(X_val_train, X_cur_fold, dim=0) y_val_train = nd.concat(y_val_train, y_cur_fold, dim=0) net = get_net() train_loss = train(net, X_val_train, y_val_train, epochs, verbose_epoch, learning_rate, weight_decay, batch_size) train_loss_sum += train_loss test_loss = get_rmse_log(net, X_val_test, y_val_test) print("Test loss: %f" % test_loss) test_loss_sum += test_loss return train_loss_sum / k, test_loss_sum / k
[ "Conducts", "k", "-", "fold", "cross", "validation", "for", "the", "model", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/house_prices/kaggle_k_fold_cross_validation.py#L104-L135
[ "def", "k_fold_cross_valid", "(", "k", ",", "epochs", ",", "verbose_epoch", ",", "X_train", ",", "y_train", ",", "learning_rate", ",", "weight_decay", ",", "batch_size", ")", ":", "assert", "k", ">", "1", "fold_size", "=", "X_train", ".", "shape", "[", "0"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
learn
Trains the model and predicts on the test data set.
example/gluon/house_prices/kaggle_k_fold_cross_validation.py
def learn(epochs, verbose_epoch, X_train, y_train, test, learning_rate, weight_decay, batch_size): """Trains the model and predicts on the test data set.""" net = get_net() _ = train(net, X_train, y_train, epochs, verbose_epoch, learning_rate, weight_decay, batch_size) preds = net(X_test).asnumpy() test['SalePrice'] = pd.Series(preds.reshape(1, -1)[0]) submission = pd.concat([test['Id'], test['SalePrice']], axis=1) submission.to_csv('submission.csv', index=False)
def learn(epochs, verbose_epoch, X_train, y_train, test, learning_rate, weight_decay, batch_size): """Trains the model and predicts on the test data set.""" net = get_net() _ = train(net, X_train, y_train, epochs, verbose_epoch, learning_rate, weight_decay, batch_size) preds = net(X_test).asnumpy() test['SalePrice'] = pd.Series(preds.reshape(1, -1)[0]) submission = pd.concat([test['Id'], test['SalePrice']], axis=1) submission.to_csv('submission.csv', index=False)
[ "Trains", "the", "model", "and", "predicts", "on", "the", "test", "data", "set", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/house_prices/kaggle_k_fold_cross_validation.py#L152-L161
[ "def", "learn", "(", "epochs", ",", "verbose_epoch", ",", "X_train", ",", "y_train", ",", "test", ",", "learning_rate", ",", "weight_decay", ",", "batch_size", ")", ":", "net", "=", "get_net", "(", ")", "_", "=", "train", "(", "net", ",", "X_train", ",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
capsnet
Create CapsNet
example/capsnet/capsulenet.py
def capsnet(batch_size, n_class, num_routing, recon_loss_weight): """Create CapsNet""" # data.shape = [batch_size, 1, 28, 28] data = mx.sym.Variable('data') input_shape = (1, 28, 28) # Conv2D layer # net.shape = [batch_size, 256, 20, 20] conv1 = mx.sym.Convolution(data=data, num_filter=256, kernel=(9, 9), layout='NCHW', name='conv1') conv1 = mx.sym.Activation(data=conv1, act_type='relu', name='conv1_act') # net.shape = [batch_size, 256, 6, 6] primarycaps = primary_caps(data=conv1, dim_vector=8, n_channels=32, kernel=(9, 9), strides=[2, 2], name='primarycaps') primarycaps.infer_shape(data=(batch_size, 1, 28, 28)) # CapsuleLayer kernel_initializer = mx.init.Xavier(rnd_type='uniform', factor_type='avg', magnitude=3) bias_initializer = mx.init.Zero() digitcaps = CapsuleLayer(num_capsule=10, dim_vector=16, batch_size=batch_size, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, num_routing=num_routing)(primarycaps) # out_caps : (batch_size, 10) out_caps = mx.sym.sqrt(data=mx.sym.sum(mx.sym.square(digitcaps), 2)) out_caps.infer_shape(data=(batch_size, 1, 28, 28)) y = mx.sym.Variable('softmax_label', shape=(batch_size,)) y_onehot = mx.sym.one_hot(y, n_class) y_reshaped = mx.sym.Reshape(data=y_onehot, shape=(batch_size, -4, n_class, -1)) y_reshaped.infer_shape(softmax_label=(batch_size,)) # inputs_masked : (batch_size, 16) inputs_masked = mx.sym.linalg_gemm2(y_reshaped, digitcaps, transpose_a=True) inputs_masked = mx.sym.Reshape(data=inputs_masked, shape=(-3, 0)) x_recon = mx.sym.FullyConnected(data=inputs_masked, num_hidden=512, name='x_recon') x_recon = mx.sym.Activation(data=x_recon, act_type='relu', name='x_recon_act') x_recon = mx.sym.FullyConnected(data=x_recon, num_hidden=1024, name='x_recon2') x_recon = mx.sym.Activation(data=x_recon, act_type='relu', name='x_recon_act2') x_recon = mx.sym.FullyConnected(data=x_recon, num_hidden=np.prod(input_shape), name='x_recon3') x_recon = mx.sym.Activation(data=x_recon, act_type='sigmoid', name='x_recon_act3') data_flatten = mx.sym.flatten(data=data) squared_error = mx.sym.square(x_recon-data_flatten) recon_error = mx.sym.mean(squared_error) recon_error_stopped = recon_error recon_error_stopped = mx.sym.BlockGrad(recon_error_stopped) loss = mx.symbol.MakeLoss((1-recon_loss_weight)*margin_loss(y_onehot, out_caps)+recon_loss_weight*recon_error) out_caps_blocked = out_caps out_caps_blocked = mx.sym.BlockGrad(out_caps_blocked) return mx.sym.Group([out_caps_blocked, loss, recon_error_stopped])
def capsnet(batch_size, n_class, num_routing, recon_loss_weight): """Create CapsNet""" # data.shape = [batch_size, 1, 28, 28] data = mx.sym.Variable('data') input_shape = (1, 28, 28) # Conv2D layer # net.shape = [batch_size, 256, 20, 20] conv1 = mx.sym.Convolution(data=data, num_filter=256, kernel=(9, 9), layout='NCHW', name='conv1') conv1 = mx.sym.Activation(data=conv1, act_type='relu', name='conv1_act') # net.shape = [batch_size, 256, 6, 6] primarycaps = primary_caps(data=conv1, dim_vector=8, n_channels=32, kernel=(9, 9), strides=[2, 2], name='primarycaps') primarycaps.infer_shape(data=(batch_size, 1, 28, 28)) # CapsuleLayer kernel_initializer = mx.init.Xavier(rnd_type='uniform', factor_type='avg', magnitude=3) bias_initializer = mx.init.Zero() digitcaps = CapsuleLayer(num_capsule=10, dim_vector=16, batch_size=batch_size, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, num_routing=num_routing)(primarycaps) # out_caps : (batch_size, 10) out_caps = mx.sym.sqrt(data=mx.sym.sum(mx.sym.square(digitcaps), 2)) out_caps.infer_shape(data=(batch_size, 1, 28, 28)) y = mx.sym.Variable('softmax_label', shape=(batch_size,)) y_onehot = mx.sym.one_hot(y, n_class) y_reshaped = mx.sym.Reshape(data=y_onehot, shape=(batch_size, -4, n_class, -1)) y_reshaped.infer_shape(softmax_label=(batch_size,)) # inputs_masked : (batch_size, 16) inputs_masked = mx.sym.linalg_gemm2(y_reshaped, digitcaps, transpose_a=True) inputs_masked = mx.sym.Reshape(data=inputs_masked, shape=(-3, 0)) x_recon = mx.sym.FullyConnected(data=inputs_masked, num_hidden=512, name='x_recon') x_recon = mx.sym.Activation(data=x_recon, act_type='relu', name='x_recon_act') x_recon = mx.sym.FullyConnected(data=x_recon, num_hidden=1024, name='x_recon2') x_recon = mx.sym.Activation(data=x_recon, act_type='relu', name='x_recon_act2') x_recon = mx.sym.FullyConnected(data=x_recon, num_hidden=np.prod(input_shape), name='x_recon3') x_recon = mx.sym.Activation(data=x_recon, act_type='sigmoid', name='x_recon_act3') data_flatten = mx.sym.flatten(data=data) squared_error = mx.sym.square(x_recon-data_flatten) recon_error = mx.sym.mean(squared_error) recon_error_stopped = recon_error recon_error_stopped = mx.sym.BlockGrad(recon_error_stopped) loss = mx.symbol.MakeLoss((1-recon_loss_weight)*margin_loss(y_onehot, out_caps)+recon_loss_weight*recon_error) out_caps_blocked = out_caps out_caps_blocked = mx.sym.BlockGrad(out_caps_blocked) return mx.sym.Group([out_caps_blocked, loss, recon_error_stopped])
[ "Create", "CapsNet" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/capsnet/capsulenet.py#L39-L100
[ "def", "capsnet", "(", "batch_size", ",", "n_class", ",", "num_routing", ",", "recon_loss_weight", ")", ":", "# data.shape = [batch_size, 1, 28, 28]", "data", "=", "mx", ".", "sym", ".", "Variable", "(", "'data'", ")", "input_shape", "=", "(", "1", ",", "28", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
do_training
Perform CapsNet training
example/capsnet/capsulenet.py
def do_training(num_epoch, optimizer, kvstore, learning_rate, model_prefix, decay): """Perform CapsNet training""" summary_writer = SummaryWriter(args.tblog_dir) lr_scheduler = SimpleLRScheduler(learning_rate) optimizer_params = {'lr_scheduler': lr_scheduler} module.init_params() module.init_optimizer(kvstore=kvstore, optimizer=optimizer, optimizer_params=optimizer_params) n_epoch = 0 while True: if n_epoch >= num_epoch: break train_iter.reset() val_iter.reset() loss_metric.reset() for n_batch, data_batch in enumerate(train_iter): module.forward_backward(data_batch) module.update() module.update_metric(loss_metric, data_batch.label) loss_metric.get_batch_log(n_batch) train_acc, train_loss, train_recon_err = loss_metric.get_name_value() loss_metric.reset() for n_batch, data_batch in enumerate(val_iter): module.forward(data_batch) module.update_metric(loss_metric, data_batch.label) loss_metric.get_batch_log(n_batch) val_acc, val_loss, val_recon_err = loss_metric.get_name_value() summary_writer.add_scalar('train_acc', train_acc, n_epoch) summary_writer.add_scalar('train_loss', train_loss, n_epoch) summary_writer.add_scalar('train_recon_err', train_recon_err, n_epoch) summary_writer.add_scalar('val_acc', val_acc, n_epoch) summary_writer.add_scalar('val_loss', val_loss, n_epoch) summary_writer.add_scalar('val_recon_err', val_recon_err, n_epoch) print('Epoch[%d] train acc: %.4f loss: %.6f recon_err: %.6f' % (n_epoch, train_acc, train_loss, train_recon_err)) print('Epoch[%d] val acc: %.4f loss: %.6f recon_err: %.6f' % (n_epoch, val_acc, val_loss, val_recon_err)) print('SAVE CHECKPOINT') module.save_checkpoint(prefix=model_prefix, epoch=n_epoch) n_epoch += 1 lr_scheduler.learning_rate = learning_rate * (decay ** n_epoch)
def do_training(num_epoch, optimizer, kvstore, learning_rate, model_prefix, decay): """Perform CapsNet training""" summary_writer = SummaryWriter(args.tblog_dir) lr_scheduler = SimpleLRScheduler(learning_rate) optimizer_params = {'lr_scheduler': lr_scheduler} module.init_params() module.init_optimizer(kvstore=kvstore, optimizer=optimizer, optimizer_params=optimizer_params) n_epoch = 0 while True: if n_epoch >= num_epoch: break train_iter.reset() val_iter.reset() loss_metric.reset() for n_batch, data_batch in enumerate(train_iter): module.forward_backward(data_batch) module.update() module.update_metric(loss_metric, data_batch.label) loss_metric.get_batch_log(n_batch) train_acc, train_loss, train_recon_err = loss_metric.get_name_value() loss_metric.reset() for n_batch, data_batch in enumerate(val_iter): module.forward(data_batch) module.update_metric(loss_metric, data_batch.label) loss_metric.get_batch_log(n_batch) val_acc, val_loss, val_recon_err = loss_metric.get_name_value() summary_writer.add_scalar('train_acc', train_acc, n_epoch) summary_writer.add_scalar('train_loss', train_loss, n_epoch) summary_writer.add_scalar('train_recon_err', train_recon_err, n_epoch) summary_writer.add_scalar('val_acc', val_acc, n_epoch) summary_writer.add_scalar('val_loss', val_loss, n_epoch) summary_writer.add_scalar('val_recon_err', val_recon_err, n_epoch) print('Epoch[%d] train acc: %.4f loss: %.6f recon_err: %.6f' % (n_epoch, train_acc, train_loss, train_recon_err)) print('Epoch[%d] val acc: %.4f loss: %.6f recon_err: %.6f' % (n_epoch, val_acc, val_loss, val_recon_err)) print('SAVE CHECKPOINT') module.save_checkpoint(prefix=model_prefix, epoch=n_epoch) n_epoch += 1 lr_scheduler.learning_rate = learning_rate * (decay ** n_epoch)
[ "Perform", "CapsNet", "training" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/capsnet/capsulenet.py#L195-L238
[ "def", "do_training", "(", "num_epoch", ",", "optimizer", ",", "kvstore", ",", "learning_rate", ",", "model_prefix", ",", "decay", ")", ":", "summary_writer", "=", "SummaryWriter", "(", "args", ".", "tblog_dir", ")", "lr_scheduler", "=", "SimpleLRScheduler", "("...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_shuffle
Shuffle the data.
example/capsnet/capsulenet.py
def _shuffle(data, idx): """Shuffle the data.""" shuffle_data = [] for idx_k, idx_v in data: shuffle_data.append((idx_k, mx.ndarray.array(idx_v.asnumpy()[idx], idx_v.context))) return shuffle_data
def _shuffle(data, idx): """Shuffle the data.""" shuffle_data = [] for idx_k, idx_v in data: shuffle_data.append((idx_k, mx.ndarray.array(idx_v.asnumpy()[idx], idx_v.context))) return shuffle_data
[ "Shuffle", "the", "data", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/capsnet/capsulenet.py#L268-L275
[ "def", "_shuffle", "(", "data", ",", "idx", ")", ":", "shuffle_data", "=", "[", "]", "for", "idx_k", ",", "idx_v", "in", "data", ":", "shuffle_data", ".", "append", "(", "(", "idx_k", ",", "mx", ".", "ndarray", ".", "array", "(", "idx_v", ".", "asn...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
LossMetric.update
Update the hyper-parameters and loss of CapsNet
example/capsnet/capsulenet.py
def update(self, labels, preds): """Update the hyper-parameters and loss of CapsNet""" batch_sum_metric = 0 batch_num_inst = 0 for label, pred_outcaps in zip(labels[0], preds[0]): label_np = int(label.asnumpy()) pred_label = int(np.argmax(pred_outcaps.asnumpy())) batch_sum_metric += int(label_np == pred_label) batch_num_inst += 1 batch_loss = preds[1].asnumpy() recon_loss = preds[2].asnumpy() self.sum_metric += batch_sum_metric self.num_inst += batch_num_inst self.loss += batch_loss self.recon_loss += recon_loss self.batch_sum_metric = batch_sum_metric self.batch_num_inst = batch_num_inst self.batch_loss = batch_loss self.n_batch += 1
def update(self, labels, preds): """Update the hyper-parameters and loss of CapsNet""" batch_sum_metric = 0 batch_num_inst = 0 for label, pred_outcaps in zip(labels[0], preds[0]): label_np = int(label.asnumpy()) pred_label = int(np.argmax(pred_outcaps.asnumpy())) batch_sum_metric += int(label_np == pred_label) batch_num_inst += 1 batch_loss = preds[1].asnumpy() recon_loss = preds[2].asnumpy() self.sum_metric += batch_sum_metric self.num_inst += batch_num_inst self.loss += batch_loss self.recon_loss += recon_loss self.batch_sum_metric = batch_sum_metric self.batch_num_inst = batch_num_inst self.batch_loss = batch_loss self.n_batch += 1
[ "Update", "the", "hyper", "-", "parameters", "and", "loss", "of", "CapsNet" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/capsnet/capsulenet.py#L140-L158
[ "def", "update", "(", "self", ",", "labels", ",", "preds", ")", ":", "batch_sum_metric", "=", "0", "batch_num_inst", "=", "0", "for", "label", ",", "pred_outcaps", "in", "zip", "(", "labels", "[", "0", "]", ",", "preds", "[", "0", "]", ")", ":", "l...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MNISTCustomIter.reset
Reset class MNISTCustomIter(mx.io.NDArrayIter):
example/capsnet/capsulenet.py
def reset(self): """Reset class MNISTCustomIter(mx.io.NDArrayIter):""" # shuffle data if self.is_train: np.random.shuffle(self.idx) self.data = _shuffle(self.data, self.idx) self.label = _shuffle(self.label, self.idx) if self.last_batch_handle == 'roll_over' and self.cursor > self.num_data: self.cursor = -self.batch_size + (self.cursor % self.num_data) % self.batch_size else: self.cursor = -self.batch_size
def reset(self): """Reset class MNISTCustomIter(mx.io.NDArrayIter):""" # shuffle data if self.is_train: np.random.shuffle(self.idx) self.data = _shuffle(self.data, self.idx) self.label = _shuffle(self.label, self.idx) if self.last_batch_handle == 'roll_over' and self.cursor > self.num_data: self.cursor = -self.batch_size + (self.cursor % self.num_data) % self.batch_size else: self.cursor = -self.batch_size
[ "Reset", "class", "MNISTCustomIter", "(", "mx", ".", "io", ".", "NDArrayIter", ")", ":" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/capsnet/capsulenet.py#L287-L298
[ "def", "reset", "(", "self", ")", ":", "# shuffle data", "if", "self", ".", "is_train", ":", "np", ".", "random", ".", "shuffle", "(", "self", ".", "idx", ")", "self", ".", "data", "=", "_shuffle", "(", "self", ".", "data", ",", "self", ".", "idx",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MNISTCustomIter.next
Generate next of iterator
example/capsnet/capsulenet.py
def next(self): """Generate next of iterator""" if self.iter_next(): if self.is_train: data_raw_list = self.getdata() data_shifted = [] for data_raw in data_raw_list[0]: data_shifted.append(random_shift(data_raw.asnumpy(), 0.1, 0.1)) return mx.io.DataBatch(data=[mx.nd.array(data_shifted)], label=self.getlabel(), pad=self.getpad(), index=None) else: return mx.io.DataBatch(data=self.getdata(), label=self.getlabel(), pad=self.getpad(), index=None) else: raise StopIteration
def next(self): """Generate next of iterator""" if self.iter_next(): if self.is_train: data_raw_list = self.getdata() data_shifted = [] for data_raw in data_raw_list[0]: data_shifted.append(random_shift(data_raw.asnumpy(), 0.1, 0.1)) return mx.io.DataBatch(data=[mx.nd.array(data_shifted)], label=self.getlabel(), pad=self.getpad(), index=None) else: return mx.io.DataBatch(data=self.getdata(), label=self.getlabel(), pad=self.getpad(), index=None) else: raise StopIteration
[ "Generate", "next", "of", "iterator" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/capsnet/capsulenet.py#L304-L318
[ "def", "next", "(", "self", ")", ":", "if", "self", ".", "iter_next", "(", ")", ":", "if", "self", ".", "is_train", ":", "data_raw_list", "=", "self", ".", "getdata", "(", ")", "data_shifted", "=", "[", "]", "for", "data_raw", "in", "data_raw_list", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
AttrScope.get
Get the attribute dict given the attribute set by the symbol. Parameters ---------- attr : dict of string to string The attribute passed in by user during symbol creation. Returns ------- attr : dict of string to string Updated attributes to add other scope related attributes.
python/mxnet/attribute.py
def get(self, attr): """ Get the attribute dict given the attribute set by the symbol. Parameters ---------- attr : dict of string to string The attribute passed in by user during symbol creation. Returns ------- attr : dict of string to string Updated attributes to add other scope related attributes. """ if self._attr: ret = self._attr.copy() if attr: ret.update(attr) return ret else: return attr if attr else {}
def get(self, attr): """ Get the attribute dict given the attribute set by the symbol. Parameters ---------- attr : dict of string to string The attribute passed in by user during symbol creation. Returns ------- attr : dict of string to string Updated attributes to add other scope related attributes. """ if self._attr: ret = self._attr.copy() if attr: ret.update(attr) return ret else: return attr if attr else {}
[ "Get", "the", "attribute", "dict", "given", "the", "attribute", "set", "by", "the", "symbol", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/attribute.py#L47-L67
[ "def", "get", "(", "self", ",", "attr", ")", ":", "if", "self", ".", "_attr", ":", "ret", "=", "self", ".", "_attr", ".", "copy", "(", ")", "if", "attr", ":", "ret", ".", "update", "(", "attr", ")", "return", "ret", "else", ":", "return", "attr...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_create_sparse_kvstore
Create kvstore assuming some parameters' storage types are row_sparse. Parameters ---------- kvstore : KVStore or str The kvstore. Returns ------- kvstore : KVStore update_on_kvstore : bool. Always True.
python/mxnet/model.py
def _create_sparse_kvstore(kvstore): """Create kvstore assuming some parameters' storage types are row_sparse. Parameters ---------- kvstore : KVStore or str The kvstore. Returns ------- kvstore : KVStore update_on_kvstore : bool. Always True. """ # always update on kvstore update_on_kvstore = True if isinstance(kvstore, kvs.KVStore): kv = kvstore elif isinstance(kvstore, str): kv = kvs.create(kvstore) else: raise TypeError("Cannot create '%s' KVStore with row_sparse parameters. " "The type must be KVStore or str." % kvstore) return (kv, update_on_kvstore)
def _create_sparse_kvstore(kvstore): """Create kvstore assuming some parameters' storage types are row_sparse. Parameters ---------- kvstore : KVStore or str The kvstore. Returns ------- kvstore : KVStore update_on_kvstore : bool. Always True. """ # always update on kvstore update_on_kvstore = True if isinstance(kvstore, kvs.KVStore): kv = kvstore elif isinstance(kvstore, str): kv = kvs.create(kvstore) else: raise TypeError("Cannot create '%s' KVStore with row_sparse parameters. " "The type must be KVStore or str." % kvstore) return (kv, update_on_kvstore)
[ "Create", "kvstore", "assuming", "some", "parameters", "storage", "types", "are", "row_sparse", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L58-L80
[ "def", "_create_sparse_kvstore", "(", "kvstore", ")", ":", "# always update on kvstore", "update_on_kvstore", "=", "True", "if", "isinstance", "(", "kvstore", ",", "kvs", ".", "KVStore", ")", ":", "kv", "=", "kvstore", "elif", "isinstance", "(", "kvstore", ",", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_create_kvstore
Create kvstore This function select and create a proper kvstore if given the kvstore type. Parameters ---------- kvstore : KVStore or str The kvstore. num_device : int The number of devices arg_params : dict of str to `NDArray`. Model parameter, dict of name to `NDArray` of net's weights.
python/mxnet/model.py
def _create_kvstore(kvstore, num_device, arg_params): """Create kvstore This function select and create a proper kvstore if given the kvstore type. Parameters ---------- kvstore : KVStore or str The kvstore. num_device : int The number of devices arg_params : dict of str to `NDArray`. Model parameter, dict of name to `NDArray` of net's weights. """ update_on_kvstore = bool(int(os.getenv('MXNET_UPDATE_ON_KVSTORE', "1"))) if kvstore is None: kv = None elif isinstance(kvstore, kvs.KVStore): kv = kvstore elif isinstance(kvstore, str): # create kvstore using the string type if num_device == 1 and 'dist' not in kvstore: # no need to use kv for single device and single machine kv = None else: kv = kvs.create(kvstore) if kvstore == 'local': # automatically select a proper local max_size = max(np.prod(param.shape) for param in arg_params.values()) if max_size > 1024 * 1024 * 16: update_on_kvstore = False else: raise TypeError('kvstore must be KVStore, str or None') if kv is None: update_on_kvstore = False return (kv, update_on_kvstore)
def _create_kvstore(kvstore, num_device, arg_params): """Create kvstore This function select and create a proper kvstore if given the kvstore type. Parameters ---------- kvstore : KVStore or str The kvstore. num_device : int The number of devices arg_params : dict of str to `NDArray`. Model parameter, dict of name to `NDArray` of net's weights. """ update_on_kvstore = bool(int(os.getenv('MXNET_UPDATE_ON_KVSTORE', "1"))) if kvstore is None: kv = None elif isinstance(kvstore, kvs.KVStore): kv = kvstore elif isinstance(kvstore, str): # create kvstore using the string type if num_device == 1 and 'dist' not in kvstore: # no need to use kv for single device and single machine kv = None else: kv = kvs.create(kvstore) if kvstore == 'local': # automatically select a proper local max_size = max(np.prod(param.shape) for param in arg_params.values()) if max_size > 1024 * 1024 * 16: update_on_kvstore = False else: raise TypeError('kvstore must be KVStore, str or None') if kv is None: update_on_kvstore = False return (kv, update_on_kvstore)
[ "Create", "kvstore", "This", "function", "select", "and", "create", "a", "proper", "kvstore", "if", "given", "the", "kvstore", "type", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L82-L119
[ "def", "_create_kvstore", "(", "kvstore", ",", "num_device", ",", "arg_params", ")", ":", "update_on_kvstore", "=", "bool", "(", "int", "(", "os", ".", "getenv", "(", "'MXNET_UPDATE_ON_KVSTORE'", ",", "\"1\"", ")", ")", ")", "if", "kvstore", "is", "None", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_initialize_kvstore
Initialize kvstore
python/mxnet/model.py
def _initialize_kvstore(kvstore, param_arrays, arg_params, param_names, update_on_kvstore): """Initialize kvstore""" for idx, param_on_devs in enumerate(param_arrays): name = param_names[idx] kvstore.init(name, arg_params[name]) if update_on_kvstore: kvstore.pull(name, param_on_devs, priority=-idx)
def _initialize_kvstore(kvstore, param_arrays, arg_params, param_names, update_on_kvstore): """Initialize kvstore""" for idx, param_on_devs in enumerate(param_arrays): name = param_names[idx] kvstore.init(name, arg_params[name]) if update_on_kvstore: kvstore.pull(name, param_on_devs, priority=-idx)
[ "Initialize", "kvstore" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L121-L128
[ "def", "_initialize_kvstore", "(", "kvstore", ",", "param_arrays", ",", "arg_params", ",", "param_names", ",", "update_on_kvstore", ")", ":", "for", "idx", ",", "param_on_devs", "in", "enumerate", "(", "param_arrays", ")", ":", "name", "=", "param_names", "[", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_update_params_on_kvstore_nccl
Perform update of param_arrays from grad_arrays on NCCL kvstore.
python/mxnet/model.py
def _update_params_on_kvstore_nccl(param_arrays, grad_arrays, kvstore, param_names): """Perform update of param_arrays from grad_arrays on NCCL kvstore.""" valid_indices = [index for index, grad_list in enumerate(grad_arrays) if grad_list[0] is not None] valid_grad_arrays = [grad_arrays[i] for i in valid_indices] valid_param_arrays = [param_arrays[i] for i in valid_indices] valid_param_names = [param_names[i] for i in valid_indices] size = len(valid_grad_arrays) start = 0 # Use aggregation by default only with NCCL default_batch = '16' batch = int(os.getenv('MXNET_UPDATE_AGGREGATION_SIZE', default_batch)) while start < size: end = start + batch if start + batch < size else size # push gradient, priority is negative index kvstore.push(valid_param_names[start:end], valid_grad_arrays[start:end], priority=-start) # pull back the weights kvstore.pull(valid_param_names[start:end], valid_param_arrays[start:end], priority=-start) start = end
def _update_params_on_kvstore_nccl(param_arrays, grad_arrays, kvstore, param_names): """Perform update of param_arrays from grad_arrays on NCCL kvstore.""" valid_indices = [index for index, grad_list in enumerate(grad_arrays) if grad_list[0] is not None] valid_grad_arrays = [grad_arrays[i] for i in valid_indices] valid_param_arrays = [param_arrays[i] for i in valid_indices] valid_param_names = [param_names[i] for i in valid_indices] size = len(valid_grad_arrays) start = 0 # Use aggregation by default only with NCCL default_batch = '16' batch = int(os.getenv('MXNET_UPDATE_AGGREGATION_SIZE', default_batch)) while start < size: end = start + batch if start + batch < size else size # push gradient, priority is negative index kvstore.push(valid_param_names[start:end], valid_grad_arrays[start:end], priority=-start) # pull back the weights kvstore.pull(valid_param_names[start:end], valid_param_arrays[start:end], priority=-start) start = end
[ "Perform", "update", "of", "param_arrays", "from", "grad_arrays", "on", "NCCL", "kvstore", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L130-L148
[ "def", "_update_params_on_kvstore_nccl", "(", "param_arrays", ",", "grad_arrays", ",", "kvstore", ",", "param_names", ")", ":", "valid_indices", "=", "[", "index", "for", "index", ",", "grad_list", "in", "enumerate", "(", "grad_arrays", ")", "if", "grad_list", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_update_params_on_kvstore
Perform update of param_arrays from grad_arrays on kvstore.
python/mxnet/model.py
def _update_params_on_kvstore(param_arrays, grad_arrays, kvstore, param_names): """Perform update of param_arrays from grad_arrays on kvstore.""" for index, pair in enumerate(zip(param_arrays, grad_arrays)): arg_list, grad_list = pair if grad_list[0] is None: continue name = param_names[index] # push gradient, priority is negative index kvstore.push(name, grad_list, priority=-index) # pull back the weights kvstore.pull(name, arg_list, priority=-index)
def _update_params_on_kvstore(param_arrays, grad_arrays, kvstore, param_names): """Perform update of param_arrays from grad_arrays on kvstore.""" for index, pair in enumerate(zip(param_arrays, grad_arrays)): arg_list, grad_list = pair if grad_list[0] is None: continue name = param_names[index] # push gradient, priority is negative index kvstore.push(name, grad_list, priority=-index) # pull back the weights kvstore.pull(name, arg_list, priority=-index)
[ "Perform", "update", "of", "param_arrays", "from", "grad_arrays", "on", "kvstore", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L150-L160
[ "def", "_update_params_on_kvstore", "(", "param_arrays", ",", "grad_arrays", ",", "kvstore", ",", "param_names", ")", ":", "for", "index", ",", "pair", "in", "enumerate", "(", "zip", "(", "param_arrays", ",", "grad_arrays", ")", ")", ":", "arg_list", ",", "g...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_update_params
Perform update of param_arrays from grad_arrays not on kvstore.
python/mxnet/model.py
def _update_params(param_arrays, grad_arrays, updater, num_device, kvstore=None, param_names=None): """Perform update of param_arrays from grad_arrays not on kvstore.""" updates = [[] for _ in range(num_device)] for i, pair in enumerate(zip(param_arrays, grad_arrays)): arg_list, grad_list = pair if grad_list[0] is None: continue index = i if kvstore: name = param_names[index] # push gradient, priority is negative index kvstore.push(name, grad_list, priority=-index) # pull back the sum gradients, to the same locations. kvstore.pull(name, grad_list, priority=-index) for k, p in enumerate(zip(arg_list, grad_list)): # faked an index here, to make optimizer create diff # state for the same index but on diff devs, TODO(mli) # use a better solution later w, g = p updates[k].append((index*num_device+k, g, w)) for dev_updates in updates: # update params if param_arrays and grad_arrays are not empty if dev_updates: i, w, g = zip(*dev_updates) updater(i, w, g)
def _update_params(param_arrays, grad_arrays, updater, num_device, kvstore=None, param_names=None): """Perform update of param_arrays from grad_arrays not on kvstore.""" updates = [[] for _ in range(num_device)] for i, pair in enumerate(zip(param_arrays, grad_arrays)): arg_list, grad_list = pair if grad_list[0] is None: continue index = i if kvstore: name = param_names[index] # push gradient, priority is negative index kvstore.push(name, grad_list, priority=-index) # pull back the sum gradients, to the same locations. kvstore.pull(name, grad_list, priority=-index) for k, p in enumerate(zip(arg_list, grad_list)): # faked an index here, to make optimizer create diff # state for the same index but on diff devs, TODO(mli) # use a better solution later w, g = p updates[k].append((index*num_device+k, g, w)) for dev_updates in updates: # update params if param_arrays and grad_arrays are not empty if dev_updates: i, w, g = zip(*dev_updates) updater(i, w, g)
[ "Perform", "update", "of", "param_arrays", "from", "grad_arrays", "not", "on", "kvstore", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L162-L187
[ "def", "_update_params", "(", "param_arrays", ",", "grad_arrays", ",", "updater", ",", "num_device", ",", "kvstore", "=", "None", ",", "param_names", "=", "None", ")", ":", "updates", "=", "[", "[", "]", "for", "_", "in", "range", "(", "num_device", ")",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_multiple_callbacks
Sends args and kwargs to any configured callbacks. This handles the cases where the 'callbacks' variable is ``None``, a single function, or a list.
python/mxnet/model.py
def _multiple_callbacks(callbacks, *args, **kwargs): """Sends args and kwargs to any configured callbacks. This handles the cases where the 'callbacks' variable is ``None``, a single function, or a list. """ if isinstance(callbacks, list): for cb in callbacks: cb(*args, **kwargs) return if callbacks: callbacks(*args, **kwargs)
def _multiple_callbacks(callbacks, *args, **kwargs): """Sends args and kwargs to any configured callbacks. This handles the cases where the 'callbacks' variable is ``None``, a single function, or a list. """ if isinstance(callbacks, list): for cb in callbacks: cb(*args, **kwargs) return if callbacks: callbacks(*args, **kwargs)
[ "Sends", "args", "and", "kwargs", "to", "any", "configured", "callbacks", ".", "This", "handles", "the", "cases", "where", "the", "callbacks", "variable", "is", "None", "a", "single", "function", "or", "a", "list", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L190-L200
[ "def", "_multiple_callbacks", "(", "callbacks", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "isinstance", "(", "callbacks", ",", "list", ")", ":", "for", "cb", "in", "callbacks", ":", "cb", "(", "*", "args", ",", "*", "*", "kwargs", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_train_multi_device
Internal training function on multiple devices. This function will also work for single device as well. Parameters ---------- symbol : Symbol The network configuration. ctx : list of Context The training devices. arg_names: list of str Name of all arguments of the network. param_names: list of str Name of all trainable parameters of the network. aux_names: list of str Name of all auxiliary states of the network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. begin_epoch : int The begining training epoch. end_epoch : int The end training epoch. epoch_size : int, optional Number of batches in a epoch. In default, it is set to ``ceil(num_train_examples / batch_size)``. optimizer : Optimizer The optimization algorithm train_data : DataIter Training data iterator. eval_data : DataIter Validation data iterator. eval_metric : EvalMetric An evaluation function or a list of evaluation functions. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback : callable(BatchEndParams) A callback that is invoked at end of each batch. This can be used to measure speed, get result from evaluation metric. etc. kvstore : KVStore The KVStore. update_on_kvstore : bool Whether or not perform weight updating on kvstore. logger : logging logger When not specified, default logger will be used. work_load_list : list of float or int, optional The list of work load for different devices, in the same order as ``ctx``. monitor : Monitor, optional Monitor installed to executor, for monitoring outputs, weights, and gradients for debugging. Notes ----- - This function will inplace update the NDArrays in `arg_params` and `aux_states`.
python/mxnet/model.py
def _train_multi_device(symbol, ctx, arg_names, param_names, aux_names, arg_params, aux_params, begin_epoch, end_epoch, epoch_size, optimizer, kvstore, update_on_kvstore, train_data, eval_data=None, eval_metric=None, epoch_end_callback=None, batch_end_callback=None, logger=None, work_load_list=None, monitor=None, eval_end_callback=None, eval_batch_end_callback=None, sym_gen=None): """Internal training function on multiple devices. This function will also work for single device as well. Parameters ---------- symbol : Symbol The network configuration. ctx : list of Context The training devices. arg_names: list of str Name of all arguments of the network. param_names: list of str Name of all trainable parameters of the network. aux_names: list of str Name of all auxiliary states of the network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. begin_epoch : int The begining training epoch. end_epoch : int The end training epoch. epoch_size : int, optional Number of batches in a epoch. In default, it is set to ``ceil(num_train_examples / batch_size)``. optimizer : Optimizer The optimization algorithm train_data : DataIter Training data iterator. eval_data : DataIter Validation data iterator. eval_metric : EvalMetric An evaluation function or a list of evaluation functions. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback : callable(BatchEndParams) A callback that is invoked at end of each batch. This can be used to measure speed, get result from evaluation metric. etc. kvstore : KVStore The KVStore. update_on_kvstore : bool Whether or not perform weight updating on kvstore. logger : logging logger When not specified, default logger will be used. work_load_list : list of float or int, optional The list of work load for different devices, in the same order as ``ctx``. monitor : Monitor, optional Monitor installed to executor, for monitoring outputs, weights, and gradients for debugging. Notes ----- - This function will inplace update the NDArrays in `arg_params` and `aux_states`. """ if logger is None: logger = logging executor_manager = DataParallelExecutorManager(symbol=symbol, sym_gen=sym_gen, ctx=ctx, train_data=train_data, param_names=param_names, arg_names=arg_names, aux_names=aux_names, work_load_list=work_load_list, logger=logger) if monitor: executor_manager.install_monitor(monitor) executor_manager.set_params(arg_params, aux_params) if not update_on_kvstore: updater = get_updater(optimizer) else: kvstore.set_optimizer(optimizer) if kvstore: _initialize_kvstore(kvstore=kvstore, param_arrays=executor_manager.param_arrays, arg_params=arg_params, param_names=executor_manager.param_names, update_on_kvstore=update_on_kvstore) # Now start training train_data.reset() for epoch in range(begin_epoch, end_epoch): # Training phase tic = time.time() eval_metric.reset() nbatch = 0 # Iterate over training data. while True: do_reset = True for data_batch in train_data: executor_manager.load_data_batch(data_batch) if monitor is not None: monitor.tic() executor_manager.forward(is_train=True) executor_manager.backward() if update_on_kvstore: if 'nccl' in kvstore.type: _update_params_on_kvstore_nccl(executor_manager.param_arrays, executor_manager.grad_arrays, kvstore, executor_manager.param_names) else: _update_params_on_kvstore(executor_manager.param_arrays, executor_manager.grad_arrays, kvstore, executor_manager.param_names) else: _update_params(executor_manager.param_arrays, executor_manager.grad_arrays, updater=updater, num_device=len(ctx), kvstore=kvstore, param_names=executor_manager.param_names) if monitor is not None: monitor.toc_print() # evaluate at end, so we can lazy copy executor_manager.update_metric(eval_metric, data_batch.label) nbatch += 1 # batch callback (for print purpose) if batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=epoch, nbatch=nbatch, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(batch_end_callback, batch_end_params) # this epoch is done possibly earlier if epoch_size is not None and nbatch >= epoch_size: do_reset = False break if do_reset: logger.info('Epoch[%d] Resetting Data Iterator', epoch) train_data.reset() # this epoch is done if epoch_size is None or nbatch >= epoch_size: break toc = time.time() logger.info('Epoch[%d] Time cost=%.3f', epoch, (toc - tic)) if epoch_end_callback or epoch + 1 == end_epoch: executor_manager.copy_to(arg_params, aux_params) _multiple_callbacks(epoch_end_callback, epoch, symbol, arg_params, aux_params) # evaluation if eval_data: eval_metric.reset() eval_data.reset() total_num_batch = 0 for i, eval_batch in enumerate(eval_data): executor_manager.load_data_batch(eval_batch) executor_manager.forward(is_train=False) executor_manager.update_metric(eval_metric, eval_batch.label) if eval_batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=epoch, nbatch=i, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(eval_batch_end_callback, batch_end_params) total_num_batch += 1 if eval_end_callback is not None: eval_end_params = BatchEndParam(epoch=epoch, nbatch=total_num_batch, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(eval_end_callback, eval_end_params) eval_data.reset()
def _train_multi_device(symbol, ctx, arg_names, param_names, aux_names, arg_params, aux_params, begin_epoch, end_epoch, epoch_size, optimizer, kvstore, update_on_kvstore, train_data, eval_data=None, eval_metric=None, epoch_end_callback=None, batch_end_callback=None, logger=None, work_load_list=None, monitor=None, eval_end_callback=None, eval_batch_end_callback=None, sym_gen=None): """Internal training function on multiple devices. This function will also work for single device as well. Parameters ---------- symbol : Symbol The network configuration. ctx : list of Context The training devices. arg_names: list of str Name of all arguments of the network. param_names: list of str Name of all trainable parameters of the network. aux_names: list of str Name of all auxiliary states of the network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. begin_epoch : int The begining training epoch. end_epoch : int The end training epoch. epoch_size : int, optional Number of batches in a epoch. In default, it is set to ``ceil(num_train_examples / batch_size)``. optimizer : Optimizer The optimization algorithm train_data : DataIter Training data iterator. eval_data : DataIter Validation data iterator. eval_metric : EvalMetric An evaluation function or a list of evaluation functions. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback : callable(BatchEndParams) A callback that is invoked at end of each batch. This can be used to measure speed, get result from evaluation metric. etc. kvstore : KVStore The KVStore. update_on_kvstore : bool Whether or not perform weight updating on kvstore. logger : logging logger When not specified, default logger will be used. work_load_list : list of float or int, optional The list of work load for different devices, in the same order as ``ctx``. monitor : Monitor, optional Monitor installed to executor, for monitoring outputs, weights, and gradients for debugging. Notes ----- - This function will inplace update the NDArrays in `arg_params` and `aux_states`. """ if logger is None: logger = logging executor_manager = DataParallelExecutorManager(symbol=symbol, sym_gen=sym_gen, ctx=ctx, train_data=train_data, param_names=param_names, arg_names=arg_names, aux_names=aux_names, work_load_list=work_load_list, logger=logger) if monitor: executor_manager.install_monitor(monitor) executor_manager.set_params(arg_params, aux_params) if not update_on_kvstore: updater = get_updater(optimizer) else: kvstore.set_optimizer(optimizer) if kvstore: _initialize_kvstore(kvstore=kvstore, param_arrays=executor_manager.param_arrays, arg_params=arg_params, param_names=executor_manager.param_names, update_on_kvstore=update_on_kvstore) # Now start training train_data.reset() for epoch in range(begin_epoch, end_epoch): # Training phase tic = time.time() eval_metric.reset() nbatch = 0 # Iterate over training data. while True: do_reset = True for data_batch in train_data: executor_manager.load_data_batch(data_batch) if monitor is not None: monitor.tic() executor_manager.forward(is_train=True) executor_manager.backward() if update_on_kvstore: if 'nccl' in kvstore.type: _update_params_on_kvstore_nccl(executor_manager.param_arrays, executor_manager.grad_arrays, kvstore, executor_manager.param_names) else: _update_params_on_kvstore(executor_manager.param_arrays, executor_manager.grad_arrays, kvstore, executor_manager.param_names) else: _update_params(executor_manager.param_arrays, executor_manager.grad_arrays, updater=updater, num_device=len(ctx), kvstore=kvstore, param_names=executor_manager.param_names) if monitor is not None: monitor.toc_print() # evaluate at end, so we can lazy copy executor_manager.update_metric(eval_metric, data_batch.label) nbatch += 1 # batch callback (for print purpose) if batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=epoch, nbatch=nbatch, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(batch_end_callback, batch_end_params) # this epoch is done possibly earlier if epoch_size is not None and nbatch >= epoch_size: do_reset = False break if do_reset: logger.info('Epoch[%d] Resetting Data Iterator', epoch) train_data.reset() # this epoch is done if epoch_size is None or nbatch >= epoch_size: break toc = time.time() logger.info('Epoch[%d] Time cost=%.3f', epoch, (toc - tic)) if epoch_end_callback or epoch + 1 == end_epoch: executor_manager.copy_to(arg_params, aux_params) _multiple_callbacks(epoch_end_callback, epoch, symbol, arg_params, aux_params) # evaluation if eval_data: eval_metric.reset() eval_data.reset() total_num_batch = 0 for i, eval_batch in enumerate(eval_data): executor_manager.load_data_batch(eval_batch) executor_manager.forward(is_train=False) executor_manager.update_metric(eval_metric, eval_batch.label) if eval_batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=epoch, nbatch=i, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(eval_batch_end_callback, batch_end_params) total_num_batch += 1 if eval_end_callback is not None: eval_end_params = BatchEndParam(epoch=epoch, nbatch=total_num_batch, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(eval_end_callback, eval_end_params) eval_data.reset()
[ "Internal", "training", "function", "on", "multiple", "devices", ".", "This", "function", "will", "also", "work", "for", "single", "device", "as", "well", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L203-L390
[ "def", "_train_multi_device", "(", "symbol", ",", "ctx", ",", "arg_names", ",", "param_names", ",", "aux_names", ",", "arg_params", ",", "aux_params", ",", "begin_epoch", ",", "end_epoch", ",", "epoch_size", ",", "optimizer", ",", "kvstore", ",", "update_on_kvst...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
save_checkpoint
Checkpoint the model data into file. Parameters ---------- prefix : str Prefix of model name. epoch : int The epoch number of the model. symbol : Symbol The input Symbol. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters.
python/mxnet/model.py
def save_checkpoint(prefix, epoch, symbol, arg_params, aux_params): """Checkpoint the model data into file. Parameters ---------- prefix : str Prefix of model name. epoch : int The epoch number of the model. symbol : Symbol The input Symbol. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ if symbol is not None: symbol.save('%s-symbol.json' % prefix) save_dict = {('arg:%s' % k) : v.as_in_context(cpu()) for k, v in arg_params.items()} save_dict.update({('aux:%s' % k) : v.as_in_context(cpu()) for k, v in aux_params.items()}) param_name = '%s-%04d.params' % (prefix, epoch) nd.save(param_name, save_dict) logging.info('Saved checkpoint to \"%s\"', param_name)
def save_checkpoint(prefix, epoch, symbol, arg_params, aux_params): """Checkpoint the model data into file. Parameters ---------- prefix : str Prefix of model name. epoch : int The epoch number of the model. symbol : Symbol The input Symbol. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ if symbol is not None: symbol.save('%s-symbol.json' % prefix) save_dict = {('arg:%s' % k) : v.as_in_context(cpu()) for k, v in arg_params.items()} save_dict.update({('aux:%s' % k) : v.as_in_context(cpu()) for k, v in aux_params.items()}) param_name = '%s-%04d.params' % (prefix, epoch) nd.save(param_name, save_dict) logging.info('Saved checkpoint to \"%s\"', param_name)
[ "Checkpoint", "the", "model", "data", "into", "file", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L394-L421
[ "def", "save_checkpoint", "(", "prefix", ",", "epoch", ",", "symbol", ",", "arg_params", ",", "aux_params", ")", ":", "if", "symbol", "is", "not", "None", ":", "symbol", ".", "save", "(", "'%s-symbol.json'", "%", "prefix", ")", "save_dict", "=", "{", "("...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
load_checkpoint
Load model checkpoint from file. Parameters ---------- prefix : str Prefix of model name. epoch : int Epoch number of model we would like to load. Returns ------- symbol : Symbol The symbol configuration of computation network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - Symbol will be loaded from ``prefix-symbol.json``. - Parameters will be loaded from ``prefix-epoch.params``.
python/mxnet/model.py
def load_checkpoint(prefix, epoch): """Load model checkpoint from file. Parameters ---------- prefix : str Prefix of model name. epoch : int Epoch number of model we would like to load. Returns ------- symbol : Symbol The symbol configuration of computation network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - Symbol will be loaded from ``prefix-symbol.json``. - Parameters will be loaded from ``prefix-epoch.params``. """ symbol = sym.load('%s-symbol.json' % prefix) save_dict = nd.load('%s-%04d.params' % (prefix, epoch)) arg_params = {} aux_params = {} for k, v in save_dict.items(): tp, name = k.split(':', 1) if tp == 'arg': arg_params[name] = v if tp == 'aux': aux_params[name] = v return (symbol, arg_params, aux_params)
def load_checkpoint(prefix, epoch): """Load model checkpoint from file. Parameters ---------- prefix : str Prefix of model name. epoch : int Epoch number of model we would like to load. Returns ------- symbol : Symbol The symbol configuration of computation network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - Symbol will be loaded from ``prefix-symbol.json``. - Parameters will be loaded from ``prefix-epoch.params``. """ symbol = sym.load('%s-symbol.json' % prefix) save_dict = nd.load('%s-%04d.params' % (prefix, epoch)) arg_params = {} aux_params = {} for k, v in save_dict.items(): tp, name = k.split(':', 1) if tp == 'arg': arg_params[name] = v if tp == 'aux': aux_params[name] = v return (symbol, arg_params, aux_params)
[ "Load", "model", "checkpoint", "from", "file", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L424-L458
[ "def", "load_checkpoint", "(", "prefix", ",", "epoch", ")", ":", "symbol", "=", "sym", ".", "load", "(", "'%s-symbol.json'", "%", "prefix", ")", "save_dict", "=", "nd", ".", "load", "(", "'%s-%04d.params'", "%", "(", "prefix", ",", "epoch", ")", ")", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward._check_arguments
verify the argument of the default symbol and user provided parameters
python/mxnet/model.py
def _check_arguments(self): """verify the argument of the default symbol and user provided parameters""" if self.argument_checked: return assert(self.symbol is not None) self.argument_checked = True # check if symbol contain duplicated names. _check_arguments(self.symbol) # rematch parameters to delete useless ones if self.allow_extra_params: if self.arg_params: arg_names = set(self.symbol.list_arguments()) self.arg_params = {k : v for k, v in self.arg_params.items() if k in arg_names} if self.aux_params: aux_names = set(self.symbol.list_auxiliary_states()) self.aux_params = {k : v for k, v in self.aux_params.items() if k in aux_names}
def _check_arguments(self): """verify the argument of the default symbol and user provided parameters""" if self.argument_checked: return assert(self.symbol is not None) self.argument_checked = True # check if symbol contain duplicated names. _check_arguments(self.symbol) # rematch parameters to delete useless ones if self.allow_extra_params: if self.arg_params: arg_names = set(self.symbol.list_arguments()) self.arg_params = {k : v for k, v in self.arg_params.items() if k in arg_names} if self.aux_params: aux_names = set(self.symbol.list_auxiliary_states()) self.aux_params = {k : v for k, v in self.aux_params.items() if k in aux_names}
[ "verify", "the", "argument", "of", "the", "default", "symbol", "and", "user", "provided", "parameters" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L546-L565
[ "def", "_check_arguments", "(", "self", ")", ":", "if", "self", ".", "argument_checked", ":", "return", "assert", "(", "self", ".", "symbol", "is", "not", "None", ")", "self", ".", "argument_checked", "=", "True", "# check if symbol contain duplicated names.", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward._init_params
Initialize weight parameters and auxiliary states.
python/mxnet/model.py
def _init_params(self, inputs, overwrite=False): """Initialize weight parameters and auxiliary states.""" inputs = [x if isinstance(x, DataDesc) else DataDesc(*x) for x in inputs] input_shapes = {item.name: item.shape for item in inputs} arg_shapes, _, aux_shapes = self.symbol.infer_shape(**input_shapes) assert arg_shapes is not None input_dtypes = {item.name: item.dtype for item in inputs} arg_dtypes, _, aux_dtypes = self.symbol.infer_type(**input_dtypes) assert arg_dtypes is not None arg_names = self.symbol.list_arguments() input_names = input_shapes.keys() param_names = [key for key in arg_names if key not in input_names] aux_names = self.symbol.list_auxiliary_states() param_name_attrs = [x for x in zip(arg_names, arg_shapes, arg_dtypes) if x[0] in param_names] arg_params = {k : nd.zeros(shape=s, dtype=t) for k, s, t in param_name_attrs} aux_name_attrs = [x for x in zip(aux_names, aux_shapes, aux_dtypes) if x[0] in aux_names] aux_params = {k : nd.zeros(shape=s, dtype=t) for k, s, t in aux_name_attrs} for k, v in arg_params.items(): if self.arg_params and k in self.arg_params and (not overwrite): arg_params[k][:] = self.arg_params[k][:] else: self.initializer(k, v) for k, v in aux_params.items(): if self.aux_params and k in self.aux_params and (not overwrite): aux_params[k][:] = self.aux_params[k][:] else: self.initializer(k, v) self.arg_params = arg_params self.aux_params = aux_params return (arg_names, list(param_names), aux_names)
def _init_params(self, inputs, overwrite=False): """Initialize weight parameters and auxiliary states.""" inputs = [x if isinstance(x, DataDesc) else DataDesc(*x) for x in inputs] input_shapes = {item.name: item.shape for item in inputs} arg_shapes, _, aux_shapes = self.symbol.infer_shape(**input_shapes) assert arg_shapes is not None input_dtypes = {item.name: item.dtype for item in inputs} arg_dtypes, _, aux_dtypes = self.symbol.infer_type(**input_dtypes) assert arg_dtypes is not None arg_names = self.symbol.list_arguments() input_names = input_shapes.keys() param_names = [key for key in arg_names if key not in input_names] aux_names = self.symbol.list_auxiliary_states() param_name_attrs = [x for x in zip(arg_names, arg_shapes, arg_dtypes) if x[0] in param_names] arg_params = {k : nd.zeros(shape=s, dtype=t) for k, s, t in param_name_attrs} aux_name_attrs = [x for x in zip(aux_names, aux_shapes, aux_dtypes) if x[0] in aux_names] aux_params = {k : nd.zeros(shape=s, dtype=t) for k, s, t in aux_name_attrs} for k, v in arg_params.items(): if self.arg_params and k in self.arg_params and (not overwrite): arg_params[k][:] = self.arg_params[k][:] else: self.initializer(k, v) for k, v in aux_params.items(): if self.aux_params and k in self.aux_params and (not overwrite): aux_params[k][:] = self.aux_params[k][:] else: self.initializer(k, v) self.arg_params = arg_params self.aux_params = aux_params return (arg_names, list(param_names), aux_names)
[ "Initialize", "weight", "parameters", "and", "auxiliary", "states", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L573-L611
[ "def", "_init_params", "(", "self", ",", "inputs", ",", "overwrite", "=", "False", ")", ":", "inputs", "=", "[", "x", "if", "isinstance", "(", "x", ",", "DataDesc", ")", "else", "DataDesc", "(", "*", "x", ")", "for", "x", "in", "inputs", "]", "inpu...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward._init_predictor
Initialize the predictor module for running prediction.
python/mxnet/model.py
def _init_predictor(self, input_shapes, type_dict=None): """Initialize the predictor module for running prediction.""" shapes = {name: self.arg_params[name].shape for name in self.arg_params} shapes.update(dict(input_shapes)) if self._pred_exec is not None: arg_shapes, _, _ = self.symbol.infer_shape(**shapes) assert arg_shapes is not None, "Incomplete input shapes" pred_shapes = [x.shape for x in self._pred_exec.arg_arrays] if arg_shapes == pred_shapes: return # for now only use the first device pred_exec = self.symbol.simple_bind( self.ctx[0], grad_req='null', type_dict=type_dict, **shapes) pred_exec.copy_params_from(self.arg_params, self.aux_params) _check_arguments(self.symbol) self._pred_exec = pred_exec
def _init_predictor(self, input_shapes, type_dict=None): """Initialize the predictor module for running prediction.""" shapes = {name: self.arg_params[name].shape for name in self.arg_params} shapes.update(dict(input_shapes)) if self._pred_exec is not None: arg_shapes, _, _ = self.symbol.infer_shape(**shapes) assert arg_shapes is not None, "Incomplete input shapes" pred_shapes = [x.shape for x in self._pred_exec.arg_arrays] if arg_shapes == pred_shapes: return # for now only use the first device pred_exec = self.symbol.simple_bind( self.ctx[0], grad_req='null', type_dict=type_dict, **shapes) pred_exec.copy_params_from(self.arg_params, self.aux_params) _check_arguments(self.symbol) self._pred_exec = pred_exec
[ "Initialize", "the", "predictor", "module", "for", "running", "prediction", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L621-L637
[ "def", "_init_predictor", "(", "self", ",", "input_shapes", ",", "type_dict", "=", "None", ")", ":", "shapes", "=", "{", "name", ":", "self", ".", "arg_params", "[", "name", "]", ".", "shape", "for", "name", "in", "self", ".", "arg_params", "}", "shape...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward._init_iter
Initialize the iterator given input.
python/mxnet/model.py
def _init_iter(self, X, y, is_train): """Initialize the iterator given input.""" if isinstance(X, (np.ndarray, nd.NDArray)): if y is None: if is_train: raise ValueError('y must be specified when X is numpy.ndarray') else: y = np.zeros(X.shape[0]) if not isinstance(y, (np.ndarray, nd.NDArray)): raise TypeError('y must be ndarray when X is numpy.ndarray') if X.shape[0] != y.shape[0]: raise ValueError("The numbers of data points and labels not equal") if y.ndim == 2 and y.shape[1] == 1: y = y.flatten() if y.ndim != 1: raise ValueError("Label must be 1D or 2D (with 2nd dimension being 1)") if is_train: return io.NDArrayIter(X, y, min(X.shape[0], self.numpy_batch_size), shuffle=is_train, last_batch_handle='roll_over') else: return io.NDArrayIter(X, y, min(X.shape[0], self.numpy_batch_size), shuffle=False) if not isinstance(X, io.DataIter): raise TypeError('X must be DataIter, NDArray or numpy.ndarray') return X
def _init_iter(self, X, y, is_train): """Initialize the iterator given input.""" if isinstance(X, (np.ndarray, nd.NDArray)): if y is None: if is_train: raise ValueError('y must be specified when X is numpy.ndarray') else: y = np.zeros(X.shape[0]) if not isinstance(y, (np.ndarray, nd.NDArray)): raise TypeError('y must be ndarray when X is numpy.ndarray') if X.shape[0] != y.shape[0]: raise ValueError("The numbers of data points and labels not equal") if y.ndim == 2 and y.shape[1] == 1: y = y.flatten() if y.ndim != 1: raise ValueError("Label must be 1D or 2D (with 2nd dimension being 1)") if is_train: return io.NDArrayIter(X, y, min(X.shape[0], self.numpy_batch_size), shuffle=is_train, last_batch_handle='roll_over') else: return io.NDArrayIter(X, y, min(X.shape[0], self.numpy_batch_size), shuffle=False) if not isinstance(X, io.DataIter): raise TypeError('X must be DataIter, NDArray or numpy.ndarray') return X
[ "Initialize", "the", "iterator", "given", "input", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L639-L662
[ "def", "_init_iter", "(", "self", ",", "X", ",", "y", ",", "is_train", ")", ":", "if", "isinstance", "(", "X", ",", "(", "np", ".", "ndarray", ",", "nd", ".", "NDArray", ")", ")", ":", "if", "y", "is", "None", ":", "if", "is_train", ":", "raise...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward._init_eval_iter
Initialize the iterator given eval_data.
python/mxnet/model.py
def _init_eval_iter(self, eval_data): """Initialize the iterator given eval_data.""" if eval_data is None: return eval_data if isinstance(eval_data, (tuple, list)) and len(eval_data) == 2: if eval_data[0] is not None: if eval_data[1] is None and isinstance(eval_data[0], io.DataIter): return eval_data[0] input_data = (np.array(eval_data[0]) if isinstance(eval_data[0], list) else eval_data[0]) input_label = (np.array(eval_data[1]) if isinstance(eval_data[1], list) else eval_data[1]) return self._init_iter(input_data, input_label, is_train=True) else: raise ValueError("Eval data is NONE") if not isinstance(eval_data, io.DataIter): raise TypeError('Eval data must be DataIter, or ' \ 'NDArray/numpy.ndarray/list pair (i.e. tuple/list of length 2)') return eval_data
def _init_eval_iter(self, eval_data): """Initialize the iterator given eval_data.""" if eval_data is None: return eval_data if isinstance(eval_data, (tuple, list)) and len(eval_data) == 2: if eval_data[0] is not None: if eval_data[1] is None and isinstance(eval_data[0], io.DataIter): return eval_data[0] input_data = (np.array(eval_data[0]) if isinstance(eval_data[0], list) else eval_data[0]) input_label = (np.array(eval_data[1]) if isinstance(eval_data[1], list) else eval_data[1]) return self._init_iter(input_data, input_label, is_train=True) else: raise ValueError("Eval data is NONE") if not isinstance(eval_data, io.DataIter): raise TypeError('Eval data must be DataIter, or ' \ 'NDArray/numpy.ndarray/list pair (i.e. tuple/list of length 2)') return eval_data
[ "Initialize", "the", "iterator", "given", "eval_data", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L664-L682
[ "def", "_init_eval_iter", "(", "self", ",", "eval_data", ")", ":", "if", "eval_data", "is", "None", ":", "return", "eval_data", "if", "isinstance", "(", "eval_data", ",", "(", "tuple", ",", "list", ")", ")", "and", "len", "(", "eval_data", ")", "==", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward.predict
Run the prediction, always only use one device. Parameters ---------- X : mxnet.DataIter num_batch : int or None The number of batch to run. Go though all batches if ``None``. Returns ------- y : numpy.ndarray or a list of numpy.ndarray if the network has multiple outputs. The predicted value of the output.
python/mxnet/model.py
def predict(self, X, num_batch=None, return_data=False, reset=True): """Run the prediction, always only use one device. Parameters ---------- X : mxnet.DataIter num_batch : int or None The number of batch to run. Go though all batches if ``None``. Returns ------- y : numpy.ndarray or a list of numpy.ndarray if the network has multiple outputs. The predicted value of the output. """ X = self._init_iter(X, None, is_train=False) if reset: X.reset() data_shapes = X.provide_data data_names = [x[0] for x in data_shapes] type_dict = dict((key, value.dtype) for (key, value) in self.arg_params.items()) for x in X.provide_data: if isinstance(x, DataDesc): type_dict[x.name] = x.dtype else: type_dict[x[0]] = mx_real_t self._init_predictor(data_shapes, type_dict) batch_size = X.batch_size data_arrays = [self._pred_exec.arg_dict[name] for name in data_names] output_list = [[] for _ in range(len(self._pred_exec.outputs))] if return_data: data_list = [[] for _ in X.provide_data] label_list = [[] for _ in X.provide_label] i = 0 for batch in X: _load_data(batch, data_arrays) self._pred_exec.forward(is_train=False) padded = batch.pad real_size = batch_size - padded for o_list, o_nd in zip(output_list, self._pred_exec.outputs): o_list.append(o_nd[0:real_size].asnumpy()) if return_data: for j, x in enumerate(batch.data): data_list[j].append(x[0:real_size].asnumpy()) for j, x in enumerate(batch.label): label_list[j].append(x[0:real_size].asnumpy()) i += 1 if num_batch is not None and i == num_batch: break outputs = [np.concatenate(x) for x in output_list] if len(outputs) == 1: outputs = outputs[0] if return_data: data = [np.concatenate(x) for x in data_list] label = [np.concatenate(x) for x in label_list] if len(data) == 1: data = data[0] if len(label) == 1: label = label[0] return outputs, data, label else: return outputs
def predict(self, X, num_batch=None, return_data=False, reset=True): """Run the prediction, always only use one device. Parameters ---------- X : mxnet.DataIter num_batch : int or None The number of batch to run. Go though all batches if ``None``. Returns ------- y : numpy.ndarray or a list of numpy.ndarray if the network has multiple outputs. The predicted value of the output. """ X = self._init_iter(X, None, is_train=False) if reset: X.reset() data_shapes = X.provide_data data_names = [x[0] for x in data_shapes] type_dict = dict((key, value.dtype) for (key, value) in self.arg_params.items()) for x in X.provide_data: if isinstance(x, DataDesc): type_dict[x.name] = x.dtype else: type_dict[x[0]] = mx_real_t self._init_predictor(data_shapes, type_dict) batch_size = X.batch_size data_arrays = [self._pred_exec.arg_dict[name] for name in data_names] output_list = [[] for _ in range(len(self._pred_exec.outputs))] if return_data: data_list = [[] for _ in X.provide_data] label_list = [[] for _ in X.provide_label] i = 0 for batch in X: _load_data(batch, data_arrays) self._pred_exec.forward(is_train=False) padded = batch.pad real_size = batch_size - padded for o_list, o_nd in zip(output_list, self._pred_exec.outputs): o_list.append(o_nd[0:real_size].asnumpy()) if return_data: for j, x in enumerate(batch.data): data_list[j].append(x[0:real_size].asnumpy()) for j, x in enumerate(batch.label): label_list[j].append(x[0:real_size].asnumpy()) i += 1 if num_batch is not None and i == num_batch: break outputs = [np.concatenate(x) for x in output_list] if len(outputs) == 1: outputs = outputs[0] if return_data: data = [np.concatenate(x) for x in data_list] label = [np.concatenate(x) for x in label_list] if len(data) == 1: data = data[0] if len(label) == 1: label = label[0] return outputs, data, label else: return outputs
[ "Run", "the", "prediction", "always", "only", "use", "one", "device", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L684-L751
[ "def", "predict", "(", "self", ",", "X", ",", "num_batch", "=", "None", ",", "return_data", "=", "False", ",", "reset", "=", "True", ")", ":", "X", "=", "self", ".", "_init_iter", "(", "X", ",", "None", ",", "is_train", "=", "False", ")", "if", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward.score
Run the model given an input and calculate the score as assessed by an evaluation metric. Parameters ---------- X : mxnet.DataIter eval_metric : metric.metric The metric for calculating score. num_batch : int or None The number of batches to run. Go though all batches if ``None``. Returns ------- s : float The final score.
python/mxnet/model.py
def score(self, X, eval_metric='acc', num_batch=None, batch_end_callback=None, reset=True): """Run the model given an input and calculate the score as assessed by an evaluation metric. Parameters ---------- X : mxnet.DataIter eval_metric : metric.metric The metric for calculating score. num_batch : int or None The number of batches to run. Go though all batches if ``None``. Returns ------- s : float The final score. """ # setup metric if not isinstance(eval_metric, metric.EvalMetric): eval_metric = metric.create(eval_metric) X = self._init_iter(X, None, is_train=False) if reset: X.reset() data_shapes = X.provide_data data_names = [x[0] for x in data_shapes] type_dict = dict((key, value.dtype) for (key, value) in self.arg_params.items()) for x in X.provide_data: if isinstance(x, DataDesc): type_dict[x.name] = x.dtype else: type_dict[x[0]] = mx_real_t self._init_predictor(data_shapes, type_dict) data_arrays = [self._pred_exec.arg_dict[name] for name in data_names] for i, batch in enumerate(X): if num_batch is not None and i == num_batch: break _load_data(batch, data_arrays) self._pred_exec.forward(is_train=False) eval_metric.update(batch.label, self._pred_exec.outputs) if batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=0, nbatch=i, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(batch_end_callback, batch_end_params) return eval_metric.get()[1]
def score(self, X, eval_metric='acc', num_batch=None, batch_end_callback=None, reset=True): """Run the model given an input and calculate the score as assessed by an evaluation metric. Parameters ---------- X : mxnet.DataIter eval_metric : metric.metric The metric for calculating score. num_batch : int or None The number of batches to run. Go though all batches if ``None``. Returns ------- s : float The final score. """ # setup metric if not isinstance(eval_metric, metric.EvalMetric): eval_metric = metric.create(eval_metric) X = self._init_iter(X, None, is_train=False) if reset: X.reset() data_shapes = X.provide_data data_names = [x[0] for x in data_shapes] type_dict = dict((key, value.dtype) for (key, value) in self.arg_params.items()) for x in X.provide_data: if isinstance(x, DataDesc): type_dict[x.name] = x.dtype else: type_dict[x[0]] = mx_real_t self._init_predictor(data_shapes, type_dict) data_arrays = [self._pred_exec.arg_dict[name] for name in data_names] for i, batch in enumerate(X): if num_batch is not None and i == num_batch: break _load_data(batch, data_arrays) self._pred_exec.forward(is_train=False) eval_metric.update(batch.label, self._pred_exec.outputs) if batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=0, nbatch=i, eval_metric=eval_metric, locals=locals()) _multiple_callbacks(batch_end_callback, batch_end_params) return eval_metric.get()[1]
[ "Run", "the", "model", "given", "an", "input", "and", "calculate", "the", "score", "as", "assessed", "by", "an", "evaluation", "metric", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L753-L802
[ "def", "score", "(", "self", ",", "X", ",", "eval_metric", "=", "'acc'", ",", "num_batch", "=", "None", ",", "batch_end_callback", "=", "None", ",", "reset", "=", "True", ")", ":", "# setup metric", "if", "not", "isinstance", "(", "eval_metric", ",", "me...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward.fit
Fit the model. Parameters ---------- X : DataIter, or numpy.ndarray/NDArray Training data. If `X` is a `DataIter`, the name or (if name not available) the position of its outputs should match the corresponding variable names defined in the symbolic graph. y : numpy.ndarray/NDArray, optional Training set label. If X is ``numpy.ndarray`` or `NDArray`, `y` is required to be set. While y can be 1D or 2D (with 2nd dimension as 1), its first dimension must be the same as `X`, i.e. the number of data points and labels should be equal. eval_data : DataIter or numpy.ndarray/list/NDArray pair If eval_data is numpy.ndarray/list/NDArray pair, it should be ``(valid_data, valid_label)``. eval_metric : metric.EvalMetric or str or callable The evaluation metric. This could be the name of evaluation metric or a custom evaluation function that returns statistics based on a minibatch. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback: callable(epoch) A callback that is invoked at end of each batch for purposes of printing. kvstore: KVStore or str, optional The KVStore or a string kvstore type: 'local', 'dist_sync', 'dist_async' In default uses 'local', often no need to change for single machiine. logger : logging logger, optional When not specified, default logger will be used. work_load_list : float or int, optional The list of work load for different devices, in the same order as `ctx`. Note ---- KVStore behavior - 'local', multi-devices on a single machine, will automatically choose best type. - 'dist_sync', multiple machines communicating via BSP. - 'dist_async', multiple machines with asynchronous communication.
python/mxnet/model.py
def fit(self, X, y=None, eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', logger=None, work_load_list=None, monitor=None, eval_end_callback=LogValidationMetricsCallback(), eval_batch_end_callback=None): """Fit the model. Parameters ---------- X : DataIter, or numpy.ndarray/NDArray Training data. If `X` is a `DataIter`, the name or (if name not available) the position of its outputs should match the corresponding variable names defined in the symbolic graph. y : numpy.ndarray/NDArray, optional Training set label. If X is ``numpy.ndarray`` or `NDArray`, `y` is required to be set. While y can be 1D or 2D (with 2nd dimension as 1), its first dimension must be the same as `X`, i.e. the number of data points and labels should be equal. eval_data : DataIter or numpy.ndarray/list/NDArray pair If eval_data is numpy.ndarray/list/NDArray pair, it should be ``(valid_data, valid_label)``. eval_metric : metric.EvalMetric or str or callable The evaluation metric. This could be the name of evaluation metric or a custom evaluation function that returns statistics based on a minibatch. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback: callable(epoch) A callback that is invoked at end of each batch for purposes of printing. kvstore: KVStore or str, optional The KVStore or a string kvstore type: 'local', 'dist_sync', 'dist_async' In default uses 'local', often no need to change for single machiine. logger : logging logger, optional When not specified, default logger will be used. work_load_list : float or int, optional The list of work load for different devices, in the same order as `ctx`. Note ---- KVStore behavior - 'local', multi-devices on a single machine, will automatically choose best type. - 'dist_sync', multiple machines communicating via BSP. - 'dist_async', multiple machines with asynchronous communication. """ data = self._init_iter(X, y, is_train=True) eval_data = self._init_eval_iter(eval_data) if self.sym_gen: self.symbol = self.sym_gen(data.default_bucket_key) # pylint: disable=no-member self._check_arguments() self.kwargs["sym"] = self.symbol arg_names, param_names, aux_names = \ self._init_params(data.provide_data+data.provide_label) # setup metric if not isinstance(eval_metric, metric.EvalMetric): eval_metric = metric.create(eval_metric) # create kvstore (kvstore, update_on_kvstore) = _create_kvstore( kvstore, len(self.ctx), self.arg_params) param_idx2name = {} if update_on_kvstore: param_idx2name.update(enumerate(param_names)) else: for i, n in enumerate(param_names): for k in range(len(self.ctx)): param_idx2name[i*len(self.ctx)+k] = n self.kwargs["param_idx2name"] = param_idx2name # init optmizer if isinstance(self.optimizer, str): batch_size = data.batch_size if kvstore and 'dist' in kvstore.type and '_async' not in kvstore.type: batch_size *= kvstore.num_workers optimizer = opt.create(self.optimizer, rescale_grad=(1.0/batch_size), **(self.kwargs)) elif isinstance(self.optimizer, opt.Optimizer): if not optimizer.idx2name: optimizer.idx2name = param_idx2name.copy() optimizer = self.optimizer # do training _train_multi_device(self.symbol, self.ctx, arg_names, param_names, aux_names, self.arg_params, self.aux_params, begin_epoch=self.begin_epoch, end_epoch=self.num_epoch, epoch_size=self.epoch_size, optimizer=optimizer, train_data=data, eval_data=eval_data, eval_metric=eval_metric, epoch_end_callback=epoch_end_callback, batch_end_callback=batch_end_callback, kvstore=kvstore, update_on_kvstore=update_on_kvstore, logger=logger, work_load_list=work_load_list, monitor=monitor, eval_end_callback=eval_end_callback, eval_batch_end_callback=eval_batch_end_callback, sym_gen=self.sym_gen)
def fit(self, X, y=None, eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', logger=None, work_load_list=None, monitor=None, eval_end_callback=LogValidationMetricsCallback(), eval_batch_end_callback=None): """Fit the model. Parameters ---------- X : DataIter, or numpy.ndarray/NDArray Training data. If `X` is a `DataIter`, the name or (if name not available) the position of its outputs should match the corresponding variable names defined in the symbolic graph. y : numpy.ndarray/NDArray, optional Training set label. If X is ``numpy.ndarray`` or `NDArray`, `y` is required to be set. While y can be 1D or 2D (with 2nd dimension as 1), its first dimension must be the same as `X`, i.e. the number of data points and labels should be equal. eval_data : DataIter or numpy.ndarray/list/NDArray pair If eval_data is numpy.ndarray/list/NDArray pair, it should be ``(valid_data, valid_label)``. eval_metric : metric.EvalMetric or str or callable The evaluation metric. This could be the name of evaluation metric or a custom evaluation function that returns statistics based on a minibatch. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback: callable(epoch) A callback that is invoked at end of each batch for purposes of printing. kvstore: KVStore or str, optional The KVStore or a string kvstore type: 'local', 'dist_sync', 'dist_async' In default uses 'local', often no need to change for single machiine. logger : logging logger, optional When not specified, default logger will be used. work_load_list : float or int, optional The list of work load for different devices, in the same order as `ctx`. Note ---- KVStore behavior - 'local', multi-devices on a single machine, will automatically choose best type. - 'dist_sync', multiple machines communicating via BSP. - 'dist_async', multiple machines with asynchronous communication. """ data = self._init_iter(X, y, is_train=True) eval_data = self._init_eval_iter(eval_data) if self.sym_gen: self.symbol = self.sym_gen(data.default_bucket_key) # pylint: disable=no-member self._check_arguments() self.kwargs["sym"] = self.symbol arg_names, param_names, aux_names = \ self._init_params(data.provide_data+data.provide_label) # setup metric if not isinstance(eval_metric, metric.EvalMetric): eval_metric = metric.create(eval_metric) # create kvstore (kvstore, update_on_kvstore) = _create_kvstore( kvstore, len(self.ctx), self.arg_params) param_idx2name = {} if update_on_kvstore: param_idx2name.update(enumerate(param_names)) else: for i, n in enumerate(param_names): for k in range(len(self.ctx)): param_idx2name[i*len(self.ctx)+k] = n self.kwargs["param_idx2name"] = param_idx2name # init optmizer if isinstance(self.optimizer, str): batch_size = data.batch_size if kvstore and 'dist' in kvstore.type and '_async' not in kvstore.type: batch_size *= kvstore.num_workers optimizer = opt.create(self.optimizer, rescale_grad=(1.0/batch_size), **(self.kwargs)) elif isinstance(self.optimizer, opt.Optimizer): if not optimizer.idx2name: optimizer.idx2name = param_idx2name.copy() optimizer = self.optimizer # do training _train_multi_device(self.symbol, self.ctx, arg_names, param_names, aux_names, self.arg_params, self.aux_params, begin_epoch=self.begin_epoch, end_epoch=self.num_epoch, epoch_size=self.epoch_size, optimizer=optimizer, train_data=data, eval_data=eval_data, eval_metric=eval_metric, epoch_end_callback=epoch_end_callback, batch_end_callback=batch_end_callback, kvstore=kvstore, update_on_kvstore=update_on_kvstore, logger=logger, work_load_list=work_load_list, monitor=monitor, eval_end_callback=eval_end_callback, eval_batch_end_callback=eval_batch_end_callback, sym_gen=self.sym_gen)
[ "Fit", "the", "model", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L804-L905
[ "def", "fit", "(", "self", ",", "X", ",", "y", "=", "None", ",", "eval_data", "=", "None", ",", "eval_metric", "=", "'acc'", ",", "epoch_end_callback", "=", "None", ",", "batch_end_callback", "=", "None", ",", "kvstore", "=", "'local'", ",", "logger", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward.save
Checkpoint the model checkpoint into file. You can also use `pickle` to do the job if you only work on Python. The advantage of `load` and `save` (as compared to `pickle`) is that the resulting file can be loaded from other MXNet language bindings. One can also directly `load`/`save` from/to cloud storage(S3, HDFS) Parameters ---------- prefix : str Prefix of model name. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters.
python/mxnet/model.py
def save(self, prefix, epoch=None): """Checkpoint the model checkpoint into file. You can also use `pickle` to do the job if you only work on Python. The advantage of `load` and `save` (as compared to `pickle`) is that the resulting file can be loaded from other MXNet language bindings. One can also directly `load`/`save` from/to cloud storage(S3, HDFS) Parameters ---------- prefix : str Prefix of model name. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ if epoch is None: epoch = self.num_epoch assert epoch is not None save_checkpoint(prefix, epoch, self.symbol, self.arg_params, self.aux_params)
def save(self, prefix, epoch=None): """Checkpoint the model checkpoint into file. You can also use `pickle` to do the job if you only work on Python. The advantage of `load` and `save` (as compared to `pickle`) is that the resulting file can be loaded from other MXNet language bindings. One can also directly `load`/`save` from/to cloud storage(S3, HDFS) Parameters ---------- prefix : str Prefix of model name. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ if epoch is None: epoch = self.num_epoch assert epoch is not None save_checkpoint(prefix, epoch, self.symbol, self.arg_params, self.aux_params)
[ "Checkpoint", "the", "model", "checkpoint", "into", "file", ".", "You", "can", "also", "use", "pickle", "to", "do", "the", "job", "if", "you", "only", "work", "on", "Python", ".", "The", "advantage", "of", "load", "and", "save", "(", "as", "compared", ...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L908-L928
[ "def", "save", "(", "self", ",", "prefix", ",", "epoch", "=", "None", ")", ":", "if", "epoch", "is", "None", ":", "epoch", "=", "self", ".", "num_epoch", "assert", "epoch", "is", "not", "None", "save_checkpoint", "(", "prefix", ",", "epoch", ",", "se...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward.load
Load model checkpoint from file. Parameters ---------- prefix : str Prefix of model name. epoch : int epoch number of model we would like to load. ctx : Context or list of Context, optional The device context of training and prediction. kwargs : dict Other parameters for model, including `num_epoch`, optimizer and `numpy_batch_size`. Returns ------- model : FeedForward The loaded model that can be used for prediction. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters.
python/mxnet/model.py
def load(prefix, epoch, ctx=None, **kwargs): """Load model checkpoint from file. Parameters ---------- prefix : str Prefix of model name. epoch : int epoch number of model we would like to load. ctx : Context or list of Context, optional The device context of training and prediction. kwargs : dict Other parameters for model, including `num_epoch`, optimizer and `numpy_batch_size`. Returns ------- model : FeedForward The loaded model that can be used for prediction. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ symbol, arg_params, aux_params = load_checkpoint(prefix, epoch) return FeedForward(symbol, ctx=ctx, arg_params=arg_params, aux_params=aux_params, begin_epoch=epoch, **kwargs)
def load(prefix, epoch, ctx=None, **kwargs): """Load model checkpoint from file. Parameters ---------- prefix : str Prefix of model name. epoch : int epoch number of model we would like to load. ctx : Context or list of Context, optional The device context of training and prediction. kwargs : dict Other parameters for model, including `num_epoch`, optimizer and `numpy_batch_size`. Returns ------- model : FeedForward The loaded model that can be used for prediction. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ symbol, arg_params, aux_params = load_checkpoint(prefix, epoch) return FeedForward(symbol, ctx=ctx, arg_params=arg_params, aux_params=aux_params, begin_epoch=epoch, **kwargs)
[ "Load", "model", "checkpoint", "from", "file", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L931-L959
[ "def", "load", "(", "prefix", ",", "epoch", ",", "ctx", "=", "None", ",", "*", "*", "kwargs", ")", ":", "symbol", ",", "arg_params", ",", "aux_params", "=", "load_checkpoint", "(", "prefix", ",", "epoch", ")", "return", "FeedForward", "(", "symbol", ",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
FeedForward.create
Functional style to create a model. This function is more consistent with functional languages such as R, where mutation is not allowed. Parameters ---------- symbol : Symbol The symbol configuration of a computation network. X : DataIter Training data. y : numpy.ndarray, optional If `X` is a ``numpy.ndarray``, `y` must be set. ctx : Context or list of Context, optional The device context of training and prediction. To use multi-GPU training, pass in a list of GPU contexts. num_epoch : int, optional The number of training epochs(epochs). epoch_size : int, optional Number of batches in a epoch. In default, it is set to ``ceil(num_train_examples / batch_size)``. optimizer : str or Optimizer, optional The name of the chosen optimizer, or an optimizer object, used for training. initializer : initializer function, optional The initialization scheme used. eval_data : DataIter or numpy.ndarray pair If `eval_set` is ``numpy.ndarray`` pair, it should be (`valid_data`, `valid_label`). eval_metric : metric.EvalMetric or str or callable The evaluation metric. Can be the name of an evaluation metric or a custom evaluation function that returns statistics based on a minibatch. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback: callable(epoch) A callback that is invoked at end of each batch for print purposes. kvstore: KVStore or str, optional The KVStore or a string kvstore type: 'local', 'dist_sync', 'dis_async'. Defaults to 'local', often no need to change for single machine. logger : logging logger, optional When not specified, default logger will be used. work_load_list : list of float or int, optional The list of work load for different devices, in the same order as `ctx`.
python/mxnet/model.py
def create(symbol, X, y=None, ctx=None, num_epoch=None, epoch_size=None, optimizer='sgd', initializer=Uniform(0.01), eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', logger=None, work_load_list=None, eval_end_callback=LogValidationMetricsCallback(), eval_batch_end_callback=None, **kwargs): """Functional style to create a model. This function is more consistent with functional languages such as R, where mutation is not allowed. Parameters ---------- symbol : Symbol The symbol configuration of a computation network. X : DataIter Training data. y : numpy.ndarray, optional If `X` is a ``numpy.ndarray``, `y` must be set. ctx : Context or list of Context, optional The device context of training and prediction. To use multi-GPU training, pass in a list of GPU contexts. num_epoch : int, optional The number of training epochs(epochs). epoch_size : int, optional Number of batches in a epoch. In default, it is set to ``ceil(num_train_examples / batch_size)``. optimizer : str or Optimizer, optional The name of the chosen optimizer, or an optimizer object, used for training. initializer : initializer function, optional The initialization scheme used. eval_data : DataIter or numpy.ndarray pair If `eval_set` is ``numpy.ndarray`` pair, it should be (`valid_data`, `valid_label`). eval_metric : metric.EvalMetric or str or callable The evaluation metric. Can be the name of an evaluation metric or a custom evaluation function that returns statistics based on a minibatch. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback: callable(epoch) A callback that is invoked at end of each batch for print purposes. kvstore: KVStore or str, optional The KVStore or a string kvstore type: 'local', 'dist_sync', 'dis_async'. Defaults to 'local', often no need to change for single machine. logger : logging logger, optional When not specified, default logger will be used. work_load_list : list of float or int, optional The list of work load for different devices, in the same order as `ctx`. """ model = FeedForward(symbol, ctx=ctx, num_epoch=num_epoch, epoch_size=epoch_size, optimizer=optimizer, initializer=initializer, **kwargs) model.fit(X, y, eval_data=eval_data, eval_metric=eval_metric, epoch_end_callback=epoch_end_callback, batch_end_callback=batch_end_callback, kvstore=kvstore, logger=logger, work_load_list=work_load_list, eval_end_callback=eval_end_callback, eval_batch_end_callback=eval_batch_end_callback) return model
def create(symbol, X, y=None, ctx=None, num_epoch=None, epoch_size=None, optimizer='sgd', initializer=Uniform(0.01), eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', logger=None, work_load_list=None, eval_end_callback=LogValidationMetricsCallback(), eval_batch_end_callback=None, **kwargs): """Functional style to create a model. This function is more consistent with functional languages such as R, where mutation is not allowed. Parameters ---------- symbol : Symbol The symbol configuration of a computation network. X : DataIter Training data. y : numpy.ndarray, optional If `X` is a ``numpy.ndarray``, `y` must be set. ctx : Context or list of Context, optional The device context of training and prediction. To use multi-GPU training, pass in a list of GPU contexts. num_epoch : int, optional The number of training epochs(epochs). epoch_size : int, optional Number of batches in a epoch. In default, it is set to ``ceil(num_train_examples / batch_size)``. optimizer : str or Optimizer, optional The name of the chosen optimizer, or an optimizer object, used for training. initializer : initializer function, optional The initialization scheme used. eval_data : DataIter or numpy.ndarray pair If `eval_set` is ``numpy.ndarray`` pair, it should be (`valid_data`, `valid_label`). eval_metric : metric.EvalMetric or str or callable The evaluation metric. Can be the name of an evaluation metric or a custom evaluation function that returns statistics based on a minibatch. epoch_end_callback : callable(epoch, symbol, arg_params, aux_states) A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch. batch_end_callback: callable(epoch) A callback that is invoked at end of each batch for print purposes. kvstore: KVStore or str, optional The KVStore or a string kvstore type: 'local', 'dist_sync', 'dis_async'. Defaults to 'local', often no need to change for single machine. logger : logging logger, optional When not specified, default logger will be used. work_load_list : list of float or int, optional The list of work load for different devices, in the same order as `ctx`. """ model = FeedForward(symbol, ctx=ctx, num_epoch=num_epoch, epoch_size=epoch_size, optimizer=optimizer, initializer=initializer, **kwargs) model.fit(X, y, eval_data=eval_data, eval_metric=eval_metric, epoch_end_callback=epoch_end_callback, batch_end_callback=batch_end_callback, kvstore=kvstore, logger=logger, work_load_list=work_load_list, eval_end_callback=eval_end_callback, eval_batch_end_callback=eval_batch_end_callback) return model
[ "Functional", "style", "to", "create", "a", "model", ".", "This", "function", "is", "more", "consistent", "with", "functional", "languages", "such", "as", "R", "where", "mutation", "is", "not", "allowed", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/model.py#L962-L1025
[ "def", "create", "(", "symbol", ",", "X", ",", "y", "=", "None", ",", "ctx", "=", "None", ",", "num_epoch", "=", "None", ",", "epoch_size", "=", "None", ",", "optimizer", "=", "'sgd'", ",", "initializer", "=", "Uniform", "(", "0.01", ")", ",", "eva...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
build_save_containers
Entry point to build and upload all built dockerimages in parallel :param platforms: List of platforms :param registry: Docker registry name :param load_cache: Load cache before building :return: 1 if error occurred, 0 otherwise
ci/docker_cache.py
def build_save_containers(platforms, registry, load_cache) -> int: """ Entry point to build and upload all built dockerimages in parallel :param platforms: List of platforms :param registry: Docker registry name :param load_cache: Load cache before building :return: 1 if error occurred, 0 otherwise """ from joblib import Parallel, delayed if len(platforms) == 0: return 0 platform_results = Parallel(n_jobs=PARALLEL_BUILDS, backend="multiprocessing")( delayed(_build_save_container)(platform, registry, load_cache) for platform in platforms) is_error = False for platform_result in platform_results: if platform_result is not None: logging.error('Failed to generate %s', platform_result) is_error = True return 1 if is_error else 0
def build_save_containers(platforms, registry, load_cache) -> int: """ Entry point to build and upload all built dockerimages in parallel :param platforms: List of platforms :param registry: Docker registry name :param load_cache: Load cache before building :return: 1 if error occurred, 0 otherwise """ from joblib import Parallel, delayed if len(platforms) == 0: return 0 platform_results = Parallel(n_jobs=PARALLEL_BUILDS, backend="multiprocessing")( delayed(_build_save_container)(platform, registry, load_cache) for platform in platforms) is_error = False for platform_result in platform_results: if platform_result is not None: logging.error('Failed to generate %s', platform_result) is_error = True return 1 if is_error else 0
[ "Entry", "point", "to", "build", "and", "upload", "all", "built", "dockerimages", "in", "parallel", ":", "param", "platforms", ":", "List", "of", "platforms", ":", "param", "registry", ":", "Docker", "registry", "name", ":", "param", "load_cache", ":", "Load...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L44-L66
[ "def", "build_save_containers", "(", "platforms", ",", "registry", ",", "load_cache", ")", "->", "int", ":", "from", "joblib", "import", "Parallel", ",", "delayed", "if", "len", "(", "platforms", ")", "==", "0", ":", "return", "0", "platform_results", "=", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_build_save_container
Build image for passed platform and upload the cache to the specified S3 bucket :param platform: Platform :param registry: Docker registry name :param load_cache: Load cache before building :return: Platform if failed, None otherwise
ci/docker_cache.py
def _build_save_container(platform, registry, load_cache) -> Optional[str]: """ Build image for passed platform and upload the cache to the specified S3 bucket :param platform: Platform :param registry: Docker registry name :param load_cache: Load cache before building :return: Platform if failed, None otherwise """ docker_tag = build_util.get_docker_tag(platform=platform, registry=registry) # Preload cache if load_cache: load_docker_cache(registry=registry, docker_tag=docker_tag) # Start building logging.debug('Building %s as %s', platform, docker_tag) try: # Increase the number of retries for building the cache. image_id = build_util.build_docker(docker_binary='docker', platform=platform, registry=registry, num_retries=10, no_cache=False) logging.info('Built %s as %s', docker_tag, image_id) # Push cache to registry _upload_image(registry=registry, docker_tag=docker_tag, image_id=image_id) return None except Exception: logging.exception('Unexpected exception during build of %s', docker_tag) return platform
def _build_save_container(platform, registry, load_cache) -> Optional[str]: """ Build image for passed platform and upload the cache to the specified S3 bucket :param platform: Platform :param registry: Docker registry name :param load_cache: Load cache before building :return: Platform if failed, None otherwise """ docker_tag = build_util.get_docker_tag(platform=platform, registry=registry) # Preload cache if load_cache: load_docker_cache(registry=registry, docker_tag=docker_tag) # Start building logging.debug('Building %s as %s', platform, docker_tag) try: # Increase the number of retries for building the cache. image_id = build_util.build_docker(docker_binary='docker', platform=platform, registry=registry, num_retries=10, no_cache=False) logging.info('Built %s as %s', docker_tag, image_id) # Push cache to registry _upload_image(registry=registry, docker_tag=docker_tag, image_id=image_id) return None except Exception: logging.exception('Unexpected exception during build of %s', docker_tag) return platform
[ "Build", "image", "for", "passed", "platform", "and", "upload", "the", "cache", "to", "the", "specified", "S3", "bucket", ":", "param", "platform", ":", "Platform", ":", "param", "registry", ":", "Docker", "registry", "name", ":", "param", "load_cache", ":",...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L69-L95
[ "def", "_build_save_container", "(", "platform", ",", "registry", ",", "load_cache", ")", "->", "Optional", "[", "str", "]", ":", "docker_tag", "=", "build_util", ".", "get_docker_tag", "(", "platform", "=", "platform", ",", "registry", "=", "registry", ")", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_upload_image
Upload the passed image by id, tag it with docker tag and upload to S3 bucket :param registry: Docker registry name :param docker_tag: Docker tag :param image_id: Image id :return: None
ci/docker_cache.py
def _upload_image(registry, docker_tag, image_id) -> None: """ Upload the passed image by id, tag it with docker tag and upload to S3 bucket :param registry: Docker registry name :param docker_tag: Docker tag :param image_id: Image id :return: None """ # We don't have to retag the image since it is already in the right format logging.info('Uploading %s (%s) to %s', docker_tag, image_id, registry) push_cmd = ['docker', 'push', docker_tag] subprocess.check_call(push_cmd)
def _upload_image(registry, docker_tag, image_id) -> None: """ Upload the passed image by id, tag it with docker tag and upload to S3 bucket :param registry: Docker registry name :param docker_tag: Docker tag :param image_id: Image id :return: None """ # We don't have to retag the image since it is already in the right format logging.info('Uploading %s (%s) to %s', docker_tag, image_id, registry) push_cmd = ['docker', 'push', docker_tag] subprocess.check_call(push_cmd)
[ "Upload", "the", "passed", "image", "by", "id", "tag", "it", "with", "docker", "tag", "and", "upload", "to", "S3", "bucket", ":", "param", "registry", ":", "Docker", "registry", "name", ":", "param", "docker_tag", ":", "Docker", "tag", ":", "param", "ima...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L100-L111
[ "def", "_upload_image", "(", "registry", ",", "docker_tag", ",", "image_id", ")", "->", "None", ":", "# We don't have to retag the image since it is already in the right format", "logging", ".", "info", "(", "'Uploading %s (%s) to %s'", ",", "docker_tag", ",", "image_id", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_login_dockerhub
Login to the Docker Hub account :return: None
ci/docker_cache.py
def _login_dockerhub(): """ Login to the Docker Hub account :return: None """ dockerhub_credentials = _get_dockerhub_credentials() logging.info('Logging in to DockerHub') # We use password-stdin instead of --password to avoid leaking passwords in case of an error. # This method will produce the following output: # > WARNING! Your password will be stored unencrypted in /home/jenkins_slave/.docker/config.json. # > Configure a credential helper to remove this warning. See # > https://docs.docker.com/engine/reference/commandline/login/#credentials-store # Since we consider the restricted slaves a secure environment, that's fine. Also, using this will require # third party applications which would need a review first as well. p = subprocess.run(['docker', 'login', '--username', dockerhub_credentials['username'], '--password-stdin'], stdout=subprocess.PIPE, input=str.encode(dockerhub_credentials['password'])) logging.info(p.stdout) logging.info('Successfully logged in to DockerHub')
def _login_dockerhub(): """ Login to the Docker Hub account :return: None """ dockerhub_credentials = _get_dockerhub_credentials() logging.info('Logging in to DockerHub') # We use password-stdin instead of --password to avoid leaking passwords in case of an error. # This method will produce the following output: # > WARNING! Your password will be stored unencrypted in /home/jenkins_slave/.docker/config.json. # > Configure a credential helper to remove this warning. See # > https://docs.docker.com/engine/reference/commandline/login/#credentials-store # Since we consider the restricted slaves a secure environment, that's fine. Also, using this will require # third party applications which would need a review first as well. p = subprocess.run(['docker', 'login', '--username', dockerhub_credentials['username'], '--password-stdin'], stdout=subprocess.PIPE, input=str.encode(dockerhub_credentials['password'])) logging.info(p.stdout) logging.info('Successfully logged in to DockerHub')
[ "Login", "to", "the", "Docker", "Hub", "account", ":", "return", ":", "None" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L116-L134
[ "def", "_login_dockerhub", "(", ")", ":", "dockerhub_credentials", "=", "_get_dockerhub_credentials", "(", ")", "logging", ".", "info", "(", "'Logging in to DockerHub'", ")", "# We use password-stdin instead of --password to avoid leaking passwords in case of an error.", "# This me...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
load_docker_cache
Load the precompiled docker cache from the registry :param registry: Docker registry name :param docker_tag: Docker tag to load :return: None
ci/docker_cache.py
def load_docker_cache(registry, docker_tag) -> None: """ Load the precompiled docker cache from the registry :param registry: Docker registry name :param docker_tag: Docker tag to load :return: None """ # We don't have to retag the image since it's already in the right format if not registry: return assert docker_tag logging.info('Loading Docker cache for %s from %s', docker_tag, registry) pull_cmd = ['docker', 'pull', docker_tag] # Don't throw an error if the image does not exist subprocess.run(pull_cmd, timeout=DOCKER_CACHE_TIMEOUT_MINS*60) logging.info('Successfully pulled docker cache')
def load_docker_cache(registry, docker_tag) -> None: """ Load the precompiled docker cache from the registry :param registry: Docker registry name :param docker_tag: Docker tag to load :return: None """ # We don't have to retag the image since it's already in the right format if not registry: return assert docker_tag logging.info('Loading Docker cache for %s from %s', docker_tag, registry) pull_cmd = ['docker', 'pull', docker_tag] # Don't throw an error if the image does not exist subprocess.run(pull_cmd, timeout=DOCKER_CACHE_TIMEOUT_MINS*60) logging.info('Successfully pulled docker cache')
[ "Load", "the", "precompiled", "docker", "cache", "from", "the", "registry", ":", "param", "registry", ":", "Docker", "registry", "name", ":", "param", "docker_tag", ":", "Docker", "tag", "to", "load", ":", "return", ":", "None" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L149-L166
[ "def", "load_docker_cache", "(", "registry", ",", "docker_tag", ")", "->", "None", ":", "# We don't have to retag the image since it's already in the right format", "if", "not", "registry", ":", "return", "assert", "docker_tag", "logging", ".", "info", "(", "'Loading Dock...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
delete_local_docker_cache
Delete the local docker cache for the entire docker image chain :param docker_tag: Docker tag :return: None
ci/docker_cache.py
def delete_local_docker_cache(docker_tag): """ Delete the local docker cache for the entire docker image chain :param docker_tag: Docker tag :return: None """ history_cmd = ['docker', 'history', '-q', docker_tag] try: image_ids_b = subprocess.check_output(history_cmd) image_ids_str = image_ids_b.decode('utf-8').strip() layer_ids = [id.strip() for id in image_ids_str.split('\n') if id != '<missing>'] delete_cmd = ['docker', 'image', 'rm', '--force'] delete_cmd.extend(layer_ids) subprocess.check_call(delete_cmd) except subprocess.CalledProcessError as error: # Could be caused by the image not being present logging.debug('Error during local cache deletion %s', error)
def delete_local_docker_cache(docker_tag): """ Delete the local docker cache for the entire docker image chain :param docker_tag: Docker tag :return: None """ history_cmd = ['docker', 'history', '-q', docker_tag] try: image_ids_b = subprocess.check_output(history_cmd) image_ids_str = image_ids_b.decode('utf-8').strip() layer_ids = [id.strip() for id in image_ids_str.split('\n') if id != '<missing>'] delete_cmd = ['docker', 'image', 'rm', '--force'] delete_cmd.extend(layer_ids) subprocess.check_call(delete_cmd) except subprocess.CalledProcessError as error: # Could be caused by the image not being present logging.debug('Error during local cache deletion %s', error)
[ "Delete", "the", "local", "docker", "cache", "for", "the", "entire", "docker", "image", "chain", ":", "param", "docker_tag", ":", "Docker", "tag", ":", "return", ":", "None" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L169-L187
[ "def", "delete_local_docker_cache", "(", "docker_tag", ")", ":", "history_cmd", "=", "[", "'docker'", ",", "'history'", ",", "'-q'", ",", "docker_tag", "]", "try", ":", "image_ids_b", "=", "subprocess", ".", "check_output", "(", "history_cmd", ")", "image_ids_st...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
main
Utility to create and publish the Docker cache to Docker Hub :return:
ci/docker_cache.py
def main() -> int: """ Utility to create and publish the Docker cache to Docker Hub :return: """ # We need to be in the same directory than the script so the commands in the dockerfiles work as # expected. But the script can be invoked from a different path base = os.path.split(os.path.realpath(__file__))[0] os.chdir(base) logging.getLogger().setLevel(logging.DEBUG) logging.getLogger('botocore').setLevel(logging.INFO) logging.getLogger('boto3').setLevel(logging.INFO) logging.getLogger('urllib3').setLevel(logging.INFO) logging.getLogger('s3transfer').setLevel(logging.INFO) def script_name() -> str: return os.path.split(sys.argv[0])[1] logging.basicConfig(format='{}: %(asctime)-15s %(message)s'.format(script_name())) parser = argparse.ArgumentParser(description="Utility for preserving and loading Docker cache", epilog="") parser.add_argument("--docker-registry", help="Docker hub registry name", type=str, required=True) args = parser.parse_args() platforms = build_util.get_platforms() try: _login_dockerhub() return build_save_containers(platforms=platforms, registry=args.docker_registry, load_cache=True) finally: _logout_dockerhub()
def main() -> int: """ Utility to create and publish the Docker cache to Docker Hub :return: """ # We need to be in the same directory than the script so the commands in the dockerfiles work as # expected. But the script can be invoked from a different path base = os.path.split(os.path.realpath(__file__))[0] os.chdir(base) logging.getLogger().setLevel(logging.DEBUG) logging.getLogger('botocore').setLevel(logging.INFO) logging.getLogger('boto3').setLevel(logging.INFO) logging.getLogger('urllib3').setLevel(logging.INFO) logging.getLogger('s3transfer').setLevel(logging.INFO) def script_name() -> str: return os.path.split(sys.argv[0])[1] logging.basicConfig(format='{}: %(asctime)-15s %(message)s'.format(script_name())) parser = argparse.ArgumentParser(description="Utility for preserving and loading Docker cache", epilog="") parser.add_argument("--docker-registry", help="Docker hub registry name", type=str, required=True) args = parser.parse_args() platforms = build_util.get_platforms() try: _login_dockerhub() return build_save_containers(platforms=platforms, registry=args.docker_registry, load_cache=True) finally: _logout_dockerhub()
[ "Utility", "to", "create", "and", "publish", "the", "Docker", "cache", "to", "Docker", "Hub", ":", "return", ":" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker_cache.py#L221-L255
[ "def", "main", "(", ")", "->", "int", ":", "# We need to be in the same directory than the script so the commands in the dockerfiles work as", "# expected. But the script can be invoked from a different path", "base", "=", "os", ".", "path", ".", "split", "(", "os", ".", "path"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_chinese_text
Download the chinese_text dataset and unzip it
example/cnn_chinese_text_classification/data_helpers.py
def get_chinese_text(): """Download the chinese_text dataset and unzip it""" if not os.path.isdir("data/"): os.system("mkdir data/") if (not os.path.exists('data/pos.txt')) or \ (not os.path.exists('data/neg')): os.system("wget -q https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/chinese_text.zip " "-P data/") os.chdir("./data") os.system("unzip -u chinese_text.zip") os.chdir("..")
def get_chinese_text(): """Download the chinese_text dataset and unzip it""" if not os.path.isdir("data/"): os.system("mkdir data/") if (not os.path.exists('data/pos.txt')) or \ (not os.path.exists('data/neg')): os.system("wget -q https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/chinese_text.zip " "-P data/") os.chdir("./data") os.system("unzip -u chinese_text.zip") os.chdir("..")
[ "Download", "the", "chinese_text", "dataset", "and", "unzip", "it" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/cnn_chinese_text_classification/data_helpers.py#L51-L61
[ "def", "get_chinese_text", "(", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "\"data/\"", ")", ":", "os", ".", "system", "(", "\"mkdir data/\"", ")", "if", "(", "not", "os", ".", "path", ".", "exists", "(", "'data/pos.txt'", ")", ")",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
load_data_and_labels
Loads MR polarity data from files, splits the data into words and generates labels. Returns split sentences and labels.
example/cnn_chinese_text_classification/data_helpers.py
def load_data_and_labels(): """Loads MR polarity data from files, splits the data into words and generates labels. Returns split sentences and labels. """ # download dataset get_chinese_text() # Load data from files positive_examples = list(codecs.open("./data/pos.txt", "r", "utf-8").readlines()) positive_examples = [s.strip() for s in positive_examples] positive_examples = [pe for pe in positive_examples if len(pe) < 100] negative_examples = list(codecs.open("./data/neg.txt", "r", "utf-8").readlines()) negative_examples = [s.strip() for s in negative_examples] negative_examples = [ne for ne in negative_examples if len(ne) < 100] # Split by words x_text = positive_examples + negative_examples # x_text = [clean_str(sent) for sent in x_text] x_text = [list(s) for s in x_text] # Generate labels positive_labels = [[0, 1] for _ in positive_examples] negative_labels = [[1, 0] for _ in negative_examples] y = np.concatenate([positive_labels, negative_labels], 0) return [x_text, y]
def load_data_and_labels(): """Loads MR polarity data from files, splits the data into words and generates labels. Returns split sentences and labels. """ # download dataset get_chinese_text() # Load data from files positive_examples = list(codecs.open("./data/pos.txt", "r", "utf-8").readlines()) positive_examples = [s.strip() for s in positive_examples] positive_examples = [pe for pe in positive_examples if len(pe) < 100] negative_examples = list(codecs.open("./data/neg.txt", "r", "utf-8").readlines()) negative_examples = [s.strip() for s in negative_examples] negative_examples = [ne for ne in negative_examples if len(ne) < 100] # Split by words x_text = positive_examples + negative_examples # x_text = [clean_str(sent) for sent in x_text] x_text = [list(s) for s in x_text] # Generate labels positive_labels = [[0, 1] for _ in positive_examples] negative_labels = [[1, 0] for _ in negative_examples] y = np.concatenate([positive_labels, negative_labels], 0) return [x_text, y]
[ "Loads", "MR", "polarity", "data", "from", "files", "splits", "the", "data", "into", "words", "and", "generates", "labels", ".", "Returns", "split", "sentences", "and", "labels", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/cnn_chinese_text_classification/data_helpers.py#L64-L87
[ "def", "load_data_and_labels", "(", ")", ":", "# download dataset", "get_chinese_text", "(", ")", "# Load data from files", "positive_examples", "=", "list", "(", "codecs", ".", "open", "(", "\"./data/pos.txt\"", ",", "\"r\"", ",", "\"utf-8\"", ")", ".", "readlines"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MultiBoxMetric.reset
override reset behavior
example/ssd/train/metric.py
def reset(self): """ override reset behavior """ if getattr(self, 'num', None) is None: self.num_inst = 0 self.sum_metric = 0.0 else: self.num_inst = [0] * self.num self.sum_metric = [0.0] * self.num
def reset(self): """ override reset behavior """ if getattr(self, 'num', None) is None: self.num_inst = 0 self.sum_metric = 0.0 else: self.num_inst = [0] * self.num self.sum_metric = [0.0] * self.num
[ "override", "reset", "behavior" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train/metric.py#L31-L40
[ "def", "reset", "(", "self", ")", ":", "if", "getattr", "(", "self", ",", "'num'", ",", "None", ")", "is", "None", ":", "self", ".", "num_inst", "=", "0", "self", ".", "sum_metric", "=", "0.0", "else", ":", "self", ".", "num_inst", "=", "[", "0",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MultiBoxMetric.reset_local
override reset behavior
example/ssd/train/metric.py
def reset_local(self): """ override reset behavior """ if getattr(self, 'num', None) is None: self.num_inst = 0 self.sum_metric = 0.0 else: self.num_inst = [0] * self.num self.sum_metric = [0.0] * self.num
def reset_local(self): """ override reset behavior """ if getattr(self, 'num', None) is None: self.num_inst = 0 self.sum_metric = 0.0 else: self.num_inst = [0] * self.num self.sum_metric = [0.0] * self.num
[ "override", "reset", "behavior" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train/metric.py#L42-L51
[ "def", "reset_local", "(", "self", ")", ":", "if", "getattr", "(", "self", ",", "'num'", ",", "None", ")", "is", "None", ":", "self", ".", "num_inst", "=", "0", "self", ".", "sum_metric", "=", "0.0", "else", ":", "self", ".", "num_inst", "=", "[", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MultiBoxMetric.update
Implementation of updating metrics
example/ssd/train/metric.py
def update(self, labels, preds): """ Implementation of updating metrics """ # get generated multi label from network cls_prob = preds[0].asnumpy() loc_loss = preds[1].asnumpy() cls_label = preds[2].asnumpy() valid_count = np.sum(cls_label >= 0) # overall accuracy & object accuracy label = cls_label.flatten() mask = np.where(label >= 0)[0] indices = np.int64(label[mask]) prob = cls_prob.transpose((0, 2, 1)).reshape((-1, cls_prob.shape[1])) prob = prob[mask, indices] self.sum_metric[0] += (-np.log(prob + self.eps)).sum() self.num_inst[0] += valid_count # smoothl1loss self.sum_metric[1] += np.sum(loc_loss) self.num_inst[1] += valid_count
def update(self, labels, preds): """ Implementation of updating metrics """ # get generated multi label from network cls_prob = preds[0].asnumpy() loc_loss = preds[1].asnumpy() cls_label = preds[2].asnumpy() valid_count = np.sum(cls_label >= 0) # overall accuracy & object accuracy label = cls_label.flatten() mask = np.where(label >= 0)[0] indices = np.int64(label[mask]) prob = cls_prob.transpose((0, 2, 1)).reshape((-1, cls_prob.shape[1])) prob = prob[mask, indices] self.sum_metric[0] += (-np.log(prob + self.eps)).sum() self.num_inst[0] += valid_count # smoothl1loss self.sum_metric[1] += np.sum(loc_loss) self.num_inst[1] += valid_count
[ "Implementation", "of", "updating", "metrics" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train/metric.py#L53-L72
[ "def", "update", "(", "self", ",", "labels", ",", "preds", ")", ":", "# get generated multi label from network", "cls_prob", "=", "preds", "[", "0", "]", ".", "asnumpy", "(", ")", "loc_loss", "=", "preds", "[", "1", "]", ".", "asnumpy", "(", ")", "cls_la...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MultiBoxMetric.get
Get the current evaluation result. Override the default behavior Returns ------- name : str Name of the metric. value : float Value of the evaluation.
example/ssd/train/metric.py
def get(self): """Get the current evaluation result. Override the default behavior Returns ------- name : str Name of the metric. value : float Value of the evaluation. """ if self.num is None: if self.num_inst == 0: return (self.name, float('nan')) else: return (self.name, self.sum_metric / self.num_inst) else: names = ['%s'%(self.name[i]) for i in range(self.num)] values = [x / y if y != 0 else float('nan') \ for x, y in zip(self.sum_metric, self.num_inst)] return (names, values)
def get(self): """Get the current evaluation result. Override the default behavior Returns ------- name : str Name of the metric. value : float Value of the evaluation. """ if self.num is None: if self.num_inst == 0: return (self.name, float('nan')) else: return (self.name, self.sum_metric / self.num_inst) else: names = ['%s'%(self.name[i]) for i in range(self.num)] values = [x / y if y != 0 else float('nan') \ for x, y in zip(self.sum_metric, self.num_inst)] return (names, values)
[ "Get", "the", "current", "evaluation", "result", ".", "Override", "the", "default", "behavior" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train/metric.py#L74-L94
[ "def", "get", "(", "self", ")", ":", "if", "self", ".", "num", "is", "None", ":", "if", "self", ".", "num_inst", "==", "0", ":", "return", "(", "self", ".", "name", ",", "float", "(", "'nan'", ")", ")", "else", ":", "return", "(", "self", ".", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
dqn_sym_nips
Structure of the Deep Q Network in the NIPS 2013 workshop paper: Playing Atari with Deep Reinforcement Learning (https://www.cs.toronto.edu/~vmnih/docs/dqn.pdf) Parameters ---------- action_num : int data : mxnet.sym.Symbol, optional name : str, optional
example/reinforcement-learning/dqn/operators.py
def dqn_sym_nips(action_num, data=None, name='dqn'): """Structure of the Deep Q Network in the NIPS 2013 workshop paper: Playing Atari with Deep Reinforcement Learning (https://www.cs.toronto.edu/~vmnih/docs/dqn.pdf) Parameters ---------- action_num : int data : mxnet.sym.Symbol, optional name : str, optional """ if data is None: net = mx.symbol.Variable('data') else: net = data net = mx.symbol.Convolution(data=net, name='conv1', kernel=(8, 8), stride=(4, 4), num_filter=16) net = mx.symbol.Activation(data=net, name='relu1', act_type="relu") net = mx.symbol.Convolution(data=net, name='conv2', kernel=(4, 4), stride=(2, 2), num_filter=32) net = mx.symbol.Activation(data=net, name='relu2', act_type="relu") net = mx.symbol.Flatten(data=net) net = mx.symbol.FullyConnected(data=net, name='fc3', num_hidden=256) net = mx.symbol.Activation(data=net, name='relu3', act_type="relu") net = mx.symbol.FullyConnected(data=net, name='fc4', num_hidden=action_num) net = mx.symbol.Custom(data=net, name=name, op_type='DQNOutput') return net
def dqn_sym_nips(action_num, data=None, name='dqn'): """Structure of the Deep Q Network in the NIPS 2013 workshop paper: Playing Atari with Deep Reinforcement Learning (https://www.cs.toronto.edu/~vmnih/docs/dqn.pdf) Parameters ---------- action_num : int data : mxnet.sym.Symbol, optional name : str, optional """ if data is None: net = mx.symbol.Variable('data') else: net = data net = mx.symbol.Convolution(data=net, name='conv1', kernel=(8, 8), stride=(4, 4), num_filter=16) net = mx.symbol.Activation(data=net, name='relu1', act_type="relu") net = mx.symbol.Convolution(data=net, name='conv2', kernel=(4, 4), stride=(2, 2), num_filter=32) net = mx.symbol.Activation(data=net, name='relu2', act_type="relu") net = mx.symbol.Flatten(data=net) net = mx.symbol.FullyConnected(data=net, name='fc3', num_hidden=256) net = mx.symbol.Activation(data=net, name='relu3', act_type="relu") net = mx.symbol.FullyConnected(data=net, name='fc4', num_hidden=action_num) net = mx.symbol.Custom(data=net, name=name, op_type='DQNOutput') return net
[ "Structure", "of", "the", "Deep", "Q", "Network", "in", "the", "NIPS", "2013", "workshop", "paper", ":", "Playing", "Atari", "with", "Deep", "Reinforcement", "Learning", "(", "https", ":", "//", "www", ".", "cs", ".", "toronto", ".", "edu", "/", "~vmnih"...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/reinforcement-learning/dqn/operators.py#L98-L121
[ "def", "dqn_sym_nips", "(", "action_num", ",", "data", "=", "None", ",", "name", "=", "'dqn'", ")", ":", "if", "data", "is", "None", ":", "net", "=", "mx", ".", "symbol", ".", "Variable", "(", "'data'", ")", "else", ":", "net", "=", "data", "net", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_monitor_callback_wrapper
A wrapper for the user-defined handle.
python/mxnet/executor.py
def _monitor_callback_wrapper(callback): """A wrapper for the user-defined handle.""" def callback_handle(name, array, _): """ ctypes function """ callback(name, array) return callback_handle
def _monitor_callback_wrapper(callback): """A wrapper for the user-defined handle.""" def callback_handle(name, array, _): """ ctypes function """ callback(name, array) return callback_handle
[ "A", "wrapper", "for", "the", "user", "-", "defined", "handle", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L38-L43
[ "def", "_monitor_callback_wrapper", "(", "callback", ")", ":", "def", "callback_handle", "(", "name", ",", "array", ",", "_", ")", ":", "\"\"\" ctypes function \"\"\"", "callback", "(", "name", ",", "array", ")", "return", "callback_handle" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor._get_dict
Get the dictionary given name and ndarray pairs.
python/mxnet/executor.py
def _get_dict(names, ndarrays): """Get the dictionary given name and ndarray pairs.""" nset = set() for nm in names: if nm in nset: raise ValueError('Duplicate names detected, %s' % str(names)) nset.add(nm) return dict(zip(names, ndarrays))
def _get_dict(names, ndarrays): """Get the dictionary given name and ndarray pairs.""" nset = set() for nm in names: if nm in nset: raise ValueError('Duplicate names detected, %s' % str(names)) nset.add(nm) return dict(zip(names, ndarrays))
[ "Get", "the", "dictionary", "given", "name", "and", "ndarray", "pairs", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L90-L97
[ "def", "_get_dict", "(", "names", ",", "ndarrays", ")", ":", "nset", "=", "set", "(", ")", "for", "nm", "in", "names", ":", "if", "nm", "in", "nset", ":", "raise", "ValueError", "(", "'Duplicate names detected, %s'", "%", "str", "(", "names", ")", ")",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor._get_outputs
List all the output NDArray. Returns ------- A list of ndarray bound to the heads of executor.
python/mxnet/executor.py
def _get_outputs(self): """List all the output NDArray. Returns ------- A list of ndarray bound to the heads of executor. """ out_size = mx_uint() handles = ctypes.POINTER(NDArrayHandle)() check_call(_LIB.MXExecutorOutputs(self.handle, ctypes.byref(out_size), ctypes.byref(handles))) num_output = out_size.value outputs = [_ndarray_cls(NDArrayHandle(handles[i])) for i in range(num_output)] return outputs
def _get_outputs(self): """List all the output NDArray. Returns ------- A list of ndarray bound to the heads of executor. """ out_size = mx_uint() handles = ctypes.POINTER(NDArrayHandle)() check_call(_LIB.MXExecutorOutputs(self.handle, ctypes.byref(out_size), ctypes.byref(handles))) num_output = out_size.value outputs = [_ndarray_cls(NDArrayHandle(handles[i])) for i in range(num_output)] return outputs
[ "List", "all", "the", "output", "NDArray", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L99-L112
[ "def", "_get_outputs", "(", "self", ")", ":", "out_size", "=", "mx_uint", "(", ")", "handles", "=", "ctypes", ".", "POINTER", "(", "NDArrayHandle", ")", "(", ")", "check_call", "(", "_LIB", ".", "MXExecutorOutputs", "(", "self", ".", "handle", ",", "ctyp...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.forward
Calculate the outputs specified by the bound symbol. Parameters ---------- is_train: bool, optional Whether this forward is for evaluation purpose. If True, a backward call is expected to follow. **kwargs Additional specification of input arguments. Examples -------- >>> # doing forward by specifying data >>> texec.forward(is_train=True, data=mydata) >>> # doing forward by not specifying things, but copy to the executor before hand >>> mydata.copyto(texec.arg_dict['data']) >>> texec.forward(is_train=True) >>> # doing forward by specifying data and get outputs >>> outputs = texec.forward(is_train=True, data=mydata) >>> print(outputs[0].asnumpy())
python/mxnet/executor.py
def forward(self, is_train=False, **kwargs): """Calculate the outputs specified by the bound symbol. Parameters ---------- is_train: bool, optional Whether this forward is for evaluation purpose. If True, a backward call is expected to follow. **kwargs Additional specification of input arguments. Examples -------- >>> # doing forward by specifying data >>> texec.forward(is_train=True, data=mydata) >>> # doing forward by not specifying things, but copy to the executor before hand >>> mydata.copyto(texec.arg_dict['data']) >>> texec.forward(is_train=True) >>> # doing forward by specifying data and get outputs >>> outputs = texec.forward(is_train=True, data=mydata) >>> print(outputs[0].asnumpy()) """ if len(kwargs) != 0: arg_dict = self.arg_dict for name, array in kwargs.items(): if not isinstance(array, (NDArray, np.ndarray)): raise ValueError('only accept keyword argument of NDArrays and numpy.ndarray') if name not in arg_dict: raise TypeError('Unknown argument %s' % name) if arg_dict[name].shape != array.shape: raise ValueError('Shape not match! Argument %s, need: %s, received: %s' %(name, str(arg_dict[name].shape), str(array.shape))) arg_dict[name][:] = array check_call(_LIB.MXExecutorForward( self.handle, ctypes.c_int(int(is_train)))) return self.outputs
def forward(self, is_train=False, **kwargs): """Calculate the outputs specified by the bound symbol. Parameters ---------- is_train: bool, optional Whether this forward is for evaluation purpose. If True, a backward call is expected to follow. **kwargs Additional specification of input arguments. Examples -------- >>> # doing forward by specifying data >>> texec.forward(is_train=True, data=mydata) >>> # doing forward by not specifying things, but copy to the executor before hand >>> mydata.copyto(texec.arg_dict['data']) >>> texec.forward(is_train=True) >>> # doing forward by specifying data and get outputs >>> outputs = texec.forward(is_train=True, data=mydata) >>> print(outputs[0].asnumpy()) """ if len(kwargs) != 0: arg_dict = self.arg_dict for name, array in kwargs.items(): if not isinstance(array, (NDArray, np.ndarray)): raise ValueError('only accept keyword argument of NDArrays and numpy.ndarray') if name not in arg_dict: raise TypeError('Unknown argument %s' % name) if arg_dict[name].shape != array.shape: raise ValueError('Shape not match! Argument %s, need: %s, received: %s' %(name, str(arg_dict[name].shape), str(array.shape))) arg_dict[name][:] = array check_call(_LIB.MXExecutorForward( self.handle, ctypes.c_int(int(is_train)))) return self.outputs
[ "Calculate", "the", "outputs", "specified", "by", "the", "bound", "symbol", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L114-L153
[ "def", "forward", "(", "self", ",", "is_train", "=", "False", ",", "*", "*", "kwargs", ")", ":", "if", "len", "(", "kwargs", ")", "!=", "0", ":", "arg_dict", "=", "self", ".", "arg_dict", "for", "name", ",", "array", "in", "kwargs", ".", "items", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.backward
Do backward pass to get the gradient of arguments. Parameters ---------- out_grads : NDArray or list of NDArray or dict of str to NDArray, optional Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function. is_train : bool, default True Whether this backward is for training or inference. Note that in rare cases you want to call backward with is_train=False to get gradient during inference. Examples -------- >>> # Example for binding on loss function symbol, which gives the loss value of the model. >>> # Equivalently it gives the head gradient for backward pass. >>> # In this example the built-in SoftmaxOutput is used as loss function. >>> # MakeLoss can be used to define customized loss function symbol. >>> net = mx.sym.Variable('data') >>> net = mx.sym.FullyConnected(net, name='fc', num_hidden=6) >>> net = mx.sym.Activation(net, name='relu', act_type="relu") >>> net = mx.sym.SoftmaxOutput(net, name='softmax') >>> args = {'data': mx.nd.ones((1, 4)), 'fc_weight': mx.nd.ones((6, 4)), >>> 'fc_bias': mx.nd.array((1, 4, 4, 4, 5, 6)), 'softmax_label': mx.nd.ones((1))} >>> args_grad = {'fc_weight': mx.nd.zeros((6, 4)), 'fc_bias': mx.nd.zeros((6))} >>> texec = net.bind(ctx=mx.cpu(), args=args, args_grad=args_grad) >>> out = texec.forward(is_train=True)[0].copy() >>> print out.asnumpy() [[ 0.00378404 0.07600445 0.07600445 0.07600445 0.20660152 0.5616011 ]] >>> texec.backward() >>> print(texec.grad_arrays[1].asnumpy()) [[ 0.00378404 0.00378404 0.00378404 0.00378404] [-0.92399555 -0.92399555 -0.92399555 -0.92399555] [ 0.07600445 0.07600445 0.07600445 0.07600445] [ 0.07600445 0.07600445 0.07600445 0.07600445] [ 0.20660152 0.20660152 0.20660152 0.20660152] [ 0.5616011 0.5616011 0.5616011 0.5616011 ]] >>> >>> # Example for binding on non-loss function symbol. >>> # Here the binding symbol is neither built-in loss function >>> # nor customized loss created by MakeLoss. >>> # As a result the head gradient is not automatically provided. >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> # c is not a loss function symbol >>> c = 2 * a + b >>> args = {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])} >>> args_grad = {'a': mx.nd.zeros((2)), 'b': mx.nd.zeros((2))} >>> texec = c.bind(ctx=mx.cpu(), args=args, args_grad=args_grad) >>> out = texec.forward(is_train=True)[0].copy() >>> print(out.asnumpy()) [ 4. 7.] >>> # out_grads is the head gradient in backward pass. >>> # Here we define 'c' as loss function. >>> # Then 'out' is passed as head gradient of backward pass. >>> texec.backward(out) >>> print(texec.grad_arrays[0].asnumpy()) [ 8. 14.] >>> print(texec.grad_arrays[1].asnumpy()) [ 4. 7.]
python/mxnet/executor.py
def backward(self, out_grads=None, is_train=True): """Do backward pass to get the gradient of arguments. Parameters ---------- out_grads : NDArray or list of NDArray or dict of str to NDArray, optional Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function. is_train : bool, default True Whether this backward is for training or inference. Note that in rare cases you want to call backward with is_train=False to get gradient during inference. Examples -------- >>> # Example for binding on loss function symbol, which gives the loss value of the model. >>> # Equivalently it gives the head gradient for backward pass. >>> # In this example the built-in SoftmaxOutput is used as loss function. >>> # MakeLoss can be used to define customized loss function symbol. >>> net = mx.sym.Variable('data') >>> net = mx.sym.FullyConnected(net, name='fc', num_hidden=6) >>> net = mx.sym.Activation(net, name='relu', act_type="relu") >>> net = mx.sym.SoftmaxOutput(net, name='softmax') >>> args = {'data': mx.nd.ones((1, 4)), 'fc_weight': mx.nd.ones((6, 4)), >>> 'fc_bias': mx.nd.array((1, 4, 4, 4, 5, 6)), 'softmax_label': mx.nd.ones((1))} >>> args_grad = {'fc_weight': mx.nd.zeros((6, 4)), 'fc_bias': mx.nd.zeros((6))} >>> texec = net.bind(ctx=mx.cpu(), args=args, args_grad=args_grad) >>> out = texec.forward(is_train=True)[0].copy() >>> print out.asnumpy() [[ 0.00378404 0.07600445 0.07600445 0.07600445 0.20660152 0.5616011 ]] >>> texec.backward() >>> print(texec.grad_arrays[1].asnumpy()) [[ 0.00378404 0.00378404 0.00378404 0.00378404] [-0.92399555 -0.92399555 -0.92399555 -0.92399555] [ 0.07600445 0.07600445 0.07600445 0.07600445] [ 0.07600445 0.07600445 0.07600445 0.07600445] [ 0.20660152 0.20660152 0.20660152 0.20660152] [ 0.5616011 0.5616011 0.5616011 0.5616011 ]] >>> >>> # Example for binding on non-loss function symbol. >>> # Here the binding symbol is neither built-in loss function >>> # nor customized loss created by MakeLoss. >>> # As a result the head gradient is not automatically provided. >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> # c is not a loss function symbol >>> c = 2 * a + b >>> args = {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])} >>> args_grad = {'a': mx.nd.zeros((2)), 'b': mx.nd.zeros((2))} >>> texec = c.bind(ctx=mx.cpu(), args=args, args_grad=args_grad) >>> out = texec.forward(is_train=True)[0].copy() >>> print(out.asnumpy()) [ 4. 7.] >>> # out_grads is the head gradient in backward pass. >>> # Here we define 'c' as loss function. >>> # Then 'out' is passed as head gradient of backward pass. >>> texec.backward(out) >>> print(texec.grad_arrays[0].asnumpy()) [ 8. 14.] >>> print(texec.grad_arrays[1].asnumpy()) [ 4. 7.] """ if out_grads is None: out_grads = [] elif isinstance(out_grads, NDArray): out_grads = [out_grads] elif isinstance(out_grads, dict): out_grads = [out_grads[k] for k in self._symbol.list_outputs()] for obj in out_grads: if not isinstance(obj, NDArray): raise TypeError("inputs must be NDArray") ndarray = c_handle_array(out_grads) check_call(_LIB.MXExecutorBackwardEx( self.handle, mx_uint(len(out_grads)), ndarray, ctypes.c_int(is_train)))
def backward(self, out_grads=None, is_train=True): """Do backward pass to get the gradient of arguments. Parameters ---------- out_grads : NDArray or list of NDArray or dict of str to NDArray, optional Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function. is_train : bool, default True Whether this backward is for training or inference. Note that in rare cases you want to call backward with is_train=False to get gradient during inference. Examples -------- >>> # Example for binding on loss function symbol, which gives the loss value of the model. >>> # Equivalently it gives the head gradient for backward pass. >>> # In this example the built-in SoftmaxOutput is used as loss function. >>> # MakeLoss can be used to define customized loss function symbol. >>> net = mx.sym.Variable('data') >>> net = mx.sym.FullyConnected(net, name='fc', num_hidden=6) >>> net = mx.sym.Activation(net, name='relu', act_type="relu") >>> net = mx.sym.SoftmaxOutput(net, name='softmax') >>> args = {'data': mx.nd.ones((1, 4)), 'fc_weight': mx.nd.ones((6, 4)), >>> 'fc_bias': mx.nd.array((1, 4, 4, 4, 5, 6)), 'softmax_label': mx.nd.ones((1))} >>> args_grad = {'fc_weight': mx.nd.zeros((6, 4)), 'fc_bias': mx.nd.zeros((6))} >>> texec = net.bind(ctx=mx.cpu(), args=args, args_grad=args_grad) >>> out = texec.forward(is_train=True)[0].copy() >>> print out.asnumpy() [[ 0.00378404 0.07600445 0.07600445 0.07600445 0.20660152 0.5616011 ]] >>> texec.backward() >>> print(texec.grad_arrays[1].asnumpy()) [[ 0.00378404 0.00378404 0.00378404 0.00378404] [-0.92399555 -0.92399555 -0.92399555 -0.92399555] [ 0.07600445 0.07600445 0.07600445 0.07600445] [ 0.07600445 0.07600445 0.07600445 0.07600445] [ 0.20660152 0.20660152 0.20660152 0.20660152] [ 0.5616011 0.5616011 0.5616011 0.5616011 ]] >>> >>> # Example for binding on non-loss function symbol. >>> # Here the binding symbol is neither built-in loss function >>> # nor customized loss created by MakeLoss. >>> # As a result the head gradient is not automatically provided. >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> # c is not a loss function symbol >>> c = 2 * a + b >>> args = {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])} >>> args_grad = {'a': mx.nd.zeros((2)), 'b': mx.nd.zeros((2))} >>> texec = c.bind(ctx=mx.cpu(), args=args, args_grad=args_grad) >>> out = texec.forward(is_train=True)[0].copy() >>> print(out.asnumpy()) [ 4. 7.] >>> # out_grads is the head gradient in backward pass. >>> # Here we define 'c' as loss function. >>> # Then 'out' is passed as head gradient of backward pass. >>> texec.backward(out) >>> print(texec.grad_arrays[0].asnumpy()) [ 8. 14.] >>> print(texec.grad_arrays[1].asnumpy()) [ 4. 7.] """ if out_grads is None: out_grads = [] elif isinstance(out_grads, NDArray): out_grads = [out_grads] elif isinstance(out_grads, dict): out_grads = [out_grads[k] for k in self._symbol.list_outputs()] for obj in out_grads: if not isinstance(obj, NDArray): raise TypeError("inputs must be NDArray") ndarray = c_handle_array(out_grads) check_call(_LIB.MXExecutorBackwardEx( self.handle, mx_uint(len(out_grads)), ndarray, ctypes.c_int(is_train)))
[ "Do", "backward", "pass", "to", "get", "the", "gradient", "of", "arguments", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L155-L235
[ "def", "backward", "(", "self", ",", "out_grads", "=", "None", ",", "is_train", "=", "True", ")", ":", "if", "out_grads", "is", "None", ":", "out_grads", "=", "[", "]", "elif", "isinstance", "(", "out_grads", ",", "NDArray", ")", ":", "out_grads", "=",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.set_monitor_callback
Install callback for monitor. Parameters ---------- callback : function Takes a string and an NDArrayHandle. monitor_all : bool, default False If true, monitor both input and output, otherwise monitor output only. Examples -------- >>> def mon_callback(*args, **kwargs): >>> print("Do your stuff here.") >>> >>> texe.set_monitor_callback(mon_callback)
python/mxnet/executor.py
def set_monitor_callback(self, callback, monitor_all=False): """Install callback for monitor. Parameters ---------- callback : function Takes a string and an NDArrayHandle. monitor_all : bool, default False If true, monitor both input and output, otherwise monitor output only. Examples -------- >>> def mon_callback(*args, **kwargs): >>> print("Do your stuff here.") >>> >>> texe.set_monitor_callback(mon_callback) """ cb_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p, NDArrayHandle, ctypes.c_void_p) self._monitor_callback = cb_type(_monitor_callback_wrapper(callback)) check_call(_LIB.MXExecutorSetMonitorCallbackEX( self.handle, self._monitor_callback, None, ctypes.c_int(monitor_all)))
def set_monitor_callback(self, callback, monitor_all=False): """Install callback for monitor. Parameters ---------- callback : function Takes a string and an NDArrayHandle. monitor_all : bool, default False If true, monitor both input and output, otherwise monitor output only. Examples -------- >>> def mon_callback(*args, **kwargs): >>> print("Do your stuff here.") >>> >>> texe.set_monitor_callback(mon_callback) """ cb_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p, NDArrayHandle, ctypes.c_void_p) self._monitor_callback = cb_type(_monitor_callback_wrapper(callback)) check_call(_LIB.MXExecutorSetMonitorCallbackEX( self.handle, self._monitor_callback, None, ctypes.c_int(monitor_all)))
[ "Install", "callback", "for", "monitor", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L237-L260
[ "def", "set_monitor_callback", "(", "self", ",", "callback", ",", "monitor_all", "=", "False", ")", ":", "cb_type", "=", "ctypes", ".", "CFUNCTYPE", "(", "None", ",", "ctypes", ".", "c_char_p", ",", "NDArrayHandle", ",", "ctypes", ".", "c_void_p", ")", "se...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.arg_dict
Get dictionary representation of argument arrrays. Returns ------- arg_dict : dict of str to NDArray The dictionary that maps the names of arguments to NDArrays. Raises ------ ValueError : if there are duplicated names in the arguments.
python/mxnet/executor.py
def arg_dict(self): """Get dictionary representation of argument arrrays. Returns ------- arg_dict : dict of str to NDArray The dictionary that maps the names of arguments to NDArrays. Raises ------ ValueError : if there are duplicated names in the arguments. """ if self._arg_dict is None: self._arg_dict = Executor._get_dict( self._symbol.list_arguments(), self.arg_arrays) return self._arg_dict
def arg_dict(self): """Get dictionary representation of argument arrrays. Returns ------- arg_dict : dict of str to NDArray The dictionary that maps the names of arguments to NDArrays. Raises ------ ValueError : if there are duplicated names in the arguments. """ if self._arg_dict is None: self._arg_dict = Executor._get_dict( self._symbol.list_arguments(), self.arg_arrays) return self._arg_dict
[ "Get", "dictionary", "representation", "of", "argument", "arrrays", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L263-L278
[ "def", "arg_dict", "(", "self", ")", ":", "if", "self", ".", "_arg_dict", "is", "None", ":", "self", ".", "_arg_dict", "=", "Executor", ".", "_get_dict", "(", "self", ".", "_symbol", ".", "list_arguments", "(", ")", ",", "self", ".", "arg_arrays", ")",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.grad_dict
Get dictionary representation of gradient arrays. Returns ------- grad_dict : dict of str to NDArray The dictionary that maps name of arguments to gradient arrays.
python/mxnet/executor.py
def grad_dict(self): """Get dictionary representation of gradient arrays. Returns ------- grad_dict : dict of str to NDArray The dictionary that maps name of arguments to gradient arrays. """ if self._grad_dict is None: self._grad_dict = Executor._get_dict( self._symbol.list_arguments(), self.grad_arrays) return self._grad_dict
def grad_dict(self): """Get dictionary representation of gradient arrays. Returns ------- grad_dict : dict of str to NDArray The dictionary that maps name of arguments to gradient arrays. """ if self._grad_dict is None: self._grad_dict = Executor._get_dict( self._symbol.list_arguments(), self.grad_arrays) return self._grad_dict
[ "Get", "dictionary", "representation", "of", "gradient", "arrays", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L281-L292
[ "def", "grad_dict", "(", "self", ")", ":", "if", "self", ".", "_grad_dict", "is", "None", ":", "self", ".", "_grad_dict", "=", "Executor", ".", "_get_dict", "(", "self", ".", "_symbol", ".", "list_arguments", "(", ")", ",", "self", ".", "grad_arrays", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.aux_dict
Get dictionary representation of auxiliary states arrays. Returns ------- aux_dict : dict of str to NDArray The dictionary that maps name of auxiliary states to NDArrays. Raises ------ ValueError : if there are duplicated names in the auxiliary states.
python/mxnet/executor.py
def aux_dict(self): """Get dictionary representation of auxiliary states arrays. Returns ------- aux_dict : dict of str to NDArray The dictionary that maps name of auxiliary states to NDArrays. Raises ------ ValueError : if there are duplicated names in the auxiliary states. """ if self._aux_dict is None: self._aux_dict = Executor._get_dict( self._symbol.list_auxiliary_states(), self.aux_arrays) return self._aux_dict
def aux_dict(self): """Get dictionary representation of auxiliary states arrays. Returns ------- aux_dict : dict of str to NDArray The dictionary that maps name of auxiliary states to NDArrays. Raises ------ ValueError : if there are duplicated names in the auxiliary states. """ if self._aux_dict is None: self._aux_dict = Executor._get_dict( self._symbol.list_auxiliary_states(), self.aux_arrays) return self._aux_dict
[ "Get", "dictionary", "representation", "of", "auxiliary", "states", "arrays", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L295-L310
[ "def", "aux_dict", "(", "self", ")", ":", "if", "self", ".", "_aux_dict", "is", "None", ":", "self", ".", "_aux_dict", "=", "Executor", ".", "_get_dict", "(", "self", ".", "_symbol", ".", "list_auxiliary_states", "(", ")", ",", "self", ".", "aux_arrays",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.output_dict
Get dictionary representation of output arrays. Returns ------- output_dict : dict of str to NDArray The dictionary that maps name of output names to NDArrays. Raises ------ ValueError : if there are duplicated names in the outputs.
python/mxnet/executor.py
def output_dict(self): """Get dictionary representation of output arrays. Returns ------- output_dict : dict of str to NDArray The dictionary that maps name of output names to NDArrays. Raises ------ ValueError : if there are duplicated names in the outputs. """ if self._output_dict is None: self._output_dict = Executor._get_dict( self._symbol.list_outputs(), self.outputs) return self._output_dict
def output_dict(self): """Get dictionary representation of output arrays. Returns ------- output_dict : dict of str to NDArray The dictionary that maps name of output names to NDArrays. Raises ------ ValueError : if there are duplicated names in the outputs. """ if self._output_dict is None: self._output_dict = Executor._get_dict( self._symbol.list_outputs(), self.outputs) return self._output_dict
[ "Get", "dictionary", "representation", "of", "output", "arrays", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L313-L328
[ "def", "output_dict", "(", "self", ")", ":", "if", "self", ".", "_output_dict", "is", "None", ":", "self", ".", "_output_dict", "=", "Executor", ".", "_get_dict", "(", "self", ".", "_symbol", ".", "list_outputs", "(", ")", ",", "self", ".", "outputs", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.copy_params_from
Copy parameters from arg_params, aux_params into executor's internal array. Parameters ---------- arg_params : dict of str to NDArray Parameters, dict of name to NDArray of arguments. aux_params : dict of str to NDArray, optional Parameters, dict of name to NDArray of auxiliary states. allow_extra_params : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Raises ------ ValueError If there is additional parameters in the dict but ``allow_extra_params=False``. Examples -------- >>> # set parameters with existing model checkpoint >>> model_prefix = 'mx_mlp' >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 0) >>> texec.copy_params_from(arg_params, aux_params)
python/mxnet/executor.py
def copy_params_from(self, arg_params, aux_params=None, allow_extra_params=False): """Copy parameters from arg_params, aux_params into executor's internal array. Parameters ---------- arg_params : dict of str to NDArray Parameters, dict of name to NDArray of arguments. aux_params : dict of str to NDArray, optional Parameters, dict of name to NDArray of auxiliary states. allow_extra_params : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Raises ------ ValueError If there is additional parameters in the dict but ``allow_extra_params=False``. Examples -------- >>> # set parameters with existing model checkpoint >>> model_prefix = 'mx_mlp' >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 0) >>> texec.copy_params_from(arg_params, aux_params) """ for name, array in arg_params.items(): if name in self.arg_dict: dst = self.arg_dict[name] array.astype(dst.dtype).copyto(dst) elif not allow_extra_params: raise ValueError('Find name \"%s\" that is not in the arguments' % name) if aux_params is None: return for name, array in aux_params.items(): if name in self.aux_dict: dst = self.aux_dict[name] array.astype(dst.dtype).copyto(dst) elif not allow_extra_params: raise ValueError('Find name %s that is not in the auxiliary states' % name)
def copy_params_from(self, arg_params, aux_params=None, allow_extra_params=False): """Copy parameters from arg_params, aux_params into executor's internal array. Parameters ---------- arg_params : dict of str to NDArray Parameters, dict of name to NDArray of arguments. aux_params : dict of str to NDArray, optional Parameters, dict of name to NDArray of auxiliary states. allow_extra_params : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Raises ------ ValueError If there is additional parameters in the dict but ``allow_extra_params=False``. Examples -------- >>> # set parameters with existing model checkpoint >>> model_prefix = 'mx_mlp' >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 0) >>> texec.copy_params_from(arg_params, aux_params) """ for name, array in arg_params.items(): if name in self.arg_dict: dst = self.arg_dict[name] array.astype(dst.dtype).copyto(dst) elif not allow_extra_params: raise ValueError('Find name \"%s\" that is not in the arguments' % name) if aux_params is None: return for name, array in aux_params.items(): if name in self.aux_dict: dst = self.aux_dict[name] array.astype(dst.dtype).copyto(dst) elif not allow_extra_params: raise ValueError('Find name %s that is not in the auxiliary states' % name)
[ "Copy", "parameters", "from", "arg_params", "aux_params", "into", "executor", "s", "internal", "array", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L330-L373
[ "def", "copy_params_from", "(", "self", ",", "arg_params", ",", "aux_params", "=", "None", ",", "allow_extra_params", "=", "False", ")", ":", "for", "name", ",", "array", "in", "arg_params", ".", "items", "(", ")", ":", "if", "name", "in", "self", ".", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.reshape
Return a new executor with the same symbol and shared memory, but different input/output shapes. For runtime reshaping, variable length sequences, etc. The returned executor shares state with the current one, and cannot be used in parallel with it. Parameters ---------- partial_shaping : bool Whether to allow changing the shape of unspecified arguments. allow_up_sizing : bool Whether to allow allocating new ndarrays that's larger than the original. kwargs : dict of string to tuple of int New shape for arguments. Returns ------- exec : Executor A new executor that shares memory with self. Examples -------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> c = 2 * a + b >>> texec = c.bind(mx.cpu(), {'a': mx.nd.zeros((2, 1)), 'b': mx.nd.ones((2,1))}) >>> new_shape = {'a': (4, 2), 'b': (4, 2)} >>> texec.reshape(allow_up_sizing=True, **new_shape)
python/mxnet/executor.py
def reshape(self, partial_shaping=False, allow_up_sizing=False, **kwargs): """Return a new executor with the same symbol and shared memory, but different input/output shapes. For runtime reshaping, variable length sequences, etc. The returned executor shares state with the current one, and cannot be used in parallel with it. Parameters ---------- partial_shaping : bool Whether to allow changing the shape of unspecified arguments. allow_up_sizing : bool Whether to allow allocating new ndarrays that's larger than the original. kwargs : dict of string to tuple of int New shape for arguments. Returns ------- exec : Executor A new executor that shares memory with self. Examples -------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> c = 2 * a + b >>> texec = c.bind(mx.cpu(), {'a': mx.nd.zeros((2, 1)), 'b': mx.nd.ones((2,1))}) >>> new_shape = {'a': (4, 2), 'b': (4, 2)} >>> texec.reshape(allow_up_sizing=True, **new_shape) """ # pylint: disable=too-many-branches provided_arg_shape_data = [] # shape data # argument shape index in sdata, # e.g. [sdata[indptr[0]], sdata[indptr[1]]) is the shape of the first arg provided_arg_shape_idx = [0] provided_arg_shape_names = [] # provided argument names for k, v in kwargs.items(): if isinstance(v, tuple): provided_arg_shape_names.append(k) provided_arg_shape_data.extend(v) provided_arg_shape_idx.append(len(provided_arg_shape_data)) ctx_map_keys = [] ctx_map_dev_types = [] ctx_map_dev_ids = [] if self._group2ctx: for key, val in self._group2ctx.items(): ctx_map_keys.append(key) ctx_map_dev_types.append(val.device_typeid) ctx_map_dev_ids.append(val.device_id) handle = ExecutorHandle() shared_handle = self.handle num_in_args = ctypes.c_uint() in_arg_handles = ctypes.POINTER(NDArrayHandle)() arg_grad_handles = ctypes.POINTER(NDArrayHandle)() num_aux_states = ctypes.c_uint() aux_state_handles = ctypes.POINTER(NDArrayHandle)() check_call(_LIB.MXExecutorReshapeEx(ctypes.c_int(int(partial_shaping)), ctypes.c_int(int(allow_up_sizing)), ctypes.c_int(self._ctx.device_typeid), ctypes.c_int(self._ctx.device_id), mx_uint(len(ctx_map_keys)), c_str_array(ctx_map_keys), c_array_buf(ctypes.c_int, py_array('i', ctx_map_dev_types)), c_array_buf(ctypes.c_int, py_array('i', ctx_map_dev_ids)), mx_uint(len(provided_arg_shape_names)), c_str_array(provided_arg_shape_names), c_array_buf(mx_int, py_array('i', provided_arg_shape_data)), c_array_buf(mx_uint, py_array('I', provided_arg_shape_idx)), ctypes.byref(num_in_args), ctypes.byref(in_arg_handles), ctypes.byref(arg_grad_handles), ctypes.byref(num_aux_states), ctypes.byref(aux_state_handles), shared_handle, ctypes.byref(handle))) arg_arrays = [_ndarray_cls(NDArrayHandle(in_arg_handles[i])) for i in range(num_in_args.value)] grad_arrays = [_ndarray_cls(NDArrayHandle(arg_grad_handles[i])) if arg_grad_handles[i] is not None else None for i in range(num_in_args.value)] aux_arrays = [_ndarray_cls(NDArrayHandle(aux_state_handles[i])) for i in range(num_aux_states.value)] executor = Executor(handle, self._symbol, self._ctx, self._grad_req, self._group2ctx) executor.arg_arrays = arg_arrays executor.grad_arrays = grad_arrays executor.aux_arrays = aux_arrays return executor
def reshape(self, partial_shaping=False, allow_up_sizing=False, **kwargs): """Return a new executor with the same symbol and shared memory, but different input/output shapes. For runtime reshaping, variable length sequences, etc. The returned executor shares state with the current one, and cannot be used in parallel with it. Parameters ---------- partial_shaping : bool Whether to allow changing the shape of unspecified arguments. allow_up_sizing : bool Whether to allow allocating new ndarrays that's larger than the original. kwargs : dict of string to tuple of int New shape for arguments. Returns ------- exec : Executor A new executor that shares memory with self. Examples -------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> c = 2 * a + b >>> texec = c.bind(mx.cpu(), {'a': mx.nd.zeros((2, 1)), 'b': mx.nd.ones((2,1))}) >>> new_shape = {'a': (4, 2), 'b': (4, 2)} >>> texec.reshape(allow_up_sizing=True, **new_shape) """ # pylint: disable=too-many-branches provided_arg_shape_data = [] # shape data # argument shape index in sdata, # e.g. [sdata[indptr[0]], sdata[indptr[1]]) is the shape of the first arg provided_arg_shape_idx = [0] provided_arg_shape_names = [] # provided argument names for k, v in kwargs.items(): if isinstance(v, tuple): provided_arg_shape_names.append(k) provided_arg_shape_data.extend(v) provided_arg_shape_idx.append(len(provided_arg_shape_data)) ctx_map_keys = [] ctx_map_dev_types = [] ctx_map_dev_ids = [] if self._group2ctx: for key, val in self._group2ctx.items(): ctx_map_keys.append(key) ctx_map_dev_types.append(val.device_typeid) ctx_map_dev_ids.append(val.device_id) handle = ExecutorHandle() shared_handle = self.handle num_in_args = ctypes.c_uint() in_arg_handles = ctypes.POINTER(NDArrayHandle)() arg_grad_handles = ctypes.POINTER(NDArrayHandle)() num_aux_states = ctypes.c_uint() aux_state_handles = ctypes.POINTER(NDArrayHandle)() check_call(_LIB.MXExecutorReshapeEx(ctypes.c_int(int(partial_shaping)), ctypes.c_int(int(allow_up_sizing)), ctypes.c_int(self._ctx.device_typeid), ctypes.c_int(self._ctx.device_id), mx_uint(len(ctx_map_keys)), c_str_array(ctx_map_keys), c_array_buf(ctypes.c_int, py_array('i', ctx_map_dev_types)), c_array_buf(ctypes.c_int, py_array('i', ctx_map_dev_ids)), mx_uint(len(provided_arg_shape_names)), c_str_array(provided_arg_shape_names), c_array_buf(mx_int, py_array('i', provided_arg_shape_data)), c_array_buf(mx_uint, py_array('I', provided_arg_shape_idx)), ctypes.byref(num_in_args), ctypes.byref(in_arg_handles), ctypes.byref(arg_grad_handles), ctypes.byref(num_aux_states), ctypes.byref(aux_state_handles), shared_handle, ctypes.byref(handle))) arg_arrays = [_ndarray_cls(NDArrayHandle(in_arg_handles[i])) for i in range(num_in_args.value)] grad_arrays = [_ndarray_cls(NDArrayHandle(arg_grad_handles[i])) if arg_grad_handles[i] is not None else None for i in range(num_in_args.value)] aux_arrays = [_ndarray_cls(NDArrayHandle(aux_state_handles[i])) for i in range(num_aux_states.value)] executor = Executor(handle, self._symbol, self._ctx, self._grad_req, self._group2ctx) executor.arg_arrays = arg_arrays executor.grad_arrays = grad_arrays executor.aux_arrays = aux_arrays return executor
[ "Return", "a", "new", "executor", "with", "the", "same", "symbol", "and", "shared", "memory", "but", "different", "input", "/", "output", "shapes", ".", "For", "runtime", "reshaping", "variable", "length", "sequences", "etc", ".", "The", "returned", "executor"...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L375-L472
[ "def", "reshape", "(", "self", ",", "partial_shaping", "=", "False", ",", "allow_up_sizing", "=", "False", ",", "*", "*", "kwargs", ")", ":", "# pylint: disable=too-many-branches", "provided_arg_shape_data", "=", "[", "]", "# shape data", "# argument shape index in sd...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Executor.debug_str
Get a debug string about internal execution plan. Returns ------- debug_str : string Debug string of the executor. Examples -------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.sin(a) >>> c = 2 * a + b >>> texec = c.bind(mx.cpu(), {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])}) >>> print(texec.debug_str()) Symbol Outputs: output[0]=_plus0(0) Variable:a -------------------- Op:_mul_scalar, Name=_mulscalar0 Inputs: arg[0]=a(0) version=0 Attrs: scalar=2 -------------------- Op:sin, Name=sin0 Inputs: arg[0]=a(0) version=0 -------------------- Op:elemwise_add, Name=_plus0 Inputs: arg[0]=_mulscalar0(0) arg[1]=sin0(0) Total 0 MB allocated Total 11 TempSpace resource requested
python/mxnet/executor.py
def debug_str(self): """Get a debug string about internal execution plan. Returns ------- debug_str : string Debug string of the executor. Examples -------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.sin(a) >>> c = 2 * a + b >>> texec = c.bind(mx.cpu(), {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])}) >>> print(texec.debug_str()) Symbol Outputs: output[0]=_plus0(0) Variable:a -------------------- Op:_mul_scalar, Name=_mulscalar0 Inputs: arg[0]=a(0) version=0 Attrs: scalar=2 -------------------- Op:sin, Name=sin0 Inputs: arg[0]=a(0) version=0 -------------------- Op:elemwise_add, Name=_plus0 Inputs: arg[0]=_mulscalar0(0) arg[1]=sin0(0) Total 0 MB allocated Total 11 TempSpace resource requested """ debug_str = ctypes.c_char_p() check_call(_LIB.MXExecutorPrint( self.handle, ctypes.byref(debug_str))) return py_str(debug_str.value)
def debug_str(self): """Get a debug string about internal execution plan. Returns ------- debug_str : string Debug string of the executor. Examples -------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.sin(a) >>> c = 2 * a + b >>> texec = c.bind(mx.cpu(), {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])}) >>> print(texec.debug_str()) Symbol Outputs: output[0]=_plus0(0) Variable:a -------------------- Op:_mul_scalar, Name=_mulscalar0 Inputs: arg[0]=a(0) version=0 Attrs: scalar=2 -------------------- Op:sin, Name=sin0 Inputs: arg[0]=a(0) version=0 -------------------- Op:elemwise_add, Name=_plus0 Inputs: arg[0]=_mulscalar0(0) arg[1]=sin0(0) Total 0 MB allocated Total 11 TempSpace resource requested """ debug_str = ctypes.c_char_p() check_call(_LIB.MXExecutorPrint( self.handle, ctypes.byref(debug_str))) return py_str(debug_str.value)
[ "Get", "a", "debug", "string", "about", "internal", "execution", "plan", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/executor.py#L474-L513
[ "def", "debug_str", "(", "self", ")", ":", "debug_str", "=", "ctypes", ".", "c_char_p", "(", ")", "check_call", "(", "_LIB", ".", "MXExecutorPrint", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "debug_str", ")", ")", ")", "return", "py...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
parse_voc_rec
parse pascal voc record into a dictionary :param filename: xml file path :return: list of dict
example/ssd/evaluate/eval_voc.py
def parse_voc_rec(filename): """ parse pascal voc record into a dictionary :param filename: xml file path :return: list of dict """ import xml.etree.ElementTree as ET tree = ET.parse(filename) objects = [] for obj in tree.findall('object'): obj_dict = dict() obj_dict['name'] = obj.find('name').text obj_dict['difficult'] = int(obj.find('difficult').text) bbox = obj.find('bndbox') obj_dict['bbox'] = [int(bbox.find('xmin').text), int(bbox.find('ymin').text), int(bbox.find('xmax').text), int(bbox.find('ymax').text)] objects.append(obj_dict) return objects
def parse_voc_rec(filename): """ parse pascal voc record into a dictionary :param filename: xml file path :return: list of dict """ import xml.etree.ElementTree as ET tree = ET.parse(filename) objects = [] for obj in tree.findall('object'): obj_dict = dict() obj_dict['name'] = obj.find('name').text obj_dict['difficult'] = int(obj.find('difficult').text) bbox = obj.find('bndbox') obj_dict['bbox'] = [int(bbox.find('xmin').text), int(bbox.find('ymin').text), int(bbox.find('xmax').text), int(bbox.find('ymax').text)] objects.append(obj_dict) return objects
[ "parse", "pascal", "voc", "record", "into", "a", "dictionary", ":", "param", "filename", ":", "xml", "file", "path", ":", "return", ":", "list", "of", "dict" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_voc.py#L30-L49
[ "def", "parse_voc_rec", "(", "filename", ")", ":", "import", "xml", ".", "etree", ".", "ElementTree", "as", "ET", "tree", "=", "ET", ".", "parse", "(", "filename", ")", "objects", "=", "[", "]", "for", "obj", "in", "tree", ".", "findall", "(", "'obje...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
voc_eval
pascal voc evaluation :param detpath: detection results detpath.format(classname) :param annopath: annotations annopath.format(classname) :param imageset_file: text file containing list of images :param classname: category name :param cache_dir: caching annotations :param ovthresh: overlap threshold :param use_07_metric: whether to use voc07's 11 point ap computation :return: rec, prec, ap
example/ssd/evaluate/eval_voc.py
def voc_eval(detpath, annopath, imageset_file, classname, cache_dir, ovthresh=0.5, use_07_metric=False): """ pascal voc evaluation :param detpath: detection results detpath.format(classname) :param annopath: annotations annopath.format(classname) :param imageset_file: text file containing list of images :param classname: category name :param cache_dir: caching annotations :param ovthresh: overlap threshold :param use_07_metric: whether to use voc07's 11 point ap computation :return: rec, prec, ap """ if not os.path.isdir(cache_dir): os.mkdir(cache_dir) cache_file = os.path.join(cache_dir, 'annotations.pkl') with open(imageset_file, 'r') as f: lines = f.readlines() image_filenames = [x.strip() for x in lines] # load annotations from cache if not os.path.isfile(cache_file): recs = {} for ind, image_filename in enumerate(image_filenames): recs[image_filename] = parse_voc_rec(annopath.format(image_filename)) if ind % 100 == 0: print('reading annotations for {:d}/{:d}'.format(ind + 1, len(image_filenames))) print('saving annotations cache to {:s}'.format(cache_file)) with open(cache_file, 'wb') as f: pickle.dump(recs, f) else: with open(cache_file, 'rb') as f: recs = pickle.load(f) # extract objects in :param classname: class_recs = {} npos = 0 for image_filename in image_filenames: objects = [obj for obj in recs[image_filename] if obj['name'] == classname] bbox = np.array([x['bbox'] for x in objects]) difficult = np.array([x['difficult'] for x in objects]).astype(np.bool) det = [False] * len(objects) # stand for detected npos = npos + sum(~difficult) class_recs[image_filename] = {'bbox': bbox, 'difficult': difficult, 'det': det} # read detections detfile = detpath.format(classname) with open(detfile, 'r') as f: lines = f.readlines() splitlines = [x.strip().split(' ') for x in lines] image_ids = [x[0] for x in splitlines] confidence = np.array([float(x[1]) for x in splitlines]) bbox = np.array([[float(z) for z in x[2:]] for x in splitlines]) # sort by confidence sorted_inds = np.argsort(-confidence) sorted_scores = np.sort(-confidence) bbox = bbox[sorted_inds, :] image_ids = [image_ids[x] for x in sorted_inds] # go down detections and mark true positives and false positives nd = len(image_ids) tp = np.zeros(nd) fp = np.zeros(nd) for d in range(nd): r = class_recs[image_ids[d]] bb = bbox[d, :].astype(float) ovmax = -np.inf bbgt = r['bbox'].astype(float) if bbgt.size > 0: # compute overlaps # intersection ixmin = np.maximum(bbgt[:, 0], bb[0]) iymin = np.maximum(bbgt[:, 1], bb[1]) ixmax = np.minimum(bbgt[:, 2], bb[2]) iymax = np.minimum(bbgt[:, 3], bb[3]) iw = np.maximum(ixmax - ixmin + 1., 0.) ih = np.maximum(iymax - iymin + 1., 0.) inters = iw * ih # union uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) + (bbgt[:, 2] - bbgt[:, 0] + 1.) * (bbgt[:, 3] - bbgt[:, 1] + 1.) - inters) overlaps = inters / uni ovmax = np.max(overlaps) jmax = np.argmax(overlaps) if ovmax > ovthresh: if not r['difficult'][jmax]: if not r['det'][jmax]: tp[d] = 1. r['det'][jmax] = 1 else: fp[d] = 1. else: fp[d] = 1. # compute precision recall fp = np.cumsum(fp) tp = np.cumsum(tp) rec = tp / float(npos) # avoid division by zero in case first detection matches a difficult ground ruth prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps) ap = voc_ap(rec, prec, use_07_metric) return rec, prec, ap
def voc_eval(detpath, annopath, imageset_file, classname, cache_dir, ovthresh=0.5, use_07_metric=False): """ pascal voc evaluation :param detpath: detection results detpath.format(classname) :param annopath: annotations annopath.format(classname) :param imageset_file: text file containing list of images :param classname: category name :param cache_dir: caching annotations :param ovthresh: overlap threshold :param use_07_metric: whether to use voc07's 11 point ap computation :return: rec, prec, ap """ if not os.path.isdir(cache_dir): os.mkdir(cache_dir) cache_file = os.path.join(cache_dir, 'annotations.pkl') with open(imageset_file, 'r') as f: lines = f.readlines() image_filenames = [x.strip() for x in lines] # load annotations from cache if not os.path.isfile(cache_file): recs = {} for ind, image_filename in enumerate(image_filenames): recs[image_filename] = parse_voc_rec(annopath.format(image_filename)) if ind % 100 == 0: print('reading annotations for {:d}/{:d}'.format(ind + 1, len(image_filenames))) print('saving annotations cache to {:s}'.format(cache_file)) with open(cache_file, 'wb') as f: pickle.dump(recs, f) else: with open(cache_file, 'rb') as f: recs = pickle.load(f) # extract objects in :param classname: class_recs = {} npos = 0 for image_filename in image_filenames: objects = [obj for obj in recs[image_filename] if obj['name'] == classname] bbox = np.array([x['bbox'] for x in objects]) difficult = np.array([x['difficult'] for x in objects]).astype(np.bool) det = [False] * len(objects) # stand for detected npos = npos + sum(~difficult) class_recs[image_filename] = {'bbox': bbox, 'difficult': difficult, 'det': det} # read detections detfile = detpath.format(classname) with open(detfile, 'r') as f: lines = f.readlines() splitlines = [x.strip().split(' ') for x in lines] image_ids = [x[0] for x in splitlines] confidence = np.array([float(x[1]) for x in splitlines]) bbox = np.array([[float(z) for z in x[2:]] for x in splitlines]) # sort by confidence sorted_inds = np.argsort(-confidence) sorted_scores = np.sort(-confidence) bbox = bbox[sorted_inds, :] image_ids = [image_ids[x] for x in sorted_inds] # go down detections and mark true positives and false positives nd = len(image_ids) tp = np.zeros(nd) fp = np.zeros(nd) for d in range(nd): r = class_recs[image_ids[d]] bb = bbox[d, :].astype(float) ovmax = -np.inf bbgt = r['bbox'].astype(float) if bbgt.size > 0: # compute overlaps # intersection ixmin = np.maximum(bbgt[:, 0], bb[0]) iymin = np.maximum(bbgt[:, 1], bb[1]) ixmax = np.minimum(bbgt[:, 2], bb[2]) iymax = np.minimum(bbgt[:, 3], bb[3]) iw = np.maximum(ixmax - ixmin + 1., 0.) ih = np.maximum(iymax - iymin + 1., 0.) inters = iw * ih # union uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) + (bbgt[:, 2] - bbgt[:, 0] + 1.) * (bbgt[:, 3] - bbgt[:, 1] + 1.) - inters) overlaps = inters / uni ovmax = np.max(overlaps) jmax = np.argmax(overlaps) if ovmax > ovthresh: if not r['difficult'][jmax]: if not r['det'][jmax]: tp[d] = 1. r['det'][jmax] = 1 else: fp[d] = 1. else: fp[d] = 1. # compute precision recall fp = np.cumsum(fp) tp = np.cumsum(tp) rec = tp / float(npos) # avoid division by zero in case first detection matches a difficult ground ruth prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps) ap = voc_ap(rec, prec, use_07_metric) return rec, prec, ap
[ "pascal", "voc", "evaluation", ":", "param", "detpath", ":", "detection", "results", "detpath", ".", "format", "(", "classname", ")", ":", "param", "annopath", ":", "annotations", "annopath", ".", "format", "(", "classname", ")", ":", "param", "imageset_file",...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_voc.py#L86-L196
[ "def", "voc_eval", "(", "detpath", ",", "annopath", ",", "imageset_file", ",", "classname", ",", "cache_dir", ",", "ovthresh", "=", "0.5", ",", "use_07_metric", "=", "False", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "cache_dir", ")", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MXNetGraph.register
Register operators
python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
def register(op_name): """Register operators""" def wrapper(func): """Helper function to map functions""" try: import onnx as _ MXNetGraph.registry_[op_name] = func except ImportError: pass return func return wrapper
def register(op_name): """Register operators""" def wrapper(func): """Helper function to map functions""" try: import onnx as _ MXNetGraph.registry_[op_name] = func except ImportError: pass return func return wrapper
[ "Register", "operators" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py#L72-L83
[ "def", "register", "(", "op_name", ")", ":", "def", "wrapper", "(", "func", ")", ":", "\"\"\"Helper function to map functions\"\"\"", "try", ":", "import", "onnx", "as", "_", "MXNetGraph", ".", "registry_", "[", "op_name", "]", "=", "func", "except", "ImportEr...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MXNetGraph.convert_layer
Convert MXNet layer to ONNX
python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
def convert_layer(node, **kwargs): """Convert MXNet layer to ONNX""" op = str(node["op"]) if op not in MXNetGraph.registry_: raise AttributeError("No conversion function registered for op type %s yet." % op) convert_func = MXNetGraph.registry_[op] return convert_func(node, **kwargs)
def convert_layer(node, **kwargs): """Convert MXNet layer to ONNX""" op = str(node["op"]) if op not in MXNetGraph.registry_: raise AttributeError("No conversion function registered for op type %s yet." % op) convert_func = MXNetGraph.registry_[op] return convert_func(node, **kwargs)
[ "Convert", "MXNet", "layer", "to", "ONNX" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py#L86-L92
[ "def", "convert_layer", "(", "node", ",", "*", "*", "kwargs", ")", ":", "op", "=", "str", "(", "node", "[", "\"op\"", "]", ")", "if", "op", "not", "in", "MXNetGraph", ".", "registry_", ":", "raise", "AttributeError", "(", "\"No conversion function register...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MXNetGraph.split_params
Helper function to split params dictionary into args and aux params Parameters ---------- sym : :class:`~mxnet.symbol.Symbol` MXNet symbol object params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format Returns ------- arg_params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format aux_params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format
python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
def split_params(sym, params): """Helper function to split params dictionary into args and aux params Parameters ---------- sym : :class:`~mxnet.symbol.Symbol` MXNet symbol object params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format Returns ------- arg_params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format aux_params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format """ arg_params = {} aux_params = {} for args in sym.list_arguments(): if args in params: arg_params.update({args: nd.array(params[args])}) for aux in sym.list_auxiliary_states(): if aux in params: aux_params.update({aux: nd.array(params[aux])}) return arg_params, aux_params
def split_params(sym, params): """Helper function to split params dictionary into args and aux params Parameters ---------- sym : :class:`~mxnet.symbol.Symbol` MXNet symbol object params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format Returns ------- arg_params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format aux_params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format """ arg_params = {} aux_params = {} for args in sym.list_arguments(): if args in params: arg_params.update({args: nd.array(params[args])}) for aux in sym.list_auxiliary_states(): if aux in params: aux_params.update({aux: nd.array(params[aux])}) return arg_params, aux_params
[ "Helper", "function", "to", "split", "params", "dictionary", "into", "args", "and", "aux", "params" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py#L95-L120
[ "def", "split_params", "(", "sym", ",", "params", ")", ":", "arg_params", "=", "{", "}", "aux_params", "=", "{", "}", "for", "args", "in", "sym", ".", "list_arguments", "(", ")", ":", "if", "args", "in", "params", ":", "arg_params", ".", "update", "(...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MXNetGraph.get_outputs
Infer output shapes and return dictionary of output name to shape :param :class:`~mxnet.symbol.Symbol` sym: symbol to perform infer shape on :param dic of (str, nd.NDArray) params: :param list of tuple(int, ...) in_shape: list of all input shapes :param in_label: name of label typically used in loss that may be left in graph. This name is removed from list of inputs required by symbol :return: dictionary of output name to shape :rtype: dict of (str, tuple(int, ...))
python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
def get_outputs(sym, params, in_shape, in_label): """ Infer output shapes and return dictionary of output name to shape :param :class:`~mxnet.symbol.Symbol` sym: symbol to perform infer shape on :param dic of (str, nd.NDArray) params: :param list of tuple(int, ...) in_shape: list of all input shapes :param in_label: name of label typically used in loss that may be left in graph. This name is removed from list of inputs required by symbol :return: dictionary of output name to shape :rtype: dict of (str, tuple(int, ...)) """ # remove any input listed in params from sym.list_inputs() and bind them to the input shapes provided # by user. Also remove in_label, which is the name of the label symbol that may have been used # as the label for loss during training. inputs = {n: tuple(s) for n, s in zip([n for n in sym.list_inputs() if n not in params and n != in_label], in_shape)} # Add params and their shape to list of inputs inputs.update({n: v.shape for n, v in params.items() if n in sym.list_inputs()}) # Provide input data as well as input params to infer_shape() _, out_shapes, _ = sym.infer_shape(**inputs) out_names = list() for name in sym.list_outputs(): if name.endswith('_output'): out_names.append(name[:-len('_output')]) else: logging.info("output '%s' does not end with '_output'", name) out_names.append(name) assert len(out_shapes) == len(out_names) # bind output shapes with output names graph_outputs = {n: s for n, s in zip(out_names, out_shapes)} return graph_outputs
def get_outputs(sym, params, in_shape, in_label): """ Infer output shapes and return dictionary of output name to shape :param :class:`~mxnet.symbol.Symbol` sym: symbol to perform infer shape on :param dic of (str, nd.NDArray) params: :param list of tuple(int, ...) in_shape: list of all input shapes :param in_label: name of label typically used in loss that may be left in graph. This name is removed from list of inputs required by symbol :return: dictionary of output name to shape :rtype: dict of (str, tuple(int, ...)) """ # remove any input listed in params from sym.list_inputs() and bind them to the input shapes provided # by user. Also remove in_label, which is the name of the label symbol that may have been used # as the label for loss during training. inputs = {n: tuple(s) for n, s in zip([n for n in sym.list_inputs() if n not in params and n != in_label], in_shape)} # Add params and their shape to list of inputs inputs.update({n: v.shape for n, v in params.items() if n in sym.list_inputs()}) # Provide input data as well as input params to infer_shape() _, out_shapes, _ = sym.infer_shape(**inputs) out_names = list() for name in sym.list_outputs(): if name.endswith('_output'): out_names.append(name[:-len('_output')]) else: logging.info("output '%s' does not end with '_output'", name) out_names.append(name) assert len(out_shapes) == len(out_names) # bind output shapes with output names graph_outputs = {n: s for n, s in zip(out_names, out_shapes)} return graph_outputs
[ "Infer", "output", "shapes", "and", "return", "dictionary", "of", "output", "name", "to", "shape" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py#L123-L156
[ "def", "get_outputs", "(", "sym", ",", "params", ",", "in_shape", ",", "in_label", ")", ":", "# remove any input listed in params from sym.list_inputs() and bind them to the input shapes provided", "# by user. Also remove in_label, which is the name of the label symbol that may have been u...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MXNetGraph.convert_weights_to_numpy
Convert weights to numpy
python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
def convert_weights_to_numpy(weights_dict): """Convert weights to numpy""" return dict([(k.replace("arg:", "").replace("aux:", ""), v.asnumpy()) for k, v in weights_dict.items()])
def convert_weights_to_numpy(weights_dict): """Convert weights to numpy""" return dict([(k.replace("arg:", "").replace("aux:", ""), v.asnumpy()) for k, v in weights_dict.items()])
[ "Convert", "weights", "to", "numpy" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py#L159-L162
[ "def", "convert_weights_to_numpy", "(", "weights_dict", ")", ":", "return", "dict", "(", "[", "(", "k", ".", "replace", "(", "\"arg:\"", ",", "\"\"", ")", ".", "replace", "(", "\"aux:\"", ",", "\"\"", ")", ",", "v", ".", "asnumpy", "(", ")", ")", "fo...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MXNetGraph.create_onnx_graph_proto
Convert MXNet graph to ONNX graph Parameters ---------- sym : :class:`~mxnet.symbol.Symbol` MXNet symbol object params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format in_shape : List of tuple Input shape of the model e.g [(1,3,224,224)] in_type : data type Input data type e.g. np.float32 verbose : Boolean If true will print logs of the model conversion Returns ------- graph : GraphProto ONNX graph
python/mxnet/contrib/onnx/mx2onnx/export_onnx.py
def create_onnx_graph_proto(self, sym, params, in_shape, in_type, verbose=False): """Convert MXNet graph to ONNX graph Parameters ---------- sym : :class:`~mxnet.symbol.Symbol` MXNet symbol object params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format in_shape : List of tuple Input shape of the model e.g [(1,3,224,224)] in_type : data type Input data type e.g. np.float32 verbose : Boolean If true will print logs of the model conversion Returns ------- graph : GraphProto ONNX graph """ try: from onnx import (checker, helper, NodeProto, ValueInfoProto, TensorProto) from onnx.helper import make_tensor_value_info except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") # When MXNet model is saved to json file , MXNet adds a node for label. # The name of this node is, name of the last node + "_label" ( i.e if last node # name is "Softmax", this node will have a name "Softmax_label". Also, the new node # will always be second last node in the json graph. # Deriving the output_label name. output_label = sym.get_internals()[len(sym.get_internals()) - 1].name + "_label" weights = MXNetGraph.convert_weights_to_numpy(params) mx_graph = json.loads(sym.tojson())["nodes"] initializer = [] all_processed_nodes = [] onnx_processed_nodes = [] onnx_processed_inputs = [] onnx_processed_outputs = [] index_lookup = [] # Determine output shape graph_outputs = MXNetGraph.get_outputs(sym, params, in_shape, output_label) graph_input_idx = 0 for idx, node in enumerate(mx_graph): op = node["op"] name = node["name"] if verbose: logging.info("Converting idx: %d, op: %s, name: %s", idx, op, name) # A node is an input node if its op_name is "null" and is not # in params dict if op == "null" and name not in params: # Handling graph input # Skipping output_label node, as this node is not part of graph # Refer "output_label" assignment above for more details. if name == output_label: continue converted = MXNetGraph.convert_layer( node, is_input=True, mx_graph=mx_graph, weights=weights, in_shape=in_shape[graph_input_idx], in_type=in_type, proc_nodes=all_processed_nodes, initializer=initializer, index_lookup=index_lookup) graph_input_idx += 1 else: # Handling graph layers converted = MXNetGraph.convert_layer( node, is_input=False, mx_graph=mx_graph, weights=weights, in_shape=in_shape, in_type=in_type, proc_nodes=all_processed_nodes, initializer=initializer, index_lookup=index_lookup, idx=idx ) if isinstance(converted, list): # Iterate for all converted nodes for converted_node in converted: # If converted node is ValueInfoProto, add it in inputs if isinstance(converted_node, ValueInfoProto): onnx_processed_inputs.append(converted_node) # If converted node is NodeProto, add it in processed nodes list elif isinstance(converted_node, NodeProto): onnx_processed_nodes.append(converted_node) # some operators have multiple outputs, # therefore, check all output node names node_names = list(converted_node.output) for nodename in node_names: if nodename in graph_outputs: onnx_processed_outputs.append( make_tensor_value_info( name=nodename, elem_type=in_type, shape=graph_outputs[nodename] ) ) if verbose: logging.info("Output node is: %s", nodename) elif isinstance(converted_node, TensorProto): raise ValueError("Did not expect TensorProto") else: raise ValueError("node is of an unrecognized type: %s" % type(node)) all_processed_nodes.append(converted_node) if idx > 0: # Handling extra node added to the graph if the MXNet model was # saved to json file, # refer "output_label" initialization above for more details. # if extra node was added then prev_index to the last node is adjusted. if idx == (len(mx_graph) - 1) and \ mx_graph[len(mx_graph)-2]["name"] == output_label: prev_index = index_lookup[idx - 2] else: prev_index = index_lookup[idx - 1] index_lookup.append(prev_index+len(converted)) else: index_lookup.append(len(converted) - 1) else: logging.info("Operator converter function should always return a list") graph = helper.make_graph( onnx_processed_nodes, "mxnet_converted_model", onnx_processed_inputs, onnx_processed_outputs ) graph.initializer.extend(initializer) checker.check_graph(graph) return graph
def create_onnx_graph_proto(self, sym, params, in_shape, in_type, verbose=False): """Convert MXNet graph to ONNX graph Parameters ---------- sym : :class:`~mxnet.symbol.Symbol` MXNet symbol object params : dict of ``str`` to :class:`~mxnet.ndarray.NDArray` Dict of converted parameters stored in ``mxnet.ndarray.NDArray`` format in_shape : List of tuple Input shape of the model e.g [(1,3,224,224)] in_type : data type Input data type e.g. np.float32 verbose : Boolean If true will print logs of the model conversion Returns ------- graph : GraphProto ONNX graph """ try: from onnx import (checker, helper, NodeProto, ValueInfoProto, TensorProto) from onnx.helper import make_tensor_value_info except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") # When MXNet model is saved to json file , MXNet adds a node for label. # The name of this node is, name of the last node + "_label" ( i.e if last node # name is "Softmax", this node will have a name "Softmax_label". Also, the new node # will always be second last node in the json graph. # Deriving the output_label name. output_label = sym.get_internals()[len(sym.get_internals()) - 1].name + "_label" weights = MXNetGraph.convert_weights_to_numpy(params) mx_graph = json.loads(sym.tojson())["nodes"] initializer = [] all_processed_nodes = [] onnx_processed_nodes = [] onnx_processed_inputs = [] onnx_processed_outputs = [] index_lookup = [] # Determine output shape graph_outputs = MXNetGraph.get_outputs(sym, params, in_shape, output_label) graph_input_idx = 0 for idx, node in enumerate(mx_graph): op = node["op"] name = node["name"] if verbose: logging.info("Converting idx: %d, op: %s, name: %s", idx, op, name) # A node is an input node if its op_name is "null" and is not # in params dict if op == "null" and name not in params: # Handling graph input # Skipping output_label node, as this node is not part of graph # Refer "output_label" assignment above for more details. if name == output_label: continue converted = MXNetGraph.convert_layer( node, is_input=True, mx_graph=mx_graph, weights=weights, in_shape=in_shape[graph_input_idx], in_type=in_type, proc_nodes=all_processed_nodes, initializer=initializer, index_lookup=index_lookup) graph_input_idx += 1 else: # Handling graph layers converted = MXNetGraph.convert_layer( node, is_input=False, mx_graph=mx_graph, weights=weights, in_shape=in_shape, in_type=in_type, proc_nodes=all_processed_nodes, initializer=initializer, index_lookup=index_lookup, idx=idx ) if isinstance(converted, list): # Iterate for all converted nodes for converted_node in converted: # If converted node is ValueInfoProto, add it in inputs if isinstance(converted_node, ValueInfoProto): onnx_processed_inputs.append(converted_node) # If converted node is NodeProto, add it in processed nodes list elif isinstance(converted_node, NodeProto): onnx_processed_nodes.append(converted_node) # some operators have multiple outputs, # therefore, check all output node names node_names = list(converted_node.output) for nodename in node_names: if nodename in graph_outputs: onnx_processed_outputs.append( make_tensor_value_info( name=nodename, elem_type=in_type, shape=graph_outputs[nodename] ) ) if verbose: logging.info("Output node is: %s", nodename) elif isinstance(converted_node, TensorProto): raise ValueError("Did not expect TensorProto") else: raise ValueError("node is of an unrecognized type: %s" % type(node)) all_processed_nodes.append(converted_node) if idx > 0: # Handling extra node added to the graph if the MXNet model was # saved to json file, # refer "output_label" initialization above for more details. # if extra node was added then prev_index to the last node is adjusted. if idx == (len(mx_graph) - 1) and \ mx_graph[len(mx_graph)-2]["name"] == output_label: prev_index = index_lookup[idx - 2] else: prev_index = index_lookup[idx - 1] index_lookup.append(prev_index+len(converted)) else: index_lookup.append(len(converted) - 1) else: logging.info("Operator converter function should always return a list") graph = helper.make_graph( onnx_processed_nodes, "mxnet_converted_model", onnx_processed_inputs, onnx_processed_outputs ) graph.initializer.extend(initializer) checker.check_graph(graph) return graph
[ "Convert", "MXNet", "graph", "to", "ONNX", "graph" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py#L164-L313
[ "def", "create_onnx_graph_proto", "(", "self", ",", "sym", ",", "params", ",", "in_shape", ",", "in_type", ",", "verbose", "=", "False", ")", ":", "try", ":", "from", "onnx", "import", "(", "checker", ",", "helper", ",", "NodeProto", ",", "ValueInfoProto",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_lr_scheduler
Compute learning rate and refactor scheduler Parameters: --------- learning_rate : float original learning rate lr_refactor_step : comma separated str epochs to change learning rate lr_refactor_ratio : float lr *= ratio at certain steps num_example : int number of training images, used to estimate the iterations given epochs batch_size : int training batch size begin_epoch : int starting epoch Returns: --------- (learning_rate, mx.lr_scheduler) as tuple
example/ssd/train/train_net.py
def get_lr_scheduler(learning_rate, lr_refactor_step, lr_refactor_ratio, num_example, batch_size, begin_epoch): """ Compute learning rate and refactor scheduler Parameters: --------- learning_rate : float original learning rate lr_refactor_step : comma separated str epochs to change learning rate lr_refactor_ratio : float lr *= ratio at certain steps num_example : int number of training images, used to estimate the iterations given epochs batch_size : int training batch size begin_epoch : int starting epoch Returns: --------- (learning_rate, mx.lr_scheduler) as tuple """ assert lr_refactor_ratio > 0 iter_refactor = [int(r) for r in lr_refactor_step.split(',') if r.strip()] if lr_refactor_ratio >= 1: return (learning_rate, None) else: lr = learning_rate epoch_size = num_example // batch_size for s in iter_refactor: if begin_epoch >= s: lr *= lr_refactor_ratio if lr != learning_rate: logging.getLogger().info("Adjusted learning rate to {} for epoch {}".format(lr, begin_epoch)) steps = [epoch_size * (x - begin_epoch) for x in iter_refactor if x > begin_epoch] if not steps: return (lr, None) lr_scheduler = mx.lr_scheduler.MultiFactorScheduler(step=steps, factor=lr_refactor_ratio) return (lr, lr_scheduler)
def get_lr_scheduler(learning_rate, lr_refactor_step, lr_refactor_ratio, num_example, batch_size, begin_epoch): """ Compute learning rate and refactor scheduler Parameters: --------- learning_rate : float original learning rate lr_refactor_step : comma separated str epochs to change learning rate lr_refactor_ratio : float lr *= ratio at certain steps num_example : int number of training images, used to estimate the iterations given epochs batch_size : int training batch size begin_epoch : int starting epoch Returns: --------- (learning_rate, mx.lr_scheduler) as tuple """ assert lr_refactor_ratio > 0 iter_refactor = [int(r) for r in lr_refactor_step.split(',') if r.strip()] if lr_refactor_ratio >= 1: return (learning_rate, None) else: lr = learning_rate epoch_size = num_example // batch_size for s in iter_refactor: if begin_epoch >= s: lr *= lr_refactor_ratio if lr != learning_rate: logging.getLogger().info("Adjusted learning rate to {} for epoch {}".format(lr, begin_epoch)) steps = [epoch_size * (x - begin_epoch) for x in iter_refactor if x > begin_epoch] if not steps: return (lr, None) lr_scheduler = mx.lr_scheduler.MultiFactorScheduler(step=steps, factor=lr_refactor_ratio) return (lr, lr_scheduler)
[ "Compute", "learning", "rate", "and", "refactor", "scheduler" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train/train_net.py#L48-L88
[ "def", "get_lr_scheduler", "(", "learning_rate", ",", "lr_refactor_step", ",", "lr_refactor_ratio", ",", "num_example", ",", "batch_size", ",", "begin_epoch", ")", ":", "assert", "lr_refactor_ratio", ">", "0", "iter_refactor", "=", "[", "int", "(", "r", ")", "fo...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
train_net
Wrapper for training phase. Parameters: ---------- net : str symbol name for the network structure train_path : str record file path for training num_classes : int number of object classes, not including background batch_size : int training batch-size data_shape : int or tuple width/height as integer or (3, height, width) tuple mean_pixels : tuple of floats mean pixel values for red, green and blue resume : int resume from previous checkpoint if > 0 finetune : int fine-tune from previous checkpoint if > 0 pretrained : str prefix of pretrained model, including path epoch : int load epoch of either resume/finetune/pretrained model prefix : str prefix for saving checkpoints ctx : [mx.cpu()] or [mx.gpu(x)] list of mxnet contexts begin_epoch : int starting epoch for training, should be 0 if not otherwise specified end_epoch : int end epoch of training frequent : int frequency to print out training status learning_rate : float training learning rate momentum : float trainig momentum weight_decay : float training weight decay param lr_refactor_ratio : float multiplier for reducing learning rate lr_refactor_step : comma separated integers at which epoch to rescale learning rate, e.g. '30, 60, 90' freeze_layer_pattern : str regex pattern for layers need to be fixed num_example : int number of training images label_pad_width : int force padding training and validation labels to sync their label widths nms_thresh : float non-maximum suppression threshold for validation force_nms : boolean suppress overlaped objects from different classes train_list : str list file path for training, this will replace the embeded labels in record val_path : str record file path for validation val_list : str list file path for validation, this will replace the embeded labels in record iter_monitor : int monitor internal stats in networks if > 0, specified by monitor_pattern monitor_pattern : str regex pattern for monitoring network stats log_file : str log to file if enabled
example/ssd/train/train_net.py
def train_net(net, train_path, num_classes, batch_size, data_shape, mean_pixels, resume, finetune, pretrained, epoch, prefix, ctx, begin_epoch, end_epoch, frequent, learning_rate, momentum, weight_decay, lr_refactor_step, lr_refactor_ratio, freeze_layer_pattern='', num_example=10000, label_pad_width=350, nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False, nms_topk=400, force_suppress=False, train_list="", val_path="", val_list="", iter_monitor=0, monitor_pattern=".*", log_file=None, kv_store=None): """ Wrapper for training phase. Parameters: ---------- net : str symbol name for the network structure train_path : str record file path for training num_classes : int number of object classes, not including background batch_size : int training batch-size data_shape : int or tuple width/height as integer or (3, height, width) tuple mean_pixels : tuple of floats mean pixel values for red, green and blue resume : int resume from previous checkpoint if > 0 finetune : int fine-tune from previous checkpoint if > 0 pretrained : str prefix of pretrained model, including path epoch : int load epoch of either resume/finetune/pretrained model prefix : str prefix for saving checkpoints ctx : [mx.cpu()] or [mx.gpu(x)] list of mxnet contexts begin_epoch : int starting epoch for training, should be 0 if not otherwise specified end_epoch : int end epoch of training frequent : int frequency to print out training status learning_rate : float training learning rate momentum : float trainig momentum weight_decay : float training weight decay param lr_refactor_ratio : float multiplier for reducing learning rate lr_refactor_step : comma separated integers at which epoch to rescale learning rate, e.g. '30, 60, 90' freeze_layer_pattern : str regex pattern for layers need to be fixed num_example : int number of training images label_pad_width : int force padding training and validation labels to sync their label widths nms_thresh : float non-maximum suppression threshold for validation force_nms : boolean suppress overlaped objects from different classes train_list : str list file path for training, this will replace the embeded labels in record val_path : str record file path for validation val_list : str list file path for validation, this will replace the embeded labels in record iter_monitor : int monitor internal stats in networks if > 0, specified by monitor_pattern monitor_pattern : str regex pattern for monitoring network stats log_file : str log to file if enabled """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) if log_file: fh = logging.FileHandler(log_file) logger.addHandler(fh) # check args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 prefix += '_' + net + '_' + str(data_shape[1]) if isinstance(mean_pixels, (int, float)): mean_pixels = [mean_pixels, mean_pixels, mean_pixels] assert len(mean_pixels) == 3, "must provide all RGB mean values" train_iter = DetRecordIter(train_path, batch_size, data_shape, mean_pixels=mean_pixels, label_pad_width=label_pad_width, path_imglist=train_list, **cfg.train) if val_path: val_iter = DetRecordIter(val_path, batch_size, data_shape, mean_pixels=mean_pixels, label_pad_width=label_pad_width, path_imglist=val_list, **cfg.valid) else: val_iter = None # load symbol net = get_symbol_train(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_suppress, nms_topk=nms_topk) # define layers with fixed weight/bias if freeze_layer_pattern.strip(): re_prog = re.compile(freeze_layer_pattern) fixed_param_names = [name for name in net.list_arguments() if re_prog.match(name)] else: fixed_param_names = None # load pretrained or resume from previous state ctx_str = '('+ ','.join([str(c) for c in ctx]) + ')' if resume > 0: logger.info("Resume training with {} from epoch {}" .format(ctx_str, resume)) _, args, auxs = mx.model.load_checkpoint(prefix, resume) begin_epoch = resume elif finetune > 0: logger.info("Start finetuning with {} from epoch {}" .format(ctx_str, finetune)) _, args, auxs = mx.model.load_checkpoint(prefix, finetune) begin_epoch = finetune # the prediction convolution layers name starts with relu, so it's fine fixed_param_names = [name for name in net.list_arguments() \ if name.startswith('conv')] elif pretrained: logger.info("Start training with {} from pretrained model {}" .format(ctx_str, pretrained)) _, args, auxs = mx.model.load_checkpoint(pretrained, epoch) args = convert_pretrained(pretrained, args) else: logger.info("Experimental: start training from scratch with {}" .format(ctx_str)) args = None auxs = None fixed_param_names = None # helper information if fixed_param_names: logger.info("Freezed parameters: [" + ','.join(fixed_param_names) + ']') # init training module mod = mx.mod.Module(net, label_names=('label',), logger=logger, context=ctx, fixed_param_names=fixed_param_names) # fit parameters batch_end_callback = mx.callback.Speedometer(train_iter.batch_size, frequent=frequent) epoch_end_callback = mx.callback.do_checkpoint(prefix) learning_rate, lr_scheduler = get_lr_scheduler(learning_rate, lr_refactor_step, lr_refactor_ratio, num_example, batch_size, begin_epoch) optimizer_params={'learning_rate':learning_rate, 'momentum':momentum, 'wd':weight_decay, 'lr_scheduler':lr_scheduler, 'clip_gradient':None, 'rescale_grad': 1.0 / len(ctx) if len(ctx) > 0 else 1.0 } monitor = mx.mon.Monitor(iter_monitor, pattern=monitor_pattern) if iter_monitor > 0 else None # run fit net, every n epochs we run evaluation network to get mAP if voc07_metric: valid_metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names, pred_idx=3) else: valid_metric = MApMetric(ovp_thresh, use_difficult, class_names, pred_idx=3) # create kvstore when there are gpus kv = mx.kvstore.create(kv_store) if kv_store else None mod.fit(train_iter, val_iter, eval_metric=MultiBoxMetric(), validation_metric=valid_metric, batch_end_callback=batch_end_callback, epoch_end_callback=epoch_end_callback, optimizer='sgd', optimizer_params=optimizer_params, begin_epoch=begin_epoch, num_epoch=end_epoch, initializer=mx.init.Xavier(), arg_params=args, aux_params=auxs, allow_missing=True, monitor=monitor, kvstore=kv)
def train_net(net, train_path, num_classes, batch_size, data_shape, mean_pixels, resume, finetune, pretrained, epoch, prefix, ctx, begin_epoch, end_epoch, frequent, learning_rate, momentum, weight_decay, lr_refactor_step, lr_refactor_ratio, freeze_layer_pattern='', num_example=10000, label_pad_width=350, nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False, nms_topk=400, force_suppress=False, train_list="", val_path="", val_list="", iter_monitor=0, monitor_pattern=".*", log_file=None, kv_store=None): """ Wrapper for training phase. Parameters: ---------- net : str symbol name for the network structure train_path : str record file path for training num_classes : int number of object classes, not including background batch_size : int training batch-size data_shape : int or tuple width/height as integer or (3, height, width) tuple mean_pixels : tuple of floats mean pixel values for red, green and blue resume : int resume from previous checkpoint if > 0 finetune : int fine-tune from previous checkpoint if > 0 pretrained : str prefix of pretrained model, including path epoch : int load epoch of either resume/finetune/pretrained model prefix : str prefix for saving checkpoints ctx : [mx.cpu()] or [mx.gpu(x)] list of mxnet contexts begin_epoch : int starting epoch for training, should be 0 if not otherwise specified end_epoch : int end epoch of training frequent : int frequency to print out training status learning_rate : float training learning rate momentum : float trainig momentum weight_decay : float training weight decay param lr_refactor_ratio : float multiplier for reducing learning rate lr_refactor_step : comma separated integers at which epoch to rescale learning rate, e.g. '30, 60, 90' freeze_layer_pattern : str regex pattern for layers need to be fixed num_example : int number of training images label_pad_width : int force padding training and validation labels to sync their label widths nms_thresh : float non-maximum suppression threshold for validation force_nms : boolean suppress overlaped objects from different classes train_list : str list file path for training, this will replace the embeded labels in record val_path : str record file path for validation val_list : str list file path for validation, this will replace the embeded labels in record iter_monitor : int monitor internal stats in networks if > 0, specified by monitor_pattern monitor_pattern : str regex pattern for monitoring network stats log_file : str log to file if enabled """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) if log_file: fh = logging.FileHandler(log_file) logger.addHandler(fh) # check args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 prefix += '_' + net + '_' + str(data_shape[1]) if isinstance(mean_pixels, (int, float)): mean_pixels = [mean_pixels, mean_pixels, mean_pixels] assert len(mean_pixels) == 3, "must provide all RGB mean values" train_iter = DetRecordIter(train_path, batch_size, data_shape, mean_pixels=mean_pixels, label_pad_width=label_pad_width, path_imglist=train_list, **cfg.train) if val_path: val_iter = DetRecordIter(val_path, batch_size, data_shape, mean_pixels=mean_pixels, label_pad_width=label_pad_width, path_imglist=val_list, **cfg.valid) else: val_iter = None # load symbol net = get_symbol_train(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_suppress, nms_topk=nms_topk) # define layers with fixed weight/bias if freeze_layer_pattern.strip(): re_prog = re.compile(freeze_layer_pattern) fixed_param_names = [name for name in net.list_arguments() if re_prog.match(name)] else: fixed_param_names = None # load pretrained or resume from previous state ctx_str = '('+ ','.join([str(c) for c in ctx]) + ')' if resume > 0: logger.info("Resume training with {} from epoch {}" .format(ctx_str, resume)) _, args, auxs = mx.model.load_checkpoint(prefix, resume) begin_epoch = resume elif finetune > 0: logger.info("Start finetuning with {} from epoch {}" .format(ctx_str, finetune)) _, args, auxs = mx.model.load_checkpoint(prefix, finetune) begin_epoch = finetune # the prediction convolution layers name starts with relu, so it's fine fixed_param_names = [name for name in net.list_arguments() \ if name.startswith('conv')] elif pretrained: logger.info("Start training with {} from pretrained model {}" .format(ctx_str, pretrained)) _, args, auxs = mx.model.load_checkpoint(pretrained, epoch) args = convert_pretrained(pretrained, args) else: logger.info("Experimental: start training from scratch with {}" .format(ctx_str)) args = None auxs = None fixed_param_names = None # helper information if fixed_param_names: logger.info("Freezed parameters: [" + ','.join(fixed_param_names) + ']') # init training module mod = mx.mod.Module(net, label_names=('label',), logger=logger, context=ctx, fixed_param_names=fixed_param_names) # fit parameters batch_end_callback = mx.callback.Speedometer(train_iter.batch_size, frequent=frequent) epoch_end_callback = mx.callback.do_checkpoint(prefix) learning_rate, lr_scheduler = get_lr_scheduler(learning_rate, lr_refactor_step, lr_refactor_ratio, num_example, batch_size, begin_epoch) optimizer_params={'learning_rate':learning_rate, 'momentum':momentum, 'wd':weight_decay, 'lr_scheduler':lr_scheduler, 'clip_gradient':None, 'rescale_grad': 1.0 / len(ctx) if len(ctx) > 0 else 1.0 } monitor = mx.mon.Monitor(iter_monitor, pattern=monitor_pattern) if iter_monitor > 0 else None # run fit net, every n epochs we run evaluation network to get mAP if voc07_metric: valid_metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names, pred_idx=3) else: valid_metric = MApMetric(ovp_thresh, use_difficult, class_names, pred_idx=3) # create kvstore when there are gpus kv = mx.kvstore.create(kv_store) if kv_store else None mod.fit(train_iter, val_iter, eval_metric=MultiBoxMetric(), validation_metric=valid_metric, batch_end_callback=batch_end_callback, epoch_end_callback=epoch_end_callback, optimizer='sgd', optimizer_params=optimizer_params, begin_epoch=begin_epoch, num_epoch=end_epoch, initializer=mx.init.Xavier(), arg_params=args, aux_params=auxs, allow_missing=True, monitor=monitor, kvstore=kv)
[ "Wrapper", "for", "training", "phase", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train/train_net.py#L90-L279
[ "def", "train_net", "(", "net", ",", "train_path", ",", "num_classes", ",", "batch_size", ",", "data_shape", ",", "mean_pixels", ",", "resume", ",", "finetune", ",", "pretrained", ",", "epoch", ",", "prefix", ",", "ctx", ",", "begin_epoch", ",", "end_epoch",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
imagenet50
This is a set of 50 images representative of ImageNet images. This dataset was collected by randomly finding a working ImageNet link and then pasting the original ImageNet image into Google image search restricted to images licensed for reuse. A similar image (now with rights to reuse) was downloaded as a rough replacment for the original ImageNet image. The point is to have a random sample of ImageNet for use as a background distribution for explaining models trained on ImageNet data. Note that because the images are only rough replacements the labels might no longer be correct.
shap/datasets.py
def imagenet50(display=False, resolution=224): """ This is a set of 50 images representative of ImageNet images. This dataset was collected by randomly finding a working ImageNet link and then pasting the original ImageNet image into Google image search restricted to images licensed for reuse. A similar image (now with rights to reuse) was downloaded as a rough replacment for the original ImageNet image. The point is to have a random sample of ImageNet for use as a background distribution for explaining models trained on ImageNet data. Note that because the images are only rough replacements the labels might no longer be correct. """ prefix = github_data_url + "imagenet50_" X = np.load(cache(prefix + "%sx%s.npy" % (resolution, resolution))).astype(np.float32) y = np.loadtxt(cache(prefix + "labels.csv")) return X, y
def imagenet50(display=False, resolution=224): """ This is a set of 50 images representative of ImageNet images. This dataset was collected by randomly finding a working ImageNet link and then pasting the original ImageNet image into Google image search restricted to images licensed for reuse. A similar image (now with rights to reuse) was downloaded as a rough replacment for the original ImageNet image. The point is to have a random sample of ImageNet for use as a background distribution for explaining models trained on ImageNet data. Note that because the images are only rough replacements the labels might no longer be correct. """ prefix = github_data_url + "imagenet50_" X = np.load(cache(prefix + "%sx%s.npy" % (resolution, resolution))).astype(np.float32) y = np.loadtxt(cache(prefix + "labels.csv")) return X, y
[ "This", "is", "a", "set", "of", "50", "images", "representative", "of", "ImageNet", "images", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L13-L28
[ "def", "imagenet50", "(", "display", "=", "False", ",", "resolution", "=", "224", ")", ":", "prefix", "=", "github_data_url", "+", "\"imagenet50_\"", "X", "=", "np", ".", "load", "(", "cache", "(", "prefix", "+", "\"%sx%s.npy\"", "%", "(", "resolution", ...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
boston
Return the boston housing data in a nice package.
shap/datasets.py
def boston(display=False): """ Return the boston housing data in a nice package. """ d = sklearn.datasets.load_boston() df = pd.DataFrame(data=d.data, columns=d.feature_names) # pylint: disable=E1101 return df, d.target
def boston(display=False): """ Return the boston housing data in a nice package. """ d = sklearn.datasets.load_boston() df = pd.DataFrame(data=d.data, columns=d.feature_names) # pylint: disable=E1101 return df, d.target
[ "Return", "the", "boston", "housing", "data", "in", "a", "nice", "package", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L30-L35
[ "def", "boston", "(", "display", "=", "False", ")", ":", "d", "=", "sklearn", ".", "datasets", ".", "load_boston", "(", ")", "df", "=", "pd", ".", "DataFrame", "(", "data", "=", "d", ".", "data", ",", "columns", "=", "d", ".", "feature_names", ")",...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
imdb
Return the clssic IMDB sentiment analysis training data in a nice package. Full data is at: http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz Paper to cite when using the data is: http://www.aclweb.org/anthology/P11-1015
shap/datasets.py
def imdb(display=False): """ Return the clssic IMDB sentiment analysis training data in a nice package. Full data is at: http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz Paper to cite when using the data is: http://www.aclweb.org/anthology/P11-1015 """ with open(cache(github_data_url + "imdb_train.txt")) as f: data = f.readlines() y = np.ones(25000, dtype=np.bool) y[:12500] = 0 return data, y
def imdb(display=False): """ Return the clssic IMDB sentiment analysis training data in a nice package. Full data is at: http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz Paper to cite when using the data is: http://www.aclweb.org/anthology/P11-1015 """ with open(cache(github_data_url + "imdb_train.txt")) as f: data = f.readlines() y = np.ones(25000, dtype=np.bool) y[:12500] = 0 return data, y
[ "Return", "the", "clssic", "IMDB", "sentiment", "analysis", "training", "data", "in", "a", "nice", "package", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L37-L48
[ "def", "imdb", "(", "display", "=", "False", ")", ":", "with", "open", "(", "cache", "(", "github_data_url", "+", "\"imdb_train.txt\"", ")", ")", "as", "f", ":", "data", "=", "f", ".", "readlines", "(", ")", "y", "=", "np", ".", "ones", "(", "25000...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
communitiesandcrime
Predict total number of non-violent crimes per 100K popuation. This dataset is from the classic UCI Machine Learning repository: https://archive.ics.uci.edu/ml/datasets/Communities+and+Crime+Unnormalized
shap/datasets.py
def communitiesandcrime(display=False): """ Predict total number of non-violent crimes per 100K popuation. This dataset is from the classic UCI Machine Learning repository: https://archive.ics.uci.edu/ml/datasets/Communities+and+Crime+Unnormalized """ raw_data = pd.read_csv( cache(github_data_url + "CommViolPredUnnormalizedData.txt"), na_values="?" ) # find the indices where the total violent crimes are known valid_inds = np.where(np.invert(np.isnan(raw_data.iloc[:,-2])))[0] y = np.array(raw_data.iloc[valid_inds,-2], dtype=np.float) # extract the predictive features and remove columns with missing values X = raw_data.iloc[valid_inds,5:-18] valid_cols = np.where(np.isnan(X.values).sum(0) == 0)[0] X = X.iloc[:,valid_cols] return X, y
def communitiesandcrime(display=False): """ Predict total number of non-violent crimes per 100K popuation. This dataset is from the classic UCI Machine Learning repository: https://archive.ics.uci.edu/ml/datasets/Communities+and+Crime+Unnormalized """ raw_data = pd.read_csv( cache(github_data_url + "CommViolPredUnnormalizedData.txt"), na_values="?" ) # find the indices where the total violent crimes are known valid_inds = np.where(np.invert(np.isnan(raw_data.iloc[:,-2])))[0] y = np.array(raw_data.iloc[valid_inds,-2], dtype=np.float) # extract the predictive features and remove columns with missing values X = raw_data.iloc[valid_inds,5:-18] valid_cols = np.where(np.isnan(X.values).sum(0) == 0)[0] X = X.iloc[:,valid_cols] return X, y
[ "Predict", "total", "number", "of", "non", "-", "violent", "crimes", "per", "100K", "popuation", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L50-L71
[ "def", "communitiesandcrime", "(", "display", "=", "False", ")", ":", "raw_data", "=", "pd", ".", "read_csv", "(", "cache", "(", "github_data_url", "+", "\"CommViolPredUnnormalizedData.txt\"", ")", ",", "na_values", "=", "\"?\"", ")", "# find the indices where the t...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
diabetes
Return the diabetes data in a nice package.
shap/datasets.py
def diabetes(display=False): """ Return the diabetes data in a nice package. """ d = sklearn.datasets.load_diabetes() df = pd.DataFrame(data=d.data, columns=d.feature_names) # pylint: disable=E1101 return df, d.target
def diabetes(display=False): """ Return the diabetes data in a nice package. """ d = sklearn.datasets.load_diabetes() df = pd.DataFrame(data=d.data, columns=d.feature_names) # pylint: disable=E1101 return df, d.target
[ "Return", "the", "diabetes", "data", "in", "a", "nice", "package", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L73-L78
[ "def", "diabetes", "(", "display", "=", "False", ")", ":", "d", "=", "sklearn", ".", "datasets", ".", "load_diabetes", "(", ")", "df", "=", "pd", ".", "DataFrame", "(", "data", "=", "d", ".", "data", ",", "columns", "=", "d", ".", "feature_names", ...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
iris
Return the classic iris data in a nice package.
shap/datasets.py
def iris(display=False): """ Return the classic iris data in a nice package. """ d = sklearn.datasets.load_iris() df = pd.DataFrame(data=d.data, columns=d.feature_names) # pylint: disable=E1101 if display: return df, [d.target_names[v] for v in d.target] # pylint: disable=E1101 else: return df, d.target
def iris(display=False): """ Return the classic iris data in a nice package. """ d = sklearn.datasets.load_iris() df = pd.DataFrame(data=d.data, columns=d.feature_names) # pylint: disable=E1101 if display: return df, [d.target_names[v] for v in d.target] # pylint: disable=E1101 else: return df, d.target
[ "Return", "the", "classic", "iris", "data", "in", "a", "nice", "package", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L81-L89
[ "def", "iris", "(", "display", "=", "False", ")", ":", "d", "=", "sklearn", ".", "datasets", ".", "load_iris", "(", ")", "df", "=", "pd", ".", "DataFrame", "(", "data", "=", "d", ".", "data", ",", "columns", "=", "d", ".", "feature_names", ")", "...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
adult
Return the Adult census data in a nice package.
shap/datasets.py
def adult(display=False): """ Return the Adult census data in a nice package. """ dtypes = [ ("Age", "float32"), ("Workclass", "category"), ("fnlwgt", "float32"), ("Education", "category"), ("Education-Num", "float32"), ("Marital Status", "category"), ("Occupation", "category"), ("Relationship", "category"), ("Race", "category"), ("Sex", "category"), ("Capital Gain", "float32"), ("Capital Loss", "float32"), ("Hours per week", "float32"), ("Country", "category"), ("Target", "category") ] raw_data = pd.read_csv( cache(github_data_url + "adult.data"), names=[d[0] for d in dtypes], na_values="?", dtype=dict(dtypes) ) data = raw_data.drop(["Education"], axis=1) # redundant with Education-Num filt_dtypes = list(filter(lambda x: not (x[0] in ["Target", "Education"]), dtypes)) data["Target"] = data["Target"] == " >50K" rcode = { "Not-in-family": 0, "Unmarried": 1, "Other-relative": 2, "Own-child": 3, "Husband": 4, "Wife": 5 } for k, dtype in filt_dtypes: if dtype == "category": if k == "Relationship": data[k] = np.array([rcode[v.strip()] for v in data[k]]) else: data[k] = data[k].cat.codes if display: return raw_data.drop(["Education", "Target", "fnlwgt"], axis=1), data["Target"].values else: return data.drop(["Target", "fnlwgt"], axis=1), data["Target"].values
def adult(display=False): """ Return the Adult census data in a nice package. """ dtypes = [ ("Age", "float32"), ("Workclass", "category"), ("fnlwgt", "float32"), ("Education", "category"), ("Education-Num", "float32"), ("Marital Status", "category"), ("Occupation", "category"), ("Relationship", "category"), ("Race", "category"), ("Sex", "category"), ("Capital Gain", "float32"), ("Capital Loss", "float32"), ("Hours per week", "float32"), ("Country", "category"), ("Target", "category") ] raw_data = pd.read_csv( cache(github_data_url + "adult.data"), names=[d[0] for d in dtypes], na_values="?", dtype=dict(dtypes) ) data = raw_data.drop(["Education"], axis=1) # redundant with Education-Num filt_dtypes = list(filter(lambda x: not (x[0] in ["Target", "Education"]), dtypes)) data["Target"] = data["Target"] == " >50K" rcode = { "Not-in-family": 0, "Unmarried": 1, "Other-relative": 2, "Own-child": 3, "Husband": 4, "Wife": 5 } for k, dtype in filt_dtypes: if dtype == "category": if k == "Relationship": data[k] = np.array([rcode[v.strip()] for v in data[k]]) else: data[k] = data[k].cat.codes if display: return raw_data.drop(["Education", "Target", "fnlwgt"], axis=1), data["Target"].values else: return data.drop(["Target", "fnlwgt"], axis=1), data["Target"].values
[ "Return", "the", "Adult", "census", "data", "in", "a", "nice", "package", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L92-L128
[ "def", "adult", "(", "display", "=", "False", ")", ":", "dtypes", "=", "[", "(", "\"Age\"", ",", "\"float32\"", ")", ",", "(", "\"Workclass\"", ",", "\"category\"", ")", ",", "(", "\"fnlwgt\"", ",", "\"float32\"", ")", ",", "(", "\"Education\"", ",", "...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
nhanesi
A nicely packaged version of NHANES I data with surivival times as labels.
shap/datasets.py
def nhanesi(display=False): """ A nicely packaged version of NHANES I data with surivival times as labels. """ X = pd.read_csv(cache(github_data_url + "NHANESI_subset_X.csv")) y = pd.read_csv(cache(github_data_url + "NHANESI_subset_y.csv"))["y"] if display: X_display = X.copy() X_display["Sex"] = ["Male" if v == 1 else "Female" for v in X["Sex"]] return X_display, np.array(y) else: return X, np.array(y)
def nhanesi(display=False): """ A nicely packaged version of NHANES I data with surivival times as labels. """ X = pd.read_csv(cache(github_data_url + "NHANESI_subset_X.csv")) y = pd.read_csv(cache(github_data_url + "NHANESI_subset_y.csv"))["y"] if display: X_display = X.copy() X_display["Sex"] = ["Male" if v == 1 else "Female" for v in X["Sex"]] return X_display, np.array(y) else: return X, np.array(y)
[ "A", "nicely", "packaged", "version", "of", "NHANES", "I", "data", "with", "surivival", "times", "as", "labels", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L131-L141
[ "def", "nhanesi", "(", "display", "=", "False", ")", ":", "X", "=", "pd", ".", "read_csv", "(", "cache", "(", "github_data_url", "+", "\"NHANESI_subset_X.csv\"", ")", ")", "y", "=", "pd", ".", "read_csv", "(", "cache", "(", "github_data_url", "+", "\"NHA...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
cric
A nicely packaged version of CRIC data with progression to ESRD within 4 years as the label.
shap/datasets.py
def cric(display=False): """ A nicely packaged version of CRIC data with progression to ESRD within 4 years as the label. """ X = pd.read_csv(cache(github_data_url + "CRIC_time_4yearESRD_X.csv")) y = np.loadtxt(cache(github_data_url + "CRIC_time_4yearESRD_y.csv")) if display: X_display = X.copy() return X_display, y else: return X, y
def cric(display=False): """ A nicely packaged version of CRIC data with progression to ESRD within 4 years as the label. """ X = pd.read_csv(cache(github_data_url + "CRIC_time_4yearESRD_X.csv")) y = np.loadtxt(cache(github_data_url + "CRIC_time_4yearESRD_y.csv")) if display: X_display = X.copy() return X_display, y else: return X, y
[ "A", "nicely", "packaged", "version", "of", "CRIC", "data", "with", "progression", "to", "ESRD", "within", "4", "years", "as", "the", "label", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L143-L152
[ "def", "cric", "(", "display", "=", "False", ")", ":", "X", "=", "pd", ".", "read_csv", "(", "cache", "(", "github_data_url", "+", "\"CRIC_time_4yearESRD_X.csv\"", ")", ")", "y", "=", "np", ".", "loadtxt", "(", "cache", "(", "github_data_url", "+", "\"CR...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
corrgroups60
Correlated Groups 60 A simulated dataset with tight correlations among distinct groups of features.
shap/datasets.py
def corrgroups60(display=False): """ Correlated Groups 60 A simulated dataset with tight correlations among distinct groups of features. """ # set a constant seed old_seed = np.random.seed() np.random.seed(0) # generate dataset with known correlation N = 1000 M = 60 # set one coefficent from each group of 3 to 1 beta = np.zeros(M) beta[0:30:3] = 1 # build a correlation matrix with groups of 3 tightly correlated features C = np.eye(M) for i in range(0,30,3): C[i,i+1] = C[i+1,i] = 0.99 C[i,i+2] = C[i+2,i] = 0.99 C[i+1,i+2] = C[i+2,i+1] = 0.99 f = lambda X: np.matmul(X, beta) # Make sure the sample correlation is a perfect match X_start = np.random.randn(N, M) X_centered = X_start - X_start.mean(0) Sigma = np.matmul(X_centered.T, X_centered) / X_centered.shape[0] W = np.linalg.cholesky(np.linalg.inv(Sigma)).T X_white = np.matmul(X_centered, W.T) assert np.linalg.norm(np.corrcoef(np.matmul(X_centered, W.T).T) - np.eye(M)) < 1e-6 # ensure this decorrelates the data # create the final data X_final = np.matmul(X_white, np.linalg.cholesky(C).T) X = X_final y = f(X) + np.random.randn(N) * 1e-2 # restore the previous numpy random seed np.random.seed(old_seed) return pd.DataFrame(X), y
def corrgroups60(display=False): """ Correlated Groups 60 A simulated dataset with tight correlations among distinct groups of features. """ # set a constant seed old_seed = np.random.seed() np.random.seed(0) # generate dataset with known correlation N = 1000 M = 60 # set one coefficent from each group of 3 to 1 beta = np.zeros(M) beta[0:30:3] = 1 # build a correlation matrix with groups of 3 tightly correlated features C = np.eye(M) for i in range(0,30,3): C[i,i+1] = C[i+1,i] = 0.99 C[i,i+2] = C[i+2,i] = 0.99 C[i+1,i+2] = C[i+2,i+1] = 0.99 f = lambda X: np.matmul(X, beta) # Make sure the sample correlation is a perfect match X_start = np.random.randn(N, M) X_centered = X_start - X_start.mean(0) Sigma = np.matmul(X_centered.T, X_centered) / X_centered.shape[0] W = np.linalg.cholesky(np.linalg.inv(Sigma)).T X_white = np.matmul(X_centered, W.T) assert np.linalg.norm(np.corrcoef(np.matmul(X_centered, W.T).T) - np.eye(M)) < 1e-6 # ensure this decorrelates the data # create the final data X_final = np.matmul(X_white, np.linalg.cholesky(C).T) X = X_final y = f(X) + np.random.randn(N) * 1e-2 # restore the previous numpy random seed np.random.seed(old_seed) return pd.DataFrame(X), y
[ "Correlated", "Groups", "60", "A", "simulated", "dataset", "with", "tight", "correlations", "among", "distinct", "groups", "of", "features", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L155-L197
[ "def", "corrgroups60", "(", "display", "=", "False", ")", ":", "# set a constant seed", "old_seed", "=", "np", ".", "random", ".", "seed", "(", ")", "np", ".", "random", ".", "seed", "(", "0", ")", "# generate dataset with known correlation", "N", "=", "1000...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
independentlinear60
A simulated dataset with tight correlations among distinct groups of features.
shap/datasets.py
def independentlinear60(display=False): """ A simulated dataset with tight correlations among distinct groups of features. """ # set a constant seed old_seed = np.random.seed() np.random.seed(0) # generate dataset with known correlation N = 1000 M = 60 # set one coefficent from each group of 3 to 1 beta = np.zeros(M) beta[0:30:3] = 1 f = lambda X: np.matmul(X, beta) # Make sure the sample correlation is a perfect match X_start = np.random.randn(N, M) X = X_start - X_start.mean(0) y = f(X) + np.random.randn(N) * 1e-2 # restore the previous numpy random seed np.random.seed(old_seed) return pd.DataFrame(X), y
def independentlinear60(display=False): """ A simulated dataset with tight correlations among distinct groups of features. """ # set a constant seed old_seed = np.random.seed() np.random.seed(0) # generate dataset with known correlation N = 1000 M = 60 # set one coefficent from each group of 3 to 1 beta = np.zeros(M) beta[0:30:3] = 1 f = lambda X: np.matmul(X, beta) # Make sure the sample correlation is a perfect match X_start = np.random.randn(N, M) X = X_start - X_start.mean(0) y = f(X) + np.random.randn(N) * 1e-2 # restore the previous numpy random seed np.random.seed(old_seed) return pd.DataFrame(X), y
[ "A", "simulated", "dataset", "with", "tight", "correlations", "among", "distinct", "groups", "of", "features", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L200-L225
[ "def", "independentlinear60", "(", "display", "=", "False", ")", ":", "# set a constant seed", "old_seed", "=", "np", ".", "random", ".", "seed", "(", ")", "np", ".", "random", ".", "seed", "(", "0", ")", "# generate dataset with known correlation", "N", "=", ...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
rank
Ranking datasets from lightgbm repository.
shap/datasets.py
def rank(): """ Ranking datasets from lightgbm repository. """ rank_data_url = 'https://raw.githubusercontent.com/Microsoft/LightGBM/master/examples/lambdarank/' x_train, y_train = sklearn.datasets.load_svmlight_file(cache(rank_data_url + 'rank.train')) x_test, y_test = sklearn.datasets.load_svmlight_file(cache(rank_data_url + 'rank.test')) q_train = np.loadtxt(cache(rank_data_url + 'rank.train.query')) q_test = np.loadtxt(cache(rank_data_url + 'rank.test.query')) return x_train, y_train, x_test, y_test, q_train, q_test
def rank(): """ Ranking datasets from lightgbm repository. """ rank_data_url = 'https://raw.githubusercontent.com/Microsoft/LightGBM/master/examples/lambdarank/' x_train, y_train = sklearn.datasets.load_svmlight_file(cache(rank_data_url + 'rank.train')) x_test, y_test = sklearn.datasets.load_svmlight_file(cache(rank_data_url + 'rank.test')) q_train = np.loadtxt(cache(rank_data_url + 'rank.train.query')) q_test = np.loadtxt(cache(rank_data_url + 'rank.test.query')) return x_train, y_train, x_test, y_test, q_train, q_test
[ "Ranking", "datasets", "from", "lightgbm", "repository", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/datasets.py#L234-L242
[ "def", "rank", "(", ")", ":", "rank_data_url", "=", "'https://raw.githubusercontent.com/Microsoft/LightGBM/master/examples/lambdarank/'", "x_train", ",", "y_train", "=", "sklearn", ".", "datasets", ".", "load_svmlight_file", "(", "cache", "(", "rank_data_url", "+", "'rank...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
batch_remove_retrain
An approximation of holdout that only retraines the model once. This is alse called ROAR (RemOve And Retrain) in work by Google. It is much more computationally efficient that the holdout method because it masks the most important features in every sample and then retrains the model once, instead of retraining the model for every test sample like the holdout metric.
shap/benchmark/measures.py
def batch_remove_retrain(nmask_train, nmask_test, X_train, y_train, X_test, y_test, attr_train, attr_test, model_generator, metric): """ An approximation of holdout that only retraines the model once. This is alse called ROAR (RemOve And Retrain) in work by Google. It is much more computationally efficient that the holdout method because it masks the most important features in every sample and then retrains the model once, instead of retraining the model for every test sample like the holdout metric. """ warnings.warn("The retrain based measures can incorrectly evaluate models in some cases!") X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # mask nmask top features for each explanation X_train_tmp = X_train.copy() X_train_mean = X_train.mean(0) tie_breaking_noise = const_rand(X_train.shape[1]) * 1e-6 for i in range(len(y_train)): if nmask_train[i] > 0: ordering = np.argsort(-attr_train[i, :] + tie_breaking_noise) X_train_tmp[i, ordering[:nmask_train[i]]] = X_train_mean[ordering[:nmask_train[i]]] X_test_tmp = X_test.copy() for i in range(len(y_test)): if nmask_test[i] > 0: ordering = np.argsort(-attr_test[i, :] + tie_breaking_noise) X_test_tmp[i, ordering[:nmask_test[i]]] = X_train_mean[ordering[:nmask_test[i]]] # train the model with all the given features masked model_masked = model_generator() model_masked.fit(X_train_tmp, y_train) yp_test_masked = model_masked.predict(X_test_tmp) return metric(y_test, yp_test_masked)
def batch_remove_retrain(nmask_train, nmask_test, X_train, y_train, X_test, y_test, attr_train, attr_test, model_generator, metric): """ An approximation of holdout that only retraines the model once. This is alse called ROAR (RemOve And Retrain) in work by Google. It is much more computationally efficient that the holdout method because it masks the most important features in every sample and then retrains the model once, instead of retraining the model for every test sample like the holdout metric. """ warnings.warn("The retrain based measures can incorrectly evaluate models in some cases!") X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # mask nmask top features for each explanation X_train_tmp = X_train.copy() X_train_mean = X_train.mean(0) tie_breaking_noise = const_rand(X_train.shape[1]) * 1e-6 for i in range(len(y_train)): if nmask_train[i] > 0: ordering = np.argsort(-attr_train[i, :] + tie_breaking_noise) X_train_tmp[i, ordering[:nmask_train[i]]] = X_train_mean[ordering[:nmask_train[i]]] X_test_tmp = X_test.copy() for i in range(len(y_test)): if nmask_test[i] > 0: ordering = np.argsort(-attr_test[i, :] + tie_breaking_noise) X_test_tmp[i, ordering[:nmask_test[i]]] = X_train_mean[ordering[:nmask_test[i]]] # train the model with all the given features masked model_masked = model_generator() model_masked.fit(X_train_tmp, y_train) yp_test_masked = model_masked.predict(X_test_tmp) return metric(y_test, yp_test_masked)
[ "An", "approximation", "of", "holdout", "that", "only", "retraines", "the", "model", "once", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L158-L193
[ "def", "batch_remove_retrain", "(", "nmask_train", ",", "nmask_test", ",", "X_train", ",", "y_train", ",", "X_test", ",", "y_test", ",", "attr_train", ",", "attr_test", ",", "model_generator", ",", "metric", ")", ":", "warnings", ".", "warn", "(", "\"The retra...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
keep_retrain
The model is retrained for each test sample with the non-important features set to a constant. If you want to know how important a set of features is you can ask how the model would be different if only those features had existed. To determine this we can mask the other features across the entire training and test datasets, then retrain the model. If we apply compare the output of this retrained model to the original model we can see the effect produced by only knowning the important features. Since for individualized explanation methods each test sample has a different set of most important features we need to retrain the model for every test sample to get the change in model performance when a specified fraction of the most important features are retained.
shap/benchmark/measures.py
def keep_retrain(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is retrained for each test sample with the non-important features set to a constant. If you want to know how important a set of features is you can ask how the model would be different if only those features had existed. To determine this we can mask the other features across the entire training and test datasets, then retrain the model. If we apply compare the output of this retrained model to the original model we can see the effect produced by only knowning the important features. Since for individualized explanation methods each test sample has a different set of most important features we need to retrain the model for every test sample to get the change in model performance when a specified fraction of the most important features are retained. """ warnings.warn("The retrain based measures can incorrectly evaluate models in some cases!") # see if we match the last cached call global _keep_cache args = (X_train, y_train, X_test, y_test, model_generator, metric) cache_match = False if "args" in _keep_cache: if all(a is b for a,b in zip(_keep_cache["args"], args)) and np.all(_keep_cache["attr_test"] == attr_test): cache_match = True X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # this is the model we will retrain many times model_masked = model_generator() # keep nkeep top features and re-train the model for each test explanation X_train_tmp = np.zeros(X_train.shape) X_test_tmp = np.zeros(X_test.shape) yp_masked_test = np.zeros(y_test.shape) tie_breaking_noise = const_rand(X_train.shape[1]) * 1e-6 last_nkeep = _keep_cache.get("nkeep", None) last_yp_masked_test = _keep_cache.get("yp_masked_test", None) for i in tqdm(range(len(y_test)), "Retraining for the 'keep' metric"): if cache_match and last_nkeep[i] == nkeep[i]: yp_masked_test[i] = last_yp_masked_test[i] elif nkeep[i] == attr_test.shape[1]: yp_masked_test[i] = trained_model.predict(X_test[i:i+1])[0] else: # mask out the most important features for this test instance X_train_tmp[:] = X_train X_test_tmp[:] = X_test ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) X_train_tmp[:,ordering[nkeep[i]:]] = X_train[:,ordering[nkeep[i]:]].mean() X_test_tmp[i,ordering[nkeep[i]:]] = X_train[:,ordering[nkeep[i]:]].mean() # retrain the model and make a prediction model_masked.fit(X_train_tmp, y_train) yp_masked_test[i] = model_masked.predict(X_test_tmp[i:i+1])[0] # save our results so the next call to us can be faster when there is redundancy _keep_cache["nkeep"] = nkeep _keep_cache["yp_masked_test"] = yp_masked_test _keep_cache["attr_test"] = attr_test _keep_cache["args"] = args return metric(y_test, yp_masked_test)
def keep_retrain(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is retrained for each test sample with the non-important features set to a constant. If you want to know how important a set of features is you can ask how the model would be different if only those features had existed. To determine this we can mask the other features across the entire training and test datasets, then retrain the model. If we apply compare the output of this retrained model to the original model we can see the effect produced by only knowning the important features. Since for individualized explanation methods each test sample has a different set of most important features we need to retrain the model for every test sample to get the change in model performance when a specified fraction of the most important features are retained. """ warnings.warn("The retrain based measures can incorrectly evaluate models in some cases!") # see if we match the last cached call global _keep_cache args = (X_train, y_train, X_test, y_test, model_generator, metric) cache_match = False if "args" in _keep_cache: if all(a is b for a,b in zip(_keep_cache["args"], args)) and np.all(_keep_cache["attr_test"] == attr_test): cache_match = True X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # this is the model we will retrain many times model_masked = model_generator() # keep nkeep top features and re-train the model for each test explanation X_train_tmp = np.zeros(X_train.shape) X_test_tmp = np.zeros(X_test.shape) yp_masked_test = np.zeros(y_test.shape) tie_breaking_noise = const_rand(X_train.shape[1]) * 1e-6 last_nkeep = _keep_cache.get("nkeep", None) last_yp_masked_test = _keep_cache.get("yp_masked_test", None) for i in tqdm(range(len(y_test)), "Retraining for the 'keep' metric"): if cache_match and last_nkeep[i] == nkeep[i]: yp_masked_test[i] = last_yp_masked_test[i] elif nkeep[i] == attr_test.shape[1]: yp_masked_test[i] = trained_model.predict(X_test[i:i+1])[0] else: # mask out the most important features for this test instance X_train_tmp[:] = X_train X_test_tmp[:] = X_test ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) X_train_tmp[:,ordering[nkeep[i]:]] = X_train[:,ordering[nkeep[i]:]].mean() X_test_tmp[i,ordering[nkeep[i]:]] = X_train[:,ordering[nkeep[i]:]].mean() # retrain the model and make a prediction model_masked.fit(X_train_tmp, y_train) yp_masked_test[i] = model_masked.predict(X_test_tmp[i:i+1])[0] # save our results so the next call to us can be faster when there is redundancy _keep_cache["nkeep"] = nkeep _keep_cache["yp_masked_test"] = yp_masked_test _keep_cache["attr_test"] = attr_test _keep_cache["args"] = args return metric(y_test, yp_masked_test)
[ "The", "model", "is", "retrained", "for", "each", "test", "sample", "with", "the", "non", "-", "important", "features", "set", "to", "a", "constant", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L196-L258
[ "def", "keep_retrain", "(", "nkeep", ",", "X_train", ",", "y_train", ",", "X_test", ",", "y_test", ",", "attr_test", ",", "model_generator", ",", "metric", ",", "trained_model", ",", "random_state", ")", ":", "warnings", ".", "warn", "(", "\"The retrain based ...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
keep_mask
The model is revaluated for each test sample with the non-important features set to their mean.
shap/benchmark/measures.py
def keep_mask(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is revaluated for each test sample with the non-important features set to their mean. """ X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # keep nkeep top features for each test explanation X_test_tmp = X_test.copy() yp_masked_test = np.zeros(y_test.shape) tie_breaking_noise = const_rand(X_train.shape[1], random_state) * 1e-6 mean_vals = X_train.mean(0) for i in range(len(y_test)): if nkeep[i] < X_test.shape[1]: ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) X_test_tmp[i,ordering[nkeep[i]:]] = mean_vals[ordering[nkeep[i]:]] yp_masked_test = trained_model.predict(X_test_tmp) return metric(y_test, yp_masked_test)
def keep_mask(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is revaluated for each test sample with the non-important features set to their mean. """ X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # keep nkeep top features for each test explanation X_test_tmp = X_test.copy() yp_masked_test = np.zeros(y_test.shape) tie_breaking_noise = const_rand(X_train.shape[1], random_state) * 1e-6 mean_vals = X_train.mean(0) for i in range(len(y_test)): if nkeep[i] < X_test.shape[1]: ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) X_test_tmp[i,ordering[nkeep[i]:]] = mean_vals[ordering[nkeep[i]:]] yp_masked_test = trained_model.predict(X_test_tmp) return metric(y_test, yp_masked_test)
[ "The", "model", "is", "revaluated", "for", "each", "test", "sample", "with", "the", "non", "-", "important", "features", "set", "to", "their", "mean", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L260-L281
[ "def", "keep_mask", "(", "nkeep", ",", "X_train", ",", "y_train", ",", "X_test", ",", "y_test", ",", "attr_test", ",", "model_generator", ",", "metric", ",", "trained_model", ",", "random_state", ")", ":", "X_train", ",", "X_test", "=", "to_array", "(", "X...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
keep_impute
The model is revaluated for each test sample with the non-important features set to an imputed value. Note that the imputation is done using a multivariate normality assumption on the dataset. This depends on being able to estimate the full data covariance matrix (and inverse) accuractly. So X_train.shape[0] should be significantly bigger than X_train.shape[1].
shap/benchmark/measures.py
def keep_impute(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is revaluated for each test sample with the non-important features set to an imputed value. Note that the imputation is done using a multivariate normality assumption on the dataset. This depends on being able to estimate the full data covariance matrix (and inverse) accuractly. So X_train.shape[0] should be significantly bigger than X_train.shape[1]. """ X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # keep nkeep top features for each test explanation C = np.cov(X_train.T) C += np.eye(C.shape[0]) * 1e-6 X_test_tmp = X_test.copy() yp_masked_test = np.zeros(y_test.shape) tie_breaking_noise = const_rand(X_train.shape[1], random_state) * 1e-6 mean_vals = X_train.mean(0) for i in range(len(y_test)): if nkeep[i] < X_test.shape[1]: ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) observe_inds = ordering[:nkeep[i]] impute_inds = ordering[nkeep[i]:] # impute missing data assuming it follows a multivariate normal distribution Coo_inv = np.linalg.inv(C[observe_inds,:][:,observe_inds]) Cio = C[impute_inds,:][:,observe_inds] impute = mean_vals[impute_inds] + Cio @ Coo_inv @ (X_test[i, observe_inds] - mean_vals[observe_inds]) X_test_tmp[i, impute_inds] = impute yp_masked_test = trained_model.predict(X_test_tmp) return metric(y_test, yp_masked_test)
def keep_impute(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is revaluated for each test sample with the non-important features set to an imputed value. Note that the imputation is done using a multivariate normality assumption on the dataset. This depends on being able to estimate the full data covariance matrix (and inverse) accuractly. So X_train.shape[0] should be significantly bigger than X_train.shape[1]. """ X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # keep nkeep top features for each test explanation C = np.cov(X_train.T) C += np.eye(C.shape[0]) * 1e-6 X_test_tmp = X_test.copy() yp_masked_test = np.zeros(y_test.shape) tie_breaking_noise = const_rand(X_train.shape[1], random_state) * 1e-6 mean_vals = X_train.mean(0) for i in range(len(y_test)): if nkeep[i] < X_test.shape[1]: ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) observe_inds = ordering[:nkeep[i]] impute_inds = ordering[nkeep[i]:] # impute missing data assuming it follows a multivariate normal distribution Coo_inv = np.linalg.inv(C[observe_inds,:][:,observe_inds]) Cio = C[impute_inds,:][:,observe_inds] impute = mean_vals[impute_inds] + Cio @ Coo_inv @ (X_test[i, observe_inds] - mean_vals[observe_inds]) X_test_tmp[i, impute_inds] = impute yp_masked_test = trained_model.predict(X_test_tmp) return metric(y_test, yp_masked_test)
[ "The", "model", "is", "revaluated", "for", "each", "test", "sample", "with", "the", "non", "-", "important", "features", "set", "to", "an", "imputed", "value", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L283-L318
[ "def", "keep_impute", "(", "nkeep", ",", "X_train", ",", "y_train", ",", "X_test", ",", "y_test", ",", "attr_test", ",", "model_generator", ",", "metric", ",", "trained_model", ",", "random_state", ")", ":", "X_train", ",", "X_test", "=", "to_array", "(", ...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
keep_resample
The model is revaluated for each test sample with the non-important features set to resample background values.
shap/benchmark/measures.py
def keep_resample(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is revaluated for each test sample with the non-important features set to resample background values. """ # why broken? overwriting? X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # how many samples to take nsamples = 100 # keep nkeep top features for each test explanation N,M = X_test.shape X_test_tmp = np.tile(X_test, [1, nsamples]).reshape(nsamples * N, M) tie_breaking_noise = const_rand(M) * 1e-6 inds = sklearn.utils.resample(np.arange(N), n_samples=nsamples, random_state=random_state) for i in range(N): if nkeep[i] < M: ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) X_test_tmp[i*nsamples:(i+1)*nsamples, ordering[nkeep[i]:]] = X_train[inds, :][:, ordering[nkeep[i]:]] yp_masked_test = trained_model.predict(X_test_tmp) yp_masked_test = np.reshape(yp_masked_test, (N, nsamples)).mean(1) # take the mean output over all samples return metric(y_test, yp_masked_test)
def keep_resample(nkeep, X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model, random_state): """ The model is revaluated for each test sample with the non-important features set to resample background values. """ # why broken? overwriting? X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # how many samples to take nsamples = 100 # keep nkeep top features for each test explanation N,M = X_test.shape X_test_tmp = np.tile(X_test, [1, nsamples]).reshape(nsamples * N, M) tie_breaking_noise = const_rand(M) * 1e-6 inds = sklearn.utils.resample(np.arange(N), n_samples=nsamples, random_state=random_state) for i in range(N): if nkeep[i] < M: ordering = np.argsort(-attr_test[i,:] + tie_breaking_noise) X_test_tmp[i*nsamples:(i+1)*nsamples, ordering[nkeep[i]:]] = X_train[inds, :][:, ordering[nkeep[i]:]] yp_masked_test = trained_model.predict(X_test_tmp) yp_masked_test = np.reshape(yp_masked_test, (N, nsamples)).mean(1) # take the mean output over all samples return metric(y_test, yp_masked_test)
[ "The", "model", "is", "revaluated", "for", "each", "test", "sample", "with", "the", "non", "-", "important", "features", "set", "to", "resample", "background", "values", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L320-L345
[ "def", "keep_resample", "(", "nkeep", ",", "X_train", ",", "y_train", ",", "X_test", ",", "y_test", ",", "attr_test", ",", "model_generator", ",", "metric", ",", "trained_model", ",", "random_state", ")", ":", "# why broken? overwriting?", "X_train", ",", "X_tes...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
local_accuracy
The how well do the features plus a constant base rate sum up to the model output.
shap/benchmark/measures.py
def local_accuracy(X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model): """ The how well do the features plus a constant base rate sum up to the model output. """ X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # keep nkeep top features and re-train the model for each test explanation yp_test = trained_model.predict(X_test) return metric(yp_test, strip_list(attr_test).sum(1))
def local_accuracy(X_train, y_train, X_test, y_test, attr_test, model_generator, metric, trained_model): """ The how well do the features plus a constant base rate sum up to the model output. """ X_train, X_test = to_array(X_train, X_test) # how many features to mask assert X_train.shape[1] == X_test.shape[1] # keep nkeep top features and re-train the model for each test explanation yp_test = trained_model.predict(X_test) return metric(yp_test, strip_list(attr_test).sum(1))
[ "The", "how", "well", "do", "the", "features", "plus", "a", "constant", "base", "rate", "sum", "up", "to", "the", "model", "output", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L384-L396
[ "def", "local_accuracy", "(", "X_train", ",", "y_train", ",", "X_test", ",", "y_test", ",", "attr_test", ",", "model_generator", ",", "metric", ",", "trained_model", ")", ":", "X_train", ",", "X_test", "=", "to_array", "(", "X_train", ",", "X_test", ")", "...
b280cb81d498b9d98565cad8dd16fc88ae52649f
train
const_rand
Generate a random array with a fixed seed.
shap/benchmark/measures.py
def const_rand(size, seed=23980): """ Generate a random array with a fixed seed. """ old_seed = np.random.seed() np.random.seed(seed) out = np.random.rand(size) np.random.seed(old_seed) return out
def const_rand(size, seed=23980): """ Generate a random array with a fixed seed. """ old_seed = np.random.seed() np.random.seed(seed) out = np.random.rand(size) np.random.seed(old_seed) return out
[ "Generate", "a", "random", "array", "with", "a", "fixed", "seed", "." ]
slundberg/shap
python
https://github.com/slundberg/shap/blob/b280cb81d498b9d98565cad8dd16fc88ae52649f/shap/benchmark/measures.py#L401-L408
[ "def", "const_rand", "(", "size", ",", "seed", "=", "23980", ")", ":", "old_seed", "=", "np", ".", "random", ".", "seed", "(", ")", "np", ".", "random", ".", "seed", "(", "seed", ")", "out", "=", "np", ".", "random", ".", "rand", "(", "size", "...
b280cb81d498b9d98565cad8dd16fc88ae52649f