From 4170100bd1f49e48a49fb44991722b397031080f Mon Sep 17 00:00:00 2001 From: jung-geun Date: Thu, 6 Jul 2023 20:46:38 +0900 Subject: [PATCH] =?UTF-8?q?23-07-06=20code=20space=EC=9A=A9=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=ED=8C=8C=EC=9D=BC=20=EC=B6=94=EA=B0=80=20mutation?= =?UTF-8?q?=20=EC=84=A4=EC=A0=95=20-=20=ED=8A=B9=EC=A0=95=20=ED=99=95?= =?UTF-8?q?=EB=A5=A0=EB=A1=9C=20=ED=8C=8C=ED=8B=B0=ED=81=B4=EC=9D=98=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=EB=A5=BC=20=EB=9E=9C=EB=8D=A4=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=EB=B0=A9=ED=96=A5=20=EC=A1=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/Dockerfile | 16 ++++++++ .devcontainer/devcontainer.json | 43 ++++++++++++++++++++++ .devcontainer/noop.txt | 3 ++ .github/workflows/python-package-conda.yml | 12 +----- pso/optimizer.py | 2 +- pso/particle.py | 13 +++++-- 6 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/noop.txt diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..0e8a64a --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,16 @@ +FROM mcr.microsoft.com/devcontainers/miniconda:0-3 + +# Copy environment.yml (if found) to a temp location so we update the environment. Also +# copy "noop.txt" so the COPY instruction does not fail if no environment.yml exists. +COPY environment.yml* .devcontainer/noop.txt /tmp/conda-tmp/ +RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then umask 0002 && /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \ + && rm -rf /tmp/conda-tmp + +# [Optional] Uncomment to install a different version of Python than the default +# RUN conda install -y python=3.6 \ +# && pip install --no-cache-dir pipx \ +# && pipx reinstall-all + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..fd75e37 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,43 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/miniconda +{ + "name": "Miniconda (Python 3)", + "build": { + "context": "..", + "dockerfile": "Dockerfile" + }, + "customizations": { + "vscode": { + "extensions": [ + "ms-python.python", + "ms-toolsai.jupyter" + ] + } + }, + "features": { + "ghcr.io/devcontainers/features/nvidia-cuda:1": { + "installCudnn": true + }, + "ghcr.io/devcontainers/features/anaconda:1": { + "version": "latest" + }, + "ghcr.io/devcontainers/features/python:1": { + "installTools": true, + "version": "3.9" + }, + "ghcr.io/rocker-org/devcontainer-features/miniforge:1": { + "version": "latest", + "variant": "Miniforge-pypy3" + } + } + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "python --version", + // Configure tool-specific properties. + // "customizations": {}, + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} \ No newline at end of file diff --git a/.devcontainer/noop.txt b/.devcontainer/noop.txt new file mode 100644 index 0000000..abee195 --- /dev/null +++ b/.devcontainer/noop.txt @@ -0,0 +1,3 @@ +This file is copied into the container along with environment.yml* from the +parent folder. This is done to prevent the Dockerfile COPY instruction from +failing if no environment.yml is found. \ No newline at end of file diff --git a/.github/workflows/python-package-conda.yml b/.github/workflows/python-package-conda.yml index 2f1a480..a0cb5f2 100644 --- a/.github/workflows/python-package-conda.yml +++ b/.github/workflows/python-package-conda.yml @@ -21,14 +21,6 @@ jobs: - name: Install dependencies run: | conda env update --file environment.yaml --name base - - name: Lint with flake8 + - name: pso mnist run run: | - conda install flake8 - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - - name: Test with pytest - run: | - conda install pytest - pytest + python mnist.py \ No newline at end of file diff --git a/pso/optimizer.py b/pso/optimizer.py index 556621e..dbf1bc0 100644 --- a/pso/optimizer.py +++ b/pso/optimizer.py @@ -90,7 +90,7 @@ class Optimizer: negative_count += 1 print(f"negative swarm : {negative_count} / {self.n_particles}") - print(f"mutation swarm : {mutation_swarm*100/self.n_particles} / {self.n_particles}") + print(f"mutation swarm : {mutation_swarm * self.n_particles} / {self.n_particles}") gc.collect() diff --git a/pso/particle.py b/pso/particle.py index dcd7a92..57c7f39 100644 --- a/pso/particle.py +++ b/pso/particle.py @@ -150,9 +150,11 @@ class Particle: + local_rate * r0 * (encode_p - encode_w) + global_rate * r1 * (encode_g - encode_w) ) - if np.random.rand() < self.mutation: - new_v += 0.5 * encode_v + if np.random.rand() < self.mutation: + m_v = np.random.uniform(-0.1, 0.1, len(encode_v)) + new_v = m_v + self.velocities = self._decode(new_v, w_sh, w_len) del encode_w, w_sh, w_len @@ -193,8 +195,11 @@ class Particle: + local_rate * r0 * (w_p * encode_p - encode_w) + global_rate * r1 * (w_g * encode_g - encode_w) ) - if self.mutation: - new_v += 0.5 * encode_v + + if np.random.rand() < self.mutation: + m_v = np.random.uniform(-0.1, 0.1, len(encode_v)) + new_v = m_v + self.velocities = self._decode(new_v, w_sh, w_len) del encode_w, w_sh, w_len