-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.py
92 lines (69 loc) · 2.27 KB
/
entry.py
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
import numpy as np
import torchvision.datasets as dataset
from PIL import Image
import imageio
import matplotlib.pyplot as plt
import easydl as edl
from easydl.nodes.activations import Sigmoid, Softmax, ReLu
from easydl.nodes.layers import Dense, Conv2d
from easydl.nodes.losses import MSE, CrossEntropySoftmax
from easydl.optimizers import Sgd
from easydl import Tape, tensor
def eval_func(data, label):
batch_size = 1
counter = 0.0
correct = 0.0
for i in range(0, data.shape[0], batch_size):
target = tensor(label[i:batch_size + i])
source = tensor(data[i:batch_size + i])
d = so(l3(re(l2(re(l(source))))))
pred = np.argmax(d.numpy)
true_pred = target.numpy.flatten()
counter += 1
correct += (pred == true_pred)
print(correct/counter)
def test_func(data, label):
for e in range(20):
batch_size = 24
for i in range(0, data.shape[0], batch_size):
target = tensor(label[i:batch_size+i])
source = tensor(data[i:batch_size+i])
with Tape() as tape:
d = l3(re(l2(re(l(source)))))
# d.to_gpu()
# target.to_gpu()
# mse.to_gpu()
r = ces([d, target])
print(np.sum(r.numpy) / batch_size)
r.backward()
optimizer.optimize(tape)
edl.init_easydl(True)
optimizer = Sgd(learning_rate=0.08, momentum=0.1)
raise Exception("dfdsd")
conv = Conv2d(10, 10, 3, 10, (3, 3), paddings=(1, 1))
conv.build()
conv.variables['w'] = filters
x = Image.open('/home/vedaevolution/Downloads/img.png')
x = np.expand_dims(np.array(x.resize((100, 100))), 0)
res, cache = conv.forward([x], 1)
conv.backward(np.ones_like(res), cache, 1)
plt.imshow(res[0, ..., 0], cmap='gray')
l = Dense(784, 128)
#l.to_gpu()
l2 = Dense(128, 10)
l3 = Dense(10, 10)
re = ReLu()
re2 = ReLu()
s = Sigmoid()
so = Softmax()
mse = MSE()
ces = CrossEntropySoftmax()
data_set = dataset.MNIST('./datasets/mnist', download=True)
data = data_set.data.numpy()
data = np.reshape(data, (data.shape[0], -1)) / 255.
label = data_set.targets.numpy()
label_raw = np.zeros((label.shape[0], 10))
label_raw[np.arange(label.shape[0]), label] = 1
test_func(data[:5000], label_raw[:5000])
eval_func(data[:1000], label[:1000])
print()