Skip to content

Instantly share code, notes, and snippets.

View erykml's full-sized avatar

Eryk Lewinson erykml

View GitHub Profile
# parameters for the analysis
effect_size = 0.8
alpha = 0.05 # significance level
power = 0.8
power_analysis = TTestIndPower()
sample_size = power_analysis.solve_power(effect_size = effect_size,
power = power,
alpha = alpha)
# power vs. number of observations
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
fig = TTestIndPower().plot_power(dep_var='nobs',
nobs= np.arange(2, 200),
effect_size=np.array([0.2, 0.5, 0.8]),
alpha=0.01,
ax=ax, title='Power of t-Test' + '\n' + r'$\alpha = 0.01$')
ax.get_legend().remove()
# connect to Google Drive
from google.colab import drive
drive.mount('/content/gdrive')
!pip install gdown
# create directory for storing data
!mkdir -p data
# download zip file with training set
!gdown https://drive.google.com/uc?id=1z_vO2muBgzNGIa7JtY8OPmaeUC348jj4 && unzip -qq training_set.zip -d data/training_set
!rm training_set.zip
# download zip with test set
# 1. defining parameters ----
# number of subprocesses to use for data loading
num_workers = 0
# number of samples to load per batch
batch_size = 32
# % of training set to use as validation
valid_size = 0.2
# define transformations that will be applied to images
# inspect loaded images ----
# obtain one batch of training images
dataiter = iter(train_loader)
data, target = dataiter.next()
data = data.numpy() # convert images to numpy for display
# plot the images in the batch, along with the corresponding labels
fig = plt.figure(figsize=(20, 10))
# display 10 images
# create the class containing the architecture of the network (inherits from nn.Module)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# define the layers
# cheatsheet
# nn.Conv2d(in_channels, out_channels, kernel_size, stride=1,
# create class Flatten to flatten the tensor
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size()[0], -1)
# use OrderedDict to give meaningful names for layers
model = nn.Sequential(OrderedDict([
('conv_1', nn.Conv2d(3, 16, 3, padding=1)),
('relu_1', nn.ReLU()),
('max_pool_1', nn.MaxPool2d(2, 2)),
def train_cnn(model, train_loader, valid_loader,
criterion, optimizer, n_epochs = 30, train_on_gpu = False,
save_model_on_improvement = True, plot_loss = True):
'''
Function for training the CNN given input parameters. Can be run on CPU or GPU.
The function automatically verifies whether the selected criterion is Binary cross-entropy and if so
converts tensors to appropriate type.
Inputs:
model - architecture of the neural network defined using either Class approach or Sequential
model = train_cnn(model, train_loader, valid_loader,
criterion, optimizer, n_epochs = 30, train_on_gpu = True)