我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用chainer.cuda.check_cuda_available()。
def _check_cupy(): """Set xp Note: cuda.cupy if gpu, numpy otherwise Example: :: self.xp = self._check_cupy() Returns: cuda.cupy if gpu, numpy otherwise """ try: cuda.check_cuda_available() return cuda.cupy # if gpu is not available, RuntimeError arises except RuntimeError: return np
def __init__(self, in_channels, out_channels, ksize, stride=1, real=0, wscale=1.0): super(ConvolutionRBM, self).__init__( conv=L.Convolution2D(in_channels, out_channels, ksize, stride=stride, wscale=wscale), ) # if gpu >= 0: # cuda.check_cuda_available() # xp = cuda.cupy # if gpu >= 0 else np self.conv.add_param("a", in_channels) # dtype=xp.float32 self.conv.a.data.fill(0.) self.in_channels = in_channels self.out_channels = out_channels self.ksize = ksize self.real = real self.rbm_train = False # default value is false
def to_gpu(self, device=None): """Copies parameter variables and persistent values to GPU. This method does not handle non-registered attributes. If some of such attributes must be copied to GPU, the link implementation must override this method to do so. Args: device: Target device specifier. If omitted, the current device is used. Returns: self """ cuda.check_cuda_available() if not self._cpu: return self d = self.__dict__ with cuda.get_device(device): for name in self._params: d[name].to_gpu() for name in self._persistent: value = d[name] if isinstance(value, numpy.ndarray): d[name] = cuda.to_gpu(value) self._cpu = False return self
def train(epoch=10, batch_size=32, gpu=False): if gpu: cuda.check_cuda_available() xp = cuda.cupy if gpu else np td = TrainingData(LABEL_FILE, img_root=IMAGES_ROOT, image_property=IMAGE_PROP) # make mean image if not os.path.isfile(MEAN_IMAGE_FILE): print("make mean image...") td.make_mean_image(MEAN_IMAGE_FILE) else: td.mean_image_file = MEAN_IMAGE_FILE # train model label_def = LabelingMachine.read_label_def(LABEL_DEF_FILE) model = alex.Alex(len(label_def)) optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9) optimizer.setup(model) epoch = epoch batch_size = batch_size print("Now our model is {0} classification task.".format(len(label_def))) print("begin training the model. epoch:{0} batch size:{1}.".format(epoch, batch_size)) if gpu: model.to_gpu() for i in range(epoch): print("epoch {0}/{1}: (learning rate={2})".format(i + 1, epoch, optimizer.lr)) td.shuffle(overwrite=True) for x_batch, y_batch in td.generate_batches(batch_size): x = chainer.Variable(xp.asarray(x_batch)) t = chainer.Variable(xp.asarray(y_batch)) optimizer.update(model, x, t) print("loss: {0}, accuracy: {1}".format(float(model.loss.data), float(model.accuracy.data))) serializers.save_npz(MODEL_FILE, model) optimizer.lr *= 0.97
def train(args): time_start = timer() if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() cuda.check_cuda_available() if args.path_vocab == '': vocab = create_from_dir(args.path_corpus) else: vocab = Vocabulary() vocab.load(args.path_vocab) logger.info("loaded vocabulary") if args.context_representation != 'word': # for deps or ner context representation, we need a new context vocab for NS or HSM loss function. vocab_context = create_from_annotated_dir(args.path_corpus, representation=args.context_representation) else : vocab_context = vocab loss_func = get_loss_func(args, vocab_context) model = get_model(args, loss_func, vocab) if args.gpu >= 0: model.to_gpu() logger.debug("model sent to gpu") optimizer = chainer.optimizers.Adam() optimizer.setup(model) if os.path.isfile(args.path_corpus): train, val = get_data(args.path_corpus, vocab) if args.test: train = train[:100] val = val[:100] train_iter = WindowIterator(train, args.window, args.batchsize) val_iter = WindowIterator(val, args.window, args.batchsize, repeat=False) else: train_iter = DirWindowIterator(path=args.path_corpus, vocab=vocab, window_size=args.window, batch_size=args.batchsize) updater = training.StandardUpdater(train_iter, optimizer, converter=convert, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.path_out) if os.path.isfile(args.path_corpus): trainer.extend(extensions.Evaluator(val_iter, model, converter=convert, device=args.gpu)) trainer.extend(extensions.LogReport()) if os.path.isfile(args.path_corpus): trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time'])) else: trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'elapsed_time'])) # trainer.extend(extensions.ProgressBar()) trainer.run() model = create_model(args, model, vocab) time_end = timer() model.metadata["execution_time"] = time_end - time_start return model