D'abords le neurone :
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))
Un réseau avec une seule couche et deux neurones :
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))
Deux couches :
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))
Et pourquoi pas trois couches :
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))
x = np.array([[0],
[0]])
print(nn(w,b,x))
#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))