code: def main():
      parser = argparse.ArgumentParser()
      parser.add_argument(
          "--device",
          "-d",
          type=str,
          default="-1",
          help="Device specifier. Either ChainerX device "
          "specifier or an integer. If non-negative integer, "
          "CuPy arrays with specified device id are used. If "
          "negative integer, NumPy arrays are used",
      )
      parser.add_argument(
          "--unit", "-u", default=100, type=int, help="number of units"
      )
      parser.add_argument(
          "--window", "-w", default=5, type=int, help="window size"
      )
      parser.add_argument(
          "--batchsize",
          "-b",
          type=int,
          default=1000,
          help="learning minibatch size",
      )
      parser.add_argument(
          "--epoch",
          "-e",
          default=20,
          type=int,
          help="number of epochs to learn",
      )
      parser.add_argument(
          "--model",
          "-m",
          choices=["skipgram", "cbow"],
          default="skipgram",
          help='model type ("skipgram", "cbow")',
      )
      parser.add_argument(
          "--negative-size",
          default=5,
          type=int,
          help="number of negative samples",
      )
      parser.add_argument(
          "--out-type",
          "-o",
          choices=["hsm", "ns", "original"],
          default="hsm",
          help='output model type ("hsm": hierarchical softmax, '
          '"ns": negative sampling, "original": '
          "no approximation)",
      )
      parser.add_argument(
          "--out",
          default="result",
          help="Directory to output the result",
      )
      parser.add_argument(
          "--resume",
          "-r",
          type=str,
          help="Resume the training from snapshot",
      )
      parser.add_argument(
          "--snapshot-interval", type=int, help="Interval of snapshots"
      )
      parser.add_argument("--test", dest="test", action="store_true")
      parser.set_defaults(test=False)
      group = parser.add_argument_group("deprecated arguments")
      group.add_argument(
          "--gpu",
          "-g",
          dest="device",
          type=int,
          nargs="?",
          const=0,
          help="GPU ID (negative value indicates CPU)",
      )
      args = parser.parse_args()
  
      if chainer.get_dtype() == np.float16:
          warnings.warn(
              "This example may cause NaN in FP16 mode.", RuntimeWarning
          )
  
      device = chainer.get_device(args.device)
      device.use()
  
      if args.snapshot_interval is None:
          args.snapshot_interval = args.epoch
      args.snapshot_interval = min(args.snapshot_interval, args.epoch)
  
      print("Device: {}".format(device))
      print("# unit: {}".format(args.unit))
      print("Window: {}".format(args.window))
      print("Minibatch-size: {}".format(args.batchsize))
      print("# epoch: {}".format(args.epoch))
      print("Training model: {}".format(args.model))
      print("Output type: {}".format(args.out_type))
      print("")
  
      # Load the dataset
      train, val, _ = chainer.datasets.get_ptb_words()
      counts = collections.Counter(train)
      counts.update(collections.Counter(val))
      n_vocab = max(train) + 1
  
      if args.test:
          train = train[:100]
          val = val[:100]
  
      vocab = chainer.datasets.get_ptb_words_vocabulary()
      index2word = {wid: word for word, wid in six.iteritems(vocab)}
  
      print("n_vocab: %d" % n_vocab)
      print("data length: %d" % len(train))
  
      if args.out_type == "hsm":
          HSM = L.BinaryHierarchicalSoftmax
          tree = HSM.create_huffman_tree(counts)
          loss_func = HSM(args.unit, tree)
          loss_func.W.array[...] = 0
      elif args.out_type == "ns":
          cs = [counts[w] for w in range(len(counts))]
          loss_func = L.NegativeSampling(
              args.unit, cs, args.negative_size
          )
          loss_func.W.array[...] = 0
      elif args.out_type == "original":
          loss_func = SoftmaxCrossEntropyLoss(args.unit, n_vocab)
      else:
          raise Exception(
              "Unknown output type: {}".format(args.out_type)
          )
  
      # Choose the model
      if args.model == "skipgram":
          model = SkipGram(n_vocab, args.unit, loss_func)
      elif args.model == "cbow":
          model = ContinuousBoW(n_vocab, args.unit, loss_func)
      else:
          raise Exception("Unknown model type: {}".format(args.model))
  
      model.to_device(device)
  
      # Set up an optimizer
      optimizer = O.Adam()
      optimizer.setup(model)
  
      # Set up an iterator
      train_iter = WindowIterator(train, args.window, args.batchsize)
      val_iter = WindowIterator(
          val, args.window, args.batchsize, repeat=False
      )
  
      # Set up an updater
      updater = training.updaters.StandardUpdater(
          train_iter, optimizer, converter=convert, device=device
      )
  
      # Set up a trainer
      trainer = training.Trainer(
          updater, (args.epoch, "epoch"), out=args.out
      )
  
      trainer.extend(
          extensions.Evaluator(
              val_iter, model, converter=convert, device=device
          )
      )
      trainer.extend(extensions.LogReport())
      trainer.extend(
          extensions.PrintReport(
              ["epoch", "main/loss", "validation/main/loss"]
          )
      )
      trainer.extend(extensions.ProgressBar())
  
      trainer.extend(
          extensions.snapshot(
              filename="snapshot_epoch_{.updater.epoch}"
          ),
          trigger=(args.snapshot_interval, "epoch"),
      )
  
      if args.resume is not None:
          chainer.serializers.load_npz(args.resume, trainer)
      trainer.run()
  
      # Save the word2vec model
      with open(os.path.join(args.out, "word2vec.model"), "w") as f:
          f.write("%d %d\n" % (len(index2word), args.unit))
          w = cuda.to_cpu(model.embed.W.array)
          for i, wi in enumerate(w):
              v = " ".join(map(str, wi))
              f.write("%s %s\n" % (index2word[i], v))
  
imports: import argparse
  import collections
  import os
  import six
  import warnings
  import numpy
  import chainer
  from chainer.backends import cuda
  import chainer.functions
  import chainer.initializers
  import chainer.links
  import chainer.optimizers
  from chainer import reporter
  from chainer import training
  from chainer.training import extensions
label_desc: Use image node_id 2 for calling a global function with example usage: main() without return types