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