Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

titanic-classification-nn.py 4.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  1. #%%
  2. import pandas as pd
  3. #%%
  4. titanic_data = pd.read_csv('C:/Users/pcuci/Downloads/pytorch-building-deep-learning-models/datasets/titanic_data/train.csv')
  5. titanic_data.head()
  6. #%%
  7. unwanted_features = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp', 'Parch', 'Embarked'] # remove unwanted features
  8. titanic_data = titanic_data.drop(unwanted_features, axis=1)
  9. titanic_data.head()
  10. #%%
  11. titanic_data = titanic_data.dropna() # get rid also of missing information rows
  12. #%%
  13. from sklearn import preprocessing
  14. le = preprocessing.LabelEncoder() # encode categorical values as numeric labels
  15. titanic_data['Sex'] = le.fit_transform(titanic_data['Sex'])
  16. titanic_data.head() # 0 for female, 1 male
  17. #%%
  18. features = ['Pclass', 'Sex', 'Age', 'Fare']
  19. titanic_features = titanic_data[features]
  20. titanic_features.head()
  21. #%%
  22. titanic_features = pd.get_dummies(titanic_features, columns=['Pclass']) # one-hot encode the passanger class
  23. titanic_features.head()
  24. #%%
  25. titanic_target = titanic_data[['Survived']]
  26. titanic_target.head() # 1 indicates survival, 0 did not survive the sinking
  27. #%%
  28. from sklearn.model_selection import train_test_split
  29. X_train, x_test, Y_train, y_test = train_test_split(titanic_features, titanic_target, test_size=0.2, random_state=0) # keep 80% of data for training, the remaining 20% for testing
  30. X_train.shape, Y_train.shape
  31. #%%
  32. # import training and test data into torch sensors
  33. import torch
  34. import numpy as np
  35. Xtrain_ = torch.from_numpy(X_train.values).float()
  36. Xtest_ = torch.from_numpy(x_test.values).float()
  37. Xtrain_.shape, Xtest_.shape
  38. #%%
  39. # reshape our data to match the y-label format required by our loss function
  40. # extract y-labels as a 1D tensor - one row containing all labels
  41. Ytrain_ = torch.from_numpy(Y_train.values).view(1, -1)[0]
  42. Ytest_ = torch.from_numpy(y_test.values).view(1, -1)[0]
  43. Ytrain_.shape, Ytest_.shape
  44. #%%
  45. import torch.nn as nn
  46. import torch.nn.functional as F # includes the log soft max function
  47. input_size = 6 # 6 input features: age, sex, fare, and the one-hot encoded 3 pclasses
  48. output_size = 2 # survived or not
  49. hidden_size = 10 # one hidden layer with 10 neurons
  50. input_size, hidden_size, output_size
  51. #%%
  52. # build our own custom nn modules by subclassing the nn.Module class
  53. class Net(nn.Module):
  54. def __init__(self):
  55. super(Net, self).__init__() # initialize the nn before we add in our layers
  56. self.fc1 = nn.Linear(input_size, hidden_size)
  57. self.fc2 = nn.Linear(hidden_size, hidden_size)
  58. self.fc3 = nn.Linear(hidden_size, output_size)
  59. def forward(self, x): # forward pass on input data x
  60. x = F.sigmoid(self.fc1(x)) # first read the input x into the fully connected linear layer, then apply the sigmoid function
  61. x = F.sigmoid(self.fc2(x)) # again
  62. x = self.fc3(x) # no activation function at the end
  63. return F.log_softmax(x, dim=-1) # better than log(softmax(x)) which is numerically unstable
  64. # dim=-1 is the dimension along which we want to compute softmax, -1 infers the right dimension on its own
  65. model = Net() # instantiate our model
  66. model.parameters
  67. #%%
  68. import torch.optim as optim
  69. optimizer = optim.Adam(model.parameters()) # adam is a momentum based optimizer as a loss function
  70. loss_fn = nn.NLLLoss() # set up the loss function
  71. #%%
  72. epoch_data = []
  73. epochs = 1001
  74. for epoch in range(1, epochs):
  75. optimizer.zero_grad() # zero out the gradients so it calculates fresh gradients in the forward pass
  76. Ypred = model(Xtrain_)
  77. # calculate the loss on the prediction and backpropagate to calculate new gradients
  78. loss = loss_fn(Ypred, Ytrain_)
  79. loss.backward()
  80. optimizer.step() # update the model parameters by applying gradients
  81. Ypred_test = model(Xtest_)
  82. loss_test = loss_fn(Ypred_test, Ytest_)
  83. _, pred = Ypred_test.data.max(1) # pick the highest probability, this is the predicted value
  84. accuracy = pred.eq(Ytest_.data).sum().item() / y_test.values.size # does predicted data match actual labels; divided by the total number of instances in our test dataset to get a percentage accuracy
  85. epoch_data.append([epoch, loss.data.item(), loss_test.data.item(), accuracy]) # append the info for each epoch
  86. if epoch % 100 == 0: # print out every 100 epochs
  87. print('epoch - %d (%d%%) train loss - %.2f test loss - %.2f accuracy - %.4f' % (epoch, epoch/150 * 10, loss.data.item(), loss_test.data.item(), accuracy))
  88. #%%
  89. df_epochs_data = pd.DataFrame(epoch_data, columns=["epoch", "train_loss", "test_loss", "accuracy"])
  90. import matplotlib.pyplot as plt
  91. f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
  92. df_epochs_data[["train_loss", "test_loss"]].plot(ax=ax1)
  93. df_epochs_data[["accuracy"]].plot(ax=ax2)
  94. plt.ylim(ymin=0.5)
  95. plt.show()
  96. #%%
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...