Example #1
0
def gamma():
    value_map = {'warm': 1.0, 'neutral': 0.5, 'cold': 0.0}

    X = data["x"][:, [0, 1, 2, 5, 6]]
    X = np.abs(X)
    maxX = np.amax(X, axis=0)
    minX = np.amax(X, axis=0)
    X = (X - minX) / maxX
    Y = data["y"][:, 1]
    Y = np.asarray([value_map[y] for y in Y])

    split_data = cross_validation.train_test_split(X, Y, test_size=0.2)
    X_train = split_data[0]
    X_test = split_data[1]
    Y_train = split_data[2]
    Y_test = split_data[3]

    nn = Regressor(
        layers=[
            Layer("Rectifier", units=3),
            Layer("Linear")],
        learning_rate=1e-3,
        n_iter=100)

    nn.fit(X_train, Y_train)

    print 'inosity accuracy'
    prediction = nn.predict(X_test)
    prediction = [closest(y[0]) for y in prediction]
    Y_test = [closest(y) for y in Y_test]
    print metrics.accuracy_score(prediction, Y_test)
class TestLinearNetwork(unittest.TestCase):

    def setUp(self):
        self.nn = MLPR(layers=[L("Linear")], n_iter=1)

    def test_LifeCycle(self):
        del self.nn

    def test_PredictNoOutputUnitsAssertion(self):
        a_in = numpy.zeros((8,16))
        assert_raises(AssertionError, self.nn.predict, a_in)

    def test_AutoInitializeWithOutputUnits(self):
        self.nn.layers[-1].units = 4
        a_in = numpy.zeros((8,16))
        self.nn.predict(a_in)

    def test_FitAutoInitialize(self):
        a_in, a_out = numpy.zeros((8,16)), numpy.zeros((8,4))
        self.nn.fit(a_in, a_out)
        assert_true(self.nn.is_initialized)

    def test_FitWrongSize(self):
        a_in, a_out = numpy.zeros((7,16)), numpy.zeros((9,4))
        assert_raises(AssertionError, self.nn.fit, a_in, a_out)
def neural_net(features,target,test_size_percent=0.2,cv_split=3,n_iter=100,learning_rate=0.01):
    '''Features -> Pandas Dataframe with attributes as columns
        target -> Pandas Dataframe with target column for prediction
        Test_size_percent -> Percentage of data point to be used for testing'''
    scale=preprocessing.MinMaxScaler()
    X_array = scale.fit_transform(features)
    y_array = scale.fit_transform(target)
    mlp = Regressor(layers=[Layer("Rectifier",units=5), # Hidden Layer1
                            Layer("Rectifier",units=3)  # Hidden Layer2
                            ,Layer("Linear")],     # Output Layer
                        n_iter = n_iter, learning_rate=0.01)
    X_train, X_test, y_train, y_test = train_test_split(X_array, y_array.T.squeeze(), test_size=test_size_percent, random_state=4)
    mlp.fit(X_train,y_train)
    test_prediction = mlp.predict(X_test)
    tscv = TimeSeriesSplit(cv_split)
    
    training_score = cross_val_score(mlp,X_train,y_train,cv=tscv.n_splits) 
    testing_score = cross_val_score(mlp,X_test,y_test,cv=tscv.n_splits)
    print"Cross-val Training score:", training_score.mean()
#    print"Cross-val Testing score:", testing_score.mean()
    training_predictions = cross_val_predict(mlp,X_train,y_train,cv=tscv.n_splits)
    testing_predictions = cross_val_predict(mlp,X_test,y_test,cv=tscv.n_splits)
    
    training_accuracy = metrics.r2_score(y_train,training_predictions) 
#    test_accuracy_model = metrics.r2_score(y_test,test_prediction_model)
    test_accuracy = metrics.r2_score(y_test,testing_predictions)
    
#    print"Cross-val predicted accuracy:", training_accuracy
    print"Test-predictions accuracy:",test_accuracy

    plot_model(target,y_train,y_test,training_predictions,testing_predictions)
    return mlp
Example #4
0
def NeuralNet(train, test, features):
    eta = 0.025
    niter = 2000

    regressor = Regressor(
        layers=[Layer("Rectifier", units=100), Layer("Tanh", units=100), Layer("Sigmoid", units=100), Layer("Linear")],
        learning_rate=eta,
        learning_rule="momentum",
        learning_momentum=0.9,
        batch_size=100,
        valid_size=0.01,
        n_stable=100,
        n_iter=niter,
        verbose=True,
    )

    print regressor.__class__.__name__
    start = time.time()
    regressor.fit(np.array(train[list(features)]), train[goal])
    print "  -> Training time:", time.time() - start

    if not os.path.exists("result/"):
        os.makedirs("result/")
    # TODO: fix this shit
    predictions = regressor.predict(np.array(test[features]))
    try:  # try to flatten a list that might be flattenable.
        predictions = list(itertools.chain.from_iterable(predictions))
    except:
        pass
    csvfile = "result/dat-nnet-eta%s-niter%s.csv" % (str(eta), str(niter))
    with open(csvfile, "w") as output:
        writer = csv.writer(output, lineterminator="\n")
        writer.writerow([myid, goal])
        for i in range(0, len(predictions)):
            writer.writerow([i + 1, predictions[i]])
Example #5
0
 def make(self, activation, seed=1234, train=False, **keywords):
     nn = MLPR(layers=[L(activation, units=16, **keywords), L("Linear", units=1)], random_state=seed, n_iter=1)
     if train:
         nn.fit(self.a_in, self.a_out)
     else:
         nn._initialize(self.a_in, self.a_out)
     return nn
Example #6
0
def CreateNetwork(data, predicates):
    # входная размерность
    dim_in = len(predicates)
    # выходная размерность
    dim_out = len(data[0]) - 1
    # конфигурация сети
    neural_network = Regressor(
        layers=[
            Layer("Rectifier", units=50),
            Layer("Linear")],
        learning_rate=0.001,
        n_iter=5000)
    # формирование обучающей выборки
    x_train = np.array([CalcPredicates(row[0], predicates) for row in data])
    y_train = np.array([apply(float, row[1:]) for row in data])
    # обучение
    logging.info('Start training')
    logging.info('\n'+str(x_train))
    logging.info('\n'+str(y_train))
    try:
        neural_network.fit(x_train, y_train)
    except KeyboardInterrupt:
        logging.info('User break')
        pass
    logging.info('Network created successfully')
    logging.info('score = '+str(neural_network.score(x_train, y_train)))
    # сохранение обученной сети
    pickle.dump(neural_network, open(datetime.datetime.now().isoformat()+'.pkl', 'wb'))
    return neural_network
class ClassificationTools():
	def __init__(self, inputVector=[], outputVector=[], filepath=''):
		if filepath == '':
			self.inputVector = numpy.asarray(inputVector)
			self.outputVector = numpy.asarray(outputVector)
			self.model = None
		else:
			self.model = pickle.load(file(filepath, 'r'))

	def setVectors(self, inputVector, outputVector):
		self.inputVector = numpy.asarray(inputVector)
		self.outputVector = numpy.asarray(outputVector)


	def trainMultilayerPerceptron(self, hlunits=10000, learningRate=0.01, iters=1000):
		# trains a simple MLP with a single hidden layer
		self.model = Regressor(
			layers=[
				Layer("Rectifier", units=hlunits),
				Layer("Linear")],
			learning_rate=learningRate,
			n_iter=iters)
		self.model.fit(self.inputVector, self.outputVector)

	def predict(self, toPredict):
		prediction = self.model.predict(numpy.asarray(toPredict))
		return prediction # this will be a 1D numpy array of floats

	def trainDeepNetwork(self):
		# trains a deep network based a multi layer autoencoder
		# which is then fine tuned using an MLP
		pass

	def serializeModel(self, filepath):
		pickle.dump(self.model, file(filepath, 'w'))
Example #8
0
 def test_VerboseRegressor(self):
     nn = MLPR(layers=[L("Linear")], verbose=1, n_iter=1)
     a_in, a_out = numpy.zeros((8,16)), numpy.zeros((8,4))
     nn.fit(a_in, a_out)
     assert_in("Epoch       Training Error       Validation Error       Time", self.buf.getvalue())
     assert_in("    1       ", self.buf.getvalue())
     assert_in("    N/A     ", self.buf.getvalue())
Example #9
0
class TestInputOutputs(unittest.TestCase):
    def setUp(self):
        self.nn = MLPR(layers=[L("Linear")], n_iter=1)

    def test_FitOneDimensional(self):
        a_in, a_out = numpy.zeros((8, 16)), numpy.zeros((8, ))
        self.nn.fit(a_in, a_out)
Example #10
0
class NeuralRegLearner(object):

    def __init__(self, verbose = False):
        self.name = "Neural net Regression Learner"
        self.network =  Regressor( layers=[
										Layer("Rectifier", units=100),
										Layer("Linear")],
									learning_rate=0.02,
									n_iter=10)

    def addEvidence(self,dataX,dataY):
        """
        @summary: Add training data to learner
        @param dataX: X values of data to add
        @param dataY: the Y training values
        """
        dataX = np.array(dataX)
        dataY = np.array(dataY)
        self.network.fit(dataX, dataY) 
        
    def query(self,points):
        """
        @summary: Estimate a set of test points given the model we built.
        @param points: should be a numpy array with each row corresponding to a specific query.
        @returns the estimated values according to the saved model.
        """
        return self.network.predict(points)
class TestInputOutputs(unittest.TestCase):

    def setUp(self):
        self.nn = MLPR(layers=[L("Linear")], n_iter=1)

    def test_FitOneDimensional(self):
        a_in, a_out = numpy.zeros((8,16)), numpy.zeros((8,))
        self.nn.fit(a_in, a_out)
def train_regression_predictor(train_x, train_y, learning_rule='sgd', learning_rate=0.002, n_iter=20, units=4):
    mlp = Regressor(layers=[Layer('Rectifier', units=units),
                            Layer('Linear')],
                   learning_rule=learning_rule,
                   learning_rate=learning_rate,
                   n_iter=n_iter)
    mlp.fit(train_x, train_y)
    print mlp.score(train_x, train_y)
    return mlp
 def _run(self, activation):
     a_in, a_out = numpy.zeros((8, 32, 16, 1)), numpy.zeros((8, 4))
     nn = MLPR(
         layers=[C(activation, channels=4, kernel_shape=(3, 3), pool_shape=(2, 2), pool_type="mean"), L("Linear")],
         n_iter=1,
     )
     nn.fit(a_in, a_out)
     a_test = nn.predict(a_in)
     assert_equal(type(a_out), type(a_in))
Example #14
0
    def check(self, a_in, a_out, a_mask):
        nn = MLPR(layers=[L("Linear")], learning_rule='adam', learning_rate=0.1, n_iter=50)
        nn.fit(a_in, a_out, a_mask)
        v_out = nn.predict(a_in)

        # Make sure the examples weighted 1.0 have low error, 0.0 high error.
        print(abs(a_out - v_out).T * a_mask)
        assert_true((abs(a_out - v_out).T * a_mask < 1E-1).all())
        assert_true((abs(a_out - v_out).T * (1.0 - a_mask) > 2.5E-1).any())
Example #15
0
 def make(self, activation, seed=1234, train=False, **keywords):
     nn = MLPR(
         layers=[L(activation, units=16, **keywords),
                 L("Linear", units=1)],
         random_state=seed,
         n_iter=1)
     if train:
         nn.fit(self.a_in, self.a_out)
     else:
         nn._initialize(self.a_in, self.a_out)
     return nn
Example #16
0
 def make(self, activation, seed=1234, train=False, **keywords):
     nn = MLPR(
         layers=[C(activation, channels=16, kernel_shape=(3, 3), **keywords), L("Linear")],
         random_state=seed,
         n_iter=1,
     )
     if train:
         nn.fit(self.a_in, self.a_out)
     else:
         nn._initialize(self.a_in, self.a_out)
     return nn
Example #17
0
    def check(self, a_in, a_out, a_mask):
        nn = MLPR(layers=[L("Linear")], learning_rule='adam', learning_rate=0.05, n_iter=250, n_stable=25)
        nn.fit(a_in, a_out, a_mask)
        v_out = nn.predict(a_in)

        # Make sure the examples weighted 1.0 have low error, 0.0 high error.
        masked = abs(a_out - v_out).T * a_mask
        print('masked', masked)
        assert_true((masked < 5.0E-1).all())
        inversed = abs(a_out - v_out).T * (1.0 - a_mask)
        print('inversed', inversed)
        assert_greater(inversed.mean(), masked.mean())
Example #18
0
def neural_net(features,
               target,
               test_size_percent=0.2,
               cv_split=3,
               n_iter=100,
               learning_rate=0.01):
    '''Features -> Pandas Dataframe with attributes as columns
        target -> Pandas Dataframe with target column for prediction
        Test_size_percent -> Percentage of data point to be used for testing'''
    scale = preprocessing.MinMaxScaler()
    X_array = scale.fit_transform(features)
    y_array = scale.fit_transform(target)
    mlp = Regressor(
        layers=[
            Layer("Rectifier", units=5),  # Hidden Layer1
            Layer("Rectifier", units=3)  # Hidden Layer2
            ,
            Layer("Linear")
        ],  # Output Layer
        n_iter=n_iter,
        learning_rate=0.01)
    X_train, X_test, y_train, y_test = train_test_split(
        X_array,
        y_array.T.squeeze(),
        test_size=test_size_percent,
        random_state=4)
    mlp.fit(X_train, y_train)
    test_prediction = mlp.predict(X_test)
    tscv = TimeSeriesSplit(cv_split)

    training_score = cross_val_score(mlp, X_train, y_train, cv=tscv.n_splits)
    testing_score = cross_val_score(mlp, X_test, y_test, cv=tscv.n_splits)
    print "Cross-val Training score:", training_score.mean()
    #    print"Cross-val Testing score:", testing_score.mean()
    training_predictions = cross_val_predict(mlp,
                                             X_train,
                                             y_train,
                                             cv=tscv.n_splits)
    testing_predictions = cross_val_predict(mlp,
                                            X_test,
                                            y_test,
                                            cv=tscv.n_splits)

    training_accuracy = metrics.r2_score(y_train, training_predictions)
    #    test_accuracy_model = metrics.r2_score(y_test,test_prediction_model)
    test_accuracy = metrics.r2_score(y_test, testing_predictions)

    #    print"Cross-val predicted accuracy:", training_accuracy
    print "Test-predictions accuracy:", test_accuracy

    plot_model(target, y_train, y_test, training_predictions,
               testing_predictions)
    return mlp
Example #19
0
 def make(self, activation, seed=1234, train=False, **keywords):
     nn = MLPR(layers=[
         C(activation, channels=16, kernel_shape=(3, 3), **keywords),
         L("Linear")
     ],
               random_state=seed,
               n_iter=1)
     if train:
         nn.fit(self.a_in, self.a_out)
     else:
         nn._initialize(self.a_in, self.a_out)
     return nn
Example #20
0
    def check(self, a_in, a_out, a_mask):
        nn = MLPR(layers=[L("Linear")],
                  learning_rule='adam',
                  learning_rate=0.1,
                  n_iter=50)
        nn.fit(a_in, a_out, a_mask)
        v_out = nn.predict(a_in)

        # Make sure the examples weighted 1.0 have low error, 0.0 high error.
        print(abs(a_out - v_out).T * a_mask)
        assert_true((abs(a_out - v_out).T * a_mask < 1E-1).all())
        assert_true((abs(a_out - v_out).T * (1.0 - a_mask) > 2.5E-1).any())
Example #21
0
 def _run(self, activation):
     a_in, a_out = numpy.zeros((8, 32, 16, 1)), numpy.zeros((8, 4))
     nn = MLPR(layers=[
         C(activation,
           channels=4,
           kernel_shape=(3, 3),
           pool_shape=(2, 2),
           pool_type='mean'),
         L("Linear")
     ],
               n_iter=1)
     nn.fit(a_in, a_out)
     a_test = nn.predict(a_in)
     assert_equal(type(a_out), type(a_in))
Example #22
0
def train_nn(train_set, validation_set):

    nn = Regressor(
        layers=[Layer("Sigmoid", units=2),
                Layer("Sigmoid")],
        learning_rate=0.0001,
        batch_size=5,
        n_iter=10000,
        valid_set=validation_set,
        verbose=True,
    )
    nn.fit(train_set[0], train_set[1])

    return nn
Example #23
0
class Learner:
    def __init__(self, iterations=5):
        results = []
        situations = []
        logging.basicConfig()
        for i in range(0, iterations):
            g = Game(print_board=False)
            round_situations = []
            while not g.game_over:
                choices = g.available_cols()
                choice = random.choice(choices)
                round_situations.append(self.game_to_sit(g, choice))
                g.place_piece(choice)
            for situation in round_situations:
                results.append(g.points)
            situations.extend(round_situations)
        #self.pipeline = Pipeline([
        #    ('min/max scaler', MinMaxScaler(feature_range=(0.0, 1.0))),
        #    ('neural network', Regressor(
        self.nn = Regressor(layers=[
                    Layer("Rectifier", units=100),
                    Layer("Linear")],
                learning_rate=0.00002,
                n_iter=10)
        #self.pipeline.fit(np.array(situations), np.array(results))
        print np.array(situations).shape
        self.nn.fit(np.array(situations), np.array(results))
        #self.clf = MLPRegressor(algorithm='l-bfgs', alpha=1e-5,
        #                         hidden_layer_sizes=(5, 2), random_state=1)
        #clf.train(situations, results)

    def game_to_sit(self, game, choice):
        sit = [float(item) / 9 for sublist in game.board for item in sublist]
        sit.append(float(choice) / 7)
        sit.append(float(game.level) / 100)
        assert(float(game.level) / 100)
        sit.append(float(game.pieces_left) / 30)
        return sit
    
    def pick_move(self, game):
        choices = game.available_cols()
        max_choice, max_val = None, 0
        for c in choices:
            sit = np.array([self.game_to_sit(game, c)])
            final_score_predict = self.nn.predict(sit)
            if final_score_predict > max_val:
                max_val = final_score_predict
                max_choice = c
        return max_choice
class RegressorNeuralNet():
	def __init__(self):
		self.nn = Regressor(
		    layers=[
		        Layer("Sigmoid", units=100),
		        Layer("Sigmoid", units=47),
		        Layer("Linear")],
		    learning_rate=0.02,
		    n_iter=200)

	def train(self):
		data = parser.load_echo_data('data/training_data.csv')
		self.nn.fit(data.data, data.target)

	def predictData(self, data):
		return self.nn.predict(data)
Example #25
0
    def check(self, a_in, a_out, a_mask):
        nn = MLPR(layers=[L("Linear")],
                  learning_rule='adam',
                  learning_rate=0.05,
                  n_iter=250,
                  n_stable=25)
        nn.fit(a_in, a_out, a_mask)
        v_out = nn.predict(a_in)

        # Make sure the examples weighted 1.0 have low error, 0.0 high error.
        masked = abs(a_out - v_out).T * a_mask
        print('masked', masked)
        assert_true((masked < 4.0E-1).all())
        inversed = abs(a_out - v_out).T * (1.0 - a_mask)
        print('inversed', inversed)
        assert_greater(inversed.mean(), masked.mean())
Example #26
0
def test():
    boom = pd.read_csv('boommagain.csv')
    boom = boom.drop(['Unnamed: 0', 'Unnamed: 0.1'], axis = 1)
    words = boom[list(boom.columns.values)[8:89]]
    y = boom['Rating']
    x = words
    print(y.shape, type(y))
    print(y[:10])
    print(x.shape, type(x))

    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.4, random_state = 1)

    X_train = X_train.as_matrix()
    y_train = y_train.as_matrix()
    y_test = y_test.as_matrix()
    X_test = X_test.as_matrix()
    print(y_train[:10])
    print(X_train.shape, type(X_train))
    print(y_train.shape, type(y_train))

    y_test = Series(y_test)
    squared = [pow(x, 2) for x in y_test]
    avg = mean(squared)
    RMSE = sqrt(avg)
    print(RMSE)

    for num_nodes in [20]:
        for epoch in [50]:
            nn = Regressor(
            layers=[
                Layer("Sigmoid", units=num_nodes),
                Layer("Softmax")],
            learning_rate=0.01,
            n_iter=epoch)
            nn.fit(X_train, y_train)
            y_pred = nn.predict(X_test)
            print(y_pred.shape)
            # y_pred = Series(y_pred)
            # y_test = Series(y_test)
            # diff = [y_pred[i] - y_test[i] for i in range(len(y_pred))]
            # squared = [pow(x, 2) for x in diff]
            # avg = mean(squared)
            # RMSE = sqrt(avg)
            # print(RMSE)

            print(metrics.r2_score(y_test, y_pred))
Example #27
0
def colour():
    Y = data["y"][:, 2]
    vals = np.unique(Y);
    value_map = {};
    for i in range(0, len(vals)):
        value_map[vals[i]] = (0.0 + i) / (len(vals) - 1)
    value_values = value_map.values()
    keys = value_map.keys()

    Ya = []
    for a in Y:
        k = []
        for i in range(0, len(keys)):
            k.append(0.0)
        k[keys.index(a)] = 1.0
        Ya.append(k)
    Y = np.asarray(Ya)
    
    X = data["x"][:, [0, 1, 2, 3, 4, 5, 6, 7]]
    X = np.abs(X)
    maxX = np.amax(X, axis=0)
    minX = np.amax(X, axis=0)
    X = (X - minX) / maxX

    split_data = cross_validation.train_test_split(X, Y, test_size=0.2)
    X_train = split_data[0]
    X_test = split_data[1]
    Y_train = split_data[2]
    Y_test = split_data[3]

    nn = Regressor(
        layers=[
            Layer("Linear", units=9),
            Layer("Softmax", units=9)],
        learning_rate=5e-2,
        n_iter=100)

    nn.fit(X_train, Y_train)

    print 'colour accuracy'
    prediction = nn.predict(X_test)
    prediction = [np.argmax(y) for y in prediction]
    Y_test = [np.argmax(y) for y in Y_test]
    print metrics.accuracy_score(prediction, Y_test)
class TestSerialization(unittest.TestCase):
    def setUp(self):
        self.nn = MLPR(layers=[L("Linear")], n_iter=1)

    def test_SerializeFail(self):
        buf = io.BytesIO()
        assert_raises(AssertionError, pickle.dump, self.nn, buf)

    def test_SerializeCorrect(self):
        a_in, a_out = numpy.zeros((8, 16)), numpy.zeros((8, 4))
        self.nn.fit(a_in, a_out)

        buf = io.BytesIO()
        pickle.dump(self.nn, buf)

        buf.seek(0)
        nn = pickle.load(buf)

        assert_is_not_none(nn.mlp)
        assert_equal(nn.layers, self.nn.layers)
Example #29
0
def evalOne(parameters):
    all_obs = []
    all_pred = []
    for location in locations:
        trainX, testX, trainY, testY = splitDataForXValidation(
            location, "location", data, all_features, "target")
        normalizer_X = StandardScaler()
        trainX = normalizer_X.fit_transform(trainX)
        testX = normalizer_X.transform(testX)
        normalizer_Y = StandardScaler()
        trainY = normalizer_Y.fit_transform(trainY)
        testY = normalizer_Y.transform(testY)

        layers = []
        for _ in range(0, parameters["hidden_layers"]):
            layers.append(
                Layer(parameters["hidden_type"],
                      units=parameters["hidden_neurons"]))
        layers.append(Layer("Linear"))
        model = Regressor(layers=layers,
                          learning_rate=parameters["learning_rate"],
                          n_iter=parameters["iteration"],
                          random_state=42)

        X = np.array(trainX)
        y = np.array(trainY)

        model.fit(X, y)

        model.fit(trainX, trainY)
        prediction = model.predict(testX)
        prediction = normalizer_Y.inverse_transform(prediction)
        testY = normalizer_Y.inverse_transform(testY)

        print("location: " + str(location) + " -> " +
              str(rmseEval(prediction, testY)[1]))

        all_obs.extend(testY)
        all_pred.extend(prediction)

    return rmseEval(all_obs, all_pred)[1]
    def classify(self, x_train, y_train, x_test):
        x_train = np.array(x_train)
        y_train = np.array(y_train)
        x_test = np.array(x_test)
        # nn = Classifier(
        #    layers=[
        #        Layer("Maxout", units=100, pieces=2),
        #        Layer("Softmax")],
        #    learning_rate=0.001,
        #    n_iter=25)
        # nn.fit(x_train, y_train)
        # y_test = nn.predict(np.array(x_test))

        nn = Regressor(layers=[Layer('Rectifier', units=400), Layer('Linear')], learning_rate=0.02, n_iter=10)
        log_to_info('Fitting a NN to labeled training data...')
        nn.fit(np.array(x_train), np.array(y_train))
        log_to_info('Predicting test value')
        y_test = nn.predict(np.array(x_test))
        log_to_info('Done!')

        return y_test
class TestSerialization(unittest.TestCase):

    def setUp(self):
        self.nn = MLPR(layers=[L("Linear")], n_iter=1)

    def test_SerializeFail(self):
        buf = io.BytesIO()
        assert_raises(AssertionError, pickle.dump, self.nn, buf)

    def test_SerializeCorrect(self):
        a_in, a_out = numpy.zeros((8,16)), numpy.zeros((8,4))
        self.nn.fit(a_in, a_out)

        buf = io.BytesIO()
        pickle.dump(self.nn, buf)

        buf.seek(0)
        nn = pickle.load(buf)

        assert_is_not_none(nn.mlp)
        assert_equal(nn.layers, self.nn.layers)
Example #32
0
class TestLinearNetwork(unittest.TestCase):
    def setUp(self):
        self.nn = MLPR(layers=[L("Linear")], n_iter=1)

    def test_LifeCycle(self):
        del self.nn

    def test_PredictNoOutputUnitsAssertion(self):
        a_in = numpy.zeros((8, 16))
        assert_raises(AssertionError, self.nn.predict, a_in)

    def test_AutoInitializeWithOutputUnits(self):
        self.nn.layers[-1].units = 4
        a_in = numpy.zeros((8, 16))
        self.nn.predict(a_in)

    def test_FitAutoInitialize(self):
        a_in, a_out = numpy.zeros((8, 16)), numpy.zeros((8, 4))
        self.nn.fit(a_in, a_out)
        assert_true(self.nn.is_initialized)

    def test_ResizeInputFrom4D(self):
        a_in, a_out = numpy.zeros((8, 4, 4, 1)), numpy.zeros((8, 4))
        self.nn.fit(a_in, a_out)
        assert_true(self.nn.is_initialized)

    def test_ResizeInputFrom3D(self):
        a_in, a_out = numpy.zeros((8, 4, 4)), numpy.zeros((8, 4))
        self.nn.fit(a_in, a_out)
        assert_true(self.nn.is_initialized)

    def test_FitWrongSize(self):
        a_in, a_out = numpy.zeros((7, 16)), numpy.zeros((9, 4))
        assert_raises(AssertionError, self.nn.fit, a_in, a_out)
def trainNeuralNetwork(data, columns, targetColumn, parameters):

    modelColumns = []
    for column in columns:
        if column != targetColumn:
            modelColumns.append(column)

    modelData = []

    for i in range(0, len(data[targetColumn])):
        record = []
        for column in modelColumns:
            record.append(data[column][i])

        modelData.append(record)

    layers = []
    layers.append(Layer("Rectifier", units=60))
    for i in range(0, parameters["hidden_layers"]):
        layers.append(
            Layer(parameters["hidden_type"],
                  units=parameters["hidden_neurons"]))
    layers.append(Layer("Linear"))
    model = Regressor(
        layers=layers
        #             Layer("Rectifier", units=60),
        #             Layer("Sigmoid", units=80),
        #             Layer("Sigmoid", units=80),
        #             Layer("Linear")
        ,
        learning_rate=0.01,
        n_iter=parameters["iteration"])

    X = np.array(modelData)
    y = np.array(data[targetColumn])

    model.fit(X, y)

    return NeuralNetworkModel(model, modelColumns)
Example #34
0
def NeuralNet(train,test,features):
    eta = 0.025
    niter = 2000

    regressor = Regressor(
                      layers=[
                          Layer('Rectifier', units=100),
                          Layer("Tanh", units=100),
                          Layer("Sigmoid", units=100),
                          Layer('Linear')],
                      learning_rate=eta,
                      learning_rule='momentum',
                      learning_momentum=0.9,
                      batch_size=100,
                      valid_size=0.01,
                      n_stable=100,
                      n_iter=niter,
                      verbose=True)

    print regressor.__class__.__name__
    start = time.time()
    regressor.fit(np.array(train[list(features)]), train[goal])
    print '  -> Training time:', time.time() - start

    if not os.path.exists('result/'):
        os.makedirs('result/')
    # TODO: fix this shit
    predictions = regressor.predict(np.array(test[features]))
    try: # try to flatten a list that might be flattenable.
        predictions = list(itertools.chain.from_iterable(predictions))
    except:
        pass
    csvfile = 'result/dat-nnet-eta%s-niter%s.csv' % (str(eta),str(niter))
    with open(csvfile, 'w') as output:
        writer = csv.writer(output, lineterminator='\n')
        writer.writerow([myid,goal])
        for i in range(0, len(predictions)):
            writer.writerow([i+1,predictions[i]])
Example #35
0
def trainNeuralNetwork(data, columns, targetColumn, params):

    modelColumns = []
    for column in columns:
        if column != targetColumn:
            modelColumns.append(column)

    modelData = []

    for i in range(0, len(data[targetColumn])):
        record = []
        for column in modelColumns:
            record.append(data[column][i])

        modelData.append(record)

    model = Regressor(layers=[Layer("Rectifier", units=100),
                              Layer("Linear")],
                      learning_rate=0.02,
                      n_iter=100)

    model.fit(modelData, data[targetColumn])

    return NeuralNetworkModel(model, modelColumns)
Example #36
0
def neural(X, Y, params=None):
	nn = Regressor(
		layers=[
		    Layer("Rectifier", units=200),
		    Layer("Sigmoid", units=200),
		    Layer("Rectifier", units=200),
		    Layer("Sigmoid", units=200),
		    Layer("Sigmoid")],
		learning_rate=0.001,
		n_iter=100,
		parameters=params,
		learning_rule='sgd',
		f_stable=0.001,
		valid_size=.2,
		verbose=True,
		n_stable=3)

	return nn.fit(X, Y)
Example #37
0
def main():
    
    #Initialize the board - the state
    state = np.ndarray(shape=(3,3), dtype=int)
    win_states = makeWinStates()
    actions = [[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]] #also spaces
    
    #Variables - might not be necessary
    k = 1
    alpha = 1/k
    gamma = .3
    eps = .1

    #Initializing our 'overkill' neural networks
    actor = Regressor(layers, warning=None, weights=None, random_state=None, 
                     learning_rule='sgd', 
                     learning_rate=0.01, 
                     learning_momentum=0.9, 
                     regularize=None, weight_decay=None, 
                     dropout_rate=None, batch_size=1, 
                     n_iter=None, n_stable=10, 
                     f_stable=0.001, valid_set=None, 
                     valid_size=0.0, loss_type=None, 
                     callback=None, debug=False, 
                     verbose=None) #???
    
    #Training the actor with a random policy
    trainStates = []; acts = []
    for i in range(500):
	sample_st = np.ndarray(shape=(3,3), dtype=int)
	for j in range(9):
	    sample_st[math.floor(j/3),j%3] = random.randint(-1,1)
	    
	act = random.randint(0,8) #action represented by its index
	trainStates.append(sample_st)
	acts.append(act)
	
    actor.fit(trainStates, acts)
    
    target_mu = actor
    
    
    critic = Regressor(layers=[Layer("Rectifier", name="layer1", units=11, pieces=2), #9 squares, 1 action, 1 bias
                       Layer("Rectifier", name="layer2", units=11, pieces=2),
                       Layer("Rectifier", name="layer3", units=11, pieces=2),
                       Layer("Softmax")], learning_rate=0.02)
    
    #Randomly initialize the critic
    statesAndActs = []; rewards = []
    for i in range(500):
	sample_st = np.ndarray(shape=(3,3), dtype=int)
	for j in range(9):
	    sample_st[math.floor(j/3),j%3] = random.randint(-1,1)

	#random action, random reward
	act = random.randint(0,8)
	rew = random.randint(-1,1)

	statesAndActs.append([sample_st,act])
	rewards.append(rew)

    critic.fit(statesAndActs,rewards)

    target_Q = critic

    
    for i in range(10):
	reward = 0; end = False; R = []
	
	while (end != True):

	    action = actor.predict(state)
	    newstate = getNextState(state, action) #Execute action
	    
	    #Observe reward
	    reward = getReward(state)
	    if reward != 0: #Game is done
		end = True		    
	    
	    #Replay buffer review
	    R.append(state, action, reward, newstate)
	    
	    N = math.floor(math.log(len(R)))
	    R2 = R; minibatch = []
	    for i in range(N):
		j = random.randint(0,len(R2)-1)
		minibatch.append(R2[j])
		R2.remove(R2[j])
		
	    ys = []; batchStates = []
	    for i in range(N):
		s_1 = minibatch[i][3]; r = minibatch[i][2]
		ys.append(r + gamma*target_Q.predict(s_1))
		
		#Make new input for retraining - includes state and action
		batchStates.append([minibatch[i][0],minibatch[i][0]])
		
	    #minimize the loss L = (1/N)*sum(ys[i] - critic.predict(state))^2 - a linear regression
	    if len(batchStates) != 0:
		critic.fit(batchStates,ys)
	    
	    #update the actor policy somehow -- this is the hard part; test the critic alone first
	    
	    #Update the target critic
	    Q_para = np.array(critic.get_parameters())
	    if i == 0:
		target_Q.set_parameters(Q_para)
	    else:
		Qp_para = np.array(target_Q.get_parameters())
		new_para = tau*Q_para + (1-tau)*Qp_para
		target_Q.set_parameters(new_para)
		
	    #Update the target actor
	    
	    
	    #How do I write this
	    
	    #Set state to the new state.
	    state = newstate
	    
	    reward = getReward(state)
	    if reward != 0: #Game is done
		end = True	    
	    
	    #We play as "O"
	    x = -1; y = -1
	    while not (x >= 0 and x <= 2 and y >= 0 and y <= 2):
		try:
		    x, y = int(input("Enter the row and column indices of the location at which you intend to draw an 'O.' (Format: x, y):    "))
		    while x <= 0 or x >= 2 or y <= 0 or y >= 2:
			x, y = int(input("Sorry, those indices are invalid. Please input integral indices between 0 and 2 inclusive, in the correct format:    "))
		except:
		    print ("I'm sorry, but x and y should be numerical.")
	    
	    state[x,y] = -1
	    reward = getLoss(state)
	    if reward != 0: #Game is done
		end = True
    # NEURAL NETWORK TRAINING AND TESTING
    # Set up neural network
    if runTraining:
        print "Starting neural network training"
        nn = Regressor(
                       layers=[
                               Layer("Rectifier", units=30),
                               Layer("Linear")],
                       learning_rate=0.01,
                       batch_size = 100,
                       #learning_rule = "momentum",
                       n_iter=100)
                       #valid_size=0.25)
        # Training
        nn.fit(X_train_bg,Y_train)
        pickle.dump(nn, open('autoencoder.pkl', 'wb'))
    if not runTraining:
        nn = pickle.load(open('autoencoder.pkl', 'rb'))

    # Testing
    predicted_diff = nn.predict(X_test_bg)
    predicted_signal = nn.predict(X_test_sig)

    # Reconstruction error
    rec_errors_diff = reconstructionError(X_test_bg,predicted_diff)
    rec_errors_sig = reconstructionError(X_test_sig,predicted_signal)

    # Reconstruction errors by variable
    rec_errors_varwise_diff = reconstructionErrorByFeature(X_test_bg,predicted_diff)
    rec_errors_varwise_sig = reconstructionErrorByFeature(X_test_sig,predicted_signal)
Example #39
0
class Learn:

    nx = 20
    ny = 20
    n_cell = nx * ny
    n_coups = 8
    coups_sautes = 60

    def __init__(self, new=False, display=False):
        self.possibilities = generate(Learn.n_coups)
        np.random.shuffle(self.possibilities)
        self.explore = 0.
        self.jeu = MJ.Jeu(autorepeat=False, display=display)
        self.jeu.restart(Learn.coups_sautes)
        self.image = self.get_image()
        if new:
            self.nn = Regressor(layers=[
                Layer("Linear", units=(Learn.n_cell + Learn.n_coups)),
                Layer("Sigmoid", units=1000),
                Layer("Sigmoid")
            ],
                                learning_rate=0.01,
                                n_iter=1)
            self.nn.fit(
                self.good_shape(self.image,
                                self.possibilities[Learn.n_coups / 2 - 1]),
                np.array([[0]]))
        else:
            self.nn = pickle.load(open('nn.pkl', 'rb'))
            self.nn.fit(
                self.good_shape(self.image,
                                self.possibilities[Learn.n_coups / 2 - 1]),
                np.array([[1]]))
        self.current_data_set = []

    def good_shape(
        self, image, instructions
    ):  #instructions est un tableau de 0 et 1 à n_coups éléments
        tab = np.zeros((1, (Learn.n_cell + Learn.n_coups)))
        tab[0, ::] = np.append(image.flatten(), instructions)
        return 10 * tab

    def play(self, num_iter=1000):
        self.set_display(True)
        predicted_outcome = np.zeros(2**Learn.n_coups)
        for s in xrange(num_iter):
            self.image = self.get_image()
            if (s % 100 == 0):
                print s
            outcome = 1
            indice_max = 0
            for j, elt in enumerate(self.possibilities):
                a = self.nn.predict(self.good_shape(self.image, elt))[0][0]
                predicted_outcome[j] = a
                if a > 0.99:
                    i = j
                    break
                elif (a > predicted_outcome[indice_max]):
                    indice_max = j
            i = indice_max
            elt = self.possibilities[i][0]
            if (outcome == 1):
                if elt == 1:
                    instr = 'd'
                elif elt == 0:
                    instr = 'q'
                if (self.jeu.update_all(instr) == "Dead"):
                    outcome = 0
                    self.jeu.restart(Learn.coups_sautes)

    def auc(self, num_iter=10000):
        real_outputs = []
        predicted_outputs = []
        for s in xrange(num_iter):
            outcome = 1
            poss = self.possibilities
            i = rd.randint(0, len(poss) - 1)
            elt = poss[i]
            predicted_outputs.append(
                self.nn.predict(self.good_shape(self.image, elt))[0])
            r = rd.random()
            if (r < 0.8):
                i = int(rd.random() * 2**(Learn.n_coups))
            for elt in self.possibilities[i]:
                if (outcome == 1):
                    if elt:
                        instr = 'd'
                    else:
                        instr = 'q'
                    if (self.jeu.update_all(instr) == "Dead"):
                        outcome = 0
                        self.jeu.restart(Learn.coups_sautes)
            real_outputs.append(outcome)
            self.image = self.get_image()
        fpr, tpr, thresholds = metrics.roc_curve(real_outputs,
                                                 predicted_outputs)
        return metrics.auc(fpr, tpr)

    def benchmark(self, num_iter=1000):
        temps_total = []
        t = 0
        while t < num_iter:
            self.jeu.restart(Learn.coups_sautes)
            while (True):
                r = rd.random()
                if r < 0.5:
                    instr = 'q'
                else:
                    instr = 'd'
                if self.jeu.update_all(instr) == "Dead":
                    t += 1
                    temps_total.append(self.jeu.temps)
                    self.jeu.restart()
                    break
        self.image = self.get_image()
        print str(sum(temps_total) * 1. / num_iter) + " +/- " + str(
            0.96 * np.sqrt(np.var(np.array(temps_total)) / num_iter))

    def get_image(self):
        nx_im, ny = 2 * Learn.nx, Learn.ny
        tab = np.ones((nx_im, ny)) / 2.
        x, y = self.jeu.joueur.position
        x, y = self.jeu.joueur.position
        for elt in self.jeu.missiles:
            x_m, y_m = elt.position
            x_p = x_m - (x - 0.5)
            if (y_m < 0.5):
                tab[int(nx_im * x_p) % nx_im, int(ny * y_m)] = -1

        return tab[10:30, ::]

    def set_display(self, boolean):
        self.display = boolean
        self.jeu = MJ.Jeu(autorepeat=False, display=self.display)
        self.jeu.restart(Learn.coups_sautes)
        self.image = self.get_image()

    def save_rd_train_set(
        self,
        num_iter=5000
    ):  # returns a set of situations, choice sequences, and outcomes
        #self.jeu.rand_init(40)
        train_set = []
        for i in xrange(num_iter):
            self.jeu.restart(100)
            im = self.get_image()
            choice = self.possibilities[rd.randint(0, 2**Learn.n_coups - 1)]
            outcome = 1
            #print [b.position for b in self.jeu.missiles]
            for elt in choice:
                if (outcome == 1):
                    if elt:
                        instr = 'd'
                    else:
                        instr = 'q'
                    if (self.jeu.update_all(instr) == "Dead"):
                        outcome = 0
            train_set.append((im, choice, outcome))
        self.current_data_set = train_set
        return

    def intensive_train(self):
        for training in self.current_data_set:
            im, choice, outcome = training
            self.nn.fit(self.good_shape(im, choice), np.array([[outcome]]))
        print "Commence à sauver"
        pickle.dump(self.nn, open('nn.pkl', 'wb'))
        print "NN Saved"

    def error_on_train_set(self):
        error = 0.
        for training in self.current_data_set:
            im, choice, outcome = training
            s = self.nn.predict(self.good_shape(im, choice))
            error += abs(s[0][0] - outcome)
        error = error / len(train_set)
        return error

    def auc_on_train_set(self):
        real_outputs = []
        predicted_outputs = []
        for training in self.current_data_set:
            im, choice, outcome = training
            predicted_outputs.append(
                self.nn.predict(self.good_shape(im, choice))[0])
            real_outputs.append(outcome)
        fpr, tpr, thresholds = metrics.roc_curve(real_outputs,
                                                 predicted_outputs)
        return metrics.auc(fpr, tpr)
Example #40
0
n_output = 1

layer = Layer('Sigmoid', units=3)
layer2 = Layer('Sigmoid', units=1)
nn = Regressor([layer,layer2], n_iter=100)

#build x-train y train
x_train = np.zeros((len(time_series)-lag,lag))
y_train = np.zeros(len(time_series)-lag)

for i in range(len(time_series)-lag):
    x_train[i,:] = time_series[i:i+lag]
    y_train[i] = np.cos(i+lag)

#training
nn.fit(x_train, y_train)

#testing
x_test = np.zeros((len(test_series)-lag,lag))
y_test = np.zeros(len(test_series)-lag)
predictions = np.zeros(len(test_series)-lag)

for i in range(len(test_series)-lag):
    x_test[i,:] = test_series[i:i+lag]
    y_test[i] = test_series[i+lag]

predictions = nn.predict( x_test )

# RMSE Training error
rmse = mean_squared_error(y_test, predictions)**0.5
print rmse
Example #41
0
    learningRateAE = set[2]
    learningRateCL = set[3]
    nCyclesAE = set[4]
    nCyclesCL = set[5]
    outputFileNameAE = 'trained_fine/ae_' + str(hiddenUnitsAE) + '_' + str(
        learningRateAE) + '_' + str(nCyclesAE) + '.pkl'
    outputFileNameCL = 'trained_fine/cl_' + str(hiddenUnitsCL) + '_' + str(
        learningRateCL) + '_' + str(nCyclesCL) + '.pkl'

    # AUTOENCODER
    if (runAE == True):
        nn = Regressor(
            layers=[Layer("Rectifier", units=hiddenUnitsAE),
                    Layer("Linear")],
            learning_rate=learningRateAE,
            n_iter=nCyclesAE)
        nn.fit(X_train_background, Y_autoencoder)
        pickle.dump(nn, open(outputFileNameAE, 'wb'))

    # CLASSIFIER
    if (runCL == True):
        nn = Classifier(
            layers=[Layer("Rectifier", units=hiddenUnitsCL),
                    Layer("Softmax")],
            learning_rate=learningRateCL,
            n_iter=nCyclesCL)
        nn.fit(X_train, Y)
        pickle.dump(nn, open(outputFileNameCL, 'wb'))

    counter = counter + 1
def main():

    print "Loading Data..."
    data = readData("./data.csv") #, shuffle=shuffle_data, test=test)
    # Ymotor = np.squeeze(np.asarray([example[1][0] for example in data[0]]))
    # Ytotal= np.squeeze(np.asarray([example[1][1] for example in data[0]]))

    # X = SelectKBest(f_regression, k=9).fit_transform(X, target[:,1])
    # print  X.shape

    # print Ytotal
    # print Ymotor.shape
    # print Ytotal, "," , Ymotor
    # print X

    # degrees = [1, 2, 3]
    # for i in range(len(degrees)):


    Error= []
    MSE=[]
    Avg_score= []
    record = []

    # X = SelectKBest(f_regression, k=9).fit_transform(X, target[:,1])
    # # print  X.shape
    #
    # polynomial_features = PolynomialFeatures(degree=1,include_bias=True)
    # X= polynomial_features.fit_transform(X)
    # # X= np.c_[np.ones(len(X)),X] #concatente 2 columns vertically ;)
    # # print len(X[1])
    # # print  X[0,:]



    for layer in [[400,5]]:#,[400,5],[400,6],[400,7],[450,6],[450,7],[485,5],[485,6],[485,7],[500,6],[600,300],[800,6],[800,500],[1000,6],[1000,100],[1000,500]]:

        for lr in [0.0009]:
        # for layer in [[6],[10],[15],[20],[25],[30],[35],[40],[50],[60],[75],[100],[400],[600]]:

            for alpha in [0.9]:#[0.01, 0.1, 0.7, 0.8 , 0.9]:

                # for k in range(6,8):   #k=[5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
                # for n_folds in range(10,11):

                X = np.squeeze(np.asarray([example[0] for example in data])) #Total examples of 5875
                target= np.squeeze(np.asarray([example[1] for example in data]))

                polynomial_features = PolynomialFeatures(degree=2, include_bias=True)
                X= polynomial_features.fit_transform(X)
                # print len(X[1])
                # print  X[0,:]

                X = SelectKBest(f_regression, k=6).fit_transform(X, target[:,1])
                # print  X.shape



                n_folds =5

                kf = KFold(len(X[0]), n_folds)
                # print len(kf)
                # print(kf)
                for train_index, test_index in kf:
                    # print("TRAIN:", train_index, "TEST:", test_index)
                    train_X, valid_X = X[train_index], X[test_index]
                    train_target, valid_target = target[train_index], target[test_index]



                    # print 'Standardizing...'
                    scaler = preprocessing.StandardScaler().fit(train_X)  #fit only on training data (Compute the mean and std to be used for later scaling)
                    train_set = scaler.transform(train_X)  #Perform standardization by centering and scaling
                    valid_set = scaler.transform(valid_X) # apply same transformation to test data
                    # print train_X[:4]
                    # print train_set[:4]



                    #
                    # nn = Regressor(
                    # layers=[
                    #     # Layer("Sigmoid", units=1000),
                    #     Layer("Sigmoid", units=500),
                    #     Layer("Linear", units=6),
                    #     Layer("Linear", units=2)],
                    # learning_rule='sgd',
                    # regularize='L2',
                    # weight_decay=0.7,
                    # learning_rate=0.0009,
                    # batch_size=30,
                    # n_iter=10,
                    # loss_type='mse',)



                    nn = Regressor(
                        layers=[
                            Layer("Sigmoid", units=layer[0]),
                            # Layer("Sigmoid", units=600),
                            Layer("Linear", units=layer[1]),
                            Layer("Linear", units=2)],
                        learning_rule='sgd',
                        regularize='L2',
                        weight_decay= alpha,
                        learning_rate=lr,
                        batch_size=30,
                        n_iter=10,
                        loss_type='mse',)

                    nn.fit(train_set, train_target)
                    y_example = np.squeeze(np.asarray(nn.predict(valid_set)))
                    # print valid_target, y_example
                    Error.append(np.absolute(valid_target - y_example))
                    MSE.append(np.power(Error[-1], 2).mean(0))
                    # MSE= np.mean(MSE, axis=0)
                    # print 'Fold:',f, np.matrix(MSE).mean(0)
                y1= np.array([example[0] for example in MSE]).tolist()
                y2= np.array([example[1] for example in MSE]).tolist()

                Avg_score= np.matrix(zip(y1,y2)).min(0)

                #
                # record.append([lr,layer_size_list,err_list[best_fold]])
                # record.append([k,Avg_score])
                # print record
                print  " layer:", layer,","," lr:",lr,",","alpha:",alpha,",", np.around(Avg_score,decimals=3)
    predict_DF_RF_CV["AC_cons"], predict_DF_RF_CV["AC_ConsPred_RF_CV"])
mean_squared_error_DF_CV = mean_squared_error(
    predict_DF_RF_CV["AC_cons"], predict_DF_RF_CV["AC_ConsPred_RF_CV"])
coeff_variation_DF_CV = np.sqrt(
    mean_squared_error_DF_CV) / predict_DF_RF_CV["AC_cons"].mean()

from sknn.mlp import Regressor, Layer
reg_NN = Regressor(
    layers=[
        Layer("Rectifier", units=5),  # Hidden Layer1
        Layer("Rectifier", units=3),  # Hidden Layer2
        Layer("Linear")
    ],  # Output Layer
    n_iter=100,
    learning_rate=0.02)
reg_NN.fit(X_train_norm.as_matrix(), y_train_norm.as_matrix())
predict_DF_NN = reg_NN.predict(X_test_norm.as_matrix())

predict_DF_NN_CV = pd.DataFrame(predict_DF_NN,
                                index=y_test_norm.index,
                                columns=["AC_ConsPred_NN_CV"])
predict_DF_NN_CV = predict_DF_NN_CV.join(y_test_norm).dropna()
predict_DF_NN_CV['2014-08-01':'2014-08-20'].plot()

R2_score_DF_NN_CV = r2_score(predict_DF_NN_CV["AC_cons"],
                             predict_DF_NN_CV["AC_ConsPred_NN_CV"])
mean_absolute_error_DF_CV = mean_absolute_error(
    predict_DF_NN_CV["AC_cons"], predict_DF_NN_CV["AC_ConsPred_NN_CV"])
mean_squared_error_DF_CV = mean_squared_error(
    predict_DF_NN_CV["AC_cons"], predict_DF_NN_CV["AC_ConsPred_NN_CV"])
coeff_variation_DF_CV = np.sqrt(
Example #44
0
    outputs = []

    melee.listen(formattedReplay, lambda x, y: listener(x, y, inputs, outputs))

    # with open(sys.argv[2], 'w') as outfile:
    #     json.dump(inputs + outputs, outfile)
    nn = Regressor(
        layers=[
            # Layer("Sigmoid",units=100),
            Layer("Sigmoid", units=200),
            Layer("Linear")
        ],
        learning_rate=0.02,
        n_iter=80)

    inScaler = StandardScaler()
    npin = np.array(inputs)
    inScaler.fit(npin)
    npout = np.array(outputs)
    # print(insc)
    # for i in inScaler.transform(npin):
    #     print(i)
    nn.fit(inScaler.transform(npin), npout)
    pickle.dump((acceptedInputs, nn), open('nn4.pkl', 'wb'))
    # print(nn.predict(i for i in inputs[10]))
    # for i in inputs:
    #     print(nn.predict(np.array([i]))[0][9])
    # print(nn.predict(np.array([inputs[2]]))[0][0])
    print(len(inputs))
    print(len(outputs))
Example #45
0
#               pyplot.show()
    
                pre_label = bst.predict(xgbtest)
                loss = t-pre_label 
                MAPE = (sum(abs(loss)/t))/len(t)
                mapeSet.append(MAPE)
                para.append([inn,im,ie])
                count=count+1
                print('---> ',count,'......')
    
# scikit-neuralnetwork for regression(有问题,无法训练)
if 0:
    
    mlp = Regressor(layers=[Layer('Rectifier',units=100,weight_decay=0.0001,dropout=0.5),Layer('Linear')],learning_rule='sgd', learning_rate=0.01,
                            batch_size=500, n_iter=10,loss_type = 'mse')
    mlp.fit(X,y)
    pre_label = mlp.predict(T)
'''
# %%
# plot 
loss = t-pre_label # 误差
Z = np.zeros([len(loss)])
plt.plot(loss,'g')
plt.plot(Z,'r')
plt.xlabel('Number of the sample')
plt.ylabel('loss(s)')
plt.title('Visualizing loss')
plt.show()

# %%
# MAPE
Example #46
0
#============================Save pre-processed data===========================
data.to_csv('revised_data.csv', index=False)
data = pd.read_csv('revised_data.csv')
#==============================================================================


def calculate_RMSE(predicted, actual):
    return math.sqrt(mean_squared_error(actual, predicted))


#===========================Neural Network Fitting=============================
training_data = data.copy()
training_data.drop('duration', 1, inplace=True)
target_data = training_data.pop('size')

#cross validation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(
    training_data.values, target_data.values, test_size=0.1, random_state=42)

i = 0.1
neu_net_reg = Regressor(layers=[Layer("Sigmoid", units=30),
                                Layer("Linear")],
                        learning_rate=i,
                        n_iter=19)
neu_net_reg.fit(X_train, y_train)
predicted_target_data = neu_net_reg.predict(X_test)
print 'Learning rate: ' + str(i) + '   RMSE is: ' + str(
    calculate_RMSE(y_test, predicted_target_data))

#==============================================================================
Example #47
0
class Learn:
    
    nx = 20
    ny = 20
    n_cell = nx * ny
    coups_sautes = 60
    
    #This calculates the distance between two images
    def dist(self, prediction, im3):
        err = 0.
        for i in xrange(len(prediction)):
            err += abs(prediction[i] - im3[i])
        return err / len(prediction)

    def good_shape_2(self, im1, im2):#Good shape for the input (two images)
        tab = np.zeros((1, (Learn.n_cell*2)))
        tab[0, ::] = np.append(im1.flatten(), im2.flatten())
        return tab
    
    def good_shape_1(self, im3):#Good shape for the output (1 image)
        tab = np.zeros((1, (Learn.n_cell)))
        tab[0, ::] = im3.flatten()
        return tab

    def __init__(self, new=False, display=False):
        #       self.possibilities = generate(Learn.n_coups)
#        np.random.shuffle(self.possibilities)
        self.jeu = MJ.Jeu(autorepeat=False, display=display)
        self.jeu.restart(Learn.coups_sautes)
        self.previous_image = self.get_image()
        self.jeu.update_all()
        self.current_image=self.get_image()
        if new:
            self.nn = Regressor(layers=[Layer("Linear", units=(Learn.n_cell*2)), Layer("Linear", units=Learn.n_cell*4), Layer("Linear",units=Learn.n_cell)], learning_rate=0.01, n_iter=1)
            self.nn.fit(self.good_shape_2(self.previous_image, self.current_image), self.good_shape_1(self.current_image))
        else:
            self.nn = pickle.load(open('nn_image_prediction.pkl', 'rb'))
            self.nn.fit(self.good_shape_2(self.previous_image, self.current_image), self.good_shape_1(self.current_image))
        self.current_data_set = []
    

    def play(self, num_iter=1000):
        self.set_display(True)
        predicted_outcome = np.zeros(2**Learn.n_coups)
        for s in xrange(num_iter):
            self.image = self.get_image()
            if (s%100 == 0):
                print s
            outcome = 1
            indice_max=0
            for j, elt in enumerate(self.possibilities):
                a = self.nn.predict(self.good_shape(self.image, elt))[0][0]
                predicted_outcome[j] = a
                if (a>predicted_outcome[indice_max]):
                    indice_max=j
            i=indice_max
            elt = self.possibilities[i][0]
            if (outcome == 1):
                if elt == 1:
                    instr = 'd'
                elif elt == 0:
                    instr = 'q'
                if (self.jeu.update_all(instr) == "Dead"):
                    outcome = 0
                    self.jeu.restart(Learn.coups_sautes)   



    def get_image(self):
        nx_im, ny = 2 * Learn.nx, Learn.ny
        tab = np.ones((nx_im, ny))/2.
        x, y = self.jeu.joueur.position
        x,y=self.jeu.joueur.position
        for elt in self.jeu.missiles:
            x_m, y_m = elt.position
            x_p = x_m - (x - 0.5) 
            if (y_m < 0.5):
                tab[int(nx_im * x_p) % nx_im, int(ny * y_m)] = -1

        return tab[10:30, ::]

    def save_rd_train_set(self, num_iter=5000): # returns a set of situations, choice sequences, and outcomes
        train_set = []
        for i in xrange(num_iter):
            self.jeu.restart(100)
            im1 = self.get_image().flatten()
            self.jeu.update_all()
            im2=self.get_image().flatten()
            self.jeu.update_all()
            im3=self.get_image().flatten()
            train_set.append((im1, im2, im3))
        self.current_data_set = train_set
        return
        
    def intensive_train(self): 
        for training in self.current_data_set:
            im1, im2, im3 = training
            self.nn.fit(self.good_shape_2(im1, im2), self.good_shape_1(im3))
        print "Commence à sauver"
        pickle.dump(self.nn, open('nn_image_prediction.pkl', 'wb'))
        print "NN Saved"
            
    def error_on_train_set(self):
        error = 0.
        for training in self.current_data_set:
            im1, im2, im3=training
            s = self.nn.predict(self.good_shape_2(im1, im2))[0]
            if (rd.random()<0.01):
                print "Differences : "
                print np.max(abs(s-im3))
            error += self.dist(s,im3)
        error = error / len(self.current_data_set)
        return error
    learningRateAE = set[2]
    learningRateCL = set[3]
    nCyclesAE = set[4]
    nCyclesCL = set[5]
    outputFileNameAE = 'trained_fine/ae_'+str(hiddenUnitsAE)+'_'+str(learningRateAE)+'_'+str(nCyclesAE)+'.pkl'
    outputFileNameCL = 'trained_fine/cl_'+str(hiddenUnitsCL)+'_'+str(learningRateCL)+'_'+str(nCyclesCL)+'.pkl'
    
    # AUTOENCODER
    if (runAE==True):
        nn = Regressor(
                       layers=[
                               Layer("Rectifier", units=hiddenUnitsAE),
                               Layer("Linear")],
                       learning_rate=learningRateAE,
                       n_iter=nCyclesAE)
        nn.fit(X_train_background,Y_autoencoder)
        pickle.dump(nn, open(outputFileNameAE, 'wb'))

    # CLASSIFIER
    if (runCL==True):
        nn = Classifier(
                        layers=[
                                Layer("Rectifier", units=hiddenUnitsCL),
                                Layer("Softmax")],
                        learning_rate=learningRateCL,
                        n_iter=nCyclesCL)
        nn.fit(X_train,Y)
        pickle.dump(nn, open(outputFileNameCL, 'wb'))

    counter = counter+1
Example #49
0
X_forward_train = np.append(state_one_train,motor_commands_train,axis = 1)
Y_forward_train = state_two_train
X_forward_test = np.append(state_one_test,motor_commands_test,axis = 1)
Y_forward_test = state_two_test

# <headingcell level=3>

# Train MLP on recorded data

# <codecell>

from sknn.mlp import Regressor, Layer

# Do some MLP black magic
inverse = Regressor(layers=[Layer("Sigmoid", units=4), Layer("Linear",units=2)],learning_rate=0.02)
inverse.fit(X_inverse_train,Y_inverse_train)

# <codecell>

# Save model for later use
from sklearn.externals import joblib
joblib.dump(inverse, 'neural_network_inverse_model.pkl')

# <headingcell level=1>

# Test Model

# <headingcell level=3>

# Load and standardize recorded data
Example #50
0
    return data_x, data_y
err_mse=[]  
for dim in 6*10**np.arange(1,6):    
    [train_set, valid_set,test_set]=rand_images(dim)    
    test_set_x, test_set_y = shape(test_set)
    valid_set_x, valid_set_y = shape(valid_set)
    train_set_x, train_set_y = shape(train_set)
    sz=np.shape(train_set_x)
    nn = Regressor(
        layers=[
            Layer("Tanh", units=sz[1]),
            Layer("Linear")],
            learning_rate=0.02,
            n_iter=10,
            batch_size=20)
    nn.fit(train_set_x, train_set_y)
    y_pred=nn.predict(test_set_x)
    # compute mse error
    print '.... computing error of prediction for the dataset size', str(dim) 
    rel_error=[]
    I=0
    for x in test_set_y:
        if x!=0:
            rel_error.append(np.abs(y_pred[I]-x)/np.abs(x))
        else:
            rel_error.append(np.abs(y_pred[I]-x))
        I=I+1    
    err_mse.append(np.mean(rel_error))  
    #err_mse.append(np.mean((y_pred-test_set_y)**2))
    print err_mse
f = gzip.open('mlp_errors.pkl.gz','wb')
Example #51
0
class Learn:
    
    nx = 20
    ny = 20
    n_cell = nx * ny
    n_coups = 8
    coups_sautes = 60

    def __init__(self, new=False, display=False):
        self.possibilities = generate(Learn.n_coups)
        np.random.shuffle(self.possibilities)
        self.explore = 0.
        self.jeu = MJ.Jeu(autorepeat=False, display=display)
        self.jeu.restart(Learn.coups_sautes)
        self.image = self.get_image()
        if new:
            self.nn = Regressor(layers=[Layer("Linear", units=(Learn.n_cell+Learn.n_coups)), Layer("Sigmoid", units=1000), Layer("Sigmoid")], learning_rate=0.01, n_iter=1)
            self.nn.fit(self.good_shape(self.image, self.possibilities[Learn.n_coups/2 - 1]), np.array([[0]]))
        else:
            self.nn = pickle.load(open('nn.pkl', 'rb'))
            self.nn.fit(self.good_shape(self.image, self.possibilities[Learn.n_coups/2 - 1]), np.array([[1]]))
        self.current_data_set = []

    def good_shape(self, image, instructions):#instructions est un tableau de 0 et 1 à n_coups éléments
        tab = np.zeros((1, (Learn.n_cell + Learn.n_coups)))
        tab[0, ::] = np.append(image.flatten(), instructions)
        return 10 * tab

    def play(self, num_iter=1000):
        self.set_display(True)
        predicted_outcome = np.zeros(2**Learn.n_coups)
        for s in xrange(num_iter):
            self.image = self.get_image()
            if (s%100 == 0):
                print s
            outcome = 1
            indice_max=0
            for j, elt in enumerate(self.possibilities):
                a = self.nn.predict(self.good_shape(self.image, elt))[0][0]
                predicted_outcome[j] = a
                if a>0.99:
                    i=j
                    break
                elif (a>predicted_outcome[indice_max]):
                    indice_max=j
            i=indice_max
            elt = self.possibilities[i][0]
            if (outcome == 1):
                if elt == 1:
                    instr = 'd'
                elif elt == 0:
                    instr = 'q'
                if (self.jeu.update_all(instr) == "Dead"):
                    outcome = 0
                    self.jeu.restart(Learn.coups_sautes)   
  
        
    def auc(self, num_iter=10000):
        real_outputs = []
        predicted_outputs = []
        for s in xrange(num_iter):
            outcome = 1
            poss = self.possibilities
            i = rd.randint(0, len(poss)-1)
            elt = poss[i]
            predicted_outputs.append(self.nn.predict(self.good_shape(self.image, elt))[0])
            r = rd.random()
            if (r < 0.8):
                i = int(rd.random() * 2**(Learn.n_coups))
            for elt in self.possibilities[i]:
                if (outcome == 1):
                    if elt:
                        instr = 'd'
                    else:
                        instr = 'q'
                    if (self.jeu.update_all(instr) == "Dead"):
                        outcome = 0
                        self.jeu.restart(Learn.coups_sautes)
            real_outputs.append(outcome)
            self.image = self.get_image()        
        fpr, tpr, thresholds = metrics.roc_curve(real_outputs, predicted_outputs)
        return metrics.auc(fpr, tpr)
    
    
    def benchmark(self, num_iter=1000):
        temps_total = []
        t = 0
        while t < num_iter:
            self.jeu.restart(Learn.coups_sautes)
            while(True):
                r = rd.random()
                if r < 0.5:
                    instr = 'q'
                else:
                    instr = 'd'
                if self.jeu.update_all(instr) == "Dead":
                    t += 1
                    temps_total.append(self.jeu.temps)
                    self.jeu.restart()
                    break
        self.image = self.get_image()
        print str(sum(temps_total)*1. / num_iter) +" +/- "+ str(0.96 * np.sqrt(np.var(np.array(temps_total)) / num_iter)  )


    def get_image(self):
        nx_im, ny = 2 * Learn.nx, Learn.ny
        tab = np.ones((nx_im, ny))/2.
        x, y = self.jeu.joueur.position
        x,y=self.jeu.joueur.position
        for elt in self.jeu.missiles:
            x_m, y_m = elt.position
            x_p = x_m - (x - 0.5) 
            if (y_m < 0.5):
                tab[int(nx_im * x_p) % nx_im, int(ny * y_m)] = -1

        return tab[10:30, ::]

    def set_display(self, boolean):
        self.display = boolean
        self.jeu = MJ.Jeu(autorepeat=False, display=self.display)
        self.jeu.restart(Learn.coups_sautes)
        self.image = self.get_image()

    def save_rd_train_set(self, num_iter=5000): # returns a set of situations, choice sequences, and outcomes
        #self.jeu.rand_init(40)
        train_set = []
        for i in xrange(num_iter):
            self.jeu.restart(100)
            im = self.get_image()
            choice = self.possibilities[rd.randint(0, 2**Learn.n_coups-1)]
            outcome = 1
            #print [b.position for b in self.jeu.missiles]
            for elt in choice:
                if (outcome == 1):
                    if elt:
                        instr = 'd'
                    else:
                        instr = 'q'
                    if (self.jeu.update_all(instr) == "Dead"):
                        outcome = 0
            train_set.append((im, choice, outcome))
        self.current_data_set = train_set
        return
        
    def intensive_train(self): 
        for training in self.current_data_set:
            im, choice, outcome = training
            self.nn.fit(self.good_shape(im, choice), np.array([[outcome]]))
        print "Commence à sauver"
        pickle.dump(self.nn, open('nn.pkl', 'wb'))
        print "NN Saved"
            
    def error_on_train_set(self):
        error = 0.
        for training in self.current_data_set:
            im, choice, outcome=training
            s = self.nn.predict(self.good_shape(im,choice))
            error += abs(s[0][0]-outcome)
        error = error / len(train_set)
        return error
        
    def auc_on_train_set(self):
        real_outputs = []    
        predicted_outputs = []
        for training in self.current_data_set:
            im, choice, outcome = training
            predicted_outputs.append(self.nn.predict(self.good_shape(im, choice))[0])
            real_outputs.append(outcome)
        fpr, tpr, thresholds = metrics.roc_curve(real_outputs, predicted_outputs)
        return metrics.auc(fpr, tpr)
class DeepNeuralNetwork(object):

    def __init__(self, params=None, seq_pre_processor=None): 	              
                 
        self.scale = StandardScaler()                          
        self.pre_processor = seq_pre_processor
        self.params = params      
        
        if params != None:
            # Initialize the network
            self.net = Regressor(layers=params['layers'], learning_rate=params['learning_rate'], n_iter=params['n_iter'], dropout_rate=params['dropout_rate'],
                                     batch_size=params['batch_size'], regularize=params['regularize'], valid_size=params['valid_size'])

            # Initialize the vectorizer
            self.vectorizer = graph.Vectorizer(r=params['radius'], d=params['d_seq'], min_r=params['min_r'], normalization=params['normalization'], 
                                                    inner_normalization=params['inner_normalization'], nbits=params['nbits_seq'])  
                                 

    # Save the neural network object to the model_name path
    def save(self, model_name=None):        

        joblib.dump(self, model_name , compress=1)

    # Loads the neural network object from its respective path
    def load(self, model_name=None):

        self.__dict__.update(joblib.load(model_name).__dict__)  

    # Converts sequences to matrix
    def seq_to_data_matrix(self, sequences=None):               
                
        # Transform sequences to matrix
        graphs = mp_pre_process(sequences, pre_processor=self.pre_processor, pre_processor_args={}, n_jobs=-1)	        
                      
        seq_data_matrix = vectorize(graphs, vectorizer=self.vectorizer, n_jobs=-1)                

        # Densify the matrix
        seq_data_matrx = seq_data_matrix.toarray()

        # Standardize the matrix
        self.scale.fit(seq_data_matrx)
        std_seq_data_matrx = self.scale.transform(seq_data_matrx)        

        return std_seq_data_matrx

    # Training the network using traing sequences and the train structure matrix
    def fit(self, sequences=None, X_struct_std_train=None):

        # Convert sequences to data matrix
        X_seq_std_train = self.seq_to_data_matrix(sequences)    

        # Train the network
        self.net.fit(X_seq_std_train, X_struct_std_train)        

    # Predict the structure data matrix using testing sequences
    def predict(self, sequences=None):        

        # Convert sequences to data matrix
        X_seq_std_test = self.seq_to_data_matrix(sequences)
        
        # Predict the output matrix
        pred_data_matrix_out = self.net.predict(X_seq_std_test)             

        return pred_data_matrix_out                     

    # Function to train the network and predict the testing data
    def fit_predict(self, sequences_train=None, sequences_test=None, struct_matrix_train=None):                                               

        # Training the network using training sequences and the train structure matrix
        self.fit(sequences_train, struct_matrix_train)

        #Transform seq features to struct features using testing sequences
        return self.predict(sequences_test)                        
Y_train = X_train

# NEURAL NETWORK TRAINING AND TESTING
# Set up neural network
if runTraining:
    print "Starting neural network training"
    nn = Regressor(
        layers=[Layer("Rectifier", units=30),
                Layer("Linear")],
        learning_rate=0.01,
        batch_size=100,
        #learning_rule = "momentum",
        n_iter=2000,
        valid_size=0.25)
    # Training
    nn.fit(X_train, Y_train)
    pickle.dump(nn, open('autoencoder.pkl', 'wb'))
if not runTraining:
    nn = pickle.load(open('autoencoder.pkl', 'rb'))

# Testing
predicted_same = nn.predict(X_train)
predicted_diff = nn.predict(X_test)
predicted_signal = nn.predict(X_signal)

# Reconstruction error
rec_errors_same = reconstructionError(X_train, predicted_same)
rec_errors_diff = reconstructionError(X_test, predicted_diff)
rec_errors_sig = reconstructionError(X_signal, predicted_signal)

# Reconstruction errors by variable
# np.save("data_x_combined", data_x)
# np.save("data_y_combined", data_y)

data_x = np.load("data_x_combined.npy")[100:]
data_y = np.load("data_y_combined.npy")[100:]
print data_y
nn = Regressor(
    layers=[
        Layer("Sigmoid", units=512),
        Layer("Sigmoid")],
    learning_rate=0.0001,
    n_iter=40
    )
print("Generating Fit")
nn.fit(data_x, data_y)
print("Fit generated")
fs = open('nn_combined_reg.pkl', 'wb')
pickle.dump(nn, fs)
fs = open('nn_combined_reg.pkl', 'rb')
nn = pickle.load(fs)
n = 2590
for x in nn.predict(data_x[n:n+2000]):
    if np.count_nonzero(x):
        print 'nonzero', x
# print()
# print(data_y[n:n+2000])
# nn.score(data_x, data_y)
# fs.close()
# print("NN Pickled")
# pickle.save()
Example #55
0
import numpy
import time
from sklearn import preprocessing
from sklearn import metrics
from sknn.mlp import Classifier, Regressor, Layer
training_data = "FullModemConfigSpace-2015-07-19.csv"
training = numpy.loadtxt(open(training_data), delimiter=",", skiprows=1)
testing_data = "path to dataset used for testing here"
testing = numpy.loadtxt(
    open(testing_data),
    delimiter=
    "space, comma, semicolon, whatever separates the attributes in the samples"
)
"""skiprows=1 if the attribute names are at the top"""
tr_x = dataset[:, 0:38]
tr_y = training[:, 39:43]
ts_x = testing[:, 0:38]
ts_y = testing[:, 39:43]
network = Regressor(layers=[
    Layer("Linear", units=39),
    Layer("Sigmoid", units=22),
    Layer("Linear", units=4)
],
                    learning_rate=0.001,
                    n_iter=25)
network.fit(tr_x, tr_y)
cont_prediction = cont_network.predict(ts_x)
"""This will write the predictions to an output file, I'm pretty sure you can change the extension as you like."""
with open("full_prediction.txt", "w") as output:
    for y in cont_prediction:
        print(y, file=output)
Example #56
0
    layers = []
    for _ in range(0, parameters["hidden_layers"]):
        layers.append(
            Layer(parameters["hidden_type"],
                  units=parameters["hidden_neurons"]))
    layers.append(Layer("Linear"))
    model = Regressor(layers=layers,
                      learning_rate=parameters["learning_rate"],
                      n_iter=parameters["iteration"],
                      random_state=42)

    X = np.array(trainX)
    y = np.array(trainY)

    model.fit(X, y)

    model.fit(trainX, trainY)
    prediction = model.predict(testX)
    prediction = normalizer_Y.inverse_transform(prediction)
    testY = normalizer_Y.inverse_transform(testY)

    for i in range(0, len(testY)):
        output.write(str(location))
        output.write(",")
        output.write(str(testY[i]))
        output.write(",")
        output.write(str(prediction[i][0]))
        output.write("\n")

output.close()
Example #57
0
nn = Regressor([hiddenLayer, outputLayer],
               learning_rule='sgd',
               learning_rate=.001,
               batch_size=5,
               loss_type="mse")


# Generate Data
def cubic(x):
    return x**3 + x**2 - x - 1


def get_cubic_data(start, end, step_size):
    X = np.arange(start, end, step_size)
    X.shape = (len(X), 1)
    y = np.array([cubic(X[i]) for i in range(len(X))])
    y.shape = (len(y), 1)
    return X, y


# Train Model
X, y = get_cubic_data(-2, 2, .1)
nn.fit(X, y)

# Predict
predictions = nn.predict(X)

# Visualize
plt.plot(predictions)
plt.plot(y)
plt.savefig("approximate2.png")