-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加总代价函数cost,非正规运算符threshold和accuracy,完成了浅层神经网络逻辑回归
- Loading branch information
zhaoyi
committed
Jun 25, 2023
1 parent
578628d
commit 4542c9d
Showing
11 changed files
with
276 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from erud.cg.payload import payload | ||
import numpy as np | ||
|
||
# 总代价函数 | ||
class cost (payload) : | ||
__x = any | ||
|
||
def fprop(self, x) -> float : | ||
r = x | ||
if isinstance(x, np.ndarray) : | ||
r = 1.0 / np.size(x) * np.sum(x) | ||
|
||
self.__x = x | ||
|
||
return r | ||
|
||
def bprop(self, dz) -> list[any] : | ||
_x = self.__x | ||
|
||
dx = 1 | ||
if isinstance(_x, np.ndarray) : | ||
dx = np.ones_like(_x) / np.size(_x) * dz | ||
|
||
return [dx] | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from erud.cg.payload import payload | ||
import numpy as np | ||
|
||
# 精准度 | ||
class accuracy (payload) : | ||
__yhat : any = None | ||
__y : any = None | ||
|
||
def fprop(self, yhat, y) -> any : | ||
return 100 - np.mean(np.abs(yhat - y) * 100) | ||
|
||
def bprop(self, dz) -> list[any] : | ||
return [np.zeros_like(self.__yhat), np.zeros_like(self.__y)] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from erud.cg.payload import payload | ||
import numpy as np | ||
|
||
class threshold (payload) : | ||
__x : any | ||
|
||
def fprop(self, x, thresholds) -> any : | ||
self.__x = x | ||
return x > thresholds | ||
|
||
def bprop(self, dz) -> list[any] : | ||
return [np.zeros_like(self.__x), 0] |
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
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import numpy as np | ||
import h5py | ||
from erud.nous import nous | ||
|
||
def load_dataset(): | ||
path = __file__[:__file__.rfind('\\')] | ||
print(path) | ||
|
||
|
||
train_dataset = h5py.File(path + '/datasets/train_catvnoncat.h5', "r") | ||
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features | ||
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels | ||
|
||
test_dataset = h5py.File(path + '/datasets/test_catvnoncat.h5', "r") | ||
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features | ||
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels | ||
|
||
classes = np.array(test_dataset["list_classes"][:]) # the list of classes | ||
|
||
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0])) | ||
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0])) | ||
|
||
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes | ||
|
||
def test_logistic_regression () : | ||
train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes = load_dataset() | ||
|
||
assert train_set_x_orig.shape == (209, 64, 64, 3) | ||
assert train_set_y_orig.shape == (1, 209) | ||
assert test_set_x_orig.shape == (50, 64, 64, 3) | ||
assert test_set_y_orig.shape == (1, 50) | ||
|
||
# 中心化、向量化 | ||
train_set_x = train_set_x_orig.reshape((209, 64 * 64 * 3)).T / 255 | ||
test_set_x = test_set_x_orig.reshape((50, 64 * 64 * 3)).T / 255 | ||
|
||
assert train_set_x.shape == (12288, 209) | ||
assert test_set_x.shape == (12288, 50) | ||
|
||
# 训练 | ||
num_iterations = 1000 | ||
rate = 0.005 | ||
|
||
g = nous(''' | ||
W:zeros(1, 12288) matmul X add b:0 -> sigmoid as temp -> cross_entropy Y -> cost -> J:$$ | ||
''').parse() | ||
|
||
g.setData('X', train_set_x) | ||
g.setData('Y', train_set_y_orig) | ||
g.setUpdateFunc('W', lambda z, dz : z - rate * dz) | ||
g.setUpdateFunc('b', lambda z, dz : z - rate * dz) | ||
|
||
for i in range(num_iterations) : | ||
|
||
g.fprop() | ||
g.bprop() | ||
|
||
if i % 100 == 0 : | ||
print('Cost after iteration %i : %f' % (i, g.getData('J') ) ) | ||
|
||
|
||
# 测试 | ||
|
||
g1 = nous(''' | ||
W matmul X add b -> sigmoid -> threshold 0.5 -> accuracy Y -> J:$$ | ||
''').parse() | ||
|
||
g1.setData('X', train_set_x) | ||
g1.setData('Y', train_set_y_orig) | ||
g1.setData('W', g.getData('W')) | ||
g1.setData('b', g.getData('b')) | ||
|
||
g1.fprop() | ||
|
||
print('train accuracy: %s' %(g1.getData('J'))) | ||
|
||
g1.setData('X', test_set_x) | ||
g1.setData('Y', test_set_y_orig) | ||
|
||
g1.fprop() | ||
|
||
print('test accuracy: %s' %(g1.getData('J'))) |
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