本文整理汇总了Python中chainer.cuda.check_cuda_available方法的典型用法代码示例。如果您正苦于以下问题:Python cuda.check_cuda_available方法的具体用法?Python cuda.check_cuda_available怎么用?Python cuda.check_cuda_available使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.cuda
的用法示例。
在下文中一共展示了cuda.check_cuda_available方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import check_cuda_available [as 别名]
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
示例2: train
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import check_cuda_available [as 别名]
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