-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tutorial_deep_learning.py
325 lines (265 loc) · 11.3 KB
/
Tutorial_deep_learning.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# Este ambiente Python 3 vem com muitas bibliotecas de análise úteis instaladas
# É definido pela imagem do docker kaggle / python: https://github.com/kaggle/docker-python
# Por exemplo, aqui estão vários pacotes úteis para carregar
import numpy as np #algebra linear
import pandas as pd # Processamento de dados , arquivos .csv
import matplotlib.pyplot as plt # para plotagem
import warnings # obter avisos do kernel
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from keras.models import Sequential # initialize neural network library
from keras.layers import Dense # build our layers library
warnings.filterwarnings('ignore')
from subprocess import check_output
# listar conteúdo do arquivo matriz do kernel
print(check_output(["ls", "/home/nelson/Documents/Script Python/Tutorial Deep Learning"])
.decode("utf8"))
# carregar o dataset e plotar
x_l = np.load('/home/nelson/Documents/Script Python/Tutorial Deep Learning/X.npy')
y_l = np.load('/home/nelson/Documents/Script Python/Tutorial Deep Learning/Y.npy')
img_size = 64
plt.subplot(1, 2, 1)
plt.imshow(x_l[260].reshape(img_size, img_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(x_l[900].reshape(img_size, img_size))
plt.axis('off')
# Associe uma sequência de matrizes ao longo de um eixo de linha.
X = np.concatenate((x_l[204:409], x_l[822:1027]), axis=0) # de 0 a 204 é sinal de zero e de 205 a 410 é de um
z = np.zeros(205)
o = np.ones(205)
Y = np.concatenate((z,o), axis=0).reshape(X.shape[0],1)
print("Forma de X: ", X.shape)
print("Forma de Y: ", Y.shape)
# vamos criar matrizes x_train, y_train, x_test, y_test
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.15, random_state=42)
number_of_train = X_train.shape[0]
number_of_test = X_test.shape[0]
X_train_flatten = X_train.reshape(number_of_train, X_train.shape[1]*X_train.shape[2])
X_test_flatten = X_test.reshape(number_of_test, X_test.shape[1]*X_test.shape[2])
print("X train flatten", X_train_flatten.shape)
print("X teste flatten", X_test_flatten.shape)
x_train = X_train_flatten.T
x_test = X_test_flatten.T
y_train = Y_train.T
y_test = Y_test.T
print("x train", x_train.shape)
print("x test", x_test.shape)
print("y train", y_train.shape)
print("y test", y_test.shape)
#
#REGRESSÃO LOGISTICA
#
# descrição curta e exemplo de definição (def)
def dummy(parameter):
dummy_parameter = parameter + 5
return dummy_parameter
result = dummy(3)
# permite inicializar parâmetros
# Então, precisamos da dimensão 4096, que é o número de pixels como parâmetro
# para o nosso método de inicialização (def)
def initialize_weights_and_bias(dimension):
w = np.full((dimension,1), 0.01)
b = 0.0
return w, b
w,b = initialize_weights_and_bias(4096)
#
# PROPAGAÇÃO DIRETA
#
def sigmoid (z):
y_head = 1/(1+np.exp(-z))
return y_head
y_head = sigmoid(0)
y_head
# Etapas de propagação direta:
# encontrar z = w.T * x + b
# y_head = sigmoid (z)
# loss(error) = loss(y,y_head)
# cost = sum(loss)
def forward_propagation(w,b,x_train,y_train):
z = np.dot(w.T, x_train) + b
y_head = sigmoid(z) # probabilistic 0-1
loss = -y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)
cost = (np.sum(loss))/x_train.shape[1] # x_train.shape [1] é para dimensionamento
return cost
#
# ALGORITMO DE OTIMIZAÇÃO COM DESCIDA GRADIENTE
#
# Na propagação para trás, usaremos y_head que é encontrado na progressão para frente
# Portanto, em vez de escrever o método de propagação para trás, vamos combinar
# propagação para frente e propagação para trás
def forward_backward_propagation (w,b,x_train,y_train):
z = np.dot(w.T, x_train) + b
y_head = sigmoid(z)
loss = -y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)
cost = (np.sum(loss)) / x_train.shape[1] # x_train.shape [1] é para dimensionamento da # propagação para trás
derivate_weight = (np.dot(x_train,((y_head-y_train).T)))/x_train.shape[1] # x_train.shape [1] é #para dimensionamento
derivate_bias = np.sum(y_head-y_train) / x_train.shape[1]
gradients = {"derivate_weight": derivate_weight, "derivate_bias": derivate_bias}
return cost, gradients
# Atualizando parametros de aprendizagem
def update(w, b, x_train, y_train, learning_rate,number_of_iterarion):
cost_list = []
cost_list2 = []
index = []
for i in range(number_of_iterarion):
cost,gradients = forward_backward_propagation(w,b,x_train,y_train)
cost_list.append(cost)
w = w - learning_rate * gradients["derivate_weight"]
b = b - learning_rate * gradients["derivate_bias"]
if i % 10 == 0:
cost_list2.append(cost)
index.append(i)
print ("Cost after iteration %i: %f" %(i, cost))
parameters = {"weight": w,"bias": b}
plt.plot(index,cost_list2)
plt.xticks(index,rotation='vertical')
plt.xlabel("Number of Iterarion")
plt.ylabel("Cost")
plt.show()
return parameters, gradients, cost_list
# previsão
def predict(w,b,x_test):
# x_test é uma entrada para propagação direta
z = sigmoid(np.dot(w.T,x_test)+b)
Y_prediction = np.zeros((1,x_test.shape[1]))
# se z for maior que 0,5, nossa previsão é o primeiro sinal (y_head = 1),
# se z for menor que 0,5, nossa previsão é sinal zero (y_head = 0),
for i in range(z.shape[1]):
if z[0,i]<= 0.5:
Y_prediction[0,i] = 0
else:
Y_prediction[0,i] = 1
return Y_prediction
def logistic_regression(x_train, y_train, x_test, y_test, learning_rate , num_iterations):
# inicialização
dimension = x_train.shape[0] # é 4096
w,b = initialize_weights_and_bias(dimension)
# não altere a taxa de aprendizado
parameters, gradients, cost_list = update(w, b, x_train, y_train, learning_rate,num_iterations)
y_prediction_test = predict(parameters["weight"],parameters["bias"],x_test)
y_prediction_train = predict(parameters["weight"],parameters["bias"],x_train)
# Imprimir erros de treino / teste
print("acuracia de treino: {} %".format(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100))
print("acuracia de teste: {} %".format(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100))
logistic_regression(x_train, y_train, x_test, y_test,learning_rate = 0.01, num_iterations = 150)
#
# REGRESSÃO LOGISTICA COM SKLEARN
#
from sklearn import linear_model
logreg = linear_model.LogisticRegression(random_state = 42,max_iter= 150)
print("acuracia do teste: {} ".format(logreg.fit(x_train.T, y_train.T).score(x_test.T, y_test.T)))
print("acuracia do treino {} ".format(logreg.fit(x_train.T, y_train.T).score(x_train.T, y_train.T)))
#
# Artificial Neural Network (ANN)
#
def initialize_parameters_and_layer_sizes_NN(x_train, y_train):
parameters = {"weight1": np.random.randn(3,x_train.shape[0]) * 0.1,
"bias1": np.zeros((3,1)),
"weight2": np.random.randn(y_train.shape[0],3) * 0.1,
"bias2": np.zeros((y_train.shape[0],1))}
return parameters
#
# Propagação direta
#
def forward_propagation_NN(x_train, parameters):
Z1 = np.dot(parameters["weight1"], x_train) + parameters["bias1"]
A1 = np.tanh(Z1)
Z2 = np.dot(parameters["weight2"], A1) + parameters["bias2"]
A2 = sigmoid(Z2)
cache = {"Z1": Z1,
"A1": A1,
"Z2": Z2,
"A2": A2}
return A2, cache
# Custo de computação
def compute_cost_NN(A2, Y, parameters):
logprobs =np.multiply(np.log(A2), Y)
cost = -np.sum(logprobs)/ Y.shape[1]
return cost
#
# Propagação para trás
#
def backward_propagation_NN(parameters, cache, X, Y):
dZ2 = cache["A2"] - Y
dW2 = np.dot(dZ2, cache["A1"].T) / X.shape[1]
db2 = np.sum(dZ2, axis=1, keepdims=True) / X.shape[1]
dZ1 = np.dot(parameters["weight2"].T, dZ2)*(1 - np.power(cache["A1"],2))
dW1 = np.dot(dZ1,X.T) / X.shape[1]
db1 = np.sum(dZ1, axis=1, keepdims=True) / X.shape[1]
grads = {"dweight1": dW1,
"dbias1": db1,
"dweight2": dW2,
"dbias2": db2}
return grads
#
# ATUALIZAÇÃO DE PARAMETROS
#
def update_parameters_NN(parameters, grads, learning_rate = 0.01):
parameters = {"weight1": parameters["weight1"]-learning_rate*grads["dweight1"],
"bias1": parameters["bias1"]- learning_rate*grads["dbias1"],
"weight2" : parameters["weight2"] - learning_rate*grads["dweight2"],
"bias2": parameters["bias2"] - learning_rate*grads["dbias2"]}
return parameters
#
# Previsão com parâmetros aprendidos weight and bias
#
# prediction
def predict_NN(parameters,x_test):
A2, cache = forward_propagation_NN(x_test,parameters)
Y_prediction = np.zeros((1,x_test.shape[1]))
for i in range(A2.shape[1]):
if A2[0,i]<= 0.5:
Y_prediction[0,i] = 0
else:
Y_prediction[0,i] = 1
return Y_prediction
#
# Criação deo modelo
#
def two_layer_neural_network(x_train, y_train,x_test,y_test, num_iterations):
cost_list = []
index_list = []
parameters = initialize_parameters_and_layer_sizes_NN(x_train, y_train)
for i in range(0, num_iterations):
A2, cache = forward_propagation_NN(x_train,parameters)
cost = compute_cost_NN(A2, y_train, parameters)
grads = backward_propagation_NN(parameters, cache, x_train, y_train)
parameters = update_parameters_NN(parameters, grads)
if i % 100 == 0:
cost_list.append(cost)
index_list.append(i)
print ("Custo depois de iteração %i: %f" %(i, cost))
plt.plot(index_list,cost_list)
plt.xticks(index_list,rotation='vertical')
plt.xlabel("Numero de iterações")
plt.ylabel("Custo")
plt.show()
y_prediction_test = predict_NN(parameters,x_test)
y_prediction_train = predict_NN(parameters,x_train)
print("acuracia de treino: {} %".format(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100))
print("acuracia de teste: {} %".format(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100))
return parameters
parameters = two_layer_neural_network(x_train, y_train,x_test,y_test, num_iterations=2500)
# L Layer Neural Networ
# reshaping
x_train, x_test, y_train, y_test = x_train.T, x_test.T, y_train.T, y_test.T
# Implementando com a Biblioteca Keras
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from keras.models import Sequential # initialize neural network library
from keras.layers import Dense # build our layers library
def build_classifier():
classifier = Sequential() # initialize neural network
classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu', input_dim = x_train.shape[1]))
classifier.add(Dense(units = 4, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = build_classifier, epochs = 100)
accuracies = cross_val_score(estimator = classifier, X = x_train, y = y_train, cv = 3)
mean = accuracies.mean()
variance = accuracies.std()
print("Acuracia da Média: "+ str(mean))
print("Accuracy da variação: "+ str(variance))