<gh_stars>0
import numpy as np
from numpy import load
import copy 

path = r'C:/Users/rotvi/Downloads/kagglecatsanddogs_3367a/PetImages/arrays/'

photos_train = load(path + 'dogs_vs_cats_photos_train.npy')
labels_train = load(path + 'dogs_vs_cats_labels_train.npy')
photos_test = load(path + 'dogs_vs_cats_photos_test.npy')
labels_test = load(path + 'dogs_vs_cats_labels_test.npy')

X_train = photos_train.reshape(photos_train.shape[0], -1).T
Y_train = np.reshape(labels_train, (1, labels_train.shape[0]))
X_test = photos_test.reshape(photos_test.shape[0], -1).T
Y_test = np.reshape(labels_test, (1, labels_test.shape[0]))

X_train = X_train/255.
X_test = X_test/255.
epsilon = 1e-6

print(X_train.shape, Y_train.shape) 
print(X_test.shape, Y_test.shape)  

def initialize_with_zeros(n_X):
    w = np.random.randn(n_X, 1) * (np.sqrt(1/n_X))
    b = 0.0
    return w, b

def propagation(w, b, X, Y): 
    m = X.shape[1]
    Z = np.dot(w.T, X) + b
    A = 1/(1 + np.exp(-Z))
    J = -(np.dot(Y, np.log(A+epsilon).T)+np.dot(1-Y,np.log(1-A+epsilon).T))/m 
    J = np.squeeze(J)
    dz = A - Y
    dw = np.dot(X, dz.T)/m
    db = np.sum(dz)/m
    grads = {"dw": dw,
             "db": db}
    return grads, J

def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost):

    w = copy.deepcopy(w)
    b = copy.deepcopy(b)
    costs = []
    for i in range(num_iterations):
        grads, J = propagation(w, b, X, Y)
        dw = grads["dw"]
        db = grads["db"]
        w = w - learning_rate*dw
        b = b - learning_rate*db
        if i % 100 == 0:
            costs.append(J)
            if print_cost:
                print ("Cost after iteration %i: %f" %(i, J))
    params = {"w": w,
              "b": b}
    
    grads = {"dw": dw,
             "db": db}
    
    return params, grads, costs


def predict(w, b, X):
    m = X.shape[1]
    Y_prediction = np.zeros((1, m))
    w = w.reshape(X.shape[0], 1)
    Z = np.dot(w.T, X) + b
    A = 1/(1 + np.exp(-Z))
    for i in range(A.shape[1]):
        
        if A[0,i] >= 0.5:
            Y_prediction[0,i] = 1
        else:
            Y_prediction[0,i] = 0

    return Y_prediction

def model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost=False):

    w, b = initialize_with_zeros(X_train.shape[0])
    params , grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
    w = params["w"]
    b = params["b"]
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)
    if print_cost:
        print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
        print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

    
    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test, 
         "Y_prediction_train" : Y_prediction_train, 
         "w" : w, 
         "b" : b,
         "learning_rate" : learning_rate,
         "num_iterations": num_iterations}
    
    return d

logistic_regression_model = model(X_train, Y_train, X_test, Y_test, num_iterations=500, learning_rate=0.0003, print_cost=True)
import numpy as np
from numpy import load
import copy 

path = r'C:/Users/rotvi/Downloads/kagglecatsanddogs_3367a/PetImages/arrays/'

photos_train = load(path + 'dogs_vs_cats_photos_train.npy')
labels_train = load(path + 'dogs_vs_cats_labels_train.npy')
photos_test = load(path + 'dogs_vs_cats_photos_test.npy')
labels_test = load(path + 'dogs_vs_cats_labels_test.npy')

X_train = photos_train.reshape(photos_train.shape[0], -1).T
Y_train = np.reshape(labels_train, (1, labels_train.shape[0]))
X_test = photos_test.reshape(photos_test.shape[0], -1).T
Y_test = np.reshape(labels_test, (1, labels_test.shape[0]))

X_train = X_train/255.
X_test = X_test/255.
epsilon = 1e-6

print(X_train.shape, Y_train.shape) 
print(X_test.shape, Y_test.shape)  

def initialize_with_zeros(n_X):
    w = np.random.randn(n_X, 1) * (np.sqrt(1/n_X))
    b = 0.0
    return w, b

def propagation(w, b, X, Y): 
    m = X.shape[1]
    Z = np.dot(w.T, X) + b
    A = 1/(1 + np.exp(-Z))
    J = -(np.dot(Y, np.log(A+epsilon).T)+np.dot(1-Y,np.log(1-A+epsilon).T))/m 
    J = np.squeeze(J)
    dz = A - Y
    dw = np.dot(X, dz.T)/m
    db = np.sum(dz)/m
    grads = {"dw": dw,
             "db": db}
    return grads, J

def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost):

    w = copy.deepcopy(w)
    b = copy.deepcopy(b)
    costs = []
    for i in range(num_iterations):
        grads, J = propagation(w, b, X, Y)
        dw = grads["dw"]
        db = grads["db"]
        w = w - learning_rate*dw
        b = b - learning_rate*db
        if i % 100 == 0:
            costs.append(J)
            if print_cost:
                print ("Cost after iteration %i: %f" %(i, J))
    params = {"w": w,
              "b": b}
    
    grads = {"dw": dw,
             "db": db}
    
    return params, grads, costs


def predict(w, b, X):
    m = X.shape[1]
    Y_prediction = np.zeros((1, m))
    w = w.reshape(X.shape[0], 1)
    Z = np.dot(w.T, X) + b
    A = 1/(1 + np.exp(-Z))
    for i in range(A.shape[1]):
        
        if A[0,i] >= 0.5:
            Y_prediction[0,i] = 1
        else:
            Y_prediction[0,i] = 0

    return Y_prediction

def model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost=False):

    w, b = initialize_with_zeros(X_train.shape[0])
    params , grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
    w = params["w"]
    b = params["b"]
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)
    if print_cost:
        print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
        print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

    
    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test, 
         "Y_prediction_train" : Y_prediction_train, 
         "w" : w, 
         "b" : b,
         "learning_rate" : learning_rate,
         "num_iterations": num_iterations}
    
    return d

logistic_regression_model = model(X_train, Y_train, X_test, Y_test, num_iterations=500, learning_rate=0.0003, print_cost=True)