Skip to content

Instantly share code, notes, and snippets.

@GiulioCMSanto
Last active September 22, 2019 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GiulioCMSanto/1a4cdd75b88f2c8fb3a9369678b376ad to your computer and use it in GitHub Desktop.
Save GiulioCMSanto/1a4cdd75b88f2c8fb3a9369678b376ad to your computer and use it in GitHub Desktop.
Classifying Flowers With Transfer Learning
#The class bellow was created based in the one provided by Udacity
class Network(nn.Module):
def __init__(self, input_size, output_size, hidden_layers, drop_p=0.5):
''' Builds a feedforward network with arbitrary hidden layers.
Arguments
---------
input_size: integer, size of the input layer
output_size: integer, size of the output layer
hidden_layers: list of integers, the sizes of the hidden layers
'''
super().__init__()
# Input to a hidden layer
self.hidden_layers = nn.ModuleList([nn.Linear(input_size, hidden_layers[0])])
# Add a variable number of more hidden layers
layer_sizes = zip(hidden_layers[:-1], hidden_layers[1:])
self.hidden_layers.extend([nn.Linear(h1, h2) for h1, h2 in layer_sizes])
self.output = nn.Linear(hidden_layers[-1], output_size)
self.dropout = nn.Dropout(p=drop_p)
def forward(self, x):
''' Forward pass through the network, returns the output logits '''
for each in self.hidden_layers:
x = F.relu(each(x))
x = self.dropout(x)
x = self.output(x)
return F.log_softmax(x, dim=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment