感知机
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
初始化
def initilize_with_zeros(dim):
w = np.zeros((dim, 1))
b = 0.0
#assert(w.shape == (dim, 1))
#assert(isinstance(b, float) or isinstance(b, int))
return w, b
前向传播
def propagate(w, b, X, Y):
m = X.shape[1]
A = sigmoid(np.dot(w.T, X) + b)
cost = -1/m * np.sum(Y*np.log(A) + (1-Y)*np.log(1-A))
dw = np.dot(X, (A-Y).T)/m
db = np.sum(A-Y)/m
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = { 'dw': dw,
'db': db
}
return grads, cost
反向传播
def backward_propagation(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
cost = []
for i in range(num_iterations):
grad, cost = propagate(w, b, X, Y)
dw = grad['dw']
db = grad['db']
w = w - learing_rate * dw
b = b - learning_rate * db
if i % 100 == 0:
cost.append(cost)
if print_cost and i % 100 == 0:
print("cost after iteration %i: %f" %(i, cost))
params = {"dw": w,
"db": 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)
for i in range(A.shape[1]):
if A[:, i] > 0.5:
Y_prediction[:, i] = 1
else:
Y_prediction[:, i] = 0
assert(Y_prediction.shape == (1, m))
return Y_prediction
简单封装
def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):
# initialize parameters with zeros (≈ 1 line of code)
w, b = initialize_with_zeros(X_train.shape[0]) # Gradient descent (≈ 1 line of code)
parameters, grads, costs = backward_propagation(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost) # Retrieve parameters w and b from dictionary "parameters"
w = parameters["w"]
b = parameters["b"] # Predict test/train set examples (≈ 2 lines of code)
Y_prediction_train = predict(w, b, X_train)
Y_prediction_test = predict(w, b, X_test) # Print train/test Errors
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
参考文献:https://blog.csdn.net/hellozhxy/article/details/81055600
本文由 admin 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Apr 8, 2019 at 04:29 am