Báo cáo học liệu
Mua học liệu
Mua học liệu:
-
Số dư ví của bạn: 0 coin - 0 Xu
-
Nếu mua học liệu này bạn sẽ bị trừ: 2 coin\Xu
Để nhận Coin\Xu, bạn có thể:
CHÚC MỪNG
Bạn đã nhận được sao học tập
Chú ý:
Thành tích của bạn sẽ được cập nhật trên bảng xếp hạng sau 1 giờ!
Code MLP và Backpropagation SVIP
Nội dung này do giáo viên tự biên soạn.
00:00
# To support both python 2 and python 3 from __future__ import division, print_function, unicode_literals import math import numpy as np N = 100 # number of points per class d0 = 2 # dimensionality C = 3 # number of classes X = np.zeros((d0, N*C)) # data matrix (each row = single example) y = np.zeros(N*C, dtype='uint8') # class labels for j in range(C): ix = range(N*j,N*(j+1)) r = np.linspace(0.0,1,N) # radius t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta X[:,ix] = np.c_[r*np.sin(t), r*np.cos(t)].T y[ix] = j print(X.T) print(y) def softmax(V): e_V = np.exp(V - np.max(V, axis=0, keepdims=True)) Z = e_V / e_V.sum(axis=0) return Z ## One-hot coding from scipy import sparse def convert_labels(y, C=3): Y = sparse.coo_matrix((np.ones_like(y), (y, np.arange(len(y)))), shape=(C, len(y))).toarray() return Y # cost or loss function def cost(Y, Yhat): return -np.sum(Y * np.log(Yhat)) / Y.shape[1] d0 = 2 d1 = h = 20 # size of hidden layer d2 = C = 3 # initialize parameters randomly W1 = 0.01*np.random.randn(d0, d1) b1 = np.zeros((d1, 1)) W2 = 0.01*np.random.randn(d1, d2) b2 = np.zeros((d2, 1)) Y = convert_labels(y, C) N = X.shape[1] eta = 1 # learning rate for i in range(10000): ## Feedforward Z1 = np.dot(W1.T, X) + b1 A1 = np.maximum(Z1, 0) Z2 = np.dot(W2.T, A1) + b2 Yhat = softmax(Z2) # print loss after each 1000 iterations if i %1000 == 0: # compute the loss: average cross-entropy loss loss = cost(Y, Yhat) print("iter %d, loss: %f" %(i, loss)) # backpropagation E2 = (Yhat - Y )/N dW2 = np.dot(A1, E2.T) db2 = np.sum(E2, axis = 1, keepdims = True) E1 = np.dot(W2, E2) E1[Z1 <= 0] = 0 # gradient of ReLU dW1 = np.dot(X, E1.T) db1 = np.sum(E1, axis = 1, keepdims = True) # Gradient Descent update W1 += -eta*dW1 b1 += -eta*db1 W2 += -eta*dW2 b2 += -eta*db2 print(W1) print(b1) print(W2) print(b2) Z1 = np.dot(W1.T, X) + b1 A1 = np.maximum(Z1, 0) Z2 = np.dot(W2.T, A1) + b2 predicted_class = np.argmax(Z2, axis=0) print('training accuracy: %.2f %%' % (100*np.mean(predicted_class == y)))
OLMc◯2022
Bạn có thể đánh giá bài học này ở đây