Skip to content

Instantly share code, notes, and snippets.

View harsh-99's full-sized avatar

Harsh Maheshwari harsh-99

View GitHub Profile
@harsh-99
harsh-99 / README.md
Created May 8, 2018 19:35 — forked from hofmannsven/README.md
My simply Git Cheatsheet
data = torchvision.datasets.MNIST(root = 'Data', transform = transform, train=True, download = True)
data_loader = torch.utils.data.DataLoader(dataset = data, shuffle = True, batch_size = batch_size)
class Generator(nn.Module):
def __init__(self, random_size, hidden_size, image_size):
super(Generator, self).__init__()
#random _size -> 64,
#Input is random noise with a fixed size
self.fc1 = nn.Linear(random_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, hidden_size)
#Final output is of shape equal to image size,
#For MNIST -> 784 (as flatten)
class Discriminator(nn.Module):
def __init__(self, image_size, hidden_size):
super(Discriminator, self).__init__()
# Instead of linear layer one can also use 2d convolution.
#Imapge_size -> 784 for MNIST, hidden size is hyperparameter
self.fc1 = nn.Linear(image_size, hidden_size)
self.relu = nn.LeakyReLU(0.2) #this is the negative slope.
self.fc2 = nn.Linear(hidden_size, hidden_size)
#the final output is of shape 1, to classify real or fake
self.fc3 = nn.Linear(hidden_size, 1)
#to train the generator
# Input to generator is a noise of size random_size
z = torch.randn(batch_size, random_size)
output_image = generator(z)
output_discriminator = discriminator(output_image)
#to train the generator the output of this should be compared with real_labels.
#so we compare the output by real label.
#criterion -> BCE Loss
g_loss = criterion(outputs, real_labels)
#To train the Discriminator
output_d_real = discriminator(real_images)
d_real_loss = criterion(output_d_real, real_labels)
z = torch.randn(batch_size, random_size).to(device)
fake_images = generator(z)
output_d_fake = discriminator(fake_images)
d_fake_loss = criterion(output_d_fake, fake_labels)
d_loss = d_real_loss + d_fake_loss
#To train the Discriminator
output_d_real = discriminator(real_images)
d_real_loss = criterion(output_d_real, real_labels)
z = torch.randn(batch_size, random_size).to(device)
fake_images = generator(z)
output_d_fake = discriminator(fake_images)
d_fake_loss = criterion(output_d_fake, fake_labels)
d_loss = d_real_loss + d_fake_loss
import os
import gensim
from collections import Counter
import json
train_path = "./aclImdb/train"
test_path = "./aclImdb/test"
#simple function which read the data from directory and return data and label
# you can make your own reader for other dataset.
import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np
import os
import gensim
class Dataset_seq(Dataset):
def __init__(self, word2id, train_path):
self.word2id = word2id
self.train_path = train_path
def collate_fn(data):
'''
We should build a custom collate_fn rather than using default collate_fn,
as the size of every sentence is different and merging sequences (including padding)
is not supported in default.
Args:
data: list of tuple (training sequence, label)
Return:
padded_seq - Padded Sequence, tensor of shape (batch_size, padded_length)
length - Original length of each sequence(without padding), tensor of shape(batch_size)