This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # connect to Google Drive | |
| from google.colab import drive | |
| drive.mount('/content/gdrive') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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)), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = train_cnn(model, train_loader, valid_loader, | |
| criterion, optimizer, n_epochs = 30, train_on_gpu = True) |
OlderNewer