Upload pavlov.195.py
Browse files- pavlov.195.py +120 -0
pavlov.195.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled2.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1ERPT573YXenYO4d-XY_q5dm6ffWei6xQ
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
|
| 13 |
+
def init_params(layer_dims):
|
| 14 |
+
np.random.seed(3)
|
| 15 |
+
params = {}
|
| 16 |
+
L = len(layer_dims)
|
| 17 |
+
|
| 18 |
+
for l in range(1, L):
|
| 19 |
+
params['W'+str(1)] = np.random.randn(layer_dims[1], layer_dims[l-11])*0.01
|
| 20 |
+
params['b'+str(1)] = np.zeros((layer_dims[1]))
|
| 21 |
+
|
| 22 |
+
return
|
| 23 |
+
|
| 24 |
+
# Z (linear hypothesis) - Z = W*X + b,
|
| 25 |
+
# W - weight matrix, b - bias vector, X- Input
|
| 26 |
+
|
| 27 |
+
def sigmoid(Z):
|
| 28 |
+
A = 1/(1+np.exp(np.dot(-1, Z)))
|
| 29 |
+
cache = (Z)
|
| 30 |
+
|
| 31 |
+
return A, cache
|
| 32 |
+
|
| 33 |
+
def forward_prop(X, params):
|
| 34 |
+
|
| 35 |
+
A = X # input to first layer i.e. training data
|
| 36 |
+
caches = []
|
| 37 |
+
L = len(params)//2
|
| 38 |
+
for l in range(1, L +1):
|
| 39 |
+
A_prev = A
|
| 40 |
+
|
| 41 |
+
# Linear Hypthesis
|
| 42 |
+
Z = np.dot(params['W'+str(1)], A_prev) + params['b'+str(1)]
|
| 43 |
+
|
| 44 |
+
# Storing the linear cache
|
| 45 |
+
linear_cache = (A_prev, params['W'+str(1)], params['b'+str(1)])
|
| 46 |
+
|
| 47 |
+
# Applying sigmoid on linear hypothesis
|
| 48 |
+
A, activation_cache = sigmoid(Z)
|
| 49 |
+
|
| 50 |
+
# storing the both linear and activation cache
|
| 51 |
+
cache = (linear_cache, activation_cache)
|
| 52 |
+
caches.append(cache)
|
| 53 |
+
|
| 54 |
+
return A, caches
|
| 55 |
+
|
| 56 |
+
def cost_function(A, Y):
|
| 57 |
+
m = Y.shape[1]
|
| 58 |
+
|
| 59 |
+
cost = (-1/m)*(np.dot(np.log(A), Y.T) + np.dot(log(1-A), 1-Y.T))
|
| 60 |
+
|
| 61 |
+
return cost
|
| 62 |
+
|
| 63 |
+
def one_layer_backward(dA, cache):
|
| 64 |
+
linear_cache, activation_cache = cache
|
| 65 |
+
|
| 66 |
+
Z = activation_cache
|
| 67 |
+
dZ = dA*sigmoid(Z)*(1-sigmoid(Z)) # The derivative of the sigmoid function
|
| 68 |
+
|
| 69 |
+
A_prev, W, b = linear_cache
|
| 70 |
+
m = A_prev.shape[1]
|
| 71 |
+
|
| 72 |
+
dW = (1/m)*np.dot(dZ, A_prev.T)
|
| 73 |
+
db = (1/m)*np.sum(dZ, axis=1, keepdims=True)
|
| 74 |
+
dA_prev = np.dot(W.T, dZ)
|
| 75 |
+
|
| 76 |
+
return dA_prev, dW, db
|
| 77 |
+
|
| 78 |
+
def backprop(AL, Y, caches):
|
| 79 |
+
grads = {}
|
| 80 |
+
L = len(caches)
|
| 81 |
+
m = AL.shape[1]
|
| 82 |
+
Y = Y.reshape(AL.shape)
|
| 83 |
+
|
| 84 |
+
dAL = (np.divide(Y, AL) - np.divide(1-Y, 1-AL))
|
| 85 |
+
|
| 86 |
+
current_cache = caches[L-1]
|
| 87 |
+
grads['dA'+str(L-1)], grads['dW'+str(L-1)], grads['db'+str(L-1)] = one_layer_backward(dAL, current_cache)
|
| 88 |
+
|
| 89 |
+
for l in reversed(range(L-1)):
|
| 90 |
+
|
| 91 |
+
current_cache = caches[1]
|
| 92 |
+
dA_prev_temp, dW_temp, db_temp = one_layer_backward(grads["dA" + str(l+1)], current_cache)
|
| 93 |
+
grads["dA" + str(1)] = dA_prev_temp
|
| 94 |
+
grads["dW" + str(1 + 1)] = dW_temp
|
| 95 |
+
grads["db" + str(l + 1 )] = db_temp
|
| 96 |
+
|
| 97 |
+
return grads
|
| 98 |
+
|
| 99 |
+
def update_parameters(parameters, grads, learning_rate):
|
| 100 |
+
L = len(parameters) // 2
|
| 101 |
+
|
| 102 |
+
for l in range(L):
|
| 103 |
+
parameters['W'+str(l+1)] = parameters['W'+str(l+1)] - learning_rate*grads['W'+str(l+1)]
|
| 104 |
+
parameters['b'+str(l+1)] = parameters['b'+str(l+1)] - learning_rate*grads['b'+str(l+1)]
|
| 105 |
+
|
| 106 |
+
return parameters
|
| 107 |
+
|
| 108 |
+
def train(X, Y, layer_dims, epochs, lr):
|
| 109 |
+
params = init_params(layer_dims)
|
| 110 |
+
cost_history = []
|
| 111 |
+
|
| 112 |
+
for i in range(epochs):
|
| 113 |
+
Y_hat, caches = forward_prop(X, params)
|
| 114 |
+
cost = cost_function(Y_hat, Y)
|
| 115 |
+
cost_history.append(cost)
|
| 116 |
+
grads = backprop(Y_hat, Y, caches)
|
| 117 |
+
|
| 118 |
+
params = update_parameters(params, grads, lr)
|
| 119 |
+
|
| 120 |
+
return params, cost_history
|