def KL(rho, rho_target, KL_flat): y = rho.copy() if KL_flat: y[gp.where(y < rho_target)] = rho_target * gp.ones( y[gp.where(y < rho_target)].shape) return rho_target * gp.log(rho_target / y) + (1 - rho_target) * gp.log( (1 - rho_target) / (1 - y))
def where(x, *args): """Delegate to gnumpy.where or numpy.where depending on the type of `x`.""" if not isinstance(x, np.ndarray): return gp.where(x, *args) else: return np.where(x, *args)
def predict(self, X): return np.where(self.net_input(X) >= 0.0, 1, -1)
def predict(self, X): return np.where(self.activation(X) >= 0.0, 1, -1)
errors += int(update != 0.0) self.errors_.append(errors) return self def net_input(self, X): return np.dot(X, self.w_[1:]) + self.w_[0] def predict(self, X): return np.where(self.net_input(X) >= 0.0, 1, -1) import pandas as pd df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) # setosa and versicolor y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', -1, 1) # sepal length and petal length X = df.iloc[0:100, [0,2]].values import matplotlib.pyplot as plt from mlxtend.evaluate import plot_decision_regions ppn = Perceptron(epochs=10, eta=0.1) ppn.train(X, y) print('Weights: %s' % ppn.w_) plot_decision_regions(X, y, clf=ppn) plt.title('Perceptron') plt.xlabel('sepal length [cm]')
def where(x): check_type(x) if is_np(x): return np.where(x) else: return gp.where(x)
def d_KL(rho, rho_target, KL_flat): y = rho.copy() if KL_flat: y[gp.where(y < rho_target)] = rho_target * gp.ones( y[gp.where(y < rho_target)].shape) return -rho_target / y + (1 - rho_target) / (1 - y)
def relu_prime_truncated(x): y = gp.ones(x.shape) #if y>.9999: print 'salam' y[gp.where(x > .9999)] = gp.zeros(x[gp.where(x > .9999)].shape) y[gp.where(x < .00001)] = gp.zeros(x[gp.where(x < .00001)].shape) return y
def relu_truncated(x): y = x.copy() y[gp.where(x > .9999)] = .9999 * gp.ones(x[gp.where(x > .9999)].shape) y[gp.where( x < .00001)] = .00001 * gp.ones(x[gp.where(x < .00001)].shape) return y