<jupyter_start><jupyter_text>Neural Networks (example from sklearn and https://github.com/amirziai/deep-learning-coursera)  The problem: determine if an image has a cat or not, given labeled data |**Cat**|  ||  ||:---:|:---:|:---:||**Non Cat**|  ||   |<jupyter_code>import numpy as np

from sklearn import neural_network
from lr_utils import load_dataset
import matplotlib.pyplot as plt

%matplotlib inline
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
# NORMALIZATION
X_train = X_train_orig.reshape(X_train_orig.shape[0], -1)
X_test = X_test_orig.reshape(X_test_orig.shape[0], -1)
X_train = X_train/255.
X_test = X_test/255.

Y_train = Y_train_orig.T
Y_test = Y_test_orig.T
index = 102
# print (X_train[index].reshape(()))
plt.imshow(X_train_orig[index])
print ("y = " + str(Y_train[index]) + ", it's a '" + classes[np.squeeze(Y_train[index])].decode("utf-8") +  "' picture.")<jupyter_output>y = [1], it's a 'cat' picture.<jupyter_text>sklearn implementation<jupyter_code>clf = neural_network.MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1, max_iter=5000)
clf.fit(X_train, Y_train.ravel())
predictions = clf.predict(X_train)
print ('Accuracy: %d ' % ((np.sum(Y_train.ravel() == predictions))/float(Y_train.size)*100))
predictions = clf.predict(X_test)
print ('Accuracy: %d ' % ((np.sum(Y_test.ravel() == predictions))/float(Y_test.size)*100))

predictions, Y_test.ravel()<jupyter_output>Accuracy: 80<jupyter_text>What if we implement it?<jupyter_code>def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))
def initialize_with_zeros(dim):
    w = np.zeros((dim, 1))
    b = 0.0
    return w, b
def propagate(w, b, X, Y):
    m = X.shape[1]    

    # FORWARD PROPAGATION
    A = sigmoid(np.dot(w.T, X) + b)                                          # compute activation
    cost = (-1.0 / m) * np.sum(Y * np.log(A) + (1.0 - Y) * np.log(1.0 - A))  # compute cost

    # BACKWARD PROPAGATION
    dw = (1.0 / m) * np.dot(X, (A - Y).T)
    db = (1.0 / m) * np.sum(A - Y)

    cost = np.squeeze(cost)
    grads = {"dw": dw,
             "db": db}
    
    return grads, cost
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):    
    costs = []
    
    for i in range(num_iterations):
        # Cost and gradient
        grads, cost = propagate(w, b, X, Y)
        if i % 100 == 0:
            costs.append(cost)
        dw = grads["dw"]
        db = grads["db"]
        
        w = w - learning_rate * dw
        b = b - learning_rate * db
        
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
    
    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)
    A = sigmoid(np.dot(w.T, X) + b)
    Y_prediction[A >= 0.5] = 1
    Y_prediction[A < 0.5] = 0
    return Y_prediction
def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):

    w, b = initialize_with_zeros(X_train.shape[0])
    parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
    w = parameters["w"]
    b = parameters["b"]
    
    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)

    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
d = model(X_train.T, Y_train.ravel(), X_test.T, Y_test.ravel(), num_iterations = 5000, learning_rate = 0.001, print_cost = True)
index = 7
num_px = X_train_orig[index].shape[0]

plt.imshow(X_test[index,:].reshape((num_px, num_px, 3)))
print ("y = " + str(Y_test[index,0]) + ", you predicted that it is a \"" + classes[int(d["Y_prediction_test"][0,index])].decode("utf-8") +  "\" picture.")<jupyter_output>y = 1, you predicted that it is a "cat" picture.
{'fraction_non_alphanumeric': 0.16213726393367112, 'fraction_numerical': 0.02349147858129894, 'mean_word_length': 3.085606773283161, 'pattern_counts': {'":': 9, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '460408', 'license': 'MIT', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1678, 'n_words': 368, 'path': 'archive/2018/demo7.ipynb', 'repo_name': 'guillepaez53/AprendizajeSupervisado', 'size': '4342'}