-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_09.py
More file actions
112 lines (87 loc) · 4.25 KB
/
assignment_09.py
File metadata and controls
112 lines (87 loc) · 4.25 KB
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
from Neural_Networks import DenseNetwork
import numpy as np
from sklearn.datasets import make_classification
import matplotlib.pyplot as plt
from typing import List
def task_1():
print('- - - Task 1 - - -')
# Design a network that implements the following function: A AND ~B
print('Network: A AND ~B')
network_1 = DenseNetwork(input_features=2, layers=[1], activation='sign', weights=[np.array([[1, -1]])],
biases=[np.array([-1.5])], loss='mse')
features = np.array([[-1, -1], [1, -1], [-1, 1], [1, 1]])
for f in features:
print(f'Input: {f}, Output: {network_1.predict(f)}')
# Design a network that implements the following function: A XOR B
print('Network: A XOR B')
network_2 = DenseNetwork(input_features=2, layers=[2, 1], activation='sign', loss='mse',
weights=[np.array([[1, 1], [1, 1]]), np.array([[1, -1]])],
biases=[np.array([1.5, -1.5]), np.array([0])])
for f in features:
print(f'Input: {f}, Output: {network_2.predict(f)}')
def task_2():
print('- - - Task 2 - - -')
# Create the network from the task
network = DenseNetwork(input_features=2, layers=[2, 1], activation='sigmoid', loss='mse',
weights=[np.array([[0.5, 0.25], [1.0, 2.0]]), np.array([[0.4, 0.5]])],
biases=[np.array([0, 0]), np.array([0])])
# Calculate the output of the network for the input [0.1, 0.2]
print(network.predict(np.array([0.1, 0.2])))
# Calculate the gradients and the deltas for the input [0.1, 0.2] and the label 1.0
gradients, deltas = network.get_gradients(np.array([0.1, 0.2]), np.array([1.0]))
for i, g in enumerate(gradients):
print(f'Gradients for layer {i}: {g}')
for i, d in enumerate(deltas):
print(f'Deltas for layer {i}: {d}')
def train_networks():
print('- - - Train Networks - - -')
# Build a network and train it to learn the AND function
and_network = DenseNetwork(input_features=2, layers=[1], activation='tanh', loss='mse', learning_rate=0.05)
features = np.array([[-1, -1], [1, -1], [-1, 1], [1, 1]])
labels = np.array([-1, -1, -1, 1])
and_network.train(features, labels, epochs=1000)
# Predict the features and evaluate the loss
for f, l in zip(features, labels):
print(f'Input: {f}, Output: {and_network.predict(f)}, Label: {l}')
print(f'Loss: {and_network.evaluate(features, labels)}')
def task_4(constant: float = None, layers: List[int] = None, title: str = None):
print(f'Initialize the weights with a constant of {constant}')
if layers is None:
layers = [1]
# Create a binary classification dataset
features, labels = make_classification(n_samples=100, n_features=5, n_redundant=0, n_informative=2,
n_clusters_per_class=1, random_state=1)
# Create a plot to visualize the network before and after training
fig, axs = plt.subplots(ncols=2, figsize=(16, 12), tight_layout=True)
# Create a network
network = DenseNetwork(input_features=features.shape[1], layers=layers, activation='sigmoid', loss='mse',
learning_rate=0.05, init_constant=constant)
# Plot and evaluate the network before training
network.plot_network(ax=axs[0])
loss = network.evaluate(features, labels)
axs[0].set_title(f'Before Training: Loss = {loss:.2f}')
# Train the network
network.train(features, labels, epochs=50)
# Plot and evaluate the network after training
network.plot_network(ax=axs[1])
loss = network.evaluate(features, labels)
axs[1].set_title(f'After Training: Loss = {loss:.2f}')
# Show the plot
fig.suptitle(f'Constant: {constant} - Layers: {layers}', fontsize=16)
fig.show()
# Save the plot if a title is given
if title is not None:
fig.savefig(f'Figures/assignment_09_{title}.png')
def main():
task_1()
print('\n')
task_2()
print('\n')
train_networks()
print('\n')
task_4(constant=0.0, layers=[1], title='Single_0')
task_4(constant=0.0, layers=[5, 1], title='Double_0')
task_4(constant=1.0, layers=[1], title='Single_1')
task_4(constant=1.0, layers=[5, 1], title='Double_1')
if __name__ == '__main__':
main()