Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #parameter setting
- import numpy as np
- from mnist import load_mnist
- np.random.seed(5)
- (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True ,one_hot_label=True)
- <br>
- /*mnist save <br><br> */
- X = x_train[0:100] /* Test data */ <br>
- W = {} <br>
- W['W1'] = np.random.randn(784, 30)
- W['W2'] = np.random.randn(30, 20)
- W['W3'] = np.random.randn(20, 10) /*set weight */
- S = {}
- A = {}
- Y = t_train[0:100]
- dA ={}
- dS ={}
- dW ={}
- ---
- #function setting
- ### def Softmax(ScoreMatrix):
- if ScoreMatrix.ndim == 2:
- temp = ScoreMatrix
- temp = temp - np.max(temp, axis=1, keepdims= True)
- y_predict = np.exp(temp) / np.sum(np.exp(temp), axis=1, keepdims=True)
- return y_predict
- temp = ScoreMatrix - np.max(ScoreMatrix, axis=0)
- expX = np.exp(temp)
- y_predict = expX / np.sum(expX)
- return y_predict
- ### def relu(Score):
- A = np.maximum(0, Score)
- return A
- ### def forward(X):
- A['A0'] = X
- S['S1'] = np.dot(X, W['W1'])
- A['A1'] = relu(S['S1'])
- S['S2'] = np.dot(A['A1'], W['W2'])
- A['A2'] = relu(S['S2'])
- S['S3'] = np.dot(A['A2'], W['W3'])
- Y_predict = Softmax(S['S3'])
- Loss = -(np.sum(Y*np.log(Y_predict+1e-7))) / Y.shape[0]
- print(Loss)
- return Y_predict
- ### def Backward(Y_predict):
- mask ={}
- dS['S3'] = (Y_predict - Y) / Y.shape[0]
- dW['W3'] = np.dot(A['A2'].T, dS['S3'])
- dA['A2'] = np.dot(dS['S3'], W['W3'].T)
- mask['R2'] = (A['A2'] > 0)
- dS['S2'] = dA['A2']*mask['R2']
- dW['W2'] = np.dot(A['A1'].T, dS['S2'])
- dA['A1'] = np.dot(dS['S2'], W['W2'].T)
- mask['R1'] = (A['A1'] > 0)
- dS['S1'] = dA['A1'] * mask['R1']
- dW['W1'] = np.dot(A['A0'].T, dS['S1'])
- ### def Gradient():
- W['W1'] -= 0.1*dW['W1']
- W['W2'] -= 0.1*dW['W2']
- W['W3'] -= 0.1*dW['W3']
- ### def Optimizer():
- for i in range(10000):
- Y_predict = forward(X)
- Backward(Y_predict)
- Gradient()
- Y_predict = forward(x_train[100:200])
- Y_predict.shape
- Last = (Y_predict - Y) / Y.shape[0]
- Optimizer() /* optimizer call
- y_predict =forward(x_train[0:100])<br>
- print(y_predict[10].shape) <br>
- count=0 <br>
- it is checked y_predict[i] checked [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- for i in range(100):
- if(np.array_equal(y_predict[i], np.ones(10)*0.1)):
- print(i)
- print(y_predict[i])
- count+=1
- print('count ', count)
- '''
- ## result is :
- y_predict[i]
- 41
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- 45
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- 54
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- 67
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- 72
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- 76
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- 82
- [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
- count 7
- Why did this result come out?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement