def neuralNet(): clf = MLPClassifier(hidden_layer_sizes=(256,128,32,16), activation='relu', alpha=0.005, batch_size=64, early_stopping=True, learning_rate_init=0.001, solver='adam', learning_rate='adaptive', nesterovs_momentum=True, max_iter=1000, tol=1e-8, verbose=False, validation_fraction=0.1) clf.classes_ = 10 return clf
def deserialize_mlp(model_dict): model = MLPClassifier(**model_dict['params']) model.coefs_ = np.array(model_dict['coefs_']) model.loss_ = model_dict['loss_'] model.intercepts_ = np.array(model_dict['intercepts_']) model.n_iter_ = model_dict['n_iter_'] model.n_layers_ = model_dict['n_layers_'] model.n_outputs_ = model_dict['n_outputs_'] model.out_activation_ = model_dict['out_activation_'] model._label_binarizer = deserialize_label_binarizer( model_dict['_label_binarizer']) model.classes_ = np.array(model_dict['classes_']) return model
def deserialize_model(path): """Deserialize JSON object storing the ml model. Model (an MLPClassifier from sklearn) is re-instantiated with proper values. INPUT: --path: filepath for loading the JSON object OUTPUT: --model: Returns an MLPClassifier (sklearn) object """ def deserialize_label_binarizer(label_binarizer_dict): label_binarizer = LabelBinarizer() label_binarizer.neg_label = label_binarizer_dict['neg_label'] label_binarizer.pos_label = label_binarizer_dict['pos_label'] label_binarizer.sparse_output = label_binarizer_dict[ 'sparse_output'] label_binarizer.y_type_ = label_binarizer_dict['y_type_'] label_binarizer.sparse_input_ = label_binarizer_dict[ 'sparse_input_'] label_binarizer.classes_ = np.array( label_binarizer_dict['classes_']) return label_binarizer # Load (or deserialize) model from JSON model_dict = {} with open(path, 'r') as in_file: model_dict = json.load(in_file) model = MLPClassifier(**model_dict['params']) model.coefs_ = np.array(model_dict['coefs_'], dtype=object) model.loss_ = model_dict['loss_'] model.intercepts_ = np.array(model_dict['intercepts_'], dtype=object) model.n_iter_ = model_dict['n_iter_'] model.n_layers_ = model_dict['n_layers_'] model.n_outputs_ = model_dict['n_outputs_'] model.out_activation_ = model_dict['out_activation_'] model._label_binarizer = deserialize_label_binarizer( model_dict['_label_binarizer']) model.features = list(model_dict['features']) model.classes_ = np.array(model_dict['classes_']) # Convert coeficients to numpy arrays to enable JSON deserialization # This is a hack to compensate for a bug in sklearn_json for i, x in enumerate(model.coefs_): model.coefs_[i] = np.array(x) return model
def test_serialize_model(): instance = HostFootprint() model = MLPClassifier() label_binarizer = LabelBinarizer() label_binarizer.neg_label = 0 label_binarizer.pos_label = 1 label_binarizer.sparse_output = False label_binarizer.y_type_ = "binary" label_binarizer.sparse_input_ = False label_binarizer.classes_ = np.array([0]) parameters = {'hidden_layer_sizes': [(64, 32)]} GridSearchCV(model, parameters, cv=5, n_jobs=-1, scoring='f1_weighted') model.coefs_ = np.array([[1], [2]]) model.loss_ = 42 model.intercepts_ = np.array([[3], [4]]) model.classes_ = np.array([[5], [6]]) model.n_iter_ = 42 model.n_layers_ = 2 model.n_outputs_ = 1 model.out_activation_ = "logistic" model._label_binarizer = label_binarizer model.features = ['test_1', 'test_2', 'test_3'] with tempfile.TemporaryDirectory() as tmpdir: model_file = os.path.join(tmpdir, 'host_footprint.json') instance.serialize_model(model, model_file) new_model = instance.deserialize_model(model_file) assert model.features == new_model.features print(f"model params: {model.get_params()}") print(f"new_model params: {new_model.get_params()}") assert len(model.get_params()['hidden_layer_sizes']) == len( new_model.get_params()['hidden_layer_sizes']) assert model._label_binarizer.y_type_ == new_model._label_binarizer.y_type_ assert len(model.coefs_) == len(new_model.coefs_) assert len(model.intercepts_) == len(new_model.intercepts_)
def test_fit(): # Test that the algorithm solution is equal to a worked out example.""" X = np.array([[0.6, 0.8, 0.7]]) y = np.array([0]) mlp = MLPClassifier(algorithm='sgd', learning_rate_init=0.1, alpha=0.1, activation='logistic', random_state=1, max_iter=1, hidden_layer_sizes=2, momentum=0) # set weights mlp.coefs_ = [0] * 2 mlp.intercepts_ = [0] * 2 mlp.classes_ = [0, 1] mlp.n_outputs_ = 1 mlp.coefs_[0] = np.array([[0.1, 0.2], [0.3, 0.1], [0.5, 0]]) mlp.coefs_[1] = np.array([[0.1], [0.2]]) mlp.intercepts_[0] = np.array([0.1, 0.1]) mlp.intercepts_[1] = np.array([1.0]) mlp._coef_grads = [] * 2 mlp._intercept_grads = [] * 2 mlp.label_binarizer_.y_type_ = 'binary' # Initialize parameters mlp.n_iter_ = 0 mlp.learning_rate_ = 0.1 # Compute the number of layers mlp.n_layers_ = 3 # Pre-allocate gradient matrices mlp._coef_grads = [0] * (mlp.n_layers_ - 1) mlp._intercept_grads = [0] * (mlp.n_layers_ - 1) mlp.out_activation_ = 'logistic' mlp.t_ = 0 mlp.best_loss_ = np.inf mlp.loss_curve_ = [] mlp._no_improvement_count = 0 mlp._intercept_velocity = [ np.zeros_like(intercepts) for intercepts in mlp.intercepts_ ] mlp._coef_velocity = [np.zeros_like(coefs) for coefs in mlp.coefs_] mlp.partial_fit(X, y, classes=[0, 1]) # Manually worked out example # h1 = g(X1 * W_i1 + b11) = g(0.6 * 0.1 + 0.8 * 0.3 + 0.7 * 0.5 + 0.1) # = 0.679178699175393 # h2 = g(X2 * W_i2 + b12) = g(0.6 * 0.2 + 0.8 * 0.1 + 0.7 * 0 + 0.1) # = 0.574442516811659 # o1 = g(h * W2 + b21) = g(0.679 * 0.1 + 0.574 * 0.2 + 1) # = 0.7654329236196236 # d21 = -(0 - 0.765) = 0.765 # d11 = (1 - 0.679) * 0.679 * 0.765 * 0.1 = 0.01667 # d12 = (1 - 0.574) * 0.574 * 0.765 * 0.2 = 0.0374 # W1grad11 = X1 * d11 + alpha * W11 = 0.6 * 0.01667 + 0.1 * 0.1 = 0.0200 # W1grad11 = X1 * d12 + alpha * W12 = 0.6 * 0.0374 + 0.1 * 0.2 = 0.04244 # W1grad21 = X2 * d11 + alpha * W13 = 0.8 * 0.01667 + 0.1 * 0.3 = 0.043336 # W1grad22 = X2 * d12 + alpha * W14 = 0.8 * 0.0374 + 0.1 * 0.1 = 0.03992 # W1grad31 = X3 * d11 + alpha * W15 = 0.6 * 0.01667 + 0.1 * 0.5 = 0.060002 # W1grad32 = X3 * d12 + alpha * W16 = 0.6 * 0.0374 + 0.1 * 0 = 0.02244 # W2grad1 = h1 * d21 + alpha * W21 = 0.679 * 0.765 + 0.1 * 0.1 = 0.5294 # W2grad2 = h2 * d21 + alpha * W22 = 0.574 * 0.765 + 0.1 * 0.2 = 0.45911 # b1grad1 = d11 = 0.01667 # b1grad2 = d12 = 0.0374 # b2grad = d21 = 0.765 # W1 = W1 - eta * [W1grad11, .., W1grad32] = [[0.1, 0.2], [0.3, 0.1], # [0.5, 0]] - 0.1 * [[0.0200, 0.04244], [0.043336, 0.03992], # [0.060002, 0.02244]] = [[0.098, 0.195756], [0.2956664, # 0.096008], [0.4939998, -0.002244]] # W2 = W2 - eta * [W2grad1, W2grad2] = [[0.1], [0.2]] - 0.1 * # [[0.5294], [0.45911]] = [[0.04706], [0.154089]] # b1 = b1 - eta * [b1grad1, b1grad2] = 0.1 - 0.1 * [0.01667, 0.0374] # = [0.098333, 0.09626] # b2 = b2 - eta * b2grad = 1.0 - 0.1 * 0.765 = 0.9235 assert_almost_equal(mlp.coefs_[0], np.array([[0.098, 0.195756], [0.2956664, 0.096008], [0.4939998, -0.002244]]), decimal=3) assert_almost_equal(mlp.coefs_[1], np.array([[0.04706], [0.154089]]), decimal=3) assert_almost_equal(mlp.intercepts_[0], np.array([0.098333, 0.09626]), decimal=3) assert_almost_equal(mlp.intercepts_[1], np.array(0.9235), decimal=3) # Testing output # h1 = g(X1 * W_i1 + b11) = g(0.6 * 0.098 + 0.8 * 0.2956664 + # 0.7 * 0.4939998 + 0.098333) = 0.677 # h2 = g(X2 * W_i2 + b12) = g(0.6 * 0.195756 + 0.8 * 0.096008 + # 0.7 * -0.002244 + 0.09626) = 0.572 # o1 = h * W2 + b21 = 0.677 * 0.04706 + # 0.572 * 0.154089 + 0.9235 = 1.043 assert_almost_equal(mlp.decision_function(X), 1.043, decimal=3)
def test_fit(): # Test that the algorithm solution is equal to a worked out example.""" X = np.array([[0.6, 0.8, 0.7]]) y = np.array([0]) mlp = MLPClassifier(algorithm='sgd', learning_rate_init=0.1, alpha=0.1, activation='logistic', random_state=1, max_iter=1, hidden_layer_sizes=2, momentum=0) # set weights mlp.coefs_ = [0] * 2 mlp.intercepts_ = [0] * 2 mlp.classes_ = [0, 1] mlp.n_outputs_ = 1 mlp.coefs_[0] = np.array([[0.1, 0.2], [0.3, 0.1], [0.5, 0]]) mlp.coefs_[1] = np.array([[0.1], [0.2]]) mlp.intercepts_[0] = np.array([0.1, 0.1]) mlp.intercepts_[1] = np.array([1.0]) mlp._coef_grads = [] * 2 mlp._intercept_grads = [] * 2 mlp.label_binarizer_.y_type_ = 'binary' # Initialize parameters mlp.n_iter_ = 0 mlp.learning_rate_ = 0.1 # Compute the number of layers mlp.n_layers_ = 3 # Pre-allocate gradient matrices mlp._coef_grads = [0] * (mlp.n_layers_ - 1) mlp._intercept_grads = [0] * (mlp.n_layers_ - 1) mlp.out_activation_ = 'logistic' mlp.t_ = 0 mlp.best_loss_ = np.inf mlp.loss_curve_ = [] mlp._no_improvement_count = 0 mlp._intercept_velocity = [np.zeros_like(intercepts) for intercepts in mlp.intercepts_] mlp._coef_velocity = [np.zeros_like(coefs) for coefs in mlp.coefs_] mlp.partial_fit(X, y, classes=[0, 1]) # Manually worked out example # h1 = g(X1 * W_i1 + b11) = g(0.6 * 0.1 + 0.8 * 0.3 + 0.7 * 0.5 + 0.1) # = 0.679178699175393 # h2 = g(X2 * W_i2 + b12) = g(0.6 * 0.2 + 0.8 * 0.1 + 0.7 * 0 + 0.1) # = 0.574442516811659 # o1 = g(h * W2 + b21) = g(0.679 * 0.1 + 0.574 * 0.2 + 1) # = 0.7654329236196236 # d21 = -(0 - 0.765) = 0.765 # d11 = (1 - 0.679) * 0.679 * 0.765 * 0.1 = 0.01667 # d12 = (1 - 0.574) * 0.574 * 0.765 * 0.2 = 0.0374 # W1grad11 = X1 * d11 + alpha * W11 = 0.6 * 0.01667 + 0.1 * 0.1 = 0.0200 # W1grad11 = X1 * d12 + alpha * W12 = 0.6 * 0.0374 + 0.1 * 0.2 = 0.04244 # W1grad21 = X2 * d11 + alpha * W13 = 0.8 * 0.01667 + 0.1 * 0.3 = 0.043336 # W1grad22 = X2 * d12 + alpha * W14 = 0.8 * 0.0374 + 0.1 * 0.1 = 0.03992 # W1grad31 = X3 * d11 + alpha * W15 = 0.6 * 0.01667 + 0.1 * 0.5 = 0.060002 # W1grad32 = X3 * d12 + alpha * W16 = 0.6 * 0.0374 + 0.1 * 0 = 0.02244 # W2grad1 = h1 * d21 + alpha * W21 = 0.679 * 0.765 + 0.1 * 0.1 = 0.5294 # W2grad2 = h2 * d21 + alpha * W22 = 0.574 * 0.765 + 0.1 * 0.2 = 0.45911 # b1grad1 = d11 = 0.01667 # b1grad2 = d12 = 0.0374 # b2grad = d21 = 0.765 # W1 = W1 - eta * [W1grad11, .., W1grad32] = [[0.1, 0.2], [0.3, 0.1], # [0.5, 0]] - 0.1 * [[0.0200, 0.04244], [0.043336, 0.03992], # [0.060002, 0.02244]] = [[0.098, 0.195756], [0.2956664, # 0.096008], [0.4939998, -0.002244]] # W2 = W2 - eta * [W2grad1, W2grad2] = [[0.1], [0.2]] - 0.1 * # [[0.5294], [0.45911]] = [[0.04706], [0.154089]] # b1 = b1 - eta * [b1grad1, b1grad2] = 0.1 - 0.1 * [0.01667, 0.0374] # = [0.098333, 0.09626] # b2 = b2 - eta * b2grad = 1.0 - 0.1 * 0.765 = 0.9235 assert_almost_equal(mlp.coefs_[0], np.array([[0.098, 0.195756], [0.2956664, 0.096008], [0.4939998, -0.002244]]), decimal=3) assert_almost_equal(mlp.coefs_[1], np.array([[0.04706], [0.154089]]), decimal=3) assert_almost_equal(mlp.intercepts_[0], np.array([0.098333, 0.09626]), decimal=3) assert_almost_equal(mlp.intercepts_[1], np.array(0.9235), decimal=3) # Testing output # h1 = g(X1 * W_i1 + b11) = g(0.6 * 0.098 + 0.8 * 0.2956664 + # 0.7 * 0.4939998 + 0.098333) = 0.677 # h2 = g(X2 * W_i2 + b12) = g(0.6 * 0.195756 + 0.8 * 0.096008 + # 0.7 * -0.002244 + 0.09626) = 0.572 # o1 = h * W2 + b21 = 0.677 * 0.04706 + # 0.572 * 0.154089 + 0.9235 = 1.043 assert_almost_equal(mlp.decision_function(X), 1.043, decimal=3)
pickleNames = os.listdir(path.join(thisPath, 'pickles')) sortedPickles = sorted(pickleNames, reverse=True) gameFile = 'games' + str(int(time.time())) + '.data' itFile = 'iteration.data' startAt = 0 with open(path.join(thisPath, 'stats', itFile), 'r') as f: startAt = int(f.readline().strip()) if len(sortedPickles) == 0: clf = MLPClassifier( hidden_layer_sizes=(15, )) #lbfgs is for smaller dataset clf.classes_ = [Cathedral.lightPlayer, Cathedral.darkPlayer, 0] #zero is draw else: print(sortedPickles[0]) clf = load(path.join(thisPath, 'pickles', sortedPickles[0])) if startAt > startNN: clfRef = clf mctsIterations = 10 for g in range(startAt, numberOfGames): print("game # ", g) keepGoing = True game = Cathedral()
hidden_layer_sizes=(100, 100), learning_rate='constant', learning_rate_init=0.001, max_iter=200, momentum=0.9, nesterovs_momentum=True, power_t=0.5, random_state=None, shuffle=True, solver='lbfgs', tol=0.0001, validation_fraction=0.1, verbose=False, warm_start=False) clf.classes_ = classes #skf = StratifiedKFold(n_splits=2) """ parameters = {'solver':('lbfgs','sgd','adam'), 'activation':('identity','logistic','tanh','relu'), 'learning_rate':('constant','invscaling','adaptive'), 'hidden_layer_sizes':((100,),(100,100,),(100,100,100,))} gcv = GridSearchCV(estimator=clf, param_grid=parameters, cv=10, verbose=100, n_jobs=-1) gcv.fit(X,y) best = gcv.best_estimator_ print(best) print(gcv.best_score_) print(gcv.best_params_) """ """ for train_index, test_index in skf.split(X, y): X_train, X_test = X[train_index], X[test_index]