def evaluate_reg(self): reg_vals = [] for group in self.param_groups: group_reg_val = 0.0 # define regularizer for this group reg = group['reg'] # evaluate the reguarizer for each parametr in group for p in group['params']: group_reg_val += reg(p) # append the group reg val reg_vals.append(group_reg_val) return reg_vals
def linear_grad(X, y, theta, reg=None, lambda_=0): """ compute the gradient of cost function :param X: matrix of dataset [x(0).T,x(1).T,...,x(m).T] y: real value for each example (=row) in X theta: vector of parameters for the feature reg: <function>: function for regularization the gradient lambda: limit the search area of theta :return: cost'(X) :efficiency: O(m*n + m*n^2 + n) """ grad = (X.T @ (X @ theta - y)) if reg: grad[1:] += lambda_ * reg(theta[1:]) return (1 / X.shape[0]) * (X.T @ (X @ theta - y))
def linear_cost(X, y, theta, reg=None, lambda_=0): """ compute the cost of the range between X*theta and y :param X: matrix of dataset [x(0).T,x(1).T,...,x(m).T] y: real value for each example (=row) in X theta: vector of parameters for the feature reg: <function>: function for regularization the cost lambda: limit the search area of theta :return: <float>: J cost of data x for the current theta :efficiency: O(m*n^2) """ J = np.sum((X @ theta - y)**2) if reg: J += lambda_ * reg(theta[1:]) return (1 / (2 * X.shape[0])) * J
def class_cost(X, y, theta, reg=None, lambda_=0): """ compute the cost of the range between sigmoid(X*theta) and y :param X: matrix of dataset [x(0).T,x(1).T,...,x(m).T] y: real value for each example (=row) in X theta: vector of parameters for the feature reg: <function>: function for regularization the cost lambda: limit the search area of theta :return: <float>: J cost of data x for the current theta :efficiency: O(m*n^2) """ m = X.shape[0] Z = sigmoid(X, theta) J = (-1 / m) * (np.sum(np.log(Z[y == 1])) + np.sum(np.log(1 - Z[y == 0]))) if reg: J += (lambda_ / (2 * m)) * reg(theta[1:]) # regularization return J
def class_grad(X, y, theta, reg=None, lambda_=0): """ compute the gradient of cost function :param X: matrix of dataset [x(0).T,x(1).T,...,x(m).T] y: real value for each example (=row) in X theta: vector of parameters for the feature reg: <function>: function for regularization the gradient lambda: limit the search area of theta :return: cost'(X) :efficiency: O(m*n + m*n^2 + n) """ m = X.shape[0] grad = (1 / m) Z = sigmoid(X, theta) grad *= (X.T @ (Z - y)) if reg: grad[1:] += (lambda_ / m) * reg(theta[1:]) # regularization gradient return grad
number = [] # Load text f = textReader("uploaded/" + filename).readText() for row in f: temp = tokenize.sent_tokenize(row) for text in temp: sentenceContainer.append(text.lower()) # Select algorithm if (algoritma == "bm"): alg = bm() elif (algoritma == "kmp"): alg = kmp() else: alg = reg() queryNumber = r"^([0-9]+([\.,:]?[0-9]*)*)$" queryDate = r".?(([1-9]|1[0-9]|2[0-9]|3[0-1])(/|-)(0?[1-9]|1[0-2])(/|-|)([0-9]{0,4})).?" queryMonth = r"\b(januari|februari|maret|april|mei|juni|juli|agustus|september|oktober|november|desember|jan|feb|mar|apr|jun|jul|ags|sep|okt|nov|des)\b" queryDay = r"\b(senin|selasa|rabu|kamis|jumat|sabtu|minggu)\b" queryTime = r"^(wib|wita|wit|pm|am)" # Sub sentence for row in sentenceContainer: subsSentence = [] row = row.split(' ') for sub in row: subsSentence.append(sub) newContainer.append(subsSentence)