コード例 #1
0
class MLP_keras:
    def __init__(self, learning_rate, layers, functions, optimizer_name,
                 beta=0.0, dropout=1.0):
        
        self.n_input = layers[0]
        self.n_hidden = layers[1:-1]
        self.n_output = layers[-1]
        
        self.model = Sequential()
        
        if len(self.n_hidden) == 0:
            # single layer
            self.model.add(Dense(self.n_output, activation=functions[0],
                             kernel_regularizer=regularizers.l2(beta),
                             input_shape=(self.n_input,)))
            
        elif len(self.n_hidden) == 1:
            # hidden layer
            self.model.add(Dense(self.n_hidden[0], activation=functions[0],
                                 kernel_regularizer=regularizers.l2(beta),
                                 input_shape=(self.n_input,)))
            self.model.add(Dropout(dropout))
            # output layer
            self.model.add(Dense(self.n_output, activation=functions[1],
                                 kernel_regularizer=regularizers.l2(beta)))
            
        else:
            # the first hidden layer
            self.model.add(Dense(self.n_hidden[0], activation=functions[0],
                                 kernel_regularizer=regularizers.l2(beta),
                                 input_shape=(self.n_input,)))
            self.model.add(Dropout(dropout))
            # the second hidden layer
            self.model.add(Dense(self.n_hidden[1], activation=functions[1],
                                 kernel_regularizer=regularizers.l2(beta)))
            self.model.add(Dropout(dropout))
            # the output layer
            self.model.add(Dense(self.n_output, activation=functions[2],
                                 kernel_regularizer=regularizers.l2(beta)))
        
        self.model.summary()
        
        if optimizer_name == 'Adam': optimizer = Adam(learning_rate)
        
        #self.model.compile(loss='mean_squared_error',
        #                   optimizer=optimizer,
        #                   metrics=['accuracy'])
        
        self.model.compile(loss='categorical_crossentropy',
                           optimizer=optimizer,
                           metrics=['accuracy'])
    
    def train(self, epochs, trn, vld=None, batch_size=32, es=0):
        if vld is not None:
            validation_data = (vld.x, vld.y)
            if es > 0:
                callbacks = [EarlyStopping(monitor='val_loss', patience=es)]
            else:
                callbacks = None
        else:
            validation_data = None
            callbacks = None
        
        self.model.fit(trn.x, trn.y,
                       batch_size=batch_size,
                       epochs=epochs,
                       verbose=2,
                       callbacks=callbacks,
                       validation_data=validation_data)
        
        loss, tst_acc = self.model.evaluate(trn.x, trn.y, verbose=0)
        if vld is not None:
            _, vld_acc = self.model.evaluate(vld.x, vld.y, verbose=0)
        else:
            vld_acc = 0
        return ['', loss, tst_acc, vld_acc, 0]
    
    def evaluate(self, x_test, y_test):
        score = self.model.evaluate(x_test, y_test, verbose=0)
        print('Test loss:', score[0])
        print('Test accuracy:', score[1])
    
    def score(self, tst):
        return self.model.evaluate(tst.x, tst.y, verbose=0)[1]
    
    def predict_proba(self, x):
        if x is None: return None
        return self.model.predict_proba(x, verbose=0)
コード例 #2
0
     model = load_model(savefile)
else:
    model = Sequential()
    model.add(Dense(128, input_dim=input_dimension, kernel_initializer=hidden_initializer, activation='relu'))
    #model.add(Dense(128, input_dim=input_dimension,  activation='relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(64, kernel_initializer=hidden_initializer, activation='relu'))
    model.add(Dense(2, kernel_initializer=hidden_initializer, activation='softmax'))
    #model.add(Dense(64, activation='relu'))
    #model.add(Dense(2, activation='softmax'))

    sgd = SGD(lr=learning_rate, momentum=momentum)
    model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['acc'])

model.fit(X_train, y_train, epochs=50, batch_size=128)
predictions = model.predict_proba(X_test)

ans = pd.DataFrame(predictions,columns=['target','dmy'])
print ans
outdf = pd.concat([outdf,ans['target']],axis=1)
outdf.to_csv('./submit_keras.csv',index=False)


    #save the model
#model_json = model.to_json()
#with open("./ans.json", "w") as json_file:
#    json_file.write(model_json)

model.save(savefile)