AI2020_Project/numbering.py

262 lines
6.9 KiB
Python
Raw Normal View History

2020-05-02 17:24:06 +02:00
from sklearn.datasets import load_digits
import pylab as pl
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import numpy as np
import os.path
import csv
import random
layers_dims=[64,60,10,10]
def predict_L_layer(X,parameters):
AL,caches=L_model_forward(X,parameters)
prediction=np.argmax(AL,axis=0)
return prediction.reshape(1,prediction.shape[0])
def initialize_parameters_deep(layer_dims):
np.random.seed(3)
parameters = {}
L = len(layer_dims)
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layer_dims[l],layer_dims[l-1])*0.01
parameters['b' + str(l)] = np.zeros((layer_dims[l],1))
return parameters
def linear_forward(A, W, b):
Z = np.dot(W,A)+b
cache = (A, W, b)
return Z, cache
def sigmoid_(Z):
return 1/(1+np.exp(-Z))
def relu_(Z):
return Z*(Z>0)
def drelu_(Z):
return 1. *(Z>0)
def dsigmoid_(Z):
return sigmoid_(Z)*(1-sigmoid_(Z))
def sigmoid(Z):
return sigmoid_(Z),Z
def relu(Z):
return relu_(Z),Z
def linear_activation_forward(A_prev,W,b,activation):
if activation == "sigmoid":
Z, linear_cache = linear_forward(A_prev,W,b)
A, activation_cache = sigmoid(Z)
elif activation == "relu":
Z, linear_cache = linear_forward(A_prev,W,b)
A, activation_cache = relu(Z)
cache = (linear_cache, activation_cache)
return A, cache
def L_model_forward(X, parameters):
caches = []
A = X
L = len(parameters) // 2
for l in range(1, L):
A_prev = A
A, cache = linear_activation_forward(A_prev,parameters['W'+str(l)],parameters['b'+str(l)],"relu")
caches.append(cache)
AL, cache = linear_activation_forward(A,parameters['W'+str(L)],parameters['b'+str(L)],"sigmoid")
caches.append(cache)
return AL, caches
def compute_cost(AL, Y):
m=Y.shape[1]
cost = -(1/m)*np.sum((Y*np.log(AL)+(1-Y)*np.log(1-AL)))
cost=np.squeeze(cost)
assert(cost.shape == ())
return cost
def linear_backward(dZ, cache):
A_prev, W, b = cache
m = A_prev.shape[1]
dW = (1/m)*np.dot(dZ,A_prev.T)
db = (1/m)*np.sum(dZ,axis=1,keepdims=True)
dA_prev = np.dot(W.T,dZ)
return dA_prev, dW, db
def relu_backward(dA,activation_cache):
return dA* drelu_(activation_cache)
def sigmoid_backward(dA,activation_cache):
return dA* dsigmoid_(activation_cache)
def linear_activation_backward(dA, cache, activation):
linear_cache, activation_cache = cache
if activation == "relu":
dZ = relu_backward(dA,activation_cache)
dA_prev, dW, db = linear_backward(dZ,linear_cache)
elif activation == "sigmoid":
dZ = sigmoid_backward(dA,activation_cache)
dA_prev, dW, db = linear_backward(dZ,linear_cache)
return dA_prev,dW,db
def L_model_backward(AL, Y, caches):
grads = {}
L = len(caches)
m = AL.shape[1]
#Y = Y.reshape(AL.shape)
dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))
current_cache = caches[L-1]
grads["dA" + str(L-1)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dAL,current_cache,"sigmoid")
for l in reversed(range(L-1)):
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA"+str(l+1)],current_cache,"relu")
grads["dA" + str(l)] = dA_prev_temp
grads["dW" + str(l + 1)] = dW_temp
grads["db" + str(l + 1)] = db_temp
return grads
def update_parameters(parameters, grads, learning_rate):
L = len(parameters) // 2
for l in range(L):
parameters["W" + str(l+1)] = parameters["W" + str(l+1)]-(learning_rate)*grads["dW"+str(l+1)]
parameters["b" + str(l+1)] = parameters["b" + str(l+1)]-(learning_rate)*grads["db"+str(l+1)]
return parameters
def L_layer_model(X, Y, layers_dims, learning_rate = 0.005, num_iterations = 3000, print_cost=False):
np.random.seed(1)
costs = []
parameters = initialize_parameters_deep(layers_dims)
for i in range(0, num_iterations):
AL, caches = L_model_forward(X, parameters)
cost = compute_cost(AL, Y)
grads = L_model_backward(AL, Y, caches)
parameters = update_parameters(parameters, grads, learning_rate)
if print_cost and i % 1000 == 0:
print ("Cost after iteration %i: %f" %(i, cost))
if print_cost and i % 1000 == 0:
costs.append(cost)
# plot the cost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
return parameters
def num(size,target):
numbers=[]
imagesx=[]
imagesy=[]
while(len(numbers)!=size):
x=random.randint(0,np.shape(target)[0]-1)
y=random.randint(0,np.shape(target)[0]-1)
num=str(target[x][1])+str(target[y][1])
if(num not in numbers):
numbers.append(num)
imagesx.append(target[x][0])
imagesy.append(target[y][0])
return(numbers,imagesx,imagesy)
def pred(xnum,ynum):
xnum=np.array(xnum)
ynum=np.array(ynum)
#pl.gray()
#pl.matshow(xnum[0])
#pl.show()
imgx=xnum.reshape((64,1)).T
imgx = sc.transform(imgx)
imgx=imgx.T
imgy=ynum.reshape((64,1)).T
imgy = sc.transform(imgy)
imgy=imgy.T
predicted_digitx=predict_L_layer(imgx,parameters)
predicted_digity=predict_L_layer(imgy,parameters)
return int(str(predicted_digitx[0][0]) + str(predicted_digity[0][0]))
digits=load_digits()
images_and_labels=list(zip(digits.images,digits.target))
print()
n_samples=len(digits.images)
x=digits.images.reshape((n_samples,-1))
y=digits.target
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)
np.save('Ytest.npy',y_test )
np.save('Xtest.npy',X_test )
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
X_train=X_train.T
X_test=X_test.T
y_train=y_train.reshape(y_train.shape[0],1)
y_test=y_test.reshape(y_test.shape[0],1)
y_train=y_train.T
y_test=y_test.T
Y_train_=np.zeros((10,y_train.shape[1]))
for i in range(y_train.shape[1]):
Y_train_[y_train[0,i],i]=1
Y_test_=np.zeros((10,y_test.shape[1]))
for i in range(y_test.shape[1]):
Y_test_[y_test[0,i],i]=1
if (os.path.isfile("NN.npy")):
parameters=np.load('NN.npy',allow_pickle='TRUE').item()
else:
parameters = L_layer_model(X_train, Y_train_, layers_dims, num_iterations = 50000, print_cost = True)
np.save('NN.npy', parameters)