<jupyter_start><jupyter_text>Réseau de Neurone<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
plt.style.use('ggplot')

import warnings
#suppress warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>1. Dataset<jupyter_code>X, y = make_blobs(n_samples=100, n_features=2, centers=2, random_state=0)
y = y.reshape(y.shape[0], 1)

print(f'Shape de X: {X.shape}\nSshape de y: {y.shape}')

plt.scatter(X[:, 0], X[:, 1], c=y, cmap='summer')<jupyter_output>Shape de X: (100, 2)
Sshape de y: (100, 1)<jupyter_text>2. Initialisation des Paramètres<jupyter_code>def initialisation(X):
    W = np.random.randn(X.shape[1], 1)
    b = np.random.randn(1)
    return (W, b)
W, b = initialisation(X)
W
b<jupyter_output><empty_output><jupyter_text>3. Modèle<jupyter_code>def modele(X, W, b):
    Z = X.dot(W) + b
    A = 1 / (1 + np.exp(-Z))
    return A
A = modele(X, W, b)
A.shape<jupyter_output><empty_output><jupyter_text>4. Fonction Coût<jupyter_code>def log_loss(A, y):
    return 1 / len(y) * np.sum(-y * np.log(A) - (1 - A) * np.log(1 - A))
Loss = log_loss(A, y)
Loss<jupyter_output><empty_output><jupyter_text>5. Gradients<jupyter_code>def gradients(X, A, y):
    dW = 1 / len(y) * np.dot(X.T, A - y)
    db = 1 / len(y) * np.sum(A - y)
    return (dW, db)
dW, db = gradients(X, A, y)
dW.shape, dW
db.shape, db<jupyter_output><empty_output><jupyter_text>6. Mise à Jour<jupyter_code>def update(dW, db, W, b, learning_rate):
    W = W - learning_rate * dW
    b = b - learning_rate * db
    return (W, b)
W,b
W, b = update(dW, db, W, b, 0.01)
W, b
from sklearn.metrics import accuracy_score
from sklearn.metrics import log_loss as log_err
def softmax(x):
    if x >= 0.5:
        return 1
    else:
        return 0

def predict(X, W, b):
    y_pred = modele(X, W, b)
    # print(y_pred)
    return y_pred >= 0.5<jupyter_output><empty_output><jupyter_text>7. Entrainement du Modèle<jupyter_code>def train_cnn(X, y, learning_rate=0.1, epochs=100):
    ## Initialisation
    W, b = initialisation(X)
    
    Loss = []
    
    for i in range(epochs):
        
        A = modele(X, W, b)
        
        Loss.append(log_err(y, A))
        
        dW, db = gradients(X, A, y)
        
        W, b = update(dW, db, W, b, learning_rate)
        
    y_pred = predict(X, W, b)
    print(f'Score: {accuracy_score(y_pred, y)}')
        
    plt.plot(Loss)
    
    return W, b
W, b = train_cnn(X, y)<jupyter_output>Score: 0.86<jupyter_text>8. Frontiere de décision<jupyter_code>new_plant = np.array([2, 1])
fig, ax = plt.subplots(figsize=(9, 6))
ax.scatter(X[:,0], X[:, 1], c=y, cmap='summer')
ax.scatter(new_plant[0], new_plant[1], c='r')

x1 = np.linspace(-2, 5, 100)
x2 = ( - W[0] * x1 - b) / W[1]

ax.plot(x1, x2, c='orange', lw=3)
predict(new_plant, W, b)<jupyter_output><empty_output><jupyter_text>9. Visualisations 3D<jupyter_code>import plotly.graph_objects as go
# fig = go.Figure(data=[go.Scatter3d( 
#     x=X[:, 0].flatten(),
#     y=X[:, 1].flatten(),
#     z=y.flatten(),
#     mode='markers',
#     marker=dict(
#         size=5,
#         color=y.flatten(),                
#         colorscale='YlGn',  
#         opacity=0.8,
#         reversescale=True
#     )
# )])

# fig.update_layout(template= "plotly_dark", margin=dict(l=0, r=0, b=0, t=0))
# fig.layout.scene.camera.projection.type = "orthographic"
# fig.show()
# X0 = np.linspace(X[:, 0].min(), X[:, 0].max(), 100)
# X1 = np.linspace(X[:, 1].min(), X[:, 1].max(), 100)
# xx0, xx1 = np.meshgrid(X0, X1)
# Z = W[0] * xx0 + W[1] * xx1 + b
# A = 1 / (1 + np.exp(-Z))

# fig = (go.Figure(data=[go.Surface(z=A, x=xx0, y=xx1, colorscale='YlGn', opacity = 0.7, reversescale=True)]))

# fig.add_scatter3d(x=X[:, 0].flatten(), y=X[:, 1].flatten(), z=y.flatten(), mode='markers', marker=dict(size=5, color=y.flatten(), colorscale='YlGn', opacity = 0.9, reversescale=True))


# fig.update_layout(template= "plotly_dark", margin=dict(l=0, r=0, b=0, t=0))
# fig.layout.scene.camera.projection.type = "orthographic"
# fig.show()
from utilities import *
X_train, y_train, X_test, y_test = load_data()
print(X_train.shape)
print(y_train.shape)
print(np.unique(y_train, return_counts=True))
plt.figure(figsize=(16, 8))
for i in range(1, 10):
    plt.subplot(4, 5, i)
    plt.imshow(X_train[i], cmap='gray')
    plt.title(y_train[i])
    plt.tight_layout()
plt.show();<jupyter_output><empty_output><jupyter_text>Flattern les images<jupyter_code>X_train_flattern = X_train.reshape(X_train.shape[0], X_train.shape[1] * X_train.shape[2])
X_test_flattern = X_test.reshape(X_test.shape[0], X_test.shape[1] * X_test.shape[2])
# X_train_flattern = X_train_flattern/250
X_train_flattern.shape
y_train.shape
W_image, b_image = train_cnn(X_train_flattern, y_train, 0.01, 30000)
X_test_flattern = X_test_flattern
y_pred_test = predict(X_test_flattern, W_image, b_image)
print(f'Score: {accuracy_score(y_pred_test, y_test)}')<jupyter_output>Score: 0.51
{'fraction_non_alphanumeric': 0.17662442164554415, 'fraction_numerical': 0.030175015087507542, 'mean_word_length': 4.032388663967612, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '345838', 'license': 'MIT', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2174, 'n_words': 413, 'path': 'python/reprise.ipynb', 'repo_name': 'camara94/formation-deep-learning', 'size': '4971'}