Deep Learning

Réseaux de neurones

D'abords le neurone :

In [20]:
import numpy as np

def sigmoid(z):
    return 1./(1 + np.exp(-z))

def neurone(x,w,b):
    z = np.vdot(x,w) + b
    return sigmoid(z) 


x = np.array([1, 1, 1])
w = np.array([1, 1, 1])
b = np.array([1])
print(neurone(x,w,b))
[ 0.98201379]

Un réseau avec une seule couche et deux neurones :

In [21]:
def nn_1(w,b,x):
    z = np.dot(w,x) + b
    return sigmoid(z)

w = np.array([[1, -2], 
              [-1, 1]])
b = np.array([[1], [0]])
x = np.array([[1], 
              [-1]])

print(nn_1(w,b,x))
[[ 0.98201379]
 [ 0.11920292]]

Deux couches :

In [22]:
def nn_2(w,b,x):
    y = nn_1(w[0],b[0],x)
    return nn_1(w[1],b[1], y)

w = np.array([[[1, -2], 
              [-1, 1]],
              [[2, -1], 
              [-2, -1]]
             ])
b = np.array([
            [[1], [0]],
            [[0], [0]]
            ])    
x = np.array([[1], 
              [-1]])
print(nn_2(w,b,x))
[[ 0.86351831]
 [ 0.11073744]]

Et pourquoi pas trois couches :

In [23]:
def nn(w,b,x):
    y1 = nn_1(w[0],b[0],x)
    y2 = nn_1(w[1],b[1], y1)
    y3 = nn_1(w[2],b[2], y2)
    return y3

w = np.array([
            [[1, -2], 
             [-1, 1]],
            [[2, -1], 
              [-2, -1]],
            [[3, -1],
             [-1, 4]]
             ])
b = np.array([
            [[1], [0]],
            [[0], [0]],
            [[-2], [2]]
            ])    
x = np.array([[1], 
              [-1]])
print(nn(w,b,x))
[[ 0.61770478]
 [ 0.82912398]]
In [24]:
x = np.array([[0], 
              [0]])
print(nn(w,b,x))
[[ 0.51184738]
 [ 0.8543839 ]]
In [17]:
#def sign(x):
#    res = [if x[i] >= 0 : 1 else 0 for i in range[length(x)]]
#    return res

#def xor(x):
#    w = np.array([
#        [[1, 1],
#         [1, 1]],
#        [[-1, 1],
#         [0, 0]]
#    ])
#    b = np.array([
#        [[-3./2],[-1./2]],
#        [[-1./2],[0]]
#    ])
#    y1 = np.dot(w[0],x) + b[0]
#    #print(y1)
#    y2 = np.dot(w[1], y1) + b[1]
#    print(y2)
#    if y2[0] >= 0.5:
#        return 1
#    return 0

#x = np.array([[1],
#    [0]
#])

#for x1 in [0, 1]:
#    for x2 in [0, 1]:
#        x = [[x1], [x2]]
#        print(x1, ' ', x2,' ',xor(x))