コード例 #1
0
def doStuffDecently():
    model = MLPRegressor()
    initial_X = get_random_state_representation()
    model.partial_fit(initial_X, np.array([2, 2]).reshape(1, -1))
    max_replays = 50
    exp = ExperienceReplayStore(model=model,
                                hash_func=lambda x: tuple(x[0].tolist()),
                                max_replays=max_replays)
    num_iter = 50
    for i in range(num_iter):
        if i % (0.1 * num_iter) == 0:
            print('Done with iter %d' % i)
        new_point = get_random_state_representation()
        reward = get_reward_for_state(new_point)
        exp.add_state(new_point, reward)
    print('Starting to get points')
    data = []
    num_iter = 1000
    for i in range(num_iter):
        if i % (0.1 * num_iter) == 0:
            print('Done with iter %d' % i)
        new_point = get_random_state_representation()
        reward = get_reward_for_state(new_point)
        exp.add_state(new_point, reward)
        # exp.iterate(1)
        new_rows = []
        for dat in exp.experiences_states:
            mapping = exp.model.predict(dat)[0]
            new_rows.append([
                mapping[0], mapping[1],
                get_reward_for_state([[dat[0][0], dat[0][1]]])
            ])
        data.extend(new_rows)
    animate_scatter_3d(data, len(exp.experiences_states))
コード例 #2
0
def test_partial_fit_regression():
    # Test partial_fit on regression.
    # `partial_fit` should yield the same results as 'fit' for regression.
    X = X_reg
    y = y_reg

    for momentum in [0, 0.9]:
        mlp = MLPRegressor(
            solver="sgd",
            max_iter=100,
            activation="relu",
            random_state=1,
            learning_rate_init=0.01,
            batch_size=X.shape[0],
            momentum=momentum,
        )
        with warnings.catch_warnings(record=True):
            # catch convergence warning
            mlp.fit(X, y)
        pred1 = mlp.predict(X)
        mlp = MLPRegressor(
            solver="sgd",
            activation="relu",
            learning_rate_init=0.01,
            random_state=1,
            batch_size=X.shape[0],
            momentum=momentum,
        )
        for i in range(100):
            mlp.partial_fit(X, y)

        pred2 = mlp.predict(X)
        assert_allclose(pred1, pred2)
        score = mlp.score(X, y)
        assert score > 0.65
コード例 #3
0
class SimpleNNPredictor(Predictor):
    def __init__(self, predictionDelta):
        Predictor.__init__(self, predictionDelta)
        self.LookBack = [89, 55, 34, 21, 13, 8, 5, 3, 2, 1]
        self.regressor = MLPRegressor(hidden_layer_sizes=(3, 2),
                                      activation='tanh',
                                      solver='sgd',
                                      learning_rate='invscaling')
        self.name = "SimpleNNPredictor"

    def extractFeatures(self, dateIndex):
        history = []
        currentPrice = self.getPrice(dateIndex)
        for xi in self.LookBack:
            history += [
                getPriceChange(self.getPrice(dateIndex - xi), currentPrice)
            ]

        return history

    def train(self, phiX, y):
        X = np.array(phiX).reshape(1, -1)
        Y = [y]
        self.regressor.partial_fit(X, Y)

    def predict(self, phiX):
        X = np.array(phiX).reshape(1, -1)
        result = self.regressor.predict(X)[0]
        return result
コード例 #4
0
ファイル: TicTacToe.py プロジェクト: r3c0d3/clashAI
def train_machine_interactive(board_size):
	X = [[0,0,0,0,0,0,0,0,0]]
	y = [[0,0,0,0,0,0,0,0,0]]
	#MACHINE_ALL = MLPRegressor(solver='sgd', alpha=1.0, hidden_layer_sizes=(1500, 29), random_state=1000, activation="relu", max_iter=4000, batch_size=5, learning_rate="constant", learning_rate_init=0.001)
	#MACHINE_ALL = MLPRegressor(solver='sgd', tol=0.0000001, alpha=0.0001, hidden_layer_sizes=(350,185), random_state=1000, activation="relu", max_iter=4000, learning_rate="invscaling", learning_rate_init=0.0000001, warm_start=True) # 3 loss # home 19
	MACHINE_ALL = MLPRegressor(solver='sgd', tol=0.0000001, alpha=0.0001, hidden_layer_sizes=(350,185), random_state=1000, activation="relu", max_iter=4000, learning_rate="invscaling", learning_rate_init=0.01, warm_start=True) # 3 loss # home 19
	MACHINE_ALL.fit(X, y)
	
	while True:
		myprint("loss : {}, n_iter : {}".format(MACHINE_ALL.loss_, MACHINE_ALL.n_iter_),5)
		a_game = TicTacToe(board_size)
		winner_moves, loser_moves, is_null = play_interactive(MACHINE_ALL, a_game)
		X, new_y = MLP_training(MACHINE_ALL, winner_moves, board_size, NULL if is_null else REWARD)
		X2, new_y2 = MLP_training(MACHINE_ALL, loser_moves, board_size, NULL if is_null else LOSS)
		
		myprint("X2 " + str(X2))
		myprint("new_y2 " + str(new_y2))
		myprint("loser_moves " + str(loser_moves))
		
		X.extend(X2)
		new_y.extend(new_y2)
		
		myprint("partial_fit X : " + str(X))
		myprint("partial_fit y : " + str(new_y))
		MACHINE_ALL.partial_fit(X, new_y)
コード例 #5
0
class SimpleNNLearner(Learner):
    def __init__(self):
        self.regressor = MLPRegressor()
        self.hasFitted = False

    def extractFeatures(self, state, action):
        # currentPrice: Float
        # history: [0, 1, 0, ...]
        # ownedStocks: Int
        currentPrice, history, ownedStocks, cash = state

        features = []
        for priorPrice in history:
            features += [float(priorPrice - currentPrice) / currentPrice]

        features += [ownedStocks]
        features += [cash]

        features += [ action > 0 ]
        return features
        
    def train(self, phiX, target):
        self.hasFitted = True
        X = np.array(phiX).reshape(1, -1)
        self.regressor.partial_fit(X, [target])

    def predict(self, phiX):
        result = 0
        if self.hasFitted:
            X = np.array(phiX).reshape(1, -1)
            result = self.regressor.predict(X)[0]
        return result
コード例 #6
0
ファイル: TicTacToe.py プロジェクト: VHRanger/clashAI
def train_machine():
    X = [[0, 0, 0, 0, 0, 0, 0, 0, 0]]
    y = [[0, 0, 0, 0, 0, 0, 0, 0, 0]]
    MACHINE_ALL = MLPRegressor(solver='sgd',
                               alpha=1.0,
                               hidden_layer_sizes=(1500, 29),
                               random_state=1000,
                               activation="relu",
                               max_iter=4000,
                               batch_size=5,
                               learning_rate="constant",
                               learning_rate_init=0.001)
    MACHINE_ALL.partial_fit(X, y)

    max_game = 75000
    actual_epsilon = Epsilon
    dec_every = int(max_game / EpsilonParts)
    for i in range(max_game):
        if i % 10 == 0:
            myprint("Game {} of {}".format(i, max_game), 5)
        run_MLP_game(MACHINE_ALL, board_size, actual_epsilon)
        if i % dec_every == 0:
            actual_epsilon -= EpsilonStep
            if actual_epsilon < 0.0:
                actual_epsilon = 0.0
            myprint("Epsilon now : " + str(actual_epsilon), 2)

    save_machine(MACHINE_ALL)

    return MACHINE_ALL
コード例 #7
0
class MLPValueFunction:
    def __init__(self, file_name, hidden_layers=(100, )):
        self.estimator = MLPRegressor(hidden_layers)

        self.init_X = np.load('init_mlp.npy')
        self.init_y = np.ones(
            self.init_X.shape[0]) * 30  # initialize to encourage exploration
        self.estimator.fit(self.init_X, self.init_y)

        self.filename = file_name

    def update(self, X, y):
        self.estimator.partial_fit(X, y)

    def get_value(self, X):
        return self.estimator.predict(X)

    def save_model(self):
        dump(self.estimator, self.filename)

    def load_model(self):
        self.estimator = load(self.filename)

    def set_coefs(self, coefs, interceps):
        self.estimator.coefs_ = coefs
        self.estimator.intercepts_ = interceps
コード例 #8
0
ファイル: test_mlp.py プロジェクト: gwgang/scikit-learn
def test_partial_fit_regression():
    # Test partial_fit on regression.
    # `partial_fit` should yield the same results as 'fit' for regression.
    X = Xboston
    y = yboston

    for momentum in [0, .9]:
        mlp = MLPRegressor(algorithm='sgd',
                           max_iter=100,
                           activation='relu',
                           random_state=1,
                           learning_rate_init=0.01,
                           batch_size=X.shape[0],
                           momentum=momentum)
        with warnings.catch_warnings(record=True):
            # catch convergence warning
            mlp.fit(X, y)
        pred1 = mlp.predict(X)
        mlp = MLPRegressor(algorithm='sgd',
                           activation='relu',
                           learning_rate_init=0.01,
                           random_state=1,
                           batch_size=X.shape[0],
                           momentum=momentum)
        for i in range(100):
            mlp.partial_fit(X, y)

        pred2 = mlp.predict(X)
        assert_almost_equal(pred1, pred2, decimal=2)
        score = mlp.score(X, y)
        assert_greater(score, 0.75)
コード例 #9
0
def SK_MLPR_training(X, y, minMSE, momentumAll, solver, hidden_layer,
                     activation, learning_rate, qtd_batch):
    print("####CRIANDO MLPR####")

    for momentum in momentumAll:

        mlp = MLPRegressor(solver=solver,
                           hidden_layer_sizes=hidden_layer,
                           activation=activation,
                           learning_rate_init=learning_rate,
                           random_state=1,
                           batch_size=qtd_batch,
                           momentum=momentum)

        #TREINO
        mse = []
        bmse = 99999
        i = 0

        #for i in range(Epochs):
        while bmse > minMSE:
            i += 1
            mlp.partial_fit(X, y)
            bmse = mean_squared_error(y, mlp.predict(X))
            mse.append(bmse)
            print(i, " M.S.E:", bmse)

        return mlp, mse
コード例 #10
0
ファイル: nets.py プロジェクト: tuckerdarby/connect4
class NeuralAgent(Agent):
    def __init__(self, grader=ScaledGrader, net=None):
        super(NeuralAgent, self).__init__()
        if net is None:
            self.net = MLPRegressor(hidden_layer_sizes=(30, 15),
                                    max_iter=50,
                                    alpha=1e-4,
                                    random_state=1,
                                    solver='sgd',
                                    tol=1e-2,
                                    learning_rate_init=0.1,
                                    warm_start=True)
        else:
            self.net = net
        self.fitted = False
        self.training = True
        self.grader = grader(self.player)

    def load_net(self, net, fitted=True, training=False):
        self.net = net
        self.fitted = fitted
        self.training = training

    def play(self, board):
        best_action = self._best_action(board)
        self._game.play(self.player, best_action)
        if self.training:
            board = self._game.get_board(self.player)
            score = self.grader.grade_board(board)
            state = np.array(board.flatten(), dtype='int64')
            self.net.partial_fit([state], [score])
            self.fitted = True

    def _value_state(self, state):
        flat = state.flatten().reshape(1, -1)
        value = self.net.predict(flat)[0]
        return value

    def _best_action(self, board):
        states, indexes = next_states(board, self.player, index=True)
        if not self.fitted:
            r = np.random.randint(0, len(indexes))
            return indexes[r]
        max_value = None
        max_indexes = []
        for state, index in zip(states, indexes):
            value = self._value_state(state)
            # print 'predict score:', value
            if max_value is None:
                max_value = value
                max_indexes = [index]
            elif value > max_value:
                max_value = value
                max_indexes = [index]
            elif value == max_value:
                max_indexes.append(index)
        size = len(max_indexes)
        r = np.random.randint(0, size)
        return max_indexes[r]
コード例 #11
0
class OnlineBoardEvaluator:
    def __init__(self, netFilename):
        try:
            print("Trying to load neural net:", netFilename + ".p")
            self.regressor = pickle.load(open(netFilename + ".p", "rb"))
            print("Loading was successful.")
        except:
            print("Creating new neural net.")
            self.regressor = MLPRegressor(
                solver='adam',
                alpha=1e-5,
                activation="logistic",
                random_state=1,
                max_iter=1000000,
            )

        self.filenameBase = netFilename

        self.updates = 0
        self.saveEveryXth = 1

    def getBoardRating(self, tokens):
        return self.regressor.predict([tokens])

    def learnBatch(self, boards, whiteWins, redWins):
        X = boards
        pWhiteWins = [
            whiteWins[i] / (whiteWins[i] + redWins[i]) for i in range(len(X))
        ]
        pRedWins = [
            redWins[i] / (whiteWins[i] + redWins[i]) for i in range(len(X))
        ]
        y = [pWhiteWins[i] - pRedWins[i] for i in range(len(X))]

        #X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
        #self.regressor.fit(X_train, y_train)
        #print("net score:", self.regressor.score(X_test, y_test))

        self.regressor.partial_fit(X, y)
        self.updates += 1
        if (self.updates >= self.saveEveryXth):
            print("Saving net.")
            dateString = str(datetime.datetime.now())
            pickle.dump(
                self.regressor,
                open(self.filenameBase + "_" + dateString + ".p", "xb"))
            pickle.dump(self.regressor, open(self.filenameBase + ".p", "wb"))
            self.updates = 0
            print("saving a sample batch.")
            batchFile = open(
                "training_batch_" + str(self.saveEveryXth) + "_" + dateString +
                "_" + str(len(boards)) + ".p", "xb")
            pickle.dump(X, batchFile)
            pickle.dump(y, batchFile)
            pickle.dump(whiteWins, batchFile)
            pickle.dump(redWins, batchFile)
            batchFile.close()
            """
コード例 #12
0
ファイル: player.py プロジェクト: IronFarm/NoughtsAndZeroes
class Player:
    def __init__(self, smart=False, seed=None):
        if seed:
            self.nn = MLPRegressor(hidden_layer_sizes=(9, 9),
                                   random_state=seed)
            self.nn.partial_fit([[0] * 9], [[0] * 9])  # Dummy fit
            self.get_moves = lambda x: np.flip(
                np.argsort(self.nn.predict([x]))[0], 0)
        elif smart:
            self.get_moves = lambda x: [4] + random.sample(
                [0, 1, 2, 3, 5, 6, 7, 8], k=8)
        else:
            self.get_moves = lambda x: random.sample(range(9), k=9)
コード例 #13
0
    def __init__(self,
                 env,
                 task,
                 n_actions,
                 alpha=None,
                 gamma=0.99,
                 epsilon=1,
                 epsilon_decay=0.9,
                 K_discountFactor=None,
                 epsilon_min=0.05,
                 lambd=0.9,
                 metrique_reward=0):
        assert (alpha != None
                and K_discountFactor == None) or (alpha == None
                                                  and K_discountFactor != None)

        # self.env = env
        self.task = task
        self.n_in = task.outdim
        self.n_actions = n_actions
        self.alpha = alpha
        self.gamma = gamma
        self.epsilon = epsilon
        self.epsilon_decay = epsilon_decay
        self.epsilon_min = epsilon_min
        self.epsilon_init = 1000
        self.K_discountFactor = K_discountFactor
        self.lambd = lambd
        self.metrique_reward = metrique_reward

        self.state_nonvisited = set(range(self.n_in))

        self.lastaction = None
        self.laststate = None

        self.history = []

        # We create a model for each action
        self.models = []
        env.reset()
        for _ in range(n_actions):
            # model = SGDRegressor()
            model = MLPRegressor(hidden_layer_sizes=())
            model.partial_fit([self.task.getObservation()], [0])
            self.models.append(model)

        self.num_episode = 0

        self.traces = np.zeros((self.task.outdim, n_actions))
        self.Q = np.zeros((self.task.outdim, n_actions))
コード例 #14
0
class NFQ(QLearning):

    gamma = 0.9
    beta = 0.8

    def __init__(self, **kwargs):
        self.mlp = MLPRegressor(hidden_layer_sizes=(5, 5),
                                activation='logistic',
                                batch_size=400)
        set_all_args(self, kwargs)

    def fit(self, data, max_iter=300, intra_step=50):
        """
        data is the triple (ss, as, rs)
        """
        for _ in range(max_iter):
            inputs, targets = self.compute_inputs_targets(data)
            for _ in range(intra_step):
                self.mlp.partial_fit(inputs, targets)

    def compute_inputs_targets(self, data):

        inputs, targets = [], []

        for i in range(len(data[0]) - 1):
            s, a, r = list(data[0][i]), data[1][i], data[2][i]
            s_next = list(data[0][i + 1])
            inputs.append(s + [self.actions.index(a)])
            to_prs = [s_next + [act] for act in range(len(self.actions))]
            try:
                q_values = self.mlp.predict(to_prs)
                targets.append(r + self.gamma * np.max(q_values))
            except NotFittedError:
                targets.append(r)

        return np.array(inputs), np.array(targets)

    def score(self, data):
        inputs, targes = self.compute_inputs_targets(data)
        return self.mlp.score(inputs, targes)

    def decision(self, state):
        state = list(state)
        to_prs = [state + [act] for act in range(len(self.actions))]
        q_values = self.mlp.predict(to_prs)
        ps = np.exp(self.beta * q_values)
        a_num = np.random.choice(len(self.actions), p=ps / np.sum(ps))
        return self.actions[a_num]
コード例 #15
0
class MLPAlg(IterativeAlgorithm):
    def __init__(self,
                 neurons_hidden,
                 learning_rate: float,
                 batch_size: int = 32):
        super(MLPAlg, self).__init__()
        self.neurons_hidden = neurons_hidden
        self.total_iterations: int = 0
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.alg = MLPRegressor(hidden_layer_sizes=neurons_hidden,
                                activation='tanh',
                                solver='sgd',
                                batch_size=batch_size,
                                learning_rate="constant",
                                learning_rate_init=learning_rate)
        self.validation_data = None
        self.train_data = None

    def partial_fit(self, X, num_iterations: int):
        self.total_iterations += num_iterations
        # data_indices = np.random.choice([i for i in range(len(X))],
        #                                 size=self.batch_size*num_iterations)
        start = process_time()
        for _ in range(num_iterations):
            self.alg.partial_fit(X, X)
        # data = X[data_indices]
        # assert len(data_indices) == len(data)
        # start = process_time()
        # for i in range(num_iterations):
        #     d = data[i*32:(i+1)*32]
        #     self.alg.partial_fit(d, d)
        duration = process_time() - start
        return duration

    def warm_up(self, X) -> float:
        return self.validate(X)

    def validate(self, X):
        prediction = self.alg.predict(X)
        diff = X - prediction
        norm = np.mean([np.linalg.norm(d) for d in diff])
        return -norm

    def should_terminate(self, *args, **kwargs) -> bool:
        return self.total_iterations >= 100
コード例 #16
0
    def _build_model(self):
        model = MLPRegressor(tol=1e-3,
                             hidden_layer_sizes=(
                                 24,
                                 24,
                             ),
                             activation='tanh',
                             learning_rate='adaptive',
                             solver='sgd',
                             warm_start=True
                             #, batch_size=1
                             )

        #initialize agent to the correct dimensions
        model.partial_fit([
            self._concat(self.initial_state, a) for a in range(self.n_actions)
        ], range(self.n_actions))
        return model
コード例 #17
0
class RedNeuronal(object):
    def __init__(self,
                 neuronas,
                 activation_function='tanh',
                 factor_descuento=0.9,
                 num_iters=200,
                 batch_size=32,
                 learning_rate=0.0001,
                 regularization=0.5,
                 momentum=0.9):
        # Inicializar capas y neuronas de la red
        if not isinstance(neuronas, str):
            print(
                '[-] Inicializando neuronas de la red con valores aleatorios')
            self.mlp = MLPRegressor(hidden_layer_sizes=neuronas,
                                    activation=activation_function,
                                    solver='adam',
                                    alpha=regularization,
                                    batch_size=batch_size,
                                    learning_rate='adaptive',
                                    learning_rate_init=learning_rate,
                                    max_iter=num_iters,
                                    momentum=momentum)
        else:
            print(
                '[-] Inicializando neuronas de la red con valores del archivo {file}'
                .format(file=neuronas))
            self.mlp = cargar_red(neuronas)

        self.factor_descuento = factor_descuento
        print('[-] Red neuronal inicializada')

    # Recibe una instancia y devuelve una valoración del tablero.
    def forwardpropagation(self, input):
        return self.mlp.predict(np.array(input).reshape(1, -1))

    # Cargar la partida del archivo seleccionado.
    # Retorna las instancias en un arreglo numpy (a su vez, cada instancia es otro arreglo numpy).
    def cargar_partida(self, archivo_instancias):
        return np.load(archivo_instancias)['arr_0']

    def cargar_evaluaciones(self, archivo_evals):
        with open(archivo_evals, 'r') as archivo:
            evals = archivo.readlines()
        for i in range(len(evals)):
            evals[i] = float(evals[i])
        return evals

    # Aplicar el algoritmo de backpropagation sobre el archivo de instancias seleccionado.
    # Devuelve el error total y promedio que se cometió en la evaluación, junto con el error total y promedio
    # de las capas de la red.
    def backpropagation(self, archivo_instancias, archivo_evals):
        # print('[-] Comenzando backpropagation')
        instancias = self.cargar_partida(archivo_instancias)
        evaluaciones = self.cargar_evaluaciones(archivo_evals)
        self.mlp = self.mlp.partial_fit(instancias, evaluaciones)
コード例 #18
0
def generateUntrainedStarterNet():

    knownBoards = [[
        -2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0, -5, 0, 0, 0,
        0, 2, 0, 0, 0, 0
    ],
                   [
                       -2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0,
                       -5, 0, 0, 0, 0, 2, 0, 0, 0, 0
                   ],
                   [
                       -2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0,
                       -5, 0, 0, 0, 0, 2, 0, 0, 0, 0
                   ],
                   [
                       -2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0,
                       -5, 0, 0, 0, 0, 2, 0, 0, 0, 0
                   ]]

    X = knownBoards
    pWhiteWins = [1, 1, 1, 1]
    pRedWins = [1, 1, 1, 1]
    y = [pWhiteWins[i] - pRedWins[i] for i in range(len(X))]

    #regressor = MLPRegressor(solver='adam', alpha=1e-5, hidden_layer_sizes=(10, 4),activation="logistic", random_state=1, max_iter=1000000,)

    # super simple net. Inputs directly to single output.
    #regressor = MLPRegressor(solver='adam', alpha=1e-5, hidden_layer_sizes=(), activation="logistic", random_state=1, max_iter=1000000,)

    # Trying to "mask" with the "rectified linear unit" (relu) activation function,
    # in order to only count "red" tokens.
    regressor = MLPRegressor(
        solver='adam',
        alpha=1e-5,
        hidden_layer_sizes=(56, 2),
        activation="relu",
        random_state=1,
        max_iter=1000000,
    )
    regressor.partial_fit(X, y)

    pickle.dump(regressor, open("trained_net_white.p", "xb"))
    pickle.dump(regressor, open("trained_net_red.p", "xb"))
コード例 #19
0
class Ann:

    def __init__(self):

        self._nn = MLPRegressor(hidden_layer_sizes=(10,), verbose=False, warm_start=True)
        self._entradas_entrenamiento = []
        self._salidas_esperadas_entrenamiento = []
        self.lambdaCoefficient = 0.9

    def evaluar(self, entrada):
        return self._nn.predict(entrada)

    def agregar_a_entrenamiento(self, tableros, resultado):

        tableros.reverse()
        for i in xrange(len(tableros)):
            tablero, valorEstimado = tableros[i][0], tableros[i][1]
            self._entradas_entrenamiento.append(tablero)
            if i == 0 or True:
                self._salidas_esperadas_entrenamiento.append(resultado.value)
            else:
                valorAAprender = valorEstimado + self.lambdaCoefficient * (self._salidas_esperadas_entrenamiento[i-1] -
                    valorEstimado)
                self._salidas_esperadas_entrenamiento.append(valorAAprender)

    def entrenar(self):
        self._nn.partial_fit(self._entradas_entrenamiento, self._salidas_esperadas_entrenamiento)
        self._entradas_entrenamiento = []
        self._salidas_esperadas_entrenamiento = []

    def almacenar(self):
        pickle.dump(self._nn, open(self.path,'wb'))

    def cargar(self, path, red):
        self.path = path
        if os.path.isfile(path):
            self._nn = pickle.load(open(path, 'rb'))
        else:
            self._nn = red
            tableroVacio = ([EnumCasilla.EMPTY.value for _ in xrange(64)],0)
            self.agregar_a_entrenamiento([tableroVacio], EnumResultado.EMPATE)
            self.entrenar()
コード例 #20
0
def main(layers):
    """The main function of the program"""
    # num_batches = int(TRAIN_SIZE / BATCH_SIZE)
    num_batches = int(DELTA_TRAIN_SIZE / BATCH_SIZE)
    print("The program will run in", num_batches)

    past_err = long(999999)
    new_err = 0
    regr = MLPRegressor(hidden_layer_sizes=layers)

    for i in range(MAX_TRIES):
        for batch in range(num_batches):
            print("\nCurrent loop =", i + 1)
            print("Current layers:", layers)
            print("Starting batch", batch + 1, "of", num_batches)

            print("Loading data...")
            # train_data, train_labels = load_train(PATH_TRAIN, batch * BATCH_SIZE, BATCH_SIZE)
            train_data, train_labels = load_simple_train(
                PATH_DELTA_TRAIN, batch * BATCH_SIZE, BATCH_SIZE)

            print("Training neural network...")
            regr.partial_fit(train_data, train_labels)

        print("\nTesting the network...")
        # val_data, val_labels = load_val(PATH_VALIDATION, BATCH_SIZE, VAL_SIZE)
        val_data, val_labels = load_simple_val(PATH_DELTA_VAL, BATCH_SIZE,
                                               DELTA_VAL_SIZE)
        new_err = test_regressor(regr, val_data, val_labels)

        if abs(past_err - new_err) < CONVERGENCE:
            print("Convergence has reached:", past_err - new_err)
            break

        print("Current error:", np.sqrt(new_err), "improvement",
              np.sqrt(past_err) - np.sqrt(new_err), "\n")
        past_err = new_err
        # store(new_err, PATH_OUTPUT)
        store(new_err, PATH_DELTA_OUTPUT)

    print("Done!\nThe regression has an average error of:", np.sqrt(new_err))
コード例 #21
0
class NeuralModelLearner(ModelLearner):
    def __init__(self, rate):
        self.learning_rate = rate
        self.network = MLPRegressor(random_state=1,
                                    learning_rate='constant',
                                    solver='sgd',
                                    learning_rate_init=rate,
                                    max_iter=500)
        self.to_adapt = False

    def observe(self, X, y, drifted):
        if drifted != False:
            self.to_adapt = True
        self.network.partial_fit(X, y)

    def predict(self, X):
        xnew_predicted = self.network.predict(X)[0]
        return xnew_predicted

    def status(self):
        return self.network
コード例 #22
0
def main(layers,
         startfile=None,
         act='relu',
         solv='adam',
         mini_batch_size='auto'):
    """The main function of the program"""

    num_batches = int(TRAIN_SIZE / BATCH_SIZE)
    print("The program will run in", num_batches)

    i = 0
    if startfile:  #pickle file
        regr = joblib.load(SAVED_NN)
    else:
        regr = MLPRegressor(hidden_layer_sizes=layers,
                            activation=act,
                            solver=solv,
                            alpha=0.0001,
                            batch_size=mini_batch_size)

    x_val = 0
    for i in range(MAX_TRIES):
        shuffled_batches = range(num_batches)
        shuffle(shuffled_batches)
        batches_passed = 0
        for batch in shuffled_batches:
            train, labels, _ = load_train(PATH_TRAIN, batch * BATCH_SIZE,
                                          BATCH_SIZE)
            # train, labels, _ = load_data(PATH_TRAIN, batch * BATCH_SIZE, BATCH_SIZE)

            regr.partial_fit(train, labels)
            batches_passed += 1
            if batches_passed % ERROR_INTERVAL == 0:
                print("\nProgress:")
                print("Loop", i + 1, ",  batch", batches_passed + 1, "of",
                      num_batches)
                print("...Saving and testing, don't interrupt the program...")
                x_val += ERROR_INTERVAL
                save_stuff(regr, x_val)
                print("...Done saving.")
コード例 #23
0
def doStuff():
    model = MLPRegressor()
    initial_X = get_random_state_representation()
    model.partial_fit(initial_X, np.array([0, 0]).reshape(1, -1))
    points = []
    MAX_SIZE = 10
    while len(points) < MAX_SIZE:
        points.append(get_random_state_representation())
    # train here?
    num_iter = 2000
    learning_rate = 0.1
    for i in range(num_iter):
        if i % (0.01 * num_iter) == 0:
            print('Done with iter %d' % i)
        new_point = get_random_state_representation()
        old_vec = model.predict(new_point)
        target_for_new_x = model.predict(new_point)
        max_val = float("-inf")
        d_v = None
        for point in points:
            smaller = min(get_reward_for_state(point),
                          get_reward_for_state(new_point))
            bigger = max(get_reward_for_state(point),
                         get_reward_for_state(new_point))
            similarity = smaller / bigger
            if abs(similarity) > 1:
                similarity = 1.0 / similarity
            direction_vector = model.predict(new_point)[0] - model.predict(
                point)[0]
            scale = 1 / (euclidean_distance(point[0], new_point[0]))
            similarity * learning_rate * direction_vector * scale
            if max_val <= similarity * scale:
                max_val = similarity * scale
                d_v = direction_vector
        target_for_new_x += max_val * learning_rate * d_v
        model.partial_fit(new_point, target_for_new_x)
        add_point_to_set(new_point, points)

    threeD_plot(points, model)
コード例 #24
0
def test_partial_fit_regression():
    # Test partial_fit on regression.
    # `partial_fit` should yield the same results as 'fit' for regression.
    boston = load_boston()
    Xboston = StandardScaler().fit_transform(boston.data)[: 200]
    yboston = boston.target[:200]
    
    X = Xboston
    y = yboston

    for momentum in [0, .9]:
        mlp = MLPRegressor(solver='sgd', max_iter=100, activation='relu',
                           random_state=1, learning_rate_init=0.01,
                           batch_size=X.shape[0], momentum=momentum)
        with warnings.catch_warnings(record=True):
            # catch convergence warning
            mlp.fit(X, y)
        pred1 = mlp.predict(X)
        mlp = MLPRegressor(solver='sgd', activation='relu',
                           learning_rate_init=0.01, random_state=1,
                           batch_size=X.shape[0], momentum=momentum)
        
        for i in range(100):
            mlp.partial_fit(X, y)

        pred2 = mlp.predict(X)
        assert_almost_equal(pred1, pred2, decimal=2)
        score = mlp.score(X, y)
        assert_greater(score, 0.75)
        
        #avalia a saida da mlp
        print(yboston)
        print(X)
        plt.plot(yboston)
        plt.plot(pred2)
        plt.legend("RP")
        plt.title("Right vs Predicted values")
        #plt.savefig('./outPut/{0}-{1}-{2}-{3}.eps'.format(len(X),hidden_layer, taxa, Epochs), format='eps', dpi=1000)    
        plt.show()        
コード例 #25
0
ファイル: runMLPR.py プロジェクト: pafil137/NN
def criarMLPR():
    print("####CRIANDO MLPR####")

    for momentum in momentumAll:

        mlp = MLPRegressor(solver=solver,
                           hidden_layer_sizes=hidden_layer,
                           activation=activation,
                           learning_rate_init=learning_rate,
                           random_state=1,
                           batch_size=qtd_batch,
                           momentum=momentum)

        #TREINO
        for i in range(Epochs):
            mlp.partial_fit(X, y)
            pred = mlp.predict(X)
            mse = mean_squared_error(y, pred)
            print((100 * i / Epochs), "% M.S.E:", mse)

        #TESTE
        pred = mlp.predict(Xt)

        score = mlp.score(Xt, yt)
        print('score:', score)

        #assert_greater(score, 0.70)
        #assert_almost_equal(pred, yt, decimal=2)

        #######SAVE
        if (saveDNN == True):
            with open(fileDNN, 'wb') as fid:
                cPickle.dump(mlp, fid)

        #avalia a saida da mlp
        ####VER DISTRIBUICAO DOS DADOS
        if (debugDeep == True):
            debugMLPR(pred)
コード例 #26
0
ファイル: test_mlp.py プロジェクト: aniryou/scikit-learn
def test_partial_fit_regression():
    # Test partial_fit on regression.
    # `partial_fit` should yield the same results as 'fit' for regression.
    X = Xboston
    y = yboston

    for momentum in [0, .9]:
        mlp = MLPRegressor(solver='sgd', max_iter=100, activation='relu',
                           random_state=1, learning_rate_init=0.01,
                           batch_size=X.shape[0], momentum=momentum)
        with warnings.catch_warnings(record=True):
            # catch convergence warning
            mlp.fit(X, y)
        pred1 = mlp.predict(X)
        mlp = MLPRegressor(solver='sgd', activation='relu',
                           learning_rate_init=0.01, random_state=1,
                           batch_size=X.shape[0], momentum=momentum)
        for i in range(100):
            mlp.partial_fit(X, y)

        pred2 = mlp.predict(X)
        assert_almost_equal(pred1, pred2, decimal=2)
        score = mlp.score(X, y)
        assert_greater(score, 0.75)
コード例 #27
0
        bcnt += 1

        # Get training data
        _tet1b = tm.time()
        X, y = read_data_indexed(t_file, N, M, Ntd, i, t_Ainx, t_cmats)
        _tet1e = tm.time()

        # Transform data
        _tet2b = tm.time()
        X = X_scaler.transform(X)
        y = y / y_min
        _tet2e = tm.time()

        # Fit model
        _tet3b = tm.time()
        clf.partial_fit(X, y)
        _tet3e = tm.time()

        # Compute training delta E
        _tet4b = tm.time()
        dE = y_min * clf.predict(X) - y_min * y
        _tet4e = tm.time()

        # Compute sum of squared diff
        btcherr = (dE * dE).sum()
        sqd += btcherr

        progress(bcnt, Ntb, 'Training progress')

        if verbose:
            print('\n  Batch: ' + str(bcnt) + ' of ' + str(Ntb))
コード例 #28
0
ファイル: AutoEncoder.py プロジェクト: tdallas/sia-tp5
class AutoEncoder():
    def __init__(self,
                 encoder_layer_sizes,
                 latent_layer_size,
                 decoder_layer_sizes,
                 activation,
                 solver,
                 eta=0.0001,
                 max_iterations=30000,
                 tol=0.0000001,
                 verbose=False):
        self.encoder_layer_sizes = encoder_layer_sizes
        self.latent_layer_size = latent_layer_size
        self.decoder_layer_sizes = decoder_layer_sizes
        self.encoder_layer = len(encoder_layer_sizes)
        self.latent_layer = self.encoder_layer + 1
        self.decoder_layer = self.latent_layer + len(decoder_layer_sizes) + 1
        self.activation = activation
        self.solver = solver
        self.eta = eta
        self.max_iterations = max_iterations
        aux = encoder_layer_sizes
        aux.append(latent_layer_size)
        self.hidden_layer_sizes = aux + decoder_layer_sizes
        self.mlp = MLPRegressor(hidden_layer_sizes=self.hidden_layer_sizes,
                                activation=activation,
                                solver=solver,
                                learning_rate_init=eta,
                                max_iter=max_iterations,
                                tol=0.0000001,
                                verbose=verbose)

    def fit(self, training_input, training_output):
        return self.mlp.fit(training_input, training_output)

    def fit_and_plot(self, training_input, training_output):
        scores_train = []
        # scores_test = []

        epoch = 0
        while epoch < self.max_iterations:
            self.mlp.partial_fit(training_input, training_output)

            # SCORE TRAIN
            scores_train.append(self.mlp.score(training_input,
                                               training_output))

            # SCORE TEST
            # scores_test.append(self.mlp.score(X_test, y_test))

            epoch += 1

        plt.plot(scores_train)
        plt.ylabel('Accuracy', fontsize=16)
        plt.xlabel('Iteration', fontsize=16)
        plt.tight_layout()
        plt.show()

        plt.plot(self.mlp.loss_curve_)
        plt.ylabel('Loss', fontsize=16)
        plt.xlabel('Iteration', fontsize=16)
        plt.tight_layout()
        plt.show()

    def predict(self, data):
        return self.mlp.predict(data.reshape(-1, len(self.mlp.coefs_[0])))

    def encode(self, data):
        return self.get_data_from_layer(data, self.latent_layer, 0)

    def decode(self, data):
        return self.get_data_from_layer(data, self.decoder_layer,
                                        self.latent_layer)

    def get_data_from_layer(self, data, to_layer, from_layer=0):
        L = ACTIVATIONS[
            self.activation](np.matmul(data, self.mlp.coefs_[from_layer]) +
                             self.mlp.intercepts_[from_layer])
        from_layer += 1
        if from_layer >= to_layer or from_layer >= len(self.mlp.coefs_):
            return L
        else:
            return self.get_data_from_layer(L, to_layer, from_layer=from_layer)

    def get_loss_curve(self):
        return self.mlp.loss_curve_
コード例 #29
0
ファイル: housesBase.py プロジェクト: manumolu/CMSC-422
# print("y-test")
# print(np.shape(y_test))

# In[46]:

from sklearn.neural_network import MLPRegressor

# model = MLPRegressor(hidden_layer_sizes=(6,1),activation ='identity',solver = 'lbfgs', learning_rate = 'constant',
#                      learning_rate_init =0.2,max_iter = 2000, random_state =13)

model = MLPRegressor(random_state=13)

# In[47]:

#To get pre-training Weights and Output Values
model.partial_fit(x_train, y_train)

# In[48]:

#Get model parameters
Params = model.get_params(deep=True)
type(Params)
print(Params)

# In[49]:

#Get Inital Parameters to compare pre-training and post-training

InitialWeightsI_H = model.coefs_[0]
# print(InitialWeightsI_H)
print(np.shape(InitialWeightsI_H))
コード例 #30
0
def getBestValidationScore(hidden_shape,
                           regularisation,
                           learn_rate=0.01,
                           noise_sigma=0,
                           stop_tolerance=EARLY_STOP_TOLERANCE):

    print(f"""\
    Hidden Shape: {hidden_shape}
    Alpha: {regularisation}\
    """)

    nn = MLPRegressor(
        # random_state=5,
        hidden_layer_sizes=hidden_shape,
        activation="logistic",
        solver="sgd",
        batch_size=len(target),
        learning_rate_init=learn_rate,
        alpha=regularisation,  # L2 Penality
    )

    early_stop_count = 0
    mse_validation_list = []

    num_failed = -1
    failed = True

    while failed and num_failed < 5:
        num_failed += 1
        for eps in range(MAX_EPOCHS):
            noise = noise_sigma * np.random.randn(pattern.shape[0],
                                                  pattern.shape[1])
            nn.partial_fit(pattern + noise, target)
            prediction_validation = nn.predict(pattern_validation)
            mse_validation_list.append(
                mean_squared_error(target_validation, prediction_validation))
            if eps > 2:  # Do Early Stopping
                # improvement from last validation less than threshold
                if mse_validation_list[-2] - mse_validation_list[
                        -1] < stop_tolerance:
                    if early_stop_count > EARLY_STOP_THRESHOLD:
                        break
                    else:
                        early_stop_count += 1
                else:
                    early_stop_count = 0

        failed = nn.score(pattern_test, target_test) < .5

    print("Num Epochs:", len(nn.loss_curve_))
    print("Validation MSE:", mse_validation_list[-1])

    # ## Plot weights histogram
    # w_ = []
    # for layerWeights in nn.coefs_:
    #     w_ += list(layerWeights.flatten())
    # plt.hist(w_, bins=50, range=(-1,1))
    # plt.ylabel("Count")
    # plt.xlabel("Weights in Range")
    # plt.title(f"Histogram of Weights (Alpha={regularisation}, 50 bins)")
    # # plt.savefig(f"pictures/4_3_histogram_alpha{regularisation}.png")
    # # plt.show()
    # plt.clf()

    # ## Plot learning curve
    # plt.plot(1 + np.arange(len(nn.loss_curve_)), nn.loss_curve_, label="Training Loss")
    # plt.plot(1 + np.arange(len(mse_validation_list)), mse_validation_list, label="Validation Loss")
    # plt.yscale("log")
    # plt.xlabel("Epochs")
    # plt.ylabel("Loss (MSE)")
    # plt.legend()
    # plt.title(f"Loss VS Epochs ({hidden_shape} Hidden)")
    # # plt.savefig(f"pictures/4_3_loss_curve_{hidden_shape[0]}_hidden.png")
    # # plt.show()
    # plt.clf()

    prediction_test = nn.predict(pattern_test)
    print("Test MSE:", mean_squared_error(target_test, prediction_test))
    print("Test R2:", nn.score(pattern_test, target_test))
    # Test Prediction
    # plt.plot(np.arange(len(prediction_test)), prediction_test, label="prediction")
    # plt.plot(np.arange(len(prediction_test)), target_test, label="actual")
    # plt.xlabel("Sample IDX")
    # plt.ylabel("Output Value")
    # plt.title(f"Prediction vs Actual (Network Shape {hidden_shape})")
    # plt.legend()
    # plt.savefig(f"pictures/4_3_prediction_vs_actual_{hidden_shape}.png")
    # # plt.show()
    # plt.clf()
    return mean_squared_error(target_test, prediction_test)

    return mse_validation_list[-1]
コード例 #31
0
]
counter = 0
print(datetime.datetime.now())
for i in range(0, 2):
    for train_df in pd.read_csv('data/train_preprocessed.csv',
                                chunksize=chunk_size):

        train_X = train_df[feature_classes]
        train_y = train_df["fare_amount"]

        X_train, X_test, y_train, y_test = train_test_split(train_X,
                                                            train_y,
                                                            test_size=0.1,
                                                            random_state=42)

        clf.partial_fit(X_train, y_train)
        val_preds = clf.predict(X_test)

        counter += 1
        print(i, counter, datetime.datetime.now(),
              np.sqrt(((val_preds - y_test)**2).mean()))

    test_df = pd.read_csv('data/test.csv', parse_dates=['pickup_datetime'])
    test_df['distance_miles'] = distance(test_df.pickup_latitude,
                                         test_df.pickup_longitude,
                                         test_df.dropoff_latitude,
                                         test_df.dropoff_longitude)
    test_df['distance_to_center'] = distance(nyc[1], nyc[0],
                                             test_df.pickup_latitude,
                                             test_df.pickup_longitude)
    test_df['distance_to_center_drop_off'] = distance(
コード例 #32
0
    Truth1[i,0] = Samples1[i,:].mean()
    Truth2[i,0] = Samples2[i,:].mean()
    Truth1[i,1] = Samples1[i,:].std()
    Truth2[i,1] = Samples2[i,:].std()    

trainSamples1,testSamples1,trainTruth1,testTruth1 = train_test_split(Samples1,Truth1,test_size=0.1)
trainSamples2,testSamples2,trainTruth2,testTruth2 = train_test_split(Samples2,Truth2,test_size=0.1)



# =============================================================================
# Train and predict
# =============================================================================
trials = 30
for i in np.arange(maxIt):
    mlp.partial_fit(trainSamples1,trainTruth1)

for i in np.arange(maxIt):
    mlp.partial_fit(trainSamples2,trainTruth2)

for j in np.arange(trials):
    print '\nTrial ' + str(j) + ':'
    
    for i in np.arange(maxIt):
        mlp.partial_fit(trainSamples1,trainTruth1)
        
    testPred1 = mlp.predict(testSamples1)
    testPred2 = mlp.predict(testSamples2)
    r2Score1 = r2_score(testTruth1,testPred1)
    r2Score2 = r2_score(testTruth2,testPred2)
    print 'Trained on set 1'
コード例 #33
0
ファイル: JugadorGrupo3.py プロジェクト: gsiriani/MAA
class Ann:
    '''
        Implementación e interfaz de la funcionalidad presentada de la ANN
    '''
    def __init__(self):

        self._nn = MLPRegressor(hidden_layer_sizes=(10,), verbose=False, warm_start=True)
        self._entradas_entrenamiento = []
        self._salidas_esperadas_entrenamiento = []
        # Parámetro de TD-lambda
        self.lambdaCoefficient = 0.9

    def evaluar(self, entrada):
        '''
            Devuelve la evaluación de la red para la entrada
        '''
        return self._nn.predict(entrada)

    def agregar_a_entrenamiento(self, tableros, resultado):
        '''
            Incorpora los datos de la partida a los ejemplos de entrenamiento
        '''

        # Presento la partida de adelante para atrás
        tableros.reverse()
        for i in xrange(len(tableros)):
            # Representación del tablero, Valor estimado
            tablero, valorEstimado = tableros[i][0], tableros[i][1]
            self._entradas_entrenamiento.append(tablero)
            if i == 0 or True:
                # Si es el resultado final, utilizo como salida esperada el resultado de la partida
                self._salidas_esperadas_entrenamiento.append(resultado.value)
            else:
                # El valor a aprender dado por según TD-lambda
                valorAAprender = valorEstimado + self.lambdaCoefficient * (
                    self._salidas_esperadas_entrenamiento[i - 1] - valorEstimado)
                self._salidas_esperadas_entrenamiento.append(valorAAprender)

    def entrenar(self):
        '''
            Aplico el entrenamiento a partir de los datos almacenados
        '''
        self._nn.partial_fit(self._entradas_entrenamiento, self._salidas_esperadas_entrenamiento)
        self._entradas_entrenamiento = []
        self._salidas_esperadas_entrenamiento = []

    def almacenar(self):
        '''
            Serializo y persisto la red
        '''
        pickle.dump(self._nn, open(self.path, 'wb'))

    def cargar(self, path, red):
        '''
            Deserealizo o creo una nueva red
        '''
        self.path = path
        if os.path.isfile(path):
            # Si el archivo especificado existe, deserealizo la red
            self._nn = pickle.load(open(path, 'rb'))
        else:
            # Si no, inicializo la red especificada
            self._nn = red
            tableroVacio = ([EnumCasilla.EMPTY.value for _ in xrange(64)], 0)
            self.agregar_a_entrenamiento([tableroVacio], EnumResultado.EMPATE)
            self.entrenar()