<jupyter_start><jupyter_text>Testing the effect of *Dropout Rates* with Different Layers*This notebook uses the **Dog or Cat** data set available [here](http://files.fast.ai/data/dogscats.zip)*<jupyter_code># Put these at the top of every notebook, to get automatic reloading and inline plotting
%reload_ext autoreload
%autoreload 2
%matplotlib inline
# This file contains all the main external libs we'll use
from fastai.imports import *

from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *

# Using the same data set as Deep Learning Part 1 - lesson 1
PATH = "data/dogscats/"
# Select an image model
arch=resnet50
# Set to the number of CPU Cores you have
workers=8
# this is an array we will iterate through to test each one
dropouts = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
bestdrop = 0.2 # established in previous notebook so it will be our static dropout rate
# these variables are used for capturing results
los1 = []
acc1 = []
los2 = []
acc2 = []
sz=64 # unchanged
bs=512 # batch size
learnrate = 1e-1 #0.1
tfms = tfms_from_model(arch, sz)
data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs, num_workers=workers)<jupyter_output><empty_output><jupyter_text>Dropout Layers in Model<jupyter_code>learn = ConvLearner.pretrained(arch, data, precompute=False, ps=bestdrop)
learn<jupyter_output><empty_output><jupyter_text>Learning Rate<jupyter_code>learn = ConvLearner.pretrained(arch, data, precompute=False, ps=[0,bestdrop])
# Finding the learning rate
lrf=learn.lr_find()
# Plotting the learning rate
learn.sched.plot()
learn = ConvLearner.pretrained(arch, data, precompute=False, ps=[bestdrop,0])
# Finding the learning rate
lrf=learn.lr_find()
# Plotting the learning rate
learn.sched.plot()<jupyter_output><empty_output><jupyter_text>Run Tests<jupyter_code>#### Dropout change in first layer only ####
#loop through all entries in the dropouts array
for dropout in dropouts:
    learn = ConvLearner.pretrained(arch, data, precompute=False, ps=[dropout,bestdrop])
    
    print('Dropout Rates: ',dropout,',',bestdrop)
    # Make sure we have enough cycles and due to dropout variability, let's make sure we are mixing things up a bit
    %time fit_array = learn.fit(learnrate, 3, cycle_len=1, cycle_mult=2)
    
    # learn.fit actually returns an array that looks like this: [[0.1983260258436203],0.9185000009536743]
    # it includes that last val_loss and accuracy values
    accuracy = fit_array[1]
    loss = fit_array[0][0]
    # add them to our arrays for charting later
    acc1.append(accuracy)
    los1.append(loss)
#### Dropout change in second layer only ####
#loop through all entries in the dropouts array
for dropout in dropouts:
    learn = ConvLearner.pretrained(arch, data, precompute=False, ps=[bestdrop,dropout])
    
    print('Dropout Rates: ',bestdrop,',',dropout)
    # Make sure we have enough cycles and due to dropout variability, let's make sure we are mixing things up a bit
    %time fit_array = learn.fit(learnrate, 3, cycle_len=1, cycle_mult=2)
    
    # learn.fit actually returns an array that looks like this: [[0.1983260258436203],0.9185000009536743]
    # it includes that last val_loss and accuracy values
    accuracy = fit_array[1]
    loss = fit_array[0][0]
    # add them to our arrays for charting later
    acc2.append(accuracy)
    los2.append(loss)
print(dropouts)
# dropout changes in first layer
print(acc1) 
print(los1)
# dropout changes in second layer
print(acc2)
print(los2)
#cheatsheet of results
#acc1 = 
#acc2 =<jupyter_output><empty_output><jupyter_text>Interpreting Results<jupyter_code>width = 0.05 # bar width
# Get current size
fig_size = plt.rcParams["figure.figsize"]
# Set new size (0=width,1=height)
fig_size[0] = 10
fig_size[1] = 6
plt.rcParams["figure.figsize"] = fig_size

plt.bar(dropouts, acc1, width, align='center')

plt.tick_params(axis='x', which='major', labelsize=10)
plt.tick_params(axis='y', which='major', labelsize=15)

xrange = np.arange(0,1,.1)
plt.xticks(xrange, dropouts)
plt.ylim(.85,.95)

plt.xlabel('Dropout X,0.2', size = 20)
plt.ylabel('Accuracy',size = 20)

plt.show()
plt.bar(dropouts, acc2, width, align='center')

plt.tick_params(axis='x', which='major', labelsize=10)
plt.tick_params(axis='y', which='major', labelsize=15)

xrange = np.arange(0,1,.1)
plt.xticks(xrange, dropouts)
plt.ylim(.85,.95)

plt.xlabel('Dropout 0.2,X', size = 20)
plt.ylabel('Accuracy',size = 20)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11778898881333626, 'fraction_numerical': 0.039262996271112086, 'mean_word_length': 5.187245590230665, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '39917', 'license': 'MIT', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1569, 'n_words': 457, 'path': 'Testing Dropout Rates (specific layers).ipynb', 'repo_name': 'ucffool/fastai-custom-learning-notebooks', 'size': '4559'}