def __init__(self, no_brain=False):
        self.movementInfo = globals()['movementInfo']
        self.score = self.playerIndex = self.loopIter = self.time_travelled = self.fitness_score = self.crash_score = 0

        # player velocity, max velocity, downward accleration, accleration on flap
        self.playerVelY = -9  # player's velocity along Y, default same as playerFlapped
        self.playerMaxVelY = 10  # max vel along Y, max descend speed
        self.playerMinVelY = -8  # min vel along Y, max ascend speed
        self.playerAccY = 1  # players downward accleration
        self.playerRot = 45  # player's rotation
        self.playerVelRot = 3  # angular speed
        self.playerRotThr = 20  # rotation threshold
        self.playerFlapAcc = -9  # players speed on flapping
        self.playerFlapped = False  # True when player flaps

        self.playerIndexGen = self.movementInfo['playerIndexGen']
        self.playerx, self.playery = int(SCREENWIDTH * 0.2), int(
            self.movementInfo['playery'])
        empty_inputs = [0] * 4

        nn = None

        if not no_brain:
            initial_input = [
                self.playerx,
                self.playery,
                # self.playerVelY
            ]
            initial_input.extend(empty_inputs)

            # MLPClassifier NN
            if nn_implementation == 'sklearn':
                nn = MLPClassifier(hidden_layer_sizes=(len(initial_input), 4,
                                                       6),
                                   shuffle=True)
                nn.fit(np.asarray(initial_input).reshape(1, -1), [True])

            # Keras NN
            elif nn_implementation == 'keras':
                nn = Sequential()
                nn.add(
                    Dense(len(initial_input),
                          input_dim=len(initial_input),
                          activation='relu'))
                nn.add(Dense(4, activation='relu'))
                nn.add(Dense(6, activation='relu'))
                nn.add(Dense(1, activation='sigmoid'))
                nn.compile(loss="binary_crossentropy",
                           optimizer="adam",
                           metrics=["accuracy"])
                nn.fit(np.asarray(initial_input).reshape(1, -1), [True],
                       verbose=0,
                       shuffle=True)

        self.brain = nn
def get_predictive_model(x_train,
                         y_train,
                         num_layers=2,
                         num_hidden=200,
                         activation='relu',
                         out_activation='sigmoid',
                         optimizer="adam",
                         loss="binary_crossentropy",
                         dropout_rate=0.5,
                         model_type="keras"):

    # create a keras model

    if model_type == "keras":

        model = keras.models.Sequential()

    elif model_type == "modified_keras":

        model = pred_model()

    elif model_type == "sklearn":
        model = MLPClassifier(hidden_layer_sizes=(
            200,
            200,
        ))

    if (model_type == "keras") or (model_type == "modified_keras"):

        # Initiate the input layer

        model.add(
            keras.layers.normalization.BatchNormalization(
                input_shape=tuple([x_train.shape[1]])))
        model.add(keras.layers.core.Dense(num_hidden, activation=activation))
        model.add(keras.layers.core.Dropout(rate=dropout_rate))

        # Add all intermediate layers

        for _ in range(num_layers - 1):

            model.add(keras.layers.normalization.BatchNormalization())
            model.add(
                keras.layers.core.Dense(num_hidden, activation=activation))
            model.add(keras.layers.core.Dropout(rate=dropout_rate))

        # Add output layer

        model.add(keras.layers.core.Dense(1, activation=out_activation))

        model.compile(loss=loss, optimizer=optimizer, metrics=["accuracy"])
        #print(model.summary())

        # Use Early-Stopping
        callback_early_stopping = keras.callbacks.EarlyStopping(
            monitor='val_loss', patience=10, verbose=0, mode='auto')

        # Train model
        model.fit(x_train,
                  y_train,
                  batch_size=1024,
                  epochs=200,
                  verbose=0,
                  callbacks=[callback_early_stopping])

    else:

        model.fit(x_train, y_train)

    return model
    def fitness_function(self, individual, data):
        if self.state.model == 'Artificial Neural Network':
            print('Next Individual')
            model = MLPClassifier(
                hidden_layer_sizes=data[0][individual[0]],
                activation='relu',
                solver='adam',
                alpha=data[1][individual[1]],
                batch_size=data[2][individual[2]],
                learning_rate='constant',
                learning_rate_init=data[3][individual[3]],
                power_t=0.5,
                max_iter=30,
                shuffle=True,
                random_state=1,
                tol=0.0001,
                verbose=False,
                warm_start=False,
                momentum=0.9,
                nesterovs_momentum=True,
                early_stopping=False,
                validation_fraction=0.18,  # 0.33 0.18
                beta_1=0.9,
                beta_2=0.999,
                epsilon=1e-08,
                n_iter_no_change=data[4][individual[4]],
                max_fun=15000)

            model.fit(self.X_train, self.y_train)
            prediction = model.predict(self.X_test)
            score = accuracy_score(self.y_test, prediction)

        if self.state.model == "Support Vector Machine":
            print('Next Individual')
            model = sklearn.svm.SVC(
                C=data[0][individual[0]],
                kernel=data[1][individual[1]],
                degree=data[2][individual[2]],
                gamma=data[3][individual[3]],
                coef0=0.0,
                shrinking=data[4][individual[4]],
                probability=data[5][individual[5]],
                tol=0.001,
                cache_size=200,
                class_weight=None,
                verbose=True,
                max_iter=-1,
                decision_function_shape=data[6][individual[6]],
                break_ties=False,
                random_state=None)
            model.fit(self.X_train, self.y_train)
            prediction = model.predict(self.X_test)
            score = accuracy_score(self.y_test, prediction)

        if self.state.model == 'Convolutional Neural Network':
            print('Next Individual')
            model = Sequential()
            model.add(
                Conv1D(filters=data[0][individual[0]],
                       kernel_size=data[4][individual[4]],
                       activation='relu',
                       input_shape=self.input_shape))
            model.add(
                Conv1D(filters=data[0][individual[0]],
                       kernel_size=data[4][individual[4]],
                       activation='relu',
                       input_shape=self.input_shape))
            model.add(BatchNormalization())
            model.add(MaxPooling1D(pool_size=2))
            model.add(
                Conv1D(filters=data[1][individual[1]],
                       kernel_size=data[5][individual[5]],
                       activation='relu'))
            model.add(
                Conv1D(filters=data[1][individual[1]],
                       kernel_size=data[5][individual[5]],
                       activation='relu'))
            model.add(BatchNormalization())
            model.add(MaxPooling1D(pool_size=2))
            model.add(
                Conv1D(filters=data[2][individual[2]],
                       kernel_size=data[6][individual[6]],
                       activation='relu'))
            model.add(
                Conv1D(filters=data[2][individual[2]],
                       kernel_size=data[6][individual[6]],
                       activation='relu'))
            model.add(BatchNormalization())
            model.add(MaxPooling1D(pool_size=2))
            model.add(
                Conv1D(filters=data[3][individual[3]],
                       kernel_size=data[7][individual[7]],
                       activation='relu'))
            model.add(
                Conv1D(filters=data[3][individual[3]],
                       kernel_size=data[7][individual[7]],
                       activation='relu'))
            model.add(BatchNormalization())
            model.add(MaxPooling1D(pool_size=2))
            model.add(Dropout(0.25))
            model.add(Flatten())
            model.add(Dense(64, activation='relu'))
            model.add(BatchNormalization())
            model.add(Dropout(0.25))
            model.add(Dense(32, activation='relu'))
            model.add(BatchNormalization())
            model.add(Dropout(0.25))
            model.add(Dense(16, activation='relu'))
            model.add(BatchNormalization())
            model.add(Dropout(0.25))
            model.add(Dense(self.number_of_classes, activation='softmax'))

            model.compile(loss=self.loss,
                          optimizer=tensorflow.keras.optimizers.Adadelta(),
                          metrics=self.metrics)

            model.fit(self.X_train,
                      np.array(self.y_train),
                      batch_size=128,
                      epochs=25,
                      verbose=1,
                      validation_data=(self.xx_val, np.array(self.yy_val)))

            score = model.evaluate(self.xx_test,
                                   np.array(self.yy_test),
                                   verbose=1)[1]

        if self.state.model == 'Recurrent Neural Network':
            print('Next Individual')
            model = Sequential()
            model.add(
                LSTM(data[0][individual[0]],
                     input_shape=self.input_shape,
                     return_sequences=False))
            model.add(Dropout(0.25))

            model.add(Dense(self.number_of_classes, activation='softmax'))

            model.compile(loss=self.loss,
                          optimizer='adam',
                          metrics=self.metrics)

            model.fit(self.X_train,
                      np.array(self.y_train),
                      batch_size=128,
                      epochs=20,
                      verbose=1,
                      validation_data=(self.xx_val, np.array(self.yy_val)))

            score = model.evaluate(self.xx_test,
                                   np.array(self.yy_test),
                                   verbose=1)[1]

        return score
def _obesity():
    df = pd.read_csv('data/ObesityDataSet_raw_and_data_sinthetic.csv')

    # Handle Categorical features
    clfs = {
        c: preprocessing.LabelEncoder()
        for c in [
            'Gender', 'family_history_with_overweight', 'FAVC', 'CAEC',
            'SMOKE', 'SCC', 'CALC', 'MTRANS', 'NObeyesdad'
        ]
    }

    for col, clf in clfs.items():
        df[col] = clfs[col].fit_transform(df[col])

    #
    # Need to split data into train/test, and transform (normalize) values to be between [0, 1]
    #

    X = df.drop(columns='NObeyesdad')
    y = df['NObeyesdad'].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

    scaler = preprocessing.StandardScaler().fit(X_train)
    X_train = scaler.transform(X_train)

    scaler = preprocessing.StandardScaler().fit(X_test)
    X_test = scaler.transform(X_test)

    model = nn_tuning(X_train, y_train, X_test, y_test,
                      'Neural Network Accuracy Normalized Data')

    model = MLPClassifier(hidden_layer_sizes=(16, 2),
                          random_state=0,
                          max_iter=10000,
                          warm_start=True,
                          activation='relu',
                          solver='sgd')
    model.fit(X_train, y_train)

    y_train_bin = preprocessing.label_binarize(y_train,
                                               classes=[0, 1, 2, 3, 4, 5, 6])
    y_test_bin = preprocessing.label_binarize(y_test,
                                              classes=[0, 1, 2, 3, 4, 5, 6])
    y_pred = model.predict(X_test)

    y_pred = preprocessing.label_binarize(y_pred,
                                          classes=[0, 1, 2, 3, 4, 5, 6])

    n_classes = 7
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    for i in range(n_classes):
        fpr[i], tpr[i], _ = metrics.roc_curve(y_test_bin[:, i], y_pred[:, i])
        roc_auc[i] = metrics.auc(fpr[i], tpr[i])
    for i in range(n_classes):
        plt.plot(fpr[i],
                 tpr[i],
                 label='ROC curve of class {0} (area = {1:0.2f})'
                 ''.format(i, roc_auc[i]))
    plt.legend()
    plt.plot([0, 1], [0, 1], 'k--')
    plt.xlim([-0.05, 1.0])
    plt.title('AUROC of Neural Network on Obesity Test Data')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')

    np.mean(list(roc_auc.values()))

    fig, axes = plt.subplots(2, 1, figsize=(8, 10))
    plot_learning_curve(MLPClassifier(hidden_layer_sizes=(16, 2),
                                      random_state=0,
                                      max_iter=10000,
                                      warm_start=True,
                                      activation='relu',
                                      solver='sgd'),
                        'Obesity Dataset Learning Curve (cv=5)',
                        X_train,
                        y_train,
                        axes=axes[0],
                        ylim=(0.0, 1.1),
                        cv=5,
                        n_jobs=4)
    plot_learning_curve(MLPClassifier(hidden_layer_sizes=(16, 2),
                                      random_state=0,
                                      max_iter=10000,
                                      warm_start=True,
                                      activation='relu',
                                      solver='sgd'),
                        'Obesity Dataset Learning Curve (cv=10)',
                        X_train,
                        y_train,
                        axes=axes[1],
                        ylim=(0.0, 1.1),
                        cv=10,
                        n_jobs=4)

    input_shape = len(X.columns)
    model = keras.Sequential()
    model.add(Dense(input_shape, activation='relu'))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(7, activation='relu'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer='Adam')
    history = model.fit(X_train,
                        y_train,
                        validation_data=(X_test, y_test),
                        epochs=100,
                        verbose=0)

    x = np.arange(100)
    plt.xlabel('epoch')
    plt.title('Neural Network Train/Validate Loss Function of Obesity Data')
    plt.plot(x, history.history['loss'], label='loss')
    plt.plot(x, history.history['val_loss'], label='validation loss')
    plt.legend()
    plt.show()
예제 #5
0
def make_network(FILENAME,sklearn=False,keras=False,normalize=True,spread=False,moneyline=False,tpot = False):
    from pandas import read_csv,get_dummies
    import numpy as np
    from sklearn import cross_validation
    from sklearn.neural_network import MLPClassifier
    
    
    """
    Given the csv input of all the box scores, arrange it such that the home and away teams are lined up, 
    unnecessary columns removed, and hot encoding is done. Other stuff too probably. Such as normalization, but I 
    didn't do that!
    
    Note that this data has already been doctored from its original form, taking out most unnecessary columns but
    those could be useful later on.
    
    
    Parameters
    ----------
    FILENAME : file
        The csv of the data.
        
    sklearn : bool
        True or false designation for if you want the MLP to be based on an sklearn version 
    keras : bool
        True or false designation for if you want the MLP to be more manually designed using Keras. 

    normalize : bool
        True or false designation for if you want to set all relevant inputs onto the same scale. 
        
    spread : bool
        True or false designation for if you want to predict the spread. 

    
    moneyline : bool 
        True or false designation for if you want to predict the outright winner. 

    
    
        
    Returns
    -------
    
    
    model : NN
        The neural network fitted on the training and validation dataset so it can be applied to future data. 
    
    scaler : 
        The scale used to normalize the training data in order to impose this onto the prediction data. 
        
    """
    
    #Read in file, remove attempted and # and only account for % since that's more predictive in nature. 
    #*retrospectively that doesn't make sense, could be worth changing!
    data = read_csv(FILENAME) 
    data['3P%'] = np.divide(data['3P'].values,data['3PA'].values) 
    del data['3P'],data['3PA']
    data['FG%'] = np.divide(data['FG'].values,data['FGA'].values)
    del data['FG'],data['FGA']
    data['FT%'] = np.divide(data['FT'].values,data['FTA'].values)
    del data['Unnamed: 0'],data['GAME_ID'],data['Date'],data['Home'],data['Away'],data['PLUS_MINUS'],data['TOTAL']
    del data['FT'],data['FTA']
    data = get_dummies(data)

    #print(data)
    

    dat = []
    
    #reshape the dataset so now each colummn has roadstats and homestats concatenated into the same row, used for NN 
    
    for i in range(len(data.values)):
        data.values[i] = np.reshape(data.values[i],newshape=[1,len(data.values[i])])
    for p in range(int(len(data.values)/2)):
        fullboxgame = np.concatenate((data.values[2*p],data.values[(2*p)+1]))
        dat.append(fullboxgame)
    
    #convert list to array, now possible to array operations previously not possible
    dat = np.array(dat)   
    
    openingspreadS = dat[:,8] #what the predicted spread of ther game was. 
    roadpts = dat[:,7]       #column of all the points scored by road team 
    homepts = dat[:,52]
    endspreadS = roadpts-homepts  #all the final spreads of the game
            #concatenating all the arrays, looks messy but explanation doen in another nb. 
    x1 = dat[:,0:7] #road offensive rebounds to blocks
    x2 = dat[:,9:42] # road 3p% to team name (hot encoded)

    x3 = dat[:,45:52] #home offensive rebounds to blocks
    x4  =  dat[:,54:87] #home 3p% to hot encoded team name   
                      
    x5 = dat[:,8]              
    X1 = np.concatenate((x1,x2),axis=1)
    X2 = np.concatenate((x3,x4),axis=1)
    X3 = np.concatenate((X1,X2),axis=1)
    
    y = []
    
    if spread:
        #include initial spread of the game. 
        X = np.column_stack((X3,x5))

        for j in range(len(endspreadS)):  
            openspread = openingspreadS[j]
       # print("this is the spread of the road team " + str(openspread))
            endspread = endspreadS[j]
       # print("the road team won by  .. " + str(endspread))
       # if endspread>openspread:
        #    y.append(np.array([0,1,0]))  #OK, now make sure this is formateed properly!
            if openspread + endspread <0:
                y.append(np.array([0,1,0]))  #home team covered
            elif openspread + endspread >0:
                y.append(np.array([1,0,0]))  #road covered
            else: 
                y.append(np.array([0,0,1]))  #push!

    
    if moneyline:
        X = np.column_stack((X3,x5))
        #Spread is still a useful property for this type of bet. The spread implies the favorite! 
        for j in range(len(endspreadS)):  
            if endspreadS[j]<0:
                #means the home team had more points
                y.append(np.array([0,1]))
            else:
                y.append(np.array([1,0])) #alternatively, a road team victory. 
          

    #Now I iterated over all these, and hot encoded the labels of each to see whether or not the spread was covered
    #and by what team. 


        
    y = np.array(y)  #same explanation as above
                         
    #since everything got out of order I have to mash it together myself. 
    if normalize:
        
        scaler = MinMaxScaler()
        MinMaxScaler(copy=True, feature_range=(0, 1))

        scaler.fit(X)
        X = scaler.transform(X)
    X_train,X_test,y_train,y_test = cross_validation.train_test_split(X,y,test_size=0.27)
    #print((X[0]))
    #print(np.shape(X[0]))

    #now to construct a model 
    if sklearn: 
        model = MLPClassifier()
        model.shuffle = True
        model.batch_size = 25
    #model.n_layers_ = 1000000
    #model.n_outputs_= 1000000
    #These don't do anything, have to adjust the layers in some different way! Keras is useful for this.
        model.fit(X_train,y_train)
        print(model.score(X_test,y_test))
    if keras:
        print("keras NN goes here")
        model=Sequential()
        model.add(Dense(80,input_dim=np.shape(X)[1],activation='relu'))
        model.add(Dense(81,activation='relu'))
        model.add(Dense(31,activation='relu'))
        model.add(Dense(24,activation='relu'))
        if spread:
            model.add(Dense(3,activation='softmax'))
        if moneyline: 
            model.add(Dense(2,activation='softmax'))  #different outputs for the 2 problems!

        model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
        model.fit(X_train,y_train,batch_size=40,epochs=20,validation_split=.2)
        scores = model.evaluate(X_test,y_test)
        print(scores[1])

    if tpot: 
        y2 = []
        for i in range(len(y_train)):
                if sum(y_train[i] == np.array([1, 0, 0])) == 3:
                    y2.append(0)
                else:
                    y2.append(1)
        
        from tpot import TPOTClassifier
        tpot = TPOTClassifier(generations = 5,population_size = 50,verbosity = 2, n_jobs = -1)
        tpot.fit(X_train,y2)
            #experimental. Trying to use genetic programming to identify optimal routine for game classification. 
    
        model = tpot #the returned model 
    return model,scaler
예제 #6
0
    def call(self, x):
        input = self.input_layer(x)
        encoded = self.code_layer(input)
        decoded = self.output_layer(encoded)
        return decoded


autoencoder = Autoencoder()
"""Una volta che il modello è stato definito, viene effettuata una fase di *compilazione* che permette di configurare la funzione *loss* e l'algoritmo per l'aggiornamento dei pesi. In particolare, vengono utilizzati:
* `binary_crossentropy`: è la funzione *loss*;
* `adam`: è un algoritmo di discesa del gradiente stocastico.


"""

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
"""Eseguo il training dell'autoencoder, indicando il numero massimo di iterazioni che devono essere effettuate (5000)."""

history = autoencoder.fit(X, X, epochs=5000)
"""Si osservino le rappresentazioni alternative dell'input usufruite dalla rete neurale, durante la fase di training."""

weights = autoencoder.code_layer.get_weights()
weights
"""Si effettui una predizione fornendo in input il medesimo dataset utilizzato nella fase di training."""

x_pred = autoencoder.predict(X)
x_pred
"""Si illustri l'andamento della funzione *loss*."""

plt.title('Binary Cross Entropy Loss')
plt.plot(history.history['loss'], label='loss')
                                alpha=learning_rate,
                                hidden_layer_sizes=hidden_sizes,
                                random_state=1)
     classifier.fit(X_train, Y_train)
 elif mode == 'use_keras':
     from keras.models import Sequential
     from keras.layers import Dense
     # define the keras model
     classifier = Sequential()
     classifier.add(
         Dense(hidden_sizes[0], input_dim=input_size, activation='relu'))
     classifier.add(Dense(hidden_sizes[1], activation='relu'))
     classifier.add(Dense(output_size, activation='sigmoid'))
     # compile the keras model
     classifier.compile(loss='binary_crossentropy',
                        optimizer='adam',
                        metrics=['accuracy'])
     # fit the keras model on the dataset
     classifier.fit(X_train, Y_train, epochs=150, batch_size=10)
 elif mode == 'use_torch':
     classifier = Torch_NN(input_size, hidden_sizes, num_classes)
     classifier.fit(X_train,
                    Y_train,
                    epochs=150,
                    batch_size=10,
                    learning_rate=learning_rate)
 elif mode == 'self_implement':  # self-implement
     classifier = Skylark_Neural_Network(input_size, hidden_sizes,
                                         num_classes)
     classifier.fit(X_train,
                    Y_train,