maxpumperla / hyperas

Keras + Hyperopt: A very simple wrapper for convenient hyperparameter optimization
http://maxpumperla.com/hyperas/
MIT License
2.17k stars 316 forks source link

remove_all_comments not working correctly #245

Open pjm4github opened 5 years ago

pjm4github commented 5 years ago

Before filing an issue, please make sure to tick the following boxes.

pjm4github commented 5 years ago

The remove_all_comments doesnt work correctly for nested comments nor for docstrings with " type quotes.

def create_model(x_train,y_train , x_test, y_test):  # val_split=0.1, params=None):
    """
    THIS DOCSTRING WILL NOT BE REMOVED
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    """
    # commented line - the next line is not fully removed.
    # model = Sequential()  # additional end of line comment
    model = Sequential()

    model.add(
        LSTM(
            80,
            input_shape=(None, 30),
            return_sequences=True,
            name='LTSM_1')
    )

Suggested replacement inside site-packages/hyperas/utils.py

def remove_all_comments(source):
    string = re.sub(re.compile("'''.*?'''", re.DOTALL), "", source)  # remove '''...''' comments
    string = re.sub(re.compile('""".*?"""', re.DOTALL), "", string)  # remove """...""" comments
    done = False
    while not done:
        len_string = len(string)
        string = re.sub(re.compile("(?<!('|\").)*#.*?\n"), "\n", string)  # remove #...\n comments
        if len(string) == len_string:
            done = True
    return string