keras-cor


Namekeras-cor JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttp://github.com/ulf1/keras-cor
SummaryAdd a regularization if the features/columns/neurons the hidden layer or output layer should be correlated. The vector with target correlation coefficient is computed before the optimization, and compared with correlation coefficients computed across the batch examples.
upload_time2023-07-10 15:54:44
maintainer
docs_urlNone
authorUlf Hamster
requires_python>=3.7
licenseApache License 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://badge.fury.io/py/keras-cor.svg)](https://badge.fury.io/py/keras-cor)
[![PyPi downloads](https://img.shields.io/pypi/dm/keras-cor)](https://img.shields.io/pypi/dm/keras-cor)


# keras-cor : Correlated Outputs Regularization
Add a regularization if the features/columns/neurons the hidden layer or output layer should be correlated. The vector with target correlation coefficient is computed before the optimization, and compared with correlation coefficients computed across the batch examples.

## Usage
See [demo notebook](demo/Correlated%20Outputs%20Regularization.ipynb)

```py
from keras_cor import CorrOutputsRegularizer
import tensorflow as tf

# Simple regression NN
def build_mymodel(input_dim, target_corr, cor_rate=0.1, 
                  activation="sigmoid", output_dim=3):
    inputs = tf.keras.Input(shape=(input_dim,))
    h = tf.keras.layers.Dense(units=output_dim)(inputs)
    h = tf.keras.layers.Activation(activation)(h)
    outputs = CorrOutputsRegularizer(target_corr, cor_rate)(h)  # <= HERE
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

# Gneerate toy dataset
BATCH_SZ = 128
INPUT_DIM = 64
OUTPUT_DIM = 3

X_train = tf.random.normal([BATCH_SZ, INPUT_DIM])
y_train = tf.random.normal([BATCH_SZ, OUTPUT_DIM])

# Normally you should comput `target_corr` based on your target outputs `y_train`
# e.g., target_corr = tf.constant(y_train)
# However, you can also use subjective correlations (aka expert opinions), e.g.,
target_corr = tf.constant([.5, -.4, .9])

# Optimization
model = build_mymodel(input_dim=INPUT_DIM, target_corr=target_corr, output_dim=OUTPUT_DIM)
model.compile(optimizer=tf.keras.optimizers.Adam(), loss="mean_squared_error")
history = model.fit(X_train, y_train, verbose=1, epochs=2)

# Inference
yhat = model.predict(X_train)
rhos = pearson_vec(yhat)
rhos
```

## Appendix

### Installation
The `keras-cor` [git repo](http://github.com/ulf1/keras-cor) is available as [PyPi package](https://pypi.org/project/keras-cor)

```sh
pip install keras-cor
pip install git+ssh://git@github.com/ulf1/keras-cor.git
```

### Install a virtual environment

```sh
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt --no-cache-dir
pip install -r requirements-dev.txt --no-cache-dir
pip install -r requirements-demo.txt --no-cache-dir
```

(If your git repo is stored in a folder with whitespaces, then don't use the subfolder `.venv`. Use an absolute path without whitespaces.)

### Python commands

* Jupyter for the examples: `jupyter lab`
* Check syntax: `flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')`
* Run Unit Tests: `PYTHONPATH=. pytest`

Publish

```sh
python setup.py sdist 
twine upload -r pypi dist/*
```

### Clean up 

```sh
find . -type f -name "*.pyc" | xargs rm
find . -type d -name "__pycache__" | xargs rm -r
rm -r .pytest_cache
rm -r .venv
```


### Support
Please [open an issue](https://github.com/ulf1/keras-cor/issues/new) for support.


### Contributing
Please contribute using [Github Flow](https://guides.github.com/introduction/flow/). Create a branch, add commits, and [open a pull request](https://github.com/ulf1/keras-cor/compare/).



            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/ulf1/keras-cor",
    "name": "keras-cor",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "Ulf Hamster",
    "author_email": "554c46@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/35/4f/e26253b905f97f5d944ca56a8434097a1b709be759f9f37ff9a1fa99c963/keras-cor-0.2.0.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://badge.fury.io/py/keras-cor.svg)](https://badge.fury.io/py/keras-cor)\n[![PyPi downloads](https://img.shields.io/pypi/dm/keras-cor)](https://img.shields.io/pypi/dm/keras-cor)\n\n\n# keras-cor : Correlated Outputs Regularization\nAdd a regularization if the features/columns/neurons the hidden layer or output layer should be correlated. The vector with target correlation coefficient is computed before the optimization, and compared with correlation coefficients computed across the batch examples.\n\n## Usage\nSee [demo notebook](demo/Correlated%20Outputs%20Regularization.ipynb)\n\n```py\nfrom keras_cor import CorrOutputsRegularizer\nimport tensorflow as tf\n\n# Simple regression NN\ndef build_mymodel(input_dim, target_corr, cor_rate=0.1, \n                  activation=\"sigmoid\", output_dim=3):\n    inputs = tf.keras.Input(shape=(input_dim,))\n    h = tf.keras.layers.Dense(units=output_dim)(inputs)\n    h = tf.keras.layers.Activation(activation)(h)\n    outputs = CorrOutputsRegularizer(target_corr, cor_rate)(h)  # <= HERE\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n    return model\n\n# Gneerate toy dataset\nBATCH_SZ = 128\nINPUT_DIM = 64\nOUTPUT_DIM = 3\n\nX_train = tf.random.normal([BATCH_SZ, INPUT_DIM])\ny_train = tf.random.normal([BATCH_SZ, OUTPUT_DIM])\n\n# Normally you should comput `target_corr` based on your target outputs `y_train`\n# e.g., target_corr = tf.constant(y_train)\n# However, you can also use subjective correlations (aka expert opinions), e.g.,\ntarget_corr = tf.constant([.5, -.4, .9])\n\n# Optimization\nmodel = build_mymodel(input_dim=INPUT_DIM, target_corr=target_corr, output_dim=OUTPUT_DIM)\nmodel.compile(optimizer=tf.keras.optimizers.Adam(), loss=\"mean_squared_error\")\nhistory = model.fit(X_train, y_train, verbose=1, epochs=2)\n\n# Inference\nyhat = model.predict(X_train)\nrhos = pearson_vec(yhat)\nrhos\n```\n\n## Appendix\n\n### Installation\nThe `keras-cor` [git repo](http://github.com/ulf1/keras-cor) is available as [PyPi package](https://pypi.org/project/keras-cor)\n\n```sh\npip install keras-cor\npip install git+ssh://git@github.com/ulf1/keras-cor.git\n```\n\n### Install a virtual environment\n\n```sh\npython3 -m venv .venv\nsource .venv/bin/activate\npip install --upgrade pip\npip install -r requirements.txt --no-cache-dir\npip install -r requirements-dev.txt --no-cache-dir\npip install -r requirements-demo.txt --no-cache-dir\n```\n\n(If your git repo is stored in a folder with whitespaces, then don't use the subfolder `.venv`. Use an absolute path without whitespaces.)\n\n### Python commands\n\n* Jupyter for the examples: `jupyter lab`\n* Check syntax: `flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')`\n* Run Unit Tests: `PYTHONPATH=. pytest`\n\nPublish\n\n```sh\npython setup.py sdist \ntwine upload -r pypi dist/*\n```\n\n### Clean up \n\n```sh\nfind . -type f -name \"*.pyc\" | xargs rm\nfind . -type d -name \"__pycache__\" | xargs rm -r\nrm -r .pytest_cache\nrm -r .venv\n```\n\n\n### Support\nPlease [open an issue](https://github.com/ulf1/keras-cor/issues/new) for support.\n\n\n### Contributing\nPlease contribute using [Github Flow](https://guides.github.com/introduction/flow/). Create a branch, add commits, and [open a pull request](https://github.com/ulf1/keras-cor/compare/).\n\n\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Add a regularization if the features/columns/neurons the hidden layer or output layer should be correlated. The vector with target correlation coefficient is computed before the optimization, and compared with correlation coefficients computed across the batch examples.",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "http://github.com/ulf1/keras-cor"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "354fe26253b905f97f5d944ca56a8434097a1b709be759f9f37ff9a1fa99c963",
                "md5": "d1e4f83a0d4a63377e1f7aba6b9e597b",
                "sha256": "59de74c7cf8e7626e549a9567467f6a504b66a07631fb5b0897ecf5ea9d2dfef"
            },
            "downloads": -1,
            "filename": "keras-cor-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d1e4f83a0d4a63377e1f7aba6b9e597b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 8208,
            "upload_time": "2023-07-10T15:54:44",
            "upload_time_iso_8601": "2023-07-10T15:54:44.002867Z",
            "url": "https://files.pythonhosted.org/packages/35/4f/e26253b905f97f5d944ca56a8434097a1b709be759f9f37ff9a1fa99c963/keras-cor-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-10 15:54:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ulf1",
    "github_project": "keras-cor",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "keras-cor"
}
        
Elapsed time: 0.09235s