def test_xor(self): X = np.array([0, 0, 1, 1, 0, 1, 1, 0], dtype=np.float32).reshape(4, 2) Y = np.array([0, 0, 1, 1], dtype=np.float32) mlp = MLP(hidden_layer_sizes=(2, ), epoch_num=1600, learning_rate=0.22) np.random.seed(2020) mlp.train(X, Y) self.assertAlmostEqual(numerical_accuracy(mlp.predict(X), Y), 1.0)
def test_multiple_layer_with_regulation(self): # test on UCI ML hand-written digits datasets mlp = MLP(hidden_layer_sizes=(30, ), epoch_num=600, batch_size=32, learning_rate=0.2, _lambda=0.05) digits = load_digits() n_samples = len(digits.images) x_train = digits.data[:n_samples // 2] y_train = digits.target[:n_samples // 2] np.random.seed(2020) mlp.train(x_train, y_train) self.assertTrue( numerical_accuracy(mlp.predict(x_train), y_train) > 0.99) x_test = digits.data[n_samples // 2:] y_test = digits.target[n_samples // 2:] self.assertTrue(numerical_accuracy(mlp.predict(x_test), y_test) > 0.93)
def random_search(train_input, train_target, valid_input, valid_target, save_path, device='/gpu:0', n_iter=100, data_name='sap500'): """Randome Search for Hyper parameter of the model Args: train(valid)_input, train(valid)_target (DataFrame): data to train and validation of network n_inter(int): the number to iterate for parameter search data_name(str): the name of target data """ n_stock = len(train_input.values[0]) conf = SearchConfig() config_list = [] loss_list = [] st = time.time() chosen_symbols = train_input.columns best_loss = np.inf st = time.time() for i in range(n_iter): try: random_conf = generate_config(conf, n_stock, device, save_path) mlp = MLP(random_conf) print('number:', i) mlp.training(train_input, train_target) loss = mlp.accuracy(valid_input, valid_target) print('loss:', loss) if best_loss > loss: # mlp.save() best_loss = loss best_conf = random_conf print(best_conf) prediction = mlp.predict(valid_input) prediction.to_csv("%s_%d.csv" % (data_name, len(chosen_symbols))) plt.plot(prediction, label='prediction') plt.plot(valid_target, label='target') plt.title('%s_%d' % (data_name, len(chosen_symbols))) plt.legend() plt.savefig('%s_%d.png' % (data_name, len(chosen_symbols))) plt.close() elapsed = time.time() - st print("elapsed time:", elapsed) print('******************************************') config_list.append(random_conf) loss_list.append(loss) except KeyboardInterrupt: break except: pass return best_conf
def test_iris(self): # test softmax on Iris dataset iris = load_iris() x_train = iris.data batch_size = int(x_train.shape[0] / 3) y_train = iris.target mlp = MLP(epoch_num=400, batch_size=batch_size, learning_rate=0.1) np.random.seed(2020) mlp.train(x_train, y_train) y_predict = mlp.predict(x_train) self.assertTrue(numerical_accuracy(y_predict, y_train) > 0.95)
def cross_validate(X, y, neurons, activations, dropout, inputDim, outputDim, lr, num_epochs, batch_size, seed, n_splits=5, **kwargs): skf = StratifiedKFold(n_splits=n_splits, random_state=seed) acc_list = list() cv_count = 0 for train_index, val_index in skf.split(X, y[:, 0]): print("Cross validation set: {}".format(cv_count + 1)) X_train = X[train_index, :] y_train = y[train_index, :] X_val = X[val_index] y_val = y[val_index] mlp = MLP() mlp.buildModel(neurons=neurons, activations=activations, dropout=dropout, inputDim=inputDim, outputDim=outputDim) mlp.train(X=X_train, y=y_train, X_test=X_val, y_test=y_val, lr=lr, num_epochs=num_epochs, batch_size=batch_size, seed=seed, printResults=False, returnResults=False) results = mlp.predict(X_val, seed=seed) # print results["y_pred_cls"][:,0] accuracy = (results["y_pred_cls"][:, 0] == y_val[:, 0]).sum() / float( len(results["y_pred_cls"])) acc_list.append(accuracy) print("Accuracy: {}".format(accuracy)) cv_count += 1 return acc_list
rf_simi = RandomForest() model_path = "data/model/rf_same_region.pkl" rf_simi.load_model(model_path) rf_sal = RandomForest() model_path = "data/model/rf_salience.pkl" rf_sal.load_model(model_path) im_data.get_multi_segs(rf_simi) segs_num = len(im_data.rlists) height = im_data.rmat.shape[0] width = im_data.rmat.shape[1] salience_map = np.zeros([segs_num, height, width]) for i, rlist in enumerate(im_data.rlists): Y = rf_sal.predict(im_data.feature93s[i])[:, 1] for j, r in enumerate(rlist): salience_map[i][r] = Y[j] X_test = salience_map.reshape([-1, height*width]).T mlp = MLP() model_path = "data/model/mlp.pkl" mlp.load_model(model_path) Y = mlp.predict(X_test).reshape([height, width])*255 img = np.zeros([height, width*2, 3], dtype=np.uint8) img[:, :width, :] = cv2.imread(img_path) img[:, width:, :] = Y.repeat(3).reshape([height, width, 3]) print("finished~( •̀ ω •́ )y") cv2.imshow("result", img) cv2.waitKey(0)
class Logistic: """ logistic regression using MLP implementation also support softmax regression Parameters ---------- tol: double, optional, the stopping criteria for the weights max_iter: int, optional, the maximal number of iteration learning_rate: double, learning rate in SGD """ def __init__(self, tol=1e-4, max_iter=400, learning_rate=0.1): self.tol = tol self.max_iter = max_iter self.mlp = MLP(batch_size='auto', learning_rate=learning_rate) def get_params(self, deep=False): """Get parameters for this estimator""" return {'tol': self.tol, 'max_iter': self.max_iter} def _iteration_step(self, x_train, y_train): # put your training code here self.mlp._epoch_iterate(x_train, y_train) weight_matrix = self.mlp.weight_value['output_weight'] self.theta = weight_matrix[:, 1] - weight_matrix[:, 0] pass def train(self, x_train, y_train): """Receive the input training data, then learn the model. using the API of multilayer perception Parameters ---------- x_train: np.array, shape (num_samples, num_features) y_train: np.array, shape (num_samples, ) Returns ------- None """ self.theta = np.zeros(x_train.shape[1]) self.mlp.construct_model(x_train, y_train) self.mlp.initWeight() y_train_inner = one_hot(y_train, 2) for _ in range(self.max_iter): last_theta = self.theta.copy() self._iteration_step(x_train, y_train_inner) if np.linalg.norm(self.theta - last_theta) < self.tol: break bias_vector = self.mlp.weight_value['output_bias'] self.intercept = bias_vector[0, 1] - bias_vector[0, 0] return def fit(self, x_train, y_train): # alias for train self.train(x_train, y_train) def score(self, X, y): y_pred = self.predict(X) return accuracy_score(y, y_pred) def predict(self, x_test): """Predict class labels for samples in x_test Parameters ---------- x_test: np.array, shape (num_samples, num_features) Returns ------- pred: np.array, shape (num_samples, ) """ return np.argmax(self.predict_proba(x_test), axis=1) def log_loss(self, x_train, y_train): """Negative of Likelihood""" y_expand = np.vstack([1 - y_train, y_train]) predict_prob = self.predict_proba(x_train) predict_prob += self.tol * (predict_prob == 0).astype(np.float) return -np.sum(y_expand.T * np.log(predict_prob)) def predict_proba(self, x_data): """Predict class labels for samples in x_test Parameters ---------- x_data: np.array, shape (num_samples, num_features) Returns ------- pred: np.array, shape (num_samples, n_classes), the probability of the sample for each class in the model """ pred = self.mlp.predict(x_data) return pred
N = 10000 M = 2000 X, y, X_t, y_t = loaddata(N, M) time.sleep(1) #init config d0 = 784 #datadimension d1 = h = 1000 #number of hidden units d2 = C = 10 #number of classes epoch = 20 #number of epochs eta = 0.2 #learning rate dur = 10 gamma = 0.0 batch = 100 num_iters = 101 #init model model = MLP(d0, d1, d2) time.sleep(1) #training model.fit(X, y, epoch, eta, gamma, dur, batch, num_iters) model.save_checkpoint('model') logging.info('Testing with testing data:') y_pred = model.predict(X_t) acc = 100 * np.mean(y_pred == y_t) print('training accuracy', acc)
prices = y[split_tr + hist_horizon:split_tr + test_len].values model = MLP(n_features, horizon) model.fit(x_train, y_train, 1) # print(x_train[-1], y_train[-1]) preds = np.zeros(len(prices)) # preds[0] = np.mean(model.predict([x_test[0]])) for i in range(horizon, len(y_test) - horizon + 1, horizon): # print([x_test[i - horizon]], [y_test[i - horizon]]) model.fit([x_test[i - horizon]], [y_test[i - horizon]], 5) # print(np.mean(model.predict([x_test[i]])), model.predict([x_test[i]])) # print(model.predict([x_test[i]])) preds[i:i + horizon] = model.predict([x_test[i]]) preds = preds[horizon:] last_price = prices[horizon - 1:-1] prices = prices[horizon:] mape_base = np.mean(np.abs((prices - last_price) / prices)) * 100 print("MAPE baseline:", mape_base) mape = np.mean(np.abs((prices - preds) / prices)) * 100 print("MAPE:", mape) errors.append(mape) plt.plot(prices) plt.plot(preds) plt.show()
Y = rf_sal.predict(im_data.feature93s[j])[:, 1] for k, r in enumerate(rlist): salience_map[j][r] = Y[k] _, _, weights = rf_sal.get_weights(im_data.feature93s[j]) rf_sal_weight += np.mean(weights, axis=0)[:, 1] rf_sal_weight /= len(im_data.rlists) ground_truth = cv2.imread(seg_paths[i])[:, :, 0] ground_truth[ground_truth == 255] = 1 x = salience_map.reshape([-1, height * width]).T salience_maps.append(x) ground_truths.append(ground_truth.reshape(-1)) result = mlp.predict(x).reshape([height, width, 1]) result[result > 0.5] = 255 result[result <= 0.5] = 0 cv2.imwrite("data/result/{}.png".format(its[i]), result.astype(np.uint8)) print("finish w {}".format(i)) X_test = np.array(salience_maps) X_test = np.concatenate(X_test, axis=0) Y_test = np.array(ground_truths) Y_test = np.concatenate(Y_test, axis=0) mlp.test(X_test, Y_test) df = pd.DataFrame(rf_sal_weight / len(img_datas)) df.to_csv("data/csv/rf_sal_weight.csv")
parsed_conllu = conllu.parse(content) sentence = parsed_conllu[0] print('Log: Parsing transitions..') T = Transition(sentence) invalid = False while T.next(): features = T.get_config() vectorized = vectorize_features([features]) input = np.concatenate( tuple((np.array(c).flatten() for c in vectorized[0]))) Y = net.predict(mx.nd.array([input])) output = Y[0].asnumpy() valid = T.get_valid_actions() for prediction in np.argsort(output, kind='quicksort')[::-1]: if prediction in valid: print('Log: Action {a} - {p}'.format(a=prediction, p=output[prediction])) break try: if prediction == T.SHIFT_INDEX: T.set_action('shift') elif prediction < T.RIGHT_ARC_INDEX: T.set_action('left_arc:' + ARC_LABELS[prediction])