Skip to content

Instantly share code, notes, and snippets.

View Mehdi-Amine's full-sized avatar
💭
Having fun with Neural Networks

Mehdi Mehdi-Amine

💭
Having fun with Neural Networks
View GitHub Profile
@Mehdi-Amine
Mehdi-Amine / precision.py
Created June 23, 2020 21:47
Calculating the precision of a neural network
def precision(tp, fp):
return tp / (tp + fp)
print(f"Precision: {precision(tp, fp)}")
'''
Out:
Precision: 0.9876712328767123
'''
@Mehdi-Amine
Mehdi-Amine / test-1.py
Last active June 22, 2020 20:33
investigating nn with the first three instances of the test set
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
@Mehdi-Amine
Mehdi-Amine / nn-train.py
Last active June 22, 2020 14:59
Training the confined but happy neural network.
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()
@Mehdi-Amine
Mehdi-Amine / neural-network.py
Last active June 20, 2020 14:37
Defining the structure of the confined but happy neural network
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.
@Mehdi-Amine
Mehdi-Amine / concat-happiness-ds.py
Last active June 17, 2020 16:10
abridged version of the concatenation of the happiness dataset
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
@Mehdi-Amine
Mehdi-Amine / data-loading.py
Last active June 22, 2020 16:07
creating data loader to feed data to the network
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)
@Mehdi-Amine
Mehdi-Amine / standardized-dataset.py
Last active June 22, 2020 16:10
Standardizing the happiness dataset
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)
@Mehdi-Amine
Mehdi-Amine / splitting-dataset.py
Created June 16, 2020 23:46
splitting dataset into train, valid and test sets
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:
@Mehdi-Amine
Mehdi-Amine / assembled-happiness-dataset.py
Last active June 16, 2020 23:36
concatenating the columns of the happiness dataset
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)
@Mehdi-Amine
Mehdi-Amine / happiness-dataset.py
Last active June 16, 2020 23:17
generating a dataset about conditions for happiness
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)