How to use the torch.cuda function in torch

To help you get started, we’ve selected a few torch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github roytseng-tw / adversarial-pose-pytorch / hgpose-ae / View on Github external
def get_args():
    opts = parser.parse_args()
    if opts.cuda and not torch.cuda.is_available():
        sys.exit('Cuda is not available.')
    return opts
github sbarratt / inception-score-pytorch / View on Github external
def inception_score(imgs, cuda=True, batch_size=32, resize=False, splits=1):
    """Computes the inception score of the generated images imgs

    imgs -- Torch dataset of (3xHxW) numpy images normalized in the range [-1, 1]
    cuda -- whether or not to run on GPU
    batch_size -- batch size for feeding into Inception v3
    splits -- number of splits
    N = len(imgs)

    assert batch_size > 0
    assert N > batch_size

    # Set up dtype
    if cuda:
        dtype = torch.cuda.FloatTensor
        if torch.cuda.is_available():
            print("WARNING: You have a CUDA device, so you should probably set cuda=True")
        dtype = torch.FloatTensor

    # Set up dataloader
    dataloader =, batch_size=batch_size)

    # Load inception model
    inception_model = inception_v3(pretrained=True, transform_input=False).type(dtype)
    up = nn.Upsample(size=(299, 299), mode='bilinear').type(dtype)
    def get_pred(x):
        if resize:
            x = up(x)
        x = inception_model(x)
github CoinCheung / DeepLab-v3-plus-cityscapes / View on Github external
def evaluate():
    ## setup
    cfg = config_factory['resnet_cityscapes']
    args = parse_args()
    if not args.local_rank == -1:
                    backend = 'nccl',
                    init_method = 'tcp://{}'.format(cfg.port),
                    world_size = torch.cuda.device_count(),
                    rank = args.local_rank
        FORMAT = '%(levelname)s %(filename)s(%(lineno)d): %(message)s'
        log_level = logging.INFO
        if dist.is_initialized() and dist.get_rank()!=0:
            log_level = logging.ERROR
        logging.basicConfig(level=log_level, format=FORMAT, stream=sys.stdout)
    logger = logging.getLogger()

    ## model
github ucbdrive / spc / spn+hierarchical_carla / View on Github external
def sample_discrete_action(args, net, obs_var, prev_action=None):
    start_time = time.time()
    obs = np.repeat(np.expand_dims(obs_var, axis=0), args.batch_size, axis=0)
    obs = Variable(torch.from_numpy(obs), requires_grad=False).type(torch.Tensor)
    if torch.cuda.is_available():
        obs = obs.cuda()
    prob = torch.ones(args.num_total_act, args.num_total_act) / float(args.num_total_act)
    with torch.no_grad():
        for i in range(6):
            all_actions = generate_action_sample(args, prob, 6 * args.batch_size, args.pred_step, prev_action)
            one_hot_actions = generate_one_hot_actions(all_actions, args.num_total_act)
            if torch.cuda.is_available():
                one_hot_actions = one_hot_actions.cuda()

            actions = Variable(one_hot_actions, requires_grad=False)
            loss = get_action_loss(args, net, obs, actions)  # needs updating
            all_losses = from_variable_to_numpy(loss)

            if i < 5:
                indices = np.argsort(all_losses)[:args.batch_size]
                prob = generate_probs(args, all_actions[indices], prev_action)
                idx = np.argmin(all_losses)
                which_action = int(from_variable_to_numpy(all_actions)[idx, 0])

    print('Sampling takes %0.2f seconds, selected action: %d.' % (time.time() - start_time, which_action))
    return which_action
github godisboy / SN-GAN / View on Github external
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
dataloader =, batch_size=opt.batchsize,
                                         shuffle=True, num_workers=int(2))

if opt.manualSeed is None:
    opt.manualSeed = random.randint(1, 10000)
print("Random Seed: ", opt.manualSeed)

if opt.cuda:

cudnn.benchmark = True

def weight_filler(m):
    classname = m.__class__.__name__
    if classname.find('Conv' or 'SNConv') != -1:, 0.02)
    elif classname.find('BatchNorm') != -1:, 0.02)

n_dis = opt.n_dis
nz =

G = SNResGenerator(64, nz, 4)
SND = SNResDiscriminator(64, 4)
github RubensZimbres / Repo-2019 / BERT_Embeddings_PyTorch / embed_tsv / View on Github external
import torch
import torch.optim as optim
from transformers import *

class Configuracao(dict):
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)
    def set(self, key, val):
        self[key] = val
        setattr(self, key, val)


config = Configuracao(

from pytorch_pretrained_bert import BertTokenizer

bert_token = BertTokenizer.from_pretrained(
github csyhhu / Awesome-Deep-Neural-Network-Compression / Codes / INQ / View on Github external
return s == 'True'

parser = argparse.ArgumentParser(description='Approximation Training')
parser.add_argument('--model', '-m', type=str, default='ResNet20', help='Model Arch')
parser.add_argument('--dataset', '-d', type=str, default='CIFAR10', help='Dataset')
parser.add_argument('--optimizer', '-o', type=str, default='Adam', help='Optimizer Method')
parser.add_argument('--bitW', '-bw', type=int, default=1, help='Number of quantization bits')
parser.add_argument('--quantize_portion','-q', nargs='+', help='Variation of Quantization Portion', required=True)
parser.add_argument('--exp_spec', '-e', type=str, default='', help='Experiment Specification')
parser.add_argument('--init_lr', '-lr', type=float, default=1e-3, help='Initial Learning rate')
parser.add_argument('--n_epoch', '-n', type=int, default=100, help='Maximum training epochs')
parser.add_argument('--batch_size', '-bs', type=int, default=128, help='Batch size')
args = parser.parse_args()

# ------------------------------------------
use_cuda = torch.cuda.is_available()
model_name = args.model
dataset_name = args.dataset
quantize_portion_list = args.quantize_portion
MAX_EPOCH = args.n_epoch
optimizer_type = args.optimizer # ['SGD', 'SGD-M', 'adam']
dataset_type = 'large' if dataset_name in ['ImageNet'] else 'small'
batch_size = args.batch_size
bitW = args.bitW
# ------------------------------------------

input('Take a look')

# Integerize the quantization portion
for idx, quantize_portion in enumerate(quantize_portion_list):
    quantize_portion_list[idx] = int(quantize_portion)
github BigRedT / vico / exp / imagenet / View on Github external
def compute_entity_attr_reps(model,dataloader,exp_const):
    num_classes = len(dataloader.dataset.wnids)
    rep_dim =

    print('Allocating memory for storing entity-attr reps ...')
    reps = np.zeros([num_classes,rep_dim],dtype=np.float32)
    num_imgs_per_class = np.zeros([num_classes],dtype=np.int32)
    img_mean = Variable(torch.cuda.FloatTensor(model.img_mean),volatile=True)
    img_std = Variable(torch.cuda.FloatTensor(model.img_std),volatile=True)
    # Set mode

    print('Aggregating image features ...')
    for it,data in enumerate(tqdm(dataloader)):
        # Forward pass
        imgs = Variable(data['img'].cuda().float()/255.,volatile=True)
        imgs = dataloader.dataset.normalize(
        imgs = imgs.permute(0,3,1,2)
        last_layer_feats_normalized, _ =
        last_layer_feats_normalized = \
github FTdiscovery / 64CrazyhouseDeepLearning / View on Github external
def trainNetwork(boards, outputs, EPOCHS=1, BATCH_SIZE=1000, LR=0.001,
                 saveDirectory='', OUTPUT_ARRAY_LEN=4504):

    outputs = torch.from_numpy(outputs)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    data = TrainingDataset(boards, outputs)  # use answers instead of actions when choosing CEL

    trainLoader =, batch_size=BATCH_SIZE, shuffle=True)
    # to create a prediction, create a new dataset with input of the states, and output should just be np.zeros()

    # this is a convolutional neural network
    #model = ChessConvNet(OUTPUT_ARRAY_LEN).double()

    # this is a residual network
    model = ChessResNet.ResNetMainBottleNeck().double()

        model = torch.load(loadDirectory)
github lipiji / dialogue-hred-vhred / View on Github external
# input gate bias for GRUs
            if self.config.mode == 'train' and self.config.checkpoint is None:
                print('Parameter initiailization')
                for name, param in self.model.named_parameters():
                    if 'weight_hh' in name:
                        print('\t' + name)

                    # bias_hh is concatenation of reset, input, new gates
                    # only set the input gate bias to 2.0
                    if 'bias_hh' in name:
                        print('\t' + name)
                        dim = int(param.size(0) / 3)
              [dim:2 * dim].fill_(2.0)

        if torch.cuda.is_available() and cuda:

        # Overview Parameters
        print('Model Parameters')
        for name, param in self.model.named_parameters():
            print('\t' + name + '\t', list(param.size()))

        if self.config.checkpoint:

        if self.is_train:
            #self.writer = TensorboardWriter(self.config.logdir)
            self.optimizer = self.config.optimizer(
                filter(lambda p: p.requires_grad, self.model.parameters()),