-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss_fun_calc_full.py
More file actions
77 lines (50 loc) · 1.77 KB
/
Copy pathloss_fun_calc_full.py
File metadata and controls
77 lines (50 loc) · 1.77 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
import numpy as np
import nnfs
from nnfs.datasets import spiral_data
nnfs.init()
class Layer_Dense:
def __init__(self, n_inputs, n_neurons):
self.weights = 0.01*np.random.randn(n_inputs, n_neurons)
self.baises = np.zeros((1,n_neurons))
def forward(self,inputs):
self.output = np.daot(inputs, self.weights) + self.biases
class Activation_ReLU:
def forward(self, inputs):
self.output = np.maximum(0, inputs)
class Activation_Softmax :
def forward(self, inputs):
exp_values = np.exp(inputs - np.max(inputs, axis =1, keepdims = True))
probablities = exp_values/np.sum(exp_values, axis =1 , keepdims = True)
self.output = probablities
class Loss:
def calculate(self, output, y):
sample_losses = self.forward(output,y)
data_loss = np.mean(sample_losses)
return data_loss
class Loss_CategoricalCrossentropy(Loss):
def forward(self, y_pred, y_true):
samples = len(y_pred)
y_pred_clipped = np.clip(y_pred, 1e-7,1,1e-7)
if len(y_true.shape) == 1:
correct_confidences = y_pred_clipped[
range(samples),
y_true
]
elif len(y_true.shape) ==2:
correct_confidences = np.sum(
y_pred_clipped*y_true,
axis =1
)
X,y = spiral_data(samples = 100, classes = 3)
dense1 = Layer_Dense(2,3)
activation1 = Activation_ReLU()
dense2 = Layer_Dense(3,3)
activation2 = Activation_Softmax()
loss_function = Loss_CategoricalCrossentropy()
dense1.forward(X)
activation1.forward(dense1.output)
dense2.forward(activation1.output)
activation2.forward(dense2.output)
print(activation2.output[:5])
loss = loss_function.calculate(activation2.output,y)
print('loss : ', loss)