-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepL.py
More file actions
70 lines (57 loc) · 1.74 KB
/
Copy pathdeepL.py
File metadata and controls
70 lines (57 loc) · 1.74 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
import numpy as np
import os
import scipy
from scipy import ndimage
from scipy import misc
groundTruth = np.zeros([496, 1])
images = np.zeros([496,1382400])
os.chdir('Pictures')
os.chdir('chagall')
count = 0
for filename in os.listdir(os.getcwd()):
image = misc.imread(filename)
image = np.reshape(image, [-1,1]).transpose()
images[count,:] = image.astype(np.float64) / 255
groundTruth[count,0] = 1
count = count + 1
os.chdir('../kand')
for filename in os.listdir(os.getcwd()):
image = misc.imread(filename)
image = np.reshape(image, [-1,1]).transpose()
images[count,:] = image.astype(np.float64) / 255
groundTruth[count,0] = 2
count = count + 1
groundTruth = groundTruth.astype(int)
print count
print groundTruth.shape
from random import shuffle
trX = images
teX = images
trY = groundTruth
teY = groundTruth
print trX.shape
print teX.shape
print trY.shape
print teY.shape
import theano
from theano import tensor as T
def floatX(X):
return np.asarray(X, dtype=theano.config.floatX)
def init_weights(shape):
return theano.shared(floatX(np.random.randn(*shape) * 0.01))
def model(X, w):
return T.nnet.softmax(T.dot(X, w))
X = T.fmatrix()
Y = T.fmatrix()
w = init_weights((784, 10))
py_x = model(X, w)
y_pred = T.argmax(py_x, axis=1)
cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y))
gradient = T.grad(cost=cost, wrt=w)
update = [[w, w - gradient * 0.05]]
train = theano.function(inputs=[X, Y], outputs=cost, updates=update, allow_input_downcast=True)
predict = theano.function(inputs=[X], outputs=y_pred, allow_input_downcast=True)
for i in range(100):
for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
cost = train(trX[start:end], trY[start:end])
print i, np.mean(np.argmax(teY, axis=1) == predict(teX))