Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.s_train_loader = torch.utils.data.DataLoader(s_train_set, batch_size=128,
shuffle=True, num_workers=8)
t_train_set = torchvision.datasets.MNIST(root='./MNIST/', train=True, download = True, transform = MNIST_transform)
self.t_train_loader = torch.utils.data.DataLoader(t_train_set, batch_size=128,
shuffle=True, num_workers=8)
# s_val_set = torchvision.datasets.SVHN(root = './SVHN/', split='train',download = True, transform = SVHN_transform)
# self.s_val_loader = torch.utils.data.DataLoader(s_val_set, batch_size=128,
# shuffle=True, num_workers=8)
s_test_set = torchvision.datasets.SVHN(root = './SVHN/', split='test', download = True, transform = SVHN_transform)
self.s_test_loader = torch.utils.data.DataLoader(s_test_set, batch_size=128,
shuffle=False, num_workers=8)
t_test_set = torchvision.datasets.MNIST(root='./MNIST/', train=False, download = True, transform = MNIST_transform)
self.t_test_loader = torch.utils.data.DataLoader(t_train_set, batch_size=128,
shuffle=False, num_workers=8)
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda:0" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
train_loader = torch.utils.data.DataLoader(
datasets.MNIST(os.path.expanduser('~/local/data'), train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(os.path.expanduser('~/local/data'), train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.test_batch_size, shuffle=True, **kwargs)
model = Net().to(device)
optimizer = optim.Adam(model.parameters(), lr=args.lr)
for epoch in range(1, args.epochs + 1):
train(args, model, device, train_loader, optimizer, epoch)
test(args, model, device, test_loader)
if (args.save_model):
torch.save(model.state_dict(),"mnist_cnn.pt")
def load_mnist():
torch.cuda.manual_seed(1)
kwargs = {'num_workers': 1, 'pin_memory': True}
path = 'data/'
if args.scratch:
path = '/scratch/eecs-share/ratzlafn/' + path
train_loader = torch.utils.data.DataLoader(
datasets.MNIST(path, train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=32, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(path, train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=32, shuffle=True, **kwargs)
return train_loader, test_loader
def go(arg):
MARGIN = 0.1
util.makedirs('./conv/')
torch.manual_seed(arg.seed)
writer = SummaryWriter()
mnist = torchvision.datasets.MNIST(root=arg.data, train=True, download=True, transform=transforms.ToTensor())
data = util.totensor(mnist, shuffle=True)
assert data.min() == 0 and data.max() == 1.0
if arg.limit is not None:
data = data[:arg.limit]
model = ConvModel(data.size(), k=arg.k, emb_size=arg.emb_size,
gadd=arg.gadditional, radd=arg.radditional, range=arg.range,
min_sigma=arg.min_sigma, fix_value=arg.fix_value, encoder=arg.encoder)
# model = ConvModelFlat(data.size(), k=arg.k,
# gadd=arg.gadditional, radd=arg.radditional, range=arg.range,
# min_sigma=arg.min_sigma, fix_value=arg.fix_value)
def val_dataloader(self):
# OPTIONAL
return DataLoader(MNIST(os.getcwd(), train=True, download=True, transform=transforms.ToTensor()), batch_size=self.hparams.batch_size)
torch.manual_seed(123)
parser = argparse.ArgumentParser(description='PyTorch MNIST WAE-MMD')
parser.add_argument('-batch_size', type=int, default=100, metavar='N',
help='input batch size for training (default: 100)')
parser.add_argument('-epochs', type=int, default=100, help='number of epochs to train (default: 100)')
parser.add_argument('-lr', type=float, default=0.0001, help='learning rate (default: 0.0001)')
parser.add_argument('-dim_h', type=int, default=128, help='hidden dimension (default: 128)')
parser.add_argument('-n_z', type=int, default=8, help='hidden dimension of z (default: 8)')
parser.add_argument('-LAMBDA', type=float, default=10, help='regularization coef MMD term (default: 10)')
parser.add_argument('-n_channel', type=int, default=1, help='input channels (default: 1)')
parser.add_argument('-sigma', type=float, default=1, help='variance of hidden dimension (default: 1)')
args = parser.parse_args()
trainset = MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True)
testset = MNIST(root='./data/',
train=False,
transform=transforms.ToTensor(),
download=True)
train_loader = DataLoader(dataset=trainset,
batch_size=args.batch_size,
shuffle=True)
test_loader = DataLoader(dataset=testset,
batch_size=104,
shuffle=False)
# Hyper Parameters
input_size = 784
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001
# MNIST Dataset (Images and Labels)
train_dataset = dsets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = dsets.MNIST(root='./data',
train=False,
transform=transforms.ToTensor())
# Dataset Loader (Input Pipline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# Model
class LogisticRegression(nn.Module):
def __init__(self, input_size, num_classes):
super(LogisticRegression, self).__init__()
def latent_loss(z_mean, z_stddev):
mean_sq = z_mean * z_mean
stddev_sq = z_stddev * z_stddev
return 0.5 * torch.mean(mean_sq + stddev_sq - torch.log(stddev_sq) - 1)
if __name__ == '__main__':
input_dim = 28 * 28
batch_size = 32
transform = transforms.Compose(
[transforms.ToTensor()])
mnist = torchvision.datasets.MNIST('./', download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(mnist, batch_size=batch_size,
shuffle=True, num_workers=2)
print('Number of samples: ', len(mnist))
latent_size = 64
encoder = Encoder(input_dim, latent_size)
decoder = Decoder(latent_size, input_dim)
vae = VAE(encoder, decoder)
criterion = nn.MSELoss()
optimizer = optim.Adam(vae.parameters(), lr=0.0001)
l = None
for epoch in range(100):
parser.add_argument('--log-interval', type=int, default=20, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--model-dir', default='./model-mnist-net-two-class',
help='directory of model for saving checkpoint')
parser.add_argument('--save-freq', '-s', default=10, type=int, metavar='N',
help='save frequency (default: 10)')
args = parser.parse_args()
# settings
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
# download MNIST dataset
dataset_train = datasets.MNIST('../data', train=True, download=True,
transform=transforms.Compose([transforms.ToTensor()]))
dataset_test = datasets.MNIST('../data', train=False,
transform=transforms.Compose([transforms.ToTensor()]))
# select class '1' and class '3'
def get_same_index(target, label_1, label_2):
label_indices = []
for i in range(len(target)):
if target[i] == label_1:
label_indices.append(i)
if target[i] == label_2:
label_indices.append(i)
return label_indices
kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
train_dataset = \
datasets.MNIST('data-%d' % hvd.rank(), train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
# Horovod: use DistributedSampler to partition the training data.
train_sampler = torch.utils.data.distributed.DistributedSampler(
train_dataset, num_replicas=hvd.size(), rank=hvd.rank())
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, sampler=train_sampler, **kwargs)
test_dataset = \
datasets.MNIST('data-%d' % hvd.rank(), train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
# Horovod: use DistributedSampler to partition the test data.
test_sampler = torch.utils.data.distributed.DistributedSampler(
test_dataset, num_replicas=hvd.size(), rank=hvd.rank())
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.test_batch_size,
sampler=test_sampler, **kwargs)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()