This file contains 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 precision(tp, fp): | |
return tp / (tp + fp) | |
print(f"Precision: {precision(tp, fp)}") | |
''' | |
Out: | |
Precision: 0.9876712328767123 | |
''' |
This file contains 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
test_dl = DataLoader(test_ds, batch_size=3) # Step 1 | |
net.eval() | |
with torch.no_grad(): | |
for testx, testy in test_dl: | |
print("batch: ", testx, testy) | |
linout = net(testx) # Step 2 | |
print("linear outputs: ", linout) | |
prob = F.softmax(linout, dim=1) # Step 3 | |
print("softmax probabilities:", prob) | |
_, pred = torch.max(prob,1) # Step 4 |
This file contains 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
import torch.optim as optim | |
learning_rate = 0.01 | |
epochs = 20 | |
net = Network(input_size=3, lin1_size=7, lin2_size=2) | |
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9) | |
criterion = nn.CrossEntropyLoss() | |
for epoch in range(epochs): | |
net.train() |
This file contains 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
import torch.nn as nn | |
import torch.nn.functional as F | |
class Network(nn.Module): | |
def __init__(self, input_size, lin1_size, lin2_size): | |
super().__init__() # initializes Network from the parent class Module. | |
# linear layers. | |
self.lin1 = nn.Linear(input_size, lin1_size) # Creates random weights and biases. | |
self.lin2 = nn.Linear(lin1_size, lin2_size) # Use lin1.weight & lin1.bias to inspect. | |
This file contains 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 assemble(col1, col2, col3, col4): | |
return np.concatenate((col1, col2, col3, col4), axis=1) | |
csd = assemble(cold[:s], slow[:s], dislike[:s], unhappy) | |
csl = assemble(cold[s:s*2], slow[s:s*2], like[:s], unhappy) | |
cfd = assemble(cold[s*2:s*3], fast[:s], dislike[s:s*2], unhappy) | |
cfl = assemble(cold[s*3:s*4], fast[s:s*2], like[s:s*2], happy) | |
# ... | |
# We repeat the same for hot(hsd, hsl, hfd, hfl) and burning(bsd, bsl, bfd, bfl) tea temperatures |
This file contains 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
import torch | |
from torch.utils.data import TensorDataset | |
from torch.utils.data import DataLoader | |
# Converting from numpy arrays to torch tensors | |
x_train = torch.from_numpy(train_set_std[:,:-1]).float() | |
y_train = torch.from_numpy(train_set_std[:,-1]).long() | |
# Creating tensor-datasets | |
train_ds = TensorDataset(x_train, y_train) |
This file contains 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
from sklearn.preprocessing import StandardScaler | |
std_scaler = StandardScaler() | |
# fitting the standardscaler on the training set. Then transforming the training set | |
train_set_std = std_scaler.fit_transform(train_set[:,:2]) | |
train_set_std = np.concatenate((train_set_std, train_set[:,-2:]), axis=1) | |
# ensuring that we only transform without fitting the validation set | |
valid_set_std = std_scaler.transform(valid_set[:,:2]) | |
valid_set_std = np.concatenate((valid_set_std, valid_set[:,-2:]), axis=1) |
This file contains 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
from sklearn.model_selection import train_test_split | |
data = np.copy(tib) | |
train_set, test_set = train_test_split(data, test_size=0.3, random_state=42) | |
train_set, valid_set = train_test_split(train_set, test_size=0.2, random_state=42) | |
# printing the shapes of the datasets | |
train_set.shape, valid_set.shape, test_set.shape | |
''' | |
Out: |
This file contains 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 assemble(col1, col2, col3, col4): | |
return np.concatenate((col1, col2, col3, col4), axis=1) | |
csd = assemble(cold[:s], slow[:s], dislike[:s], unhappy) | |
csl = assemble(cold[s:s*2], slow[s:s*2], like[:s], unhappy) | |
cfd = assemble(cold[s*2:s*3], fast[:s], dislike[s:s*2], unhappy) | |
cfl = assemble(cold[s*3:s*4], fast[s:s*2], like[s:s*2], happy) | |
hsd = assemble(hot[:s], slow[s*2:s*3], dislike[s*2:s*3], unhappy) | |
hsl = assemble(hot[s:s*2], slow[s*3:s*4], like[s*2:s*3], happy) |
This file contains 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
import numpy as np | |
np.random.seed(42) | |
s = 500 # number of rows in each combination of categories | |
# (example: 500 rows with cold tea, fast internet, and good books) | |
# Making the tea | |
cold = np.around(np.random.uniform(low=0, high=30, size=(s*4,1)), decimals=3) | |
hot = np.around(np.random.uniform(low=30, high=60, size=(s*4,1)), decimals=3) | |
burning = np.around(np.random.uniform(low=60, high=90, size=(s*4,1)), decimals=3) |