add balanced serial iterator example in tox21
@@ -41,6 +41,7 @@ def main():
# Supported preprocessing/network list
method_list = ['nfp', 'ggnn', 'schnet', 'weavenet']
label_names = D.get_tox21_label_names()
+ iterator_type = ['serial', 'balanced']
parser = argparse.ArgumentParser(
description='Multitask Learning with Tox21.')
@@ -51,6 +52,10 @@ def main():
default='', help='target label for logistic '
'regression. Use all labels if this option '
'is not specified.')
+ parser.add_argument('--iterator_type', type=str, choices=iterator_type,
+ default='serial', help='iterator type. If `balanced` '
+ 'is specified, data is sampled to take same number of'
+ 'positive/negative labels during training.')
parser.add_argument('--conv-layers', '-c', type=int, default=4,
help='number of convolution layers')
parser.add_argument('--batchsize', '-b', type=int, default=128,
@@ -81,7 +86,20 @@ def main():
predictor_ = predictor.build_predictor(
method, args.unit_num, args.conv_layers, class_num)
+ iterator_type = args.iterator_type
+ if iterator_type == 'serial':
train_iter = I.SerialIterator(train, args.batchsize)
+ elif iterator_type == 'balanced':
+ from chainer_chemistry.iterators.balanced_serial_iterator import BalancedSerialIterator # NOQA
+ if class_num > 1:
+ raise ValueError('BalancedSerialIterator can be used with only one'
+ 'label classification, please specify label to'
+ 'predict by --label option.')
+ train_iter = BalancedSerialIterator(
+ train, args.batchsize, train.features[:, -1], ignore_labels=-1)
+ train_iter.show_label_stats()
+ else:
+ raise ValueError('Invalid iterator type {}'.format(iterator_type))
val_iter = I.SerialIterator(val, args.batchsize,
repeat=False, shuffle=False)
classifier = L.Classifier(predictor_,
