Spaces:
Build error
Build error
Ashley Goluoglu
commited on
Commit
·
96283ff
1
Parent(s):
3a085d0
add files from pantelis/IDNN
Browse files- Figure_3.png +0 -0
- LICENSE +14 -0
- Pipfile +23 -0
- Pipfile.lock +0 -0
- README 2.md +49 -0
- compare_percent_mnist_5_AND_85_PERCENT_old.JPG +0 -0
- data/MNIST_data/t10k-images-idx3-ubyte.gz +3 -0
- data/MNIST_data/t10k-labels-idx1-ubyte.gz +3 -0
- data/MNIST_data/train-images-idx3-ubyte.gz +3 -0
- data/MNIST_data/train-labels-idx1-ubyte.gz +3 -0
- data/g1.mat +0 -0
- data/g2.mat +0 -0
- data/var_u.mat +0 -0
- docker-compose.yml +0 -0
- docker/Dockerfile.pytorch +76 -0
- docker/Dockerfile.tensorflow +67 -0
- docker/docker-font.conf +13 -0
- idnns/__init__.py +0 -0
- idnns/information/__init__.py +0 -0
- idnns/information/entropy_estimators.py +283 -0
- idnns/information/information_process.py +201 -0
- idnns/information/information_utilities.py +54 -0
- idnns/information/mutual_info_estimation.py +178 -0
- idnns/information/mutual_information_calculation.py +50 -0
- idnns/networks/__init__.py +0 -0
- idnns/networks/information_network.py +166 -0
- idnns/networks/model.py +212 -0
- idnns/networks/models.py +125 -0
- idnns/networks/network.py +173 -0
- idnns/networks/network_paramters.py +136 -0
- idnns/networks/ops.py +72 -0
- idnns/networks/utils.py +154 -0
- idnns/plots/__init__.py +0 -0
- idnns/plots/ops.py +20 -0
- idnns/plots/plot_figures.py +678 -0
- idnns/plots/plot_gradients.py +223 -0
- idnns/plots/utils.py +243 -0
- main.py +24 -0
- test.py +14 -0
Figure_3.png
ADDED
![]() |
LICENSE
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
LICENSE CONDITIONS
|
2 |
+
|
3 |
+
Copyright (2016) Ravid Shwartz-Ziv
|
4 |
+
All rights reserved.
|
5 |
+
|
6 |
+
For details, see the paper:
|
7 |
+
Ravid Shwartz-Ziv, Naftali Tishby,
|
8 |
+
Opening the Black Box of Deep Neural Networks via Information
|
9 |
+
Arxiv, 2017
|
10 |
+
Permission to use, copy, modify, and distribute this software and its documentation for educational, research, and non-commercial purposes, without fee and without a signed licensing agreement, is hereby granted, provided that the above copyright notice and this paragraph appear in all copies, modifications, and distributions.
|
11 |
+
|
12 |
+
Any commercial use or any redistribution of this software requires a license. For further details, contact Ravid Shwartz-Ziv ([email protected]).
|
13 |
+
|
14 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Pipfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[[source]]
|
2 |
+
url = "https://pypi.org/simple"
|
3 |
+
verify_ssl = false
|
4 |
+
name = "pip_conf_index_global"
|
5 |
+
|
6 |
+
[packages]
|
7 |
+
ipywidgets = "*"
|
8 |
+
optuna = "*"
|
9 |
+
pydantic = "*"
|
10 |
+
filterpy = "==1.4.5"
|
11 |
+
transformers = "==4.35.1"
|
12 |
+
datasets = "==2.14.6"
|
13 |
+
accelerate = "==0.24.1"
|
14 |
+
nvidia-ml-py3 = "==7.352.0"
|
15 |
+
pygobject = "==3.40.1"
|
16 |
+
pytest = "==6.2.5"
|
17 |
+
prml = {git = "git+https://github.com/pantelis-classes/PRML.git#egg=prml"}
|
18 |
+
seaborn = "*"
|
19 |
+
|
20 |
+
[dev-packages]
|
21 |
+
|
22 |
+
[requires]
|
23 |
+
python_version = "3.10"
|
Pipfile.lock
ADDED
The diff for this file is too large to render.
See raw diff
|
|
README 2.md
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# IDNNs
|
2 |
+
## Description
|
3 |
+
IDNNs is a python library that implements training and calculating of information in deep neural networks
|
4 |
+
[\[Shwartz-Ziv & Tishby, 2017\]](#IDNNs) in TensorFlow. The library allows you to investigate how networks look on the
|
5 |
+
information plane and how it changes during the learning.
|
6 |
+
<img src="https://github.com/ravidziv/IDNNs/blob/master/compare_percent_mnist_5_AND_85_PERCENT_old.JPG" width="1000px"/>
|
7 |
+
|
8 |
+
## Prerequisites
|
9 |
+
- tensorflow r1.0 or higher version
|
10 |
+
- numpy 1.11.0
|
11 |
+
- matplotlib 2.0.2
|
12 |
+
- multiprocessing
|
13 |
+
- joblib
|
14 |
+
|
15 |
+
## Usage
|
16 |
+
All the code is under the `idnns/` directory.
|
17 |
+
For training a network and calculate the MI and the gradients of it run the an example in [main.py](main.py).
|
18 |
+
Off course you can also run only specific methods for running only the training procedure/calculating the MI.
|
19 |
+
This file has command-line arguments as follow -
|
20 |
+
- `start_samples` - The number of the first sample for calculate the information
|
21 |
+
- `batch_size` - The size of the batch
|
22 |
+
- `learning_rate` - The learning rate of the network
|
23 |
+
- `num_repeat` - The number of times to run the network
|
24 |
+
- `num_epochs` - maximum number of epochs for training
|
25 |
+
- `net_arch` - The architecture of the networks
|
26 |
+
- `per_data` - The percent of the training data
|
27 |
+
- `name` - The name for saving the results
|
28 |
+
- `data_name` - The dataset name
|
29 |
+
- `num_samples` - The max number of indexes for calculate the information
|
30 |
+
- `save_ws` - True if we want to save the outputs of the network
|
31 |
+
- `calc_information` - 1 if we want to calculate the MI of the network
|
32 |
+
- `save_grads` - True if we want to save the gradients of the network
|
33 |
+
- `run_in_parallel` - True if we want to run all the networks in parallel mode
|
34 |
+
- `num_of_bins` - The number of bins that we divide the neurons' output
|
35 |
+
- `activation_function` - The activation function of the model 0 for thnh 1 for RelU'
|
36 |
+
- `interval_accuracy_display` - The interval for display accuracy
|
37 |
+
- `interval_information_display` - The interval for display the information calculation
|
38 |
+
- `cov_net` - True if we want covnet
|
39 |
+
- `rand_labels` - True if we want to set random labels
|
40 |
+
- `data_dir` - The directory for finding the data
|
41 |
+
The results are save under the folder jobs. Each run create a directory with a name that contains the run properties. In this directory there are the data.pickle file with the data of run and python file that is a copy of the file that create this run.
|
42 |
+
The data is under the data directory.
|
43 |
+
|
44 |
+
For plotting the results we have the file [plot_figures.py](idnns/plot/plot_figures.py).
|
45 |
+
This file contains methods for plotting diffrent aspects of the data (the information plane, the gradients,the norms, etc).
|
46 |
+
|
47 |
+
## References
|
48 |
+
|
49 |
+
1. <a name="IDNNs"></a> Ravid. Shwartz-Ziv, Naftali Tishby, [Opening the Black Box of Deep Neural Networks via Information](https://arxiv.org/abs/1703.00810), 2017, Arxiv.
|
compare_percent_mnist_5_AND_85_PERCENT_old.JPG
ADDED
|
data/MNIST_data/t10k-images-idx3-ubyte.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8d422c7b0a1c1c79245a5bcf07fe86e33eeafee792b84584aec276f5a2dbc4e6
|
3 |
+
size 1648877
|
data/MNIST_data/t10k-labels-idx1-ubyte.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f7ae60f92e00ec6debd23a6088c31dbd2371eca3ffa0defaefb259924204aec6
|
3 |
+
size 4542
|
data/MNIST_data/train-images-idx3-ubyte.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:440fcabf73cc546fa21475e81ea370265605f56be210a4024d2ca8f203523609
|
3 |
+
size 9912422
|
data/MNIST_data/train-labels-idx1-ubyte.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3552534a0a558bbed6aed32b30c495cca23d567ec52cac8be1a0730e8010255c
|
3 |
+
size 28881
|
data/g1.mat
ADDED
Binary file (54 kB). View file
|
|
data/g2.mat
ADDED
Binary file (54 kB). View file
|
|
data/var_u.mat
ADDED
Binary file (1 kB). View file
|
|
docker-compose.yml
ADDED
File without changes
|
docker/Dockerfile.pytorch
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM nvcr.io/nvidia/pytorch:23.05-py3
|
2 |
+
|
3 |
+
# specify vscode as the user name in the docker
|
4 |
+
# This user name should match that of the VS Code .devcontainer to allow seamless development inside the docker container via vscode
|
5 |
+
ARG USERNAME=vscode
|
6 |
+
ARG USER_UID=1001
|
7 |
+
ARG USER_GID=$USER_UID
|
8 |
+
|
9 |
+
# Create a non-root user
|
10 |
+
RUN groupadd --gid $USER_GID $USERNAME \
|
11 |
+
&& useradd -l -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
|
12 |
+
# [Optional] Add sudo support for the non-root user - this is ok for development dockers only
|
13 |
+
&& apt-get update \
|
14 |
+
&& apt-get install -y sudo \
|
15 |
+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
|
16 |
+
&& chmod 0440 /etc/sudoers.d/$USERNAME \
|
17 |
+
# Cleanup
|
18 |
+
&& rm -rf /var/lib/apt/lists/* \
|
19 |
+
# Set up git completion.
|
20 |
+
&& echo "source /usr/share/bash-completion/completions/git" >> /home/$USERNAME/.bashrc
|
21 |
+
|
22 |
+
# Packages installation (eg git-lfs)
|
23 |
+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
24 |
+
&& apt-get -y install --no-install-recommends curl git-lfs ffmpeg libsm6 libxext6 graphviz libgraphviz-dev libsndfile1-dev xdg-utils swig gringo gobject-introspection libcairo2-dev libgirepository1.0-dev pkg-config python3-dev python3-gi
|
25 |
+
|
26 |
+
COPY docker/docker-font.conf /etc/fonts/local.conf
|
27 |
+
#ENV FREETYPE_PROPERTIES="truetype:interpreter-version=35"
|
28 |
+
RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections
|
29 |
+
RUN apt-get update \
|
30 |
+
&& apt-get install -y --no-install-recommends fontconfig ttf-mscorefonts-installer
|
31 |
+
|
32 |
+
|
33 |
+
USER vscode
|
34 |
+
|
35 |
+
# ACT for executing locally Github workflows
|
36 |
+
RUN curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
|
37 |
+
|
38 |
+
# NVM for managing npm versions
|
39 |
+
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | sudo bash
|
40 |
+
|
41 |
+
# Git LFS repo configuration
|
42 |
+
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
|
43 |
+
|
44 |
+
# inscape installation for managing svg files
|
45 |
+
RUN sudo apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
46 |
+
&& sudo apt-get -y install --no-install-recommends inkscape
|
47 |
+
|
48 |
+
SHELL ["/bin/bash", "-c"]
|
49 |
+
ENV PATH="/home/vscode/.local/bin:$PATH"
|
50 |
+
ENV GI_TYPELIB_PATH="/usr/lib/x86_64-linux-gnu/girepository-1.0"
|
51 |
+
|
52 |
+
# RUN python -m pip install pip-tools
|
53 |
+
# https://github.com/jazzband/pip-tools/issues/1596
|
54 |
+
#RUN --mount=type=cache,target=/root/.cache/pip python3 -m piptools compile -v
|
55 |
+
|
56 |
+
|
57 |
+
# Install dependencies:
|
58 |
+
# COPY requirements.txt /tmp/requirements.txt
|
59 |
+
# RUN pip install -r /tmp/requirements.txt
|
60 |
+
|
61 |
+
RUN pip install --upgrade pip
|
62 |
+
# && \
|
63 |
+
# pip install pipenv && \
|
64 |
+
# pipenv lock && \
|
65 |
+
# pipenv install --dev --system && \
|
66 |
+
# pipenv --clear
|
67 |
+
RUN pip install pipenv
|
68 |
+
COPY Pipfile.lock Pipfile /tmp/
|
69 |
+
WORKDIR /tmp
|
70 |
+
RUN pipenv install --system --deploy --ignore-pipfile
|
71 |
+
|
72 |
+
WORKDIR /workspaces/artificial_intelligence
|
73 |
+
|
74 |
+
COPY . .
|
75 |
+
|
76 |
+
|
docker/Dockerfile.tensorflow
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#FROM nvcr.io/nvidia/tensorflow:23.07-tf2-py3
|
2 |
+
FROM tensorflow/tensorflow:2.15.0
|
3 |
+
# use the cpu image that the nvidia image was based on
|
4 |
+
|
5 |
+
# specify vscode as the user name in the docker
|
6 |
+
# This user name should match that of the VS Code .devcontainer to allow seamless development inside the docker container via vscode
|
7 |
+
ARG USERNAME=vscode
|
8 |
+
ARG USER_UID=1001
|
9 |
+
ARG USER_GID=$USER_UID
|
10 |
+
|
11 |
+
# Create a non-root user
|
12 |
+
RUN groupadd --gid $USER_GID $USERNAME \
|
13 |
+
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
|
14 |
+
# [Optional] Add sudo support for the non-root user - this is ok for development dockers only
|
15 |
+
&& apt-get update \
|
16 |
+
&& apt-get install -y sudo \
|
17 |
+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
|
18 |
+
&& chmod 0440 /etc/sudoers.d/$USERNAME \
|
19 |
+
# Cleanup
|
20 |
+
&& rm -rf /var/lib/apt/lists/* \
|
21 |
+
# Set up git completion.
|
22 |
+
&& echo "source /usr/share/bash-completion/completions/git" >> /home/$USERNAME/.bashrc
|
23 |
+
|
24 |
+
# Packages installation (eg git-lfs)
|
25 |
+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
26 |
+
&& apt-get -y install --no-install-recommends curl git-lfs ffmpeg libsm6 libxext6 graphviz libsndfile1-dev libgraphviz-dev xdg-utils swig gringo gobject-introspection libcairo2-dev libgirepository1.0-dev pkg-config python3-dev python3-gi python3-tk
|
27 |
+
|
28 |
+
#COPY docker/docker-font.conf /etc/fonts/local.conf
|
29 |
+
#ENV FREETYPE_PROPERTIES="truetype:interpreter-version=35"
|
30 |
+
#RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections
|
31 |
+
#RUN apt-get update \
|
32 |
+
#&& apt-get install -y --no-install-recommends fontconfig ttf-mscorefonts-installer
|
33 |
+
|
34 |
+
|
35 |
+
USER vscode
|
36 |
+
|
37 |
+
# ACT for executing locally Github workflows
|
38 |
+
RUN curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
|
39 |
+
|
40 |
+
# NVM for managing npm versions
|
41 |
+
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | sudo bash
|
42 |
+
|
43 |
+
# Git LFS repo configuration
|
44 |
+
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
|
45 |
+
|
46 |
+
# inscape installation for managing svg files
|
47 |
+
RUN sudo apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
48 |
+
&& sudo apt-get -y install --no-install-recommends inkscape
|
49 |
+
|
50 |
+
|
51 |
+
SHELL ["/bin/bash", "-c"]
|
52 |
+
ENV PATH="/home/vscode/.local/bin:$PATH"
|
53 |
+
ENV GI_TYPELIB_PATH="/usr/lib/x86_64-linux-gnu/girepository-1.0"
|
54 |
+
|
55 |
+
# Install dependencies:
|
56 |
+
# COPY . .
|
57 |
+
# RUN pip install -r requirements.txt
|
58 |
+
|
59 |
+
RUN pip install --upgrade pip
|
60 |
+
RUN pip install pipenv
|
61 |
+
COPY Pipfile.lock Pipfile /tmp/
|
62 |
+
WORKDIR /tmp
|
63 |
+
RUN pipenv install --system --deploy --ignore-pipfile
|
64 |
+
|
65 |
+
WORKDIR /workspaces/artificial_intelligence
|
66 |
+
|
67 |
+
COPY . .
|
docker/docker-font.conf
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
|
3 |
+
<fontconfig>
|
4 |
+
<match target="font">
|
5 |
+
<edit name="antialias" mode="assign"><bool>true</bool></edit>
|
6 |
+
</match>
|
7 |
+
<match target="font">
|
8 |
+
<edit name="hintstyle" mode="assign"><const>hintnone</const></edit>
|
9 |
+
</match>
|
10 |
+
<match target="font">
|
11 |
+
<edit mode="assign" name="hinting"><bool>false</bool></edit>
|
12 |
+
</match>
|
13 |
+
</fontconfig>
|
idnns/__init__.py
ADDED
File without changes
|
idnns/information/__init__.py
ADDED
File without changes
|
idnns/information/entropy_estimators.py
ADDED
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# Written by Greg Ver Steeg
|
3 |
+
# See readme.pdf for documentation
|
4 |
+
# Or go to http://www.isi.edu/~gregv/npeet.html
|
5 |
+
|
6 |
+
import scipy.spatial as ss
|
7 |
+
from scipy.special import digamma
|
8 |
+
from math import log
|
9 |
+
import numpy.random as nr
|
10 |
+
import numpy as np
|
11 |
+
import random
|
12 |
+
|
13 |
+
# CONTINUOUS ESTIMATORS
|
14 |
+
|
15 |
+
def entropy(x, k=3, base=2):
|
16 |
+
""" The classic K-L k-nearest neighbor continuous entropy estimator
|
17 |
+
x should be a list of vectors, e.g. x = [[1.3], [3.7], [5.1], [2.4]]
|
18 |
+
if x is a one-dimensional scalar and we have four samples
|
19 |
+
"""
|
20 |
+
assert k <= len(x) - 1, "Set k smaller than num. samples - 1"
|
21 |
+
d = len(x[0])
|
22 |
+
N = len(x)
|
23 |
+
intens = 1e-10 # small noise to break degeneracy, see doc.
|
24 |
+
x = [list(p + intens * nr.rand(len(x[0]))) for p in x]
|
25 |
+
tree = ss.cKDTree(x)
|
26 |
+
nn = [tree.query(point, k + 1, p=float('inf'))[0][k] for point in x]
|
27 |
+
const = digamma(N) - digamma(k) + d * log(2)
|
28 |
+
return (const + d * np.mean(map(log, nn))) / log(base)
|
29 |
+
|
30 |
+
def centropy(x, y, k=3, base=2):
|
31 |
+
""" The classic K-L k-nearest neighbor continuous entropy estimator for the
|
32 |
+
entropy of X conditioned on Y.
|
33 |
+
"""
|
34 |
+
hxy = entropy([xi + yi for (xi, yi) in zip(x, y)], k, base)
|
35 |
+
hy = entropy(y, k, base)
|
36 |
+
return hxy - hy
|
37 |
+
|
38 |
+
def column(xs, i):
|
39 |
+
return [[x[i]] for x in xs]
|
40 |
+
|
41 |
+
def tc(xs, k=3, base=2):
|
42 |
+
xis = [entropy(column(xs, i), k, base) for i in range(0, len(xs[0]))]
|
43 |
+
return np.sum(xis) - entropy(xs, k, base)
|
44 |
+
|
45 |
+
def ctc(xs, y, k=3, base=2):
|
46 |
+
xis = [centropy(column(xs, i), y, k, base) for i in range(0, len(xs[0]))]
|
47 |
+
return np.sum(xis) - centropy(xs, y, k, base)
|
48 |
+
|
49 |
+
def corex(xs, ys, k=3, base=2):
|
50 |
+
cxis = [mi(column(xs, i), ys, k, base) for i in range(0, len(xs[0]))]
|
51 |
+
return np.sum(cxis) - mi(xs, ys, k, base)
|
52 |
+
|
53 |
+
def mi(x, y, k=3, base=2):
|
54 |
+
""" Mutual information of x and y
|
55 |
+
x, y should be a list of vectors, e.g. x = [[1.3], [3.7], [5.1], [2.4]]
|
56 |
+
if x is a one-dimensional scalar and we have four samples
|
57 |
+
"""
|
58 |
+
assert len(x) == len(y), "Lists should have same length"
|
59 |
+
assert k <= len(x) - 1, "Set k smaller than num. samples - 1"
|
60 |
+
intens = 1e-10 # small noise to break degeneracy, see doc.
|
61 |
+
x = [list(p + intens * nr.rand(len(x[0]))) for p in x]
|
62 |
+
y = [list(p + intens * nr.rand(len(y[0]))) for p in y]
|
63 |
+
points = zip2(x, y)
|
64 |
+
# Find nearest neighbors in joint space, p=inf means max-norm
|
65 |
+
tree = ss.cKDTree(points)
|
66 |
+
dvec = [tree.query(point, k + 1, p=float('inf'))[0][k] for point in points]
|
67 |
+
a, b, c, d = avgdigamma(x, dvec), avgdigamma(y, dvec), digamma(k), digamma(len(x))
|
68 |
+
return (-a - b + c + d) / log(base)
|
69 |
+
|
70 |
+
|
71 |
+
def cmi(x, y, z, k=3, base=2):
|
72 |
+
""" Mutual information of x and y, conditioned on z
|
73 |
+
x, y, z should be a list of vectors, e.g. x = [[1.3], [3.7], [5.1], [2.4]]
|
74 |
+
if x is a one-dimensional scalar and we have four samples
|
75 |
+
"""
|
76 |
+
assert len(x) == len(y), "Lists should have same length"
|
77 |
+
assert k <= len(x) - 1, "Set k smaller than num. samples - 1"
|
78 |
+
intens = 1e-10 # small noise to break degeneracy, see doc.
|
79 |
+
x = [list(p + intens * nr.rand(len(x[0]))) for p in x]
|
80 |
+
y = [list(p + intens * nr.rand(len(y[0]))) for p in y]
|
81 |
+
z = [list(p + intens * nr.rand(len(z[0]))) for p in z]
|
82 |
+
points = zip2(x, y, z)
|
83 |
+
# Find nearest neighbors in joint space, p=inf means max-norm
|
84 |
+
tree = ss.cKDTree(points)
|
85 |
+
dvec = [tree.query(point, k + 1, p=float('inf'))[0][k] for point in points]
|
86 |
+
a, b, c, d = avgdigamma(zip2(x, z), dvec), avgdigamma(zip2(y, z), dvec), avgdigamma(z, dvec), digamma(k)
|
87 |
+
return (-a - b + c + d) / log(base)
|
88 |
+
|
89 |
+
|
90 |
+
def kldiv(x, xp, k=3, base=2):
|
91 |
+
""" KL Divergence between p and q for x~p(x), xp~q(x)
|
92 |
+
x, xp should be a list of vectors, e.g. x = [[1.3], [3.7], [5.1], [2.4]]
|
93 |
+
if x is a one-dimensional scalar and we have four samples
|
94 |
+
"""
|
95 |
+
assert k <= len(x) - 1, "Set k smaller than num. samples - 1"
|
96 |
+
assert k <= len(xp) - 1, "Set k smaller than num. samples - 1"
|
97 |
+
assert len(x[0]) == len(xp[0]), "Two distributions must have same dim."
|
98 |
+
d = len(x[0])
|
99 |
+
n = len(x)
|
100 |
+
m = len(xp)
|
101 |
+
const = log(m) - log(n - 1)
|
102 |
+
tree = ss.cKDTree(x)
|
103 |
+
treep = ss.cKDTree(xp)
|
104 |
+
nn = [tree.query(point, k + 1, p=float('inf'))[0][k] for point in x]
|
105 |
+
nnp = [treep.query(point, k, p=float('inf'))[0][k - 1] for point in x]
|
106 |
+
return (const + d * np.mean(map(log, nnp)) - d * np.mean(map(log, nn))) / log(base)
|
107 |
+
|
108 |
+
|
109 |
+
# DISCRETE ESTIMATORS
|
110 |
+
def entropyd(sx, base=2):
|
111 |
+
""" Discrete entropy estimator
|
112 |
+
Given a list of samples which can be any hashable object
|
113 |
+
"""
|
114 |
+
return entropyfromprobs(hist(sx), base=base)
|
115 |
+
|
116 |
+
|
117 |
+
def midd(x, y, base=2):
|
118 |
+
""" Discrete mutual information estimator
|
119 |
+
Given a list of samples which can be any hashable object
|
120 |
+
"""
|
121 |
+
return -entropyd(zip(x, y), base) + entropyd(x, base) + entropyd(y, base)
|
122 |
+
|
123 |
+
def cmidd(x, y, z):
|
124 |
+
""" Discrete mutual information estimator
|
125 |
+
Given a list of samples which can be any hashable object
|
126 |
+
"""
|
127 |
+
return entropyd(zip(y, z)) + entropyd(zip(x, z)) - entropyd(zip(x, y, z)) - entropyd(z)
|
128 |
+
|
129 |
+
def centropyd(x, y, base=2):
|
130 |
+
""" The classic K-L k-nearest neighbor continuous entropy estimator for the
|
131 |
+
entropy of X conditioned on Y.
|
132 |
+
"""
|
133 |
+
return entropyd(zip(x, y), base) - entropyd(y, base)
|
134 |
+
|
135 |
+
def tcd(xs, base=2):
|
136 |
+
xis = [entropyd(column(xs, i), base) for i in range(0, len(xs[0]))]
|
137 |
+
hx = entropyd(xs, base)
|
138 |
+
return np.sum(xis) - hx
|
139 |
+
|
140 |
+
def ctcd(xs, y, base=2):
|
141 |
+
xis = [centropyd(column(xs, i), y, base) for i in range(0, len(xs[0]))]
|
142 |
+
return np.sum(xis) - centropyd(xs, y, base)
|
143 |
+
|
144 |
+
def corexd(xs, ys, base=2):
|
145 |
+
cxis = [midd(column(xs, i), ys, base) for i in range(0, len(xs[0]))]
|
146 |
+
return np.sum(cxis) - midd(xs, ys, base)
|
147 |
+
|
148 |
+
def hist(sx):
|
149 |
+
sx = discretize(sx)
|
150 |
+
# Histogram from list of samples
|
151 |
+
d = dict()
|
152 |
+
for s in sx:
|
153 |
+
if type(s) == list:
|
154 |
+
s = tuple(s)
|
155 |
+
d[s] = d.get(s, 0) + 1
|
156 |
+
return map(lambda z: float(z) / len(sx), d.values())
|
157 |
+
|
158 |
+
|
159 |
+
def entropyfromprobs(probs, base=2):
|
160 |
+
# Turn a normalized list of probabilities of discrete outcomes into entropy (base 2)
|
161 |
+
return -sum(map(elog, probs)) / log(base)
|
162 |
+
|
163 |
+
|
164 |
+
def elog(x):
|
165 |
+
# for entropy, 0 log 0 = 0. but we get an error for putting log 0
|
166 |
+
if x <= 0. or x >= 1.:
|
167 |
+
return 0
|
168 |
+
else:
|
169 |
+
return x * log(x)
|
170 |
+
|
171 |
+
|
172 |
+
# MIXED ESTIMATORS
|
173 |
+
def micd(x, y, k=3, base=2, warning=True):
|
174 |
+
""" If x is continuous and y is discrete, compute mutual information
|
175 |
+
"""
|
176 |
+
overallentropy = entropy(x, k, base)
|
177 |
+
|
178 |
+
n = len(y)
|
179 |
+
word_dict = dict()
|
180 |
+
for i in range(len(y)):
|
181 |
+
if type(y[i]) == list:
|
182 |
+
y[i] = tuple(y[i])
|
183 |
+
for sample in y:
|
184 |
+
word_dict[sample] = word_dict.get(sample, 0) + 1. / n
|
185 |
+
yvals = list(set(word_dict.keys()))
|
186 |
+
|
187 |
+
mi = overallentropy
|
188 |
+
for yval in yvals:
|
189 |
+
xgiveny = [x[i] for i in range(n) if y[i] == yval]
|
190 |
+
if k <= len(xgiveny) - 1:
|
191 |
+
mi -= word_dict[yval] * entropy(xgiveny, k, base)
|
192 |
+
else:
|
193 |
+
if warning:
|
194 |
+
print("Warning, after conditioning, on y=", yval, " insufficient data. Assuming maximal entropy in this case.")
|
195 |
+
mi -= word_dict[yval] * overallentropy
|
196 |
+
return np.abs(mi) # units already applied
|
197 |
+
|
198 |
+
def midc(x, y, k=3, base=2, warning=True):
|
199 |
+
return micd(y, x, k, base, warning)
|
200 |
+
|
201 |
+
def centropydc(x, y, k=3, base=2, warning=True):
|
202 |
+
return entropyd(x, base) - midc(x, y, k, base, warning)
|
203 |
+
|
204 |
+
def centropycd(x, y, k=3, base=2, warning=True):
|
205 |
+
return entropy(x, k, base) - micd(x, y, k, base, warning)
|
206 |
+
|
207 |
+
def ctcdc(xs, y, k=3, base=2, warning=True):
|
208 |
+
xis = [centropydc(column(xs, i), y, k, base, warning) for i in range(0, len(xs[0]))]
|
209 |
+
return np.sum(xis) - centropydc(xs, y, k, base, warning)
|
210 |
+
|
211 |
+
def ctccd(xs, y, k=3, base=2, warning=True):
|
212 |
+
xis = [centropycd(column(xs, i), y, k, base, warning) for i in range(0, len(xs[0]))]
|
213 |
+
return np.sum(xis) - centropycd(xs, y, k, base, warning)
|
214 |
+
|
215 |
+
def corexcd(xs, ys, k=3, base=2, warning=True):
|
216 |
+
cxis = [micd(column(xs, i), ys, k, base, warning) for i in range(0, len(xs[0]))]
|
217 |
+
return np.sum(cxis) - micd(xs, ys, k, base, warning)
|
218 |
+
|
219 |
+
def corexdc(xs, ys, k=3, base=2, warning=True):
|
220 |
+
#cxis = [midc(column(xs, i), ys, k, base, warning) for i in range(0, len(xs[0]))]
|
221 |
+
#joint = midc(xs, ys, k, base, warning)
|
222 |
+
#return np.sum(cxis) - joint
|
223 |
+
return tcd(xs, base) - ctcdc(xs, ys, k, base, warning)
|
224 |
+
|
225 |
+
# UTILITY FUNCTIONS
|
226 |
+
def vectorize(scalarlist):
|
227 |
+
""" Turn a list of scalars into a list of one-d vectors
|
228 |
+
"""
|
229 |
+
return [[x] for x in scalarlist]
|
230 |
+
|
231 |
+
|
232 |
+
def shuffle_test(measure, x, y, z=False, ns=200, ci=0.95, **kwargs):
|
233 |
+
""" Shuffle test
|
234 |
+
Repeatedly shuffle the x-values and then estimate measure(x, y, [z]).
|
235 |
+
Returns the mean and conf. interval ('ci=0.95' default) over 'ns' runs.
|
236 |
+
'measure' could me mi, cmi, e.g. Keyword arguments can be passed.
|
237 |
+
Mutual information and CMI should have a mean near zero.
|
238 |
+
"""
|
239 |
+
xp = x[:] # A copy that we can shuffle
|
240 |
+
outputs = []
|
241 |
+
for i in range(ns):
|
242 |
+
random.shuffle(xp)
|
243 |
+
if z:
|
244 |
+
outputs.append(measure(xp, y, z, **kwargs))
|
245 |
+
else:
|
246 |
+
outputs.append(measure(xp, y, **kwargs))
|
247 |
+
outputs.sort()
|
248 |
+
return np.mean(outputs), (outputs[int((1. - ci) / 2 * ns)], outputs[int((1. + ci) / 2 * ns)])
|
249 |
+
|
250 |
+
|
251 |
+
# INTERNAL FUNCTIONS
|
252 |
+
|
253 |
+
def avgdigamma(points, dvec):
|
254 |
+
# This part finds number of neighbors in some radius in the marginal space
|
255 |
+
# returns expectation value of <psi(nx)>
|
256 |
+
N = len(points)
|
257 |
+
tree = ss.cKDTree(points)
|
258 |
+
avg = 0.
|
259 |
+
for i in range(N):
|
260 |
+
dist = dvec[i]
|
261 |
+
# subtlety, we don't include the boundary point,
|
262 |
+
# but we are implicitly adding 1 to kraskov def bc center point is included
|
263 |
+
num_points = len(tree.query_ball_point(points[i], dist - 1e-15, p=float('inf')))
|
264 |
+
avg += digamma(num_points) / N
|
265 |
+
return avg
|
266 |
+
|
267 |
+
|
268 |
+
def zip2(*args):
|
269 |
+
# zip2(x, y) takes the lists of vectors and makes it a list of vectors in a joint space
|
270 |
+
# E.g. zip2([[1], [2], [3]], [[4], [5], [6]]) = [[1, 4], [2, 5], [3, 6]]
|
271 |
+
return [sum(sublist, []) for sublist in zip(*args)]
|
272 |
+
|
273 |
+
def discretize(xs):
|
274 |
+
def discretize_one(x):
|
275 |
+
if len(x) > 1:
|
276 |
+
return tuple(x)
|
277 |
+
else:
|
278 |
+
return x[0]
|
279 |
+
# discretize(xs) takes a list of vectors and makes it a list of tuples or scalars
|
280 |
+
return [discretize_one(x) for x in xs]
|
281 |
+
|
282 |
+
if __name__ == "__main__":
|
283 |
+
print("NPEET: Non-parametric entropy estimation toolbox. See readme.pdf for details on usage.")
|
idnns/information/information_process.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
Calculate the information in the network
|
3 |
+
Can be by the full distribution rule (for small netowrk) or bt diffrenet approximation method
|
4 |
+
'''
|
5 |
+
import multiprocessing
|
6 |
+
import warnings
|
7 |
+
import numpy as np
|
8 |
+
import tensorflow as tf
|
9 |
+
import idnns.information.information_utilities as inf_ut
|
10 |
+
from idnns.networks import model as mo
|
11 |
+
from idnns.information.mutual_info_estimation import calc_varitional_information
|
12 |
+
warnings.filterwarnings("ignore")
|
13 |
+
from joblib import Parallel, delayed
|
14 |
+
NUM_CORES = multiprocessing.cpu_count()
|
15 |
+
from idnns.information.mutual_information_calculation import *
|
16 |
+
import numpy as np
|
17 |
+
|
18 |
+
|
19 |
+
def calc_information_for_layer(data, bins, unique_inverse_x, unique_inverse_y, pxs, pys1):
|
20 |
+
bins = bins.astype(np.float32)
|
21 |
+
digitized = bins[np.digitize(np.squeeze(data.reshape(1, -1)), bins) - 1].reshape(len(data), -1)
|
22 |
+
b2 = np.ascontiguousarray(digitized).view(
|
23 |
+
np.dtype((np.void, digitized.dtype.itemsize * digitized.shape[1])))
|
24 |
+
unique_array, unique_inverse_t, unique_counts = \
|
25 |
+
np.unique(b2, return_index=False, return_inverse=True, return_counts=True)
|
26 |
+
p_ts = unique_counts / float(sum(unique_counts))
|
27 |
+
PXs, PYs = np.asarray(pxs).T, np.asarray(pys1).T
|
28 |
+
local_IXT, local_ITY = calc_information_from_mat(PXs, PYs, p_ts, digitized, unique_inverse_x, unique_inverse_y,
|
29 |
+
unique_array)
|
30 |
+
return local_IXT, local_ITY
|
31 |
+
|
32 |
+
|
33 |
+
def calc_information_sampling(data, bins, pys1, pxs, label, b, b1, len_unique_a, p_YgX, unique_inverse_x,
|
34 |
+
unique_inverse_y, calc_DKL=False):
|
35 |
+
bins = bins.astype(np.float32)
|
36 |
+
num_of_bins = bins.shape[0]
|
37 |
+
# bins = stats.mstats.mquantiles(np.squeeze(data.reshape(1, -1)), np.linspace(0,1, num=num_of_bins))
|
38 |
+
# hist, bin_edges = np.histogram(np.squeeze(data.reshape(1, -1)), normed=True)
|
39 |
+
digitized = bins[np.digitize(np.squeeze(data.reshape(1, -1)), bins) - 1].reshape(len(data), -1)
|
40 |
+
b2 = np.ascontiguousarray(digitized).view(
|
41 |
+
np.dtype((np.void, digitized.dtype.itemsize * digitized.shape[1])))
|
42 |
+
unique_array, unique_inverse_t, unique_counts = \
|
43 |
+
np.unique(b2, return_index=False, return_inverse=True, return_counts=True)
|
44 |
+
p_ts = unique_counts / float(sum(unique_counts))
|
45 |
+
PXs, PYs = np.asarray(pxs).T, np.asarray(pys1).T
|
46 |
+
if calc_DKL:
|
47 |
+
pxy_given_T = np.array(
|
48 |
+
[calc_probs(i, unique_inverse_t, label, b, b1, len_unique_a) for i in range(0, len(unique_array))]
|
49 |
+
)
|
50 |
+
p_XgT = np.vstack(pxy_given_T[:, 0])
|
51 |
+
p_YgT = pxy_given_T[:, 1]
|
52 |
+
p_YgT = np.vstack(p_YgT).T
|
53 |
+
DKL_YgX_YgT = np.sum([inf_ut.KL(c_p_YgX, p_YgT.T) for c_p_YgX in p_YgX.T], axis=0)
|
54 |
+
H_Xgt = np.nansum(p_XgT * np.log2(p_XgT), axis=1)
|
55 |
+
local_IXT, local_ITY = calc_information_from_mat(PXs, PYs, p_ts, digitized, unique_inverse_x, unique_inverse_y,
|
56 |
+
unique_array)
|
57 |
+
return local_IXT, local_ITY
|
58 |
+
|
59 |
+
|
60 |
+
def calc_information_for_layer_with_other(data, bins, unique_inverse_x, unique_inverse_y, label,
|
61 |
+
b, b1, len_unique_a, pxs, p_YgX, pys1,
|
62 |
+
percent_of_sampling=50):
|
63 |
+
local_IXT, local_ITY = calc_information_sampling(data, bins, pys1, pxs, label, b, b1,
|
64 |
+
len_unique_a, p_YgX, unique_inverse_x,
|
65 |
+
unique_inverse_y)
|
66 |
+
number_of_indexs = int(data.shape[1] * (1. / 100 * percent_of_sampling))
|
67 |
+
indexs_of_sampls = np.random.choice(data.shape[1], number_of_indexs, replace=False)
|
68 |
+
if percent_of_sampling != 100:
|
69 |
+
sampled_data = data[:, indexs_of_sampls]
|
70 |
+
sampled_local_IXT, sampled_local_ITY = calc_information_sampling(
|
71 |
+
sampled_data, bins, pys1, pxs, label, b, b1, len_unique_a, p_YgX, unique_inverse_x, unique_inverse_y)
|
72 |
+
|
73 |
+
params = {}
|
74 |
+
params['local_IXT'] = local_IXT
|
75 |
+
params['local_ITY'] = local_ITY
|
76 |
+
return params
|
77 |
+
|
78 |
+
|
79 |
+
def calc_by_sampling_neurons(ws_iter_index, num_of_samples, label, sigma, bins, pxs):
|
80 |
+
iter_infomration = []
|
81 |
+
for j in range(len(ws_iter_index)):
|
82 |
+
data = ws_iter_index[j]
|
83 |
+
new_data = np.zeros((num_of_samples * data.shape[0], data.shape[1]))
|
84 |
+
labels = np.zeros((num_of_samples * label.shape[0], label.shape[1]))
|
85 |
+
x = np.zeros((num_of_samples * data.shape[0], 2))
|
86 |
+
for i in range(data.shape[0]):
|
87 |
+
cov_matrix = np.eye(data[i, :].shape[0]) * sigma
|
88 |
+
t_i = np.random.multivariate_normal(data[i, :], cov_matrix, num_of_samples)
|
89 |
+
new_data[num_of_samples * i:(num_of_samples * (i + 1)), :] = t_i
|
90 |
+
labels[num_of_samples * i:(num_of_samples * (i + 1)), :] = label[i, :]
|
91 |
+
x[num_of_samples * i:(num_of_samples * (i + 1)), 0] = i
|
92 |
+
b = np.ascontiguousarray(x).view(np.dtype((np.void, x.dtype.itemsize * x.shape[1])))
|
93 |
+
unique_array, unique_indices, unique_inverse_x, unique_counts = \
|
94 |
+
np.unique(b, return_index=True, return_inverse=True, return_counts=True)
|
95 |
+
b_y = np.ascontiguousarray(labels).view(np.dtype((np.void, labels.dtype.itemsize * labels.shape[1])))
|
96 |
+
unique_array_y, unique_indices_y, unique_inverse_y, unique_counts_y = \
|
97 |
+
np.unique(b_y, return_index=True, return_inverse=True, return_counts=True)
|
98 |
+
pys1 = unique_counts_y / float(np.sum(unique_counts_y))
|
99 |
+
iter_infomration.append(
|
100 |
+
calc_information_for_layer(data=new_data, bins=bins, unique_inverse_x=unique_inverse_x,
|
101 |
+
unique_inverse_y=unique_inverse_y, pxs=pxs, pys1=pys1))
|
102 |
+
params = np.array(iter_infomration)
|
103 |
+
return params
|
104 |
+
|
105 |
+
|
106 |
+
def calc_information_for_epoch(iter_index, interval_information_display, ws_iter_index, bins, unique_inverse_x,
|
107 |
+
unique_inverse_y, label, b, b1,
|
108 |
+
len_unique_a, pys, pxs, py_x, pys1, model_path, input_size, layerSize,
|
109 |
+
calc_vartional_information=False, calc_information_by_sampling=False,
|
110 |
+
calc_full_and_vartional=False, calc_regular_information=True, num_of_samples=100,
|
111 |
+
sigma=0.5, ss=[], ks=[]):
|
112 |
+
"""Calculate the information for all the layers for specific epoch"""
|
113 |
+
np.random.seed(None)
|
114 |
+
if calc_full_and_vartional:
|
115 |
+
# Vartional information
|
116 |
+
params_vartional = [
|
117 |
+
calc_varitional_information(ws_iter_index[i], label, model_path, i, len(ws_iter_index) - 1, iter_index,
|
118 |
+
input_size, layerSize, ss[i], pys, ks[i], search_sigma=False) for i in
|
119 |
+
range(len(ws_iter_index))]
|
120 |
+
# Full plug-in infomration
|
121 |
+
params_original = np.array(
|
122 |
+
[calc_information_for_layer_with_other(data=ws_iter_index[i], bins=bins, unique_inverse_x=unique_inverse_x,
|
123 |
+
unique_inverse_y=unique_inverse_y, label=label,
|
124 |
+
b=b, b1=b1, len_unique_a=len_unique_a, pxs=pxs,
|
125 |
+
p_YgX=py_x, pys1=pys1)
|
126 |
+
for i in range(len(ws_iter_index))])
|
127 |
+
# Combine them
|
128 |
+
params = []
|
129 |
+
for i in range(len(ws_iter_index)):
|
130 |
+
current_params = params_original[i]
|
131 |
+
current_params_vartional = params_vartional[i]
|
132 |
+
current_params['IXT_vartional'] = current_params_vartional['local_IXT']
|
133 |
+
current_params['ITY_vartional'] = current_params_vartional['local_ITY']
|
134 |
+
params.append(current_params)
|
135 |
+
elif calc_vartional_information:
|
136 |
+
params = [
|
137 |
+
calc_varitional_information(ws_iter_index[i], label, model_path, i, len(ws_iter_index) - 1, iter_index,
|
138 |
+
input_size, layerSize, ss[i], pys, ks[i], search_sigma=True) for i in
|
139 |
+
range(len(ws_iter_index))]
|
140 |
+
# Calc infomration of only subset of the neurons
|
141 |
+
elif calc_information_by_sampling:
|
142 |
+
parmas = calc_by_sampling_neurons(ws_iter_index=ws_iter_index, num_of_samples=num_of_samples, label=label,
|
143 |
+
sigma=sigma, bins=bins, pxs=pxs)
|
144 |
+
|
145 |
+
elif calc_regular_information:
|
146 |
+
params = np.array(
|
147 |
+
[calc_information_for_layer_with_other(data=ws_iter_index[i], bins=bins, unique_inverse_x=unique_inverse_x,
|
148 |
+
unique_inverse_y=unique_inverse_y, label=label,
|
149 |
+
b=b, b1=b1, len_unique_a=len_unique_a, pxs=pxs,
|
150 |
+
p_YgX=py_x, pys1=pys1)
|
151 |
+
for i in range(len(ws_iter_index))])
|
152 |
+
|
153 |
+
if np.mod(iter_index, interval_information_display) == 0:
|
154 |
+
print('Calculated The information of epoch number - {0}'.format(iter_index))
|
155 |
+
return params
|
156 |
+
|
157 |
+
|
158 |
+
def extract_probs(label, x):
|
159 |
+
"""calculate the probabilities of the given data and labels p(x), p(y) and (y|x)"""
|
160 |
+
pys = np.sum(label, axis=0) / float(label.shape[0])
|
161 |
+
b = np.ascontiguousarray(x).view(np.dtype((np.void, x.dtype.itemsize * x.shape[1])))
|
162 |
+
unique_array, unique_indices, unique_inverse_x, unique_counts = \
|
163 |
+
np.unique(b, return_index=True, return_inverse=True, return_counts=True)
|
164 |
+
unique_a = x[unique_indices]
|
165 |
+
b1 = np.ascontiguousarray(unique_a).view(np.dtype((np.void, unique_a.dtype.itemsize * unique_a.shape[1])))
|
166 |
+
pxs = unique_counts / float(np.sum(unique_counts))
|
167 |
+
p_y_given_x = []
|
168 |
+
for i in range(0, len(unique_array)):
|
169 |
+
indexs = unique_inverse_x == i
|
170 |
+
py_x_current = np.mean(label[indexs, :], axis=0)
|
171 |
+
p_y_given_x.append(py_x_current)
|
172 |
+
p_y_given_x = np.array(p_y_given_x).T
|
173 |
+
b_y = np.ascontiguousarray(label).view(np.dtype((np.void, label.dtype.itemsize * label.shape[1])))
|
174 |
+
unique_array_y, unique_indices_y, unique_inverse_y, unique_counts_y = \
|
175 |
+
np.unique(b_y, return_index=True, return_inverse=True, return_counts=True)
|
176 |
+
pys1 = unique_counts_y / float(np.sum(unique_counts_y))
|
177 |
+
return pys, pys1, p_y_given_x, b1, b, unique_a, unique_inverse_x, unique_inverse_y, pxs
|
178 |
+
|
179 |
+
|
180 |
+
def get_information(ws, x, label, num_of_bins, interval_information_display, model, layerSize,
|
181 |
+
calc_parallel=True, py_hats=0):
|
182 |
+
"""Calculate the information for the network for all the epochs and all the layers"""
|
183 |
+
print('Start calculating the information...')
|
184 |
+
bins = np.linspace(-1, 1, num_of_bins)
|
185 |
+
label = np.array(label).astype(float)
|
186 |
+
pys, pys1, p_y_given_x, b1, b, unique_a, unique_inverse_x, unique_inverse_y, pxs = extract_probs(label, x)
|
187 |
+
if calc_parallel:
|
188 |
+
params = np.array(Parallel(n_jobs=NUM_CORES
|
189 |
+
)(delayed(calc_information_for_epoch)
|
190 |
+
(i, interval_information_display, ws[i], bins, unique_inverse_x, unique_inverse_y,
|
191 |
+
label,
|
192 |
+
b, b1, len(unique_a), pys,
|
193 |
+
pxs, p_y_given_x, pys1, model.save_file, x.shape[1], layerSize)
|
194 |
+
for i in range(len(ws))))
|
195 |
+
else:
|
196 |
+
params = np.array([calc_information_for_epoch
|
197 |
+
(i, interval_information_display, ws[i], bins, unique_inverse_x, unique_inverse_y,
|
198 |
+
label, b, b1, len(unique_a), pys,
|
199 |
+
pxs, p_y_given_x, pys1, model.save_file, x.shape[1], layerSize)
|
200 |
+
for i in range(len(ws))])
|
201 |
+
return params
|
idnns/information/information_utilities.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
num = 1
|
3 |
+
def KL(a, b):
|
4 |
+
"""Calculate the Kullback Leibler divergence between a and b """
|
5 |
+
D_KL = np.nansum(np.multiply(a, np.log(np.divide(a, b+np.spacing(1)))), axis=1)
|
6 |
+
return D_KL
|
7 |
+
|
8 |
+
|
9 |
+
def calc_information(probTgivenXs, PYgivenTs, PXs, PYs):
|
10 |
+
"""Calculate the MI - I(X;T) and I(Y;T)"""
|
11 |
+
PTs = np.nansum(probTgivenXs*PXs, axis=1)
|
12 |
+
Ht = np.nansum(-np.dot(PTs, np.log2(PTs)))
|
13 |
+
Htx = - np.nansum((np.dot(np.multiply(probTgivenXs, np.log2(probTgivenXs)), PXs)))
|
14 |
+
Hyt = - np.nansum(np.dot(PYgivenTs*np.log2(PYgivenTs+np.spacing(1)), PTs))
|
15 |
+
Hy = np.nansum(-PYs * np.log2(PYs+np.spacing(1)))
|
16 |
+
IYT = Hy - Hyt
|
17 |
+
ITX = Ht - Htx
|
18 |
+
return ITX, IYT
|
19 |
+
|
20 |
+
def calc_information_1(probTgivenXs, PYgivenTs, PXs, PYs, PTs):
|
21 |
+
"""Calculate the MI - I(X;T) and I(Y;T)"""
|
22 |
+
#PTs = np.nansum(probTgivenXs*PXs, axis=1)
|
23 |
+
Ht = np.nansum(-np.dot(PTs, np.log2(PTs+np.spacing(1))))
|
24 |
+
Htx = - np.nansum((np.dot(np.multiply(probTgivenXs, np.log2(probTgivenXs+np.spacing(1))), PXs)))
|
25 |
+
Hyt = - np.nansum(np.dot(PYgivenTs*np.log2(PYgivenTs+np.spacing(1)), PTs))
|
26 |
+
Hy = np.nansum(-PYs * np.log2(PYs+np.spacing(1)))
|
27 |
+
IYT = Hy - Hyt
|
28 |
+
ITX = Ht - Htx
|
29 |
+
return ITX, IYT
|
30 |
+
|
31 |
+
def calc_information(probTgivenXs, PYgivenTs, PXs, PYs, PTs):
|
32 |
+
"""Calculate the MI - I(X;T) and I(Y;T)"""
|
33 |
+
#PTs = np.nansum(probTgivenXs*PXs, axis=1)
|
34 |
+
t_indeces = np.nonzero(PTs)
|
35 |
+
Ht = np.nansum(-np.dot(PTs, np.log2(PTs+np.spacing(1))))
|
36 |
+
Htx = - np.nansum((np.dot(np.multiply(probTgivenXs, np.log2(probTgivenXs)), PXs)))
|
37 |
+
Hyt = - np.nansum(np.dot(PYgivenTs*np.log2(PYgivenTs+np.spacing(1)), PTs))
|
38 |
+
Hy = np.nansum(-PYs * np.log2(PYs+np.spacing(1)))
|
39 |
+
|
40 |
+
IYT = Hy - Hyt
|
41 |
+
ITX = Ht - Htx
|
42 |
+
|
43 |
+
return ITX, IYT
|
44 |
+
|
45 |
+
|
46 |
+
def t_calc_information(p_x_given_t, PYgivenTs, PXs, PYs):
|
47 |
+
"""Calculate the MI - I(X;T) and I(Y;T)"""
|
48 |
+
Hx = np.nansum(-np.dot(PXs, np.log2(PXs)))
|
49 |
+
Hxt = - np.nansum((np.dot(np.multiply(p_x_given_t, np.log2(p_x_given_t)), PXs)))
|
50 |
+
Hyt = - np.nansum(np.dot(PYgivenTs*np.log2(PYgivenTs+np.spacing(1)), PTs))
|
51 |
+
Hy = np.nansum(-PYs * np.log2(PYs+np.spacing(1)))
|
52 |
+
IYT = Hy - Hyt
|
53 |
+
ITX = Hx - Hxt
|
54 |
+
return ITX, IYT
|
idnns/information/mutual_info_estimation.py
ADDED
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from scipy.optimize import minimize
|
3 |
+
import sys
|
4 |
+
import tensorflow as tf
|
5 |
+
from idnns.networks import model as mo
|
6 |
+
import contextlib
|
7 |
+
import idnns.information.entropy_estimators as ee
|
8 |
+
|
9 |
+
@contextlib.contextmanager
|
10 |
+
def printoptions(*args, **kwargs):
|
11 |
+
original = np.get_printoptions()
|
12 |
+
np.set_printoptions(*args, **kwargs)
|
13 |
+
try:
|
14 |
+
yield
|
15 |
+
finally:
|
16 |
+
np.set_printoptions(**original)
|
17 |
+
|
18 |
+
|
19 |
+
def optimiaze_func(s, diff_mat, d, N):
|
20 |
+
diff_mat1 = (1. / (np.sqrt(2. * np.pi) * (s ** 2) ** (d / 2.))) * np.exp(-diff_mat / (2. * s ** 2))
|
21 |
+
np.fill_diagonal(diff_mat1, 0)
|
22 |
+
diff_mat2 = (1. / (N - 1)) * np.sum(diff_mat1, axis=0)
|
23 |
+
diff_mat3 = np.sum(np.log2(diff_mat2), axis=0)
|
24 |
+
return -diff_mat3
|
25 |
+
|
26 |
+
|
27 |
+
def calc_all_sigams(data, sigmas):
|
28 |
+
batchs = 128
|
29 |
+
num_of_bins = 8
|
30 |
+
# bins = np.linspace(-1, 1, num_of_bins).astype(np.float32)
|
31 |
+
# bins = stats.mstats.mquantiles(np.squeeze(data.reshape(1, -1)), np.linspace(0,1, num=num_of_bins))
|
32 |
+
# data = bins[np.digitize(np.squeeze(data.reshape(1, -1)), bins) - 1].reshape(len(data), -1)
|
33 |
+
|
34 |
+
batch_points = np.rint(np.arange(0, data.shape[0] + 1, batchs)).astype(dtype=np.int32)
|
35 |
+
I_XT = []
|
36 |
+
num_of_rand = min(800, data.shape[1])
|
37 |
+
for sigma in sigmas:
|
38 |
+
# print sigma
|
39 |
+
I_XT_temp = 0
|
40 |
+
for i in range(0, len(batch_points) - 1):
|
41 |
+
new_data = data[batch_points[i]:batch_points[i + 1], :]
|
42 |
+
rand_indexs = np.random.randint(0, new_data.shape[1], num_of_rand)
|
43 |
+
new_data = new_data[:, :]
|
44 |
+
N = new_data.shape[0]
|
45 |
+
d = new_data.shape[1]
|
46 |
+
diff_mat = np.linalg.norm(((new_data[:, np.newaxis, :] - new_data)), axis=2)
|
47 |
+
# print diff_mat.shape, new_data.shape
|
48 |
+
s0 = 0.2
|
49 |
+
# DOTO -add leaveoneout validation
|
50 |
+
res = minimize(optimiaze_func, s0, args=(diff_mat, d, N), method='nelder-mead',
|
51 |
+
options={'xtol': 1e-8, 'disp': False, 'maxiter': 6})
|
52 |
+
eta = res.x
|
53 |
+
diff_mat0 = - 0.5 * (diff_mat / (sigma ** 2 + eta ** 2))
|
54 |
+
diff_mat1 = np.sum(np.exp(diff_mat0), axis=0)
|
55 |
+
diff_mat2 = -(1.0 / N) * np.sum(np.log2((1.0 / N) * diff_mat1))
|
56 |
+
I_XT_temp += diff_mat2 - d * np.log2((sigma ** 2) / (eta ** 2 + sigma ** 2))
|
57 |
+
# print diff_mat2 - d*np.log2((sigma**2)/(eta**2+sigma**2))
|
58 |
+
I_XT_temp /= len(batch_points)
|
59 |
+
I_XT.append(I_XT_temp)
|
60 |
+
sys.stdout.flush()
|
61 |
+
return I_XT
|
62 |
+
|
63 |
+
|
64 |
+
def estimate_IY_by_network(data, labels, from_layer=0):
|
65 |
+
if len(data.shape) > 2:
|
66 |
+
input_size = data.shape[1:]
|
67 |
+
else:
|
68 |
+
input_size = data.shape[1]
|
69 |
+
p_y_given_t_i = data
|
70 |
+
acc_all = [0]
|
71 |
+
if from_layer < 5:
|
72 |
+
|
73 |
+
acc_all = []
|
74 |
+
g1 = tf.Graph() ## This is one graph
|
75 |
+
with g1.as_default():
|
76 |
+
# For each epoch and for each layer we calculate the best decoder - we train a 2 lyaer network
|
77 |
+
cov_net = 4
|
78 |
+
model = mo.Model(input_size, [400, 100, 50], labels.shape[1], 0.0001, '', cov_net=cov_net,
|
79 |
+
from_layer=from_layer)
|
80 |
+
if from_layer < 5:
|
81 |
+
optimizer = model.optimize
|
82 |
+
init = tf.global_variables_initializer()
|
83 |
+
num_of_ephocs = 50
|
84 |
+
batch_size = 51
|
85 |
+
batch_points = np.rint(np.arange(0, data.shape[0] + 1, batch_size)).astype(dtype=np.int32)
|
86 |
+
if data.shape[0] not in batch_points:
|
87 |
+
batch_points = np.append(batch_points, [data.shape[0]])
|
88 |
+
with tf.Session(graph=g1) as sess:
|
89 |
+
sess.run(init)
|
90 |
+
if from_layer < 5:
|
91 |
+
for j in range(0, num_of_ephocs):
|
92 |
+
for i in range(0, len(batch_points) - 1):
|
93 |
+
batch_xs = data[batch_points[i]:batch_points[i + 1], :]
|
94 |
+
batch_ys = labels[batch_points[i]:batch_points[i + 1], :]
|
95 |
+
feed_dict = {model.x: batch_xs, model.labels: batch_ys}
|
96 |
+
if cov_net == 1:
|
97 |
+
feed_dict[model.drouput] = 0.5
|
98 |
+
optimizer.run(feed_dict)
|
99 |
+
p_y_given_t_i = []
|
100 |
+
batch_size = 256
|
101 |
+
batch_points = np.rint(np.arange(0, data.shape[0] + 1, batch_size)).astype(dtype=np.int32)
|
102 |
+
if data.shape[0] not in batch_points:
|
103 |
+
batch_points = np.append(batch_points, [data.shape[0]])
|
104 |
+
for i in range(0, len(batch_points) - 1):
|
105 |
+
batch_xs = data[batch_points[i]:batch_points[i + 1], :]
|
106 |
+
batch_ys = labels[batch_points[i]:batch_points[i + 1], :]
|
107 |
+
feed_dict = {model.x: batch_xs, model.labels: batch_ys}
|
108 |
+
if cov_net == 1:
|
109 |
+
feed_dict[model.drouput] = 1
|
110 |
+
p_y_given_t_i_local, acc = sess.run([model.prediction, model.accuracy],
|
111 |
+
feed_dict=feed_dict)
|
112 |
+
acc_all.append(acc)
|
113 |
+
if i == 0:
|
114 |
+
p_y_given_t_i = np.array(p_y_given_t_i_local)
|
115 |
+
else:
|
116 |
+
p_y_given_t_i = np.concatenate((p_y_given_t_i, np.array(p_y_given_t_i_local)), axis=0)
|
117 |
+
# print ("The accuracy of layer number - {} - {}".format(from_layer, np.mean(acc_all)))
|
118 |
+
max_indx = len(p_y_given_t_i)
|
119 |
+
labels_cut = labels[:max_indx, :]
|
120 |
+
true_label_index = np.argmax(labels_cut, 1)
|
121 |
+
s = np.log2(p_y_given_t_i[np.arange(len(p_y_given_t_i)), true_label_index])
|
122 |
+
I_TY = np.mean(s[np.isfinite(s)])
|
123 |
+
PYs = np.sum(labels_cut, axis=0) / labels_cut.shape[0]
|
124 |
+
Hy = np.nansum(-PYs * np.log2(PYs + np.spacing(1)))
|
125 |
+
I_TY = Hy + I_TY
|
126 |
+
I_TY = I_TY if I_TY >= 0 else 0
|
127 |
+
acc = np.mean(acc_all)
|
128 |
+
sys.stdout.flush()
|
129 |
+
return I_TY, acc
|
130 |
+
|
131 |
+
|
132 |
+
def calc_varitional_information(data, labels, model_path, layer_numer, num_of_layers, epoch_index, input_size,
|
133 |
+
layerSize, sigma, pys, ks,
|
134 |
+
search_sigma=False, estimate_y_by_network=False):
|
135 |
+
"""Calculate estimation of the information using vartional IB"""
|
136 |
+
# Assumpations
|
137 |
+
estimate_y_by_network = True
|
138 |
+
# search_sigma = False
|
139 |
+
data_x = data.reshape(data.shape[0], -1)
|
140 |
+
|
141 |
+
if search_sigma:
|
142 |
+
sigmas = np.linspace(0.2, 10, 20)
|
143 |
+
sigmas = [0.2]
|
144 |
+
|
145 |
+
else:
|
146 |
+
sigmas = [sigma]
|
147 |
+
if False:
|
148 |
+
I_XT = calc_all_sigams(data_x, sigmas)
|
149 |
+
else:
|
150 |
+
I_XT = 0
|
151 |
+
if estimate_y_by_network:
|
152 |
+
|
153 |
+
I_TY, acc = estimate_IY_by_network(data, labels, from_layer=layer_numer)
|
154 |
+
else:
|
155 |
+
I_TY = 0
|
156 |
+
with printoptions(precision=3, suppress=True, formatter={'float': '{: 0.3f}'.format}):
|
157 |
+
print('[{0}:{1}] - I(X;T) - {2}, I(X;Y) - {3}, accuracy - {4}'.format(epoch_index, layer_numer,
|
158 |
+
np.array(I_XT).flatten(), I_TY, acc))
|
159 |
+
sys.stdout.flush()
|
160 |
+
|
161 |
+
# I_est = mutual_inform[ation((data, labels[:, 0][:, None]), PYs, k=ks)
|
162 |
+
# I_est,I_XT = 0, 0
|
163 |
+
params = {}
|
164 |
+
# params['DKL_YgX_YgT'] = DKL_YgX_YgT
|
165 |
+
# params['pts'] = p_ts
|
166 |
+
# params['H_Xgt'] = H_Xgt
|
167 |
+
params['local_IXT'] = I_XT
|
168 |
+
params['local_ITY'] = I_TY
|
169 |
+
return params
|
170 |
+
|
171 |
+
def estimate_Information(Xs, Ys, Ts):
|
172 |
+
"""Estimation of the MI from missing data based on k-means clustring"""
|
173 |
+
estimate_IXT = ee.mi(Xs, Ts)
|
174 |
+
estimate_IYT = ee.mi(Ys, Ts)
|
175 |
+
# estimate_IXT1 = ee.mi(Xs, Ts)
|
176 |
+
# estimate_IYT1 = ee.mi(Ys, Ts)
|
177 |
+
return estimate_IXT, estimate_IYT
|
178 |
+
|
idnns/information/mutual_information_calculation.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'Calculation of the full plug-in distribuation'
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import multiprocessing
|
5 |
+
from joblib import Parallel, delayed
|
6 |
+
|
7 |
+
NUM_CORES = multiprocessing.cpu_count()
|
8 |
+
|
9 |
+
|
10 |
+
def calc_entropy_for_specipic_t(current_ts, px_i):
|
11 |
+
"""Calc entropy for specipic t"""
|
12 |
+
b2 = np.ascontiguousarray(current_ts).view(
|
13 |
+
np.dtype((np.void, current_ts.dtype.itemsize * current_ts.shape[1])))
|
14 |
+
unique_array, unique_inverse_t, unique_counts = \
|
15 |
+
np.unique(b2, return_index=False, return_inverse=True, return_counts=True)
|
16 |
+
p_current_ts = unique_counts / float(sum(unique_counts))
|
17 |
+
p_current_ts = np.asarray(p_current_ts, dtype=np.float64).T
|
18 |
+
H2X = px_i * (-np.sum(p_current_ts * np.log2(p_current_ts)))
|
19 |
+
return H2X
|
20 |
+
|
21 |
+
|
22 |
+
def calc_condtion_entropy(px, t_data, unique_inverse_x):
|
23 |
+
# Condition entropy of t given x
|
24 |
+
H2X_array = np.array(
|
25 |
+
Parallel(n_jobs=NUM_CORES)(delayed(calc_entropy_for_specipic_t)(t_data[unique_inverse_x == i, :], px[i])
|
26 |
+
for i in range(px.shape[0])))
|
27 |
+
H2X = np.sum(H2X_array)
|
28 |
+
return H2X
|
29 |
+
|
30 |
+
|
31 |
+
def calc_information_from_mat(px, py, ps2, data, unique_inverse_x, unique_inverse_y, unique_array):
|
32 |
+
"""Calculate the MI based on binning of the data"""
|
33 |
+
H2 = -np.sum(ps2 * np.log2(ps2))
|
34 |
+
H2X = calc_condtion_entropy(px, data, unique_inverse_x)
|
35 |
+
H2Y = calc_condtion_entropy(py.T, data, unique_inverse_y)
|
36 |
+
IY = H2 - H2Y
|
37 |
+
IX = H2 - H2X
|
38 |
+
return IX, IY
|
39 |
+
|
40 |
+
|
41 |
+
def calc_probs(t_index, unique_inverse, label, b, b1, len_unique_a):
|
42 |
+
"""Calculate the p(x|T) and p(y|T)"""
|
43 |
+
indexs = unique_inverse == t_index
|
44 |
+
p_y_ts = np.sum(label[indexs], axis=0) / label[indexs].shape[0]
|
45 |
+
unique_array_internal, unique_counts_internal = \
|
46 |
+
np.unique(b[indexs], return_index=False, return_inverse=False, return_counts=True)
|
47 |
+
indexes_x = np.where(np.in1d(b1, b[indexs]))
|
48 |
+
p_x_ts = np.zeros(len_unique_a)
|
49 |
+
p_x_ts[indexes_x] = unique_counts_internal / float(sum(unique_counts_internal))
|
50 |
+
return p_x_ts, p_y_ts
|
idnns/networks/__init__.py
ADDED
File without changes
|
idnns/networks/information_network.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import _pickle as cPickle
|
2 |
+
import multiprocessing
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
import numpy as np
|
6 |
+
from joblib import Parallel, delayed
|
7 |
+
import idnns.networks.network as nn
|
8 |
+
from idnns.information import information_process as inn
|
9 |
+
from idnns.plots import plot_figures as plt_fig
|
10 |
+
from idnns.networks import network_paramters as netp
|
11 |
+
from idnns.networks.utils import load_data
|
12 |
+
|
13 |
+
# from idnns.network import utils
|
14 |
+
# import idnns.plots.plot_gradients as plt_grads
|
15 |
+
NUM_CORES = multiprocessing.cpu_count()
|
16 |
+
|
17 |
+
|
18 |
+
class informationNetwork():
|
19 |
+
"""A class that store the network, train it and calc it's information (can be several of networks) """
|
20 |
+
|
21 |
+
def __init__(self, rand_int=0, num_of_samples=None, args=None):
|
22 |
+
if args == None:
|
23 |
+
args = netp.get_default_parser(num_of_samples)
|
24 |
+
self.cov_net = args.cov_net
|
25 |
+
self.calc_information = args.calc_information
|
26 |
+
self.run_in_parallel = args.run_in_parallel
|
27 |
+
self.num_ephocs = args.num_ephocs
|
28 |
+
self.learning_rate = args.learning_rate
|
29 |
+
self.batch_size = args.batch_size
|
30 |
+
self.activation_function = args.activation_function
|
31 |
+
self.interval_accuracy_display = args.interval_accuracy_display
|
32 |
+
self.save_grads = args.save_grads
|
33 |
+
self.num_of_repeats = args.num_of_repeats
|
34 |
+
self.calc_information_last = args.calc_information_last
|
35 |
+
self.num_of_bins = args.num_of_bins
|
36 |
+
self.interval_information_display = args.interval_information_display
|
37 |
+
self.save_ws = args.save_ws
|
38 |
+
self.name = args.data_dir + args.data_name
|
39 |
+
# The arch of the networks
|
40 |
+
self.layers_sizes = netp.select_network_arch(args.net_type)
|
41 |
+
# The percents of the train data samples
|
42 |
+
self.train_samples = np.linspace(1, 100, 199)[[[x * 2 - 2 for x in index] for index in args.inds]]
|
43 |
+
# The indexs that we want to calculate the information for them in logspace interval
|
44 |
+
self.epochs_indexes = np.unique(
|
45 |
+
np.logspace(np.log2(args.start_samples), np.log2(args.num_ephocs), args.num_of_samples, dtype=int,
|
46 |
+
base=2)) - 1
|
47 |
+
max_size = np.max([len(layers_size) for layers_size in self.layers_sizes])
|
48 |
+
# load data
|
49 |
+
self.data_sets = load_data(self.name, args.random_labels)
|
50 |
+
# create arrays for saving the data
|
51 |
+
self.ws, self.grads, self.information, self.models, self.names, self.networks, self.weights = [
|
52 |
+
[[[[None] for k in range(len(self.train_samples))] for j in range(len(self.layers_sizes))]
|
53 |
+
for i in range(self.num_of_repeats)] for _ in range(7)]
|
54 |
+
|
55 |
+
self.loss_train, self.loss_test, self.test_error, self.train_error, self.l1_norms, self.l2_norms = \
|
56 |
+
[np.zeros((self.num_of_repeats, len(self.layers_sizes), len(self.train_samples), len(self.epochs_indexes)))
|
57 |
+
for _ in range(6)]
|
58 |
+
|
59 |
+
params = {'sampleLen': len(self.train_samples),
|
60 |
+
'nDistSmpls': args.nDistSmpls,
|
61 |
+
'layerSizes': ",".join(str(i) for i in self.layers_sizes[0]), 'nEpoch': args.num_ephocs, 'batch': args.batch_size,
|
62 |
+
'nRepeats': args.num_of_repeats, 'nEpochInds': len(self.epochs_indexes),
|
63 |
+
'LastEpochsInds': self.epochs_indexes[-1], 'DataName': args.data_name,
|
64 |
+
'lr': args.learning_rate}
|
65 |
+
|
66 |
+
self.name_to_save = args.name + "_" + "_".join([str(i) + '=' + str(params[i]) for i in params])
|
67 |
+
params['train_samples'], params['CPUs'], params[
|
68 |
+
'directory'], params['epochsInds'] = self.train_samples, NUM_CORES, self.name_to_save, self.epochs_indexes
|
69 |
+
self.params = params
|
70 |
+
self.rand_int = rand_int
|
71 |
+
# If we trained already the network
|
72 |
+
self.traind_network = False
|
73 |
+
|
74 |
+
def save_data(self, parent_dir='jobs/', file_to_save='data.pickle'):
|
75 |
+
"""Save the data to the file """
|
76 |
+
directory = '{0}/{1}{2}/'.format(os.getcwd(), parent_dir, self.params['directory'])
|
77 |
+
|
78 |
+
data = {'information': self.information,
|
79 |
+
'test_error': self.test_error, 'train_error': self.train_error, 'var_grad_val': self.grads,
|
80 |
+
'loss_test': self.loss_test, 'loss_train': self.loss_train, 'params': self.params
|
81 |
+
, 'l1_norms': self.l1_norms, 'weights': self.weights, 'ws': self.ws}
|
82 |
+
|
83 |
+
if not os.path.exists(directory):
|
84 |
+
os.makedirs(directory)
|
85 |
+
self.dir_saved = directory
|
86 |
+
with open(self.dir_saved + file_to_save, 'wb') as f:
|
87 |
+
cPickle.dump(data, f, protocol=2)
|
88 |
+
|
89 |
+
def run_network(self):
|
90 |
+
"""Train and calculated the network's information"""
|
91 |
+
if self.run_in_parallel:
|
92 |
+
results = Parallel(n_jobs=NUM_CORES)(delayed(nn.train_network)
|
93 |
+
(self.layers_sizes[j],
|
94 |
+
self.num_ephocs, self.learning_rate, self.batch_size,
|
95 |
+
self.epochs_indexes, self.save_grads, self.data_sets,
|
96 |
+
self.activation_function,
|
97 |
+
self.train_samples, self.interval_accuracy_display,
|
98 |
+
self.calc_information,
|
99 |
+
self.calc_information_last, self.num_of_bins,
|
100 |
+
self.interval_information_display, self.save_ws, self.rand_int,
|
101 |
+
self.cov_net)
|
102 |
+
for i in range(len(self.train_samples)) for j in
|
103 |
+
range(len(self.layers_sizes)) for k in range(self.num_of_repeats))
|
104 |
+
|
105 |
+
else:
|
106 |
+
results = [nn.train_and_calc_inf_network(i, j, k,
|
107 |
+
self.layers_sizes[j],
|
108 |
+
self.num_ephocs, self.learning_rate, self.batch_size,
|
109 |
+
self.epochs_indexes, self.save_grads, self.data_sets,
|
110 |
+
self.activation_function,
|
111 |
+
self.train_samples, self.interval_accuracy_display,
|
112 |
+
self.calc_information,
|
113 |
+
self.calc_information_last, self.num_of_bins,
|
114 |
+
self.interval_information_display,
|
115 |
+
self.save_ws, self.rand_int, self.cov_net)
|
116 |
+
for i in range(len(self.train_samples)) for j in range(len(self.layers_sizes)) for k in
|
117 |
+
range(self.num_of_repeats)]
|
118 |
+
|
119 |
+
# Extract all the measures and orgainze it
|
120 |
+
for i in range(len(self.train_samples)):
|
121 |
+
for j in range(len(self.layers_sizes)):
|
122 |
+
for k in range(self.num_of_repeats):
|
123 |
+
index = i * len(self.layers_sizes) * self.num_of_repeats + j * self.num_of_repeats + k
|
124 |
+
current_network = results[index]
|
125 |
+
self.networks[k][j][i] = current_network
|
126 |
+
self.ws[k][j][i] = current_network['ws']
|
127 |
+
self.weights[k][j][i] = current_network['weights']
|
128 |
+
self.information[k][j][i] = current_network['information']
|
129 |
+
self.grads[k][i][i] = current_network['gradients']
|
130 |
+
self.test_error[k, j, i, :] = current_network['test_prediction']
|
131 |
+
self.train_error[k, j, i, :] = current_network['train_prediction']
|
132 |
+
self.loss_test[k, j, i, :] = current_network['loss_test']
|
133 |
+
self.loss_train[k, j, i, :] = current_network['loss_train']
|
134 |
+
self.traind_network = True
|
135 |
+
|
136 |
+
def print_information(self):
|
137 |
+
"""Print the networks params"""
|
138 |
+
for val in self.params:
|
139 |
+
if val != 'epochsInds':
|
140 |
+
print (val, self.params[val])
|
141 |
+
|
142 |
+
def calc_information(self):
|
143 |
+
"""Calculate the infomration of the network for all the epochs - only valid if we save the activation values and trained the network"""
|
144 |
+
if self.traind_network and self.save_ws:
|
145 |
+
self.information = np.array(
|
146 |
+
[inn.get_information(self.ws[k][j][i], self.data_sets.data, self.data_sets.labels,
|
147 |
+
self.args.num_of_bins, self.args.interval_information_display, self.epochs_indexes)
|
148 |
+
for i in range(len(self.train_samples)) for j in
|
149 |
+
range(len(self.layers_sizes)) for k in range(self.args.num_of_repeats)])
|
150 |
+
else:
|
151 |
+
print ('Cant calculate the infomration of the networks!!!')
|
152 |
+
|
153 |
+
def calc_information_last(self):
|
154 |
+
"""Calculate the information of the last epoch"""
|
155 |
+
if self.traind_network and self.save_ws:
|
156 |
+
return np.array([inn.get_information([self.ws[k][j][i][-1]], self.data_sets.data, self.data_sets.labels,
|
157 |
+
self.args.num_of_bins, self.args.interval_information_display,
|
158 |
+
self.epochs_indexes)
|
159 |
+
for i in range(len(self.train_samples)) for j in
|
160 |
+
range(len(self.layers_sizes)) for k in range(self.args.num_of_repeats)])
|
161 |
+
|
162 |
+
def plot_network(self):
|
163 |
+
str_names = [[self.dir_saved]]
|
164 |
+
mode = 2
|
165 |
+
save_name = 'figure'
|
166 |
+
plt_fig.plot_figures(str_names, mode, save_name)
|
idnns/networks/model.py
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import functools
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from idnns.networks.utils import _convert_string_dtype
|
5 |
+
from idnns.networks.models import multi_layer_perceptron
|
6 |
+
from idnns.networks.models import deepnn
|
7 |
+
from idnns.networks.ops import *
|
8 |
+
import tensorflow.compat.v1 as tf
|
9 |
+
tf.disable_v2_behavior()
|
10 |
+
|
11 |
+
|
12 |
+
def lazy_property(function):
|
13 |
+
attribute = '_cache_' + function.__name__
|
14 |
+
|
15 |
+
@property
|
16 |
+
@functools.wraps(function)
|
17 |
+
def decorator(self):
|
18 |
+
# print hasattr(self, attribute)
|
19 |
+
if not hasattr(self, attribute):
|
20 |
+
setattr(self, attribute, function(self))
|
21 |
+
return getattr(self, attribute)
|
22 |
+
|
23 |
+
return decorator
|
24 |
+
|
25 |
+
|
26 |
+
class Model:
|
27 |
+
"""A class that represent model of network"""
|
28 |
+
|
29 |
+
def __init__(self, input_size, layerSize, num_of_classes, learning_rate_local=0.001, save_file='',
|
30 |
+
activation_function=0, cov_net=False):
|
31 |
+
self.covnet = cov_net
|
32 |
+
self.input_size = input_size
|
33 |
+
self.layerSize = layerSize
|
34 |
+
self.all_layer_sizes = np.copy(layerSize)
|
35 |
+
self.all_layer_sizes = np.insert(self.all_layer_sizes, 0, input_size)
|
36 |
+
self.num_of_classes = num_of_classes
|
37 |
+
self._num_of_layers = len(layerSize) + 1
|
38 |
+
self.learning_rate_local = learning_rate_local
|
39 |
+
self._save_file = save_file
|
40 |
+
self.hidden = None
|
41 |
+
self.savers = []
|
42 |
+
if activation_function == 1:
|
43 |
+
self.activation_function = tf.nn.relu
|
44 |
+
elif activation_function == 2:
|
45 |
+
self.activation_function = None
|
46 |
+
else:
|
47 |
+
self.activation_function = tf.nn.tanh
|
48 |
+
self.prediction
|
49 |
+
self.optimize
|
50 |
+
self.accuracy
|
51 |
+
|
52 |
+
def initilizae_layer(self, name_scope, row_size, col_size, activation_function, last_hidden):
|
53 |
+
# Bulid layer of the network with weights and biases
|
54 |
+
weights = get_scope_variable(name_scope=name_scope, var="weights",
|
55 |
+
shape=[row_size, col_size],
|
56 |
+
initializer=tf.truncated_normal_initializer(mean=0.0, stddev=1.0 / np.sqrt(
|
57 |
+
float(row_size))))
|
58 |
+
biases = get_scope_variable(name_scope=name_scope, var='biases', shape=[col_size],
|
59 |
+
initializer=tf.constant_initializer(0.0))
|
60 |
+
|
61 |
+
self.weights_all.append(weights)
|
62 |
+
self.biases_all.append(biases)
|
63 |
+
variable_summaries(weights)
|
64 |
+
variable_summaries(biases)
|
65 |
+
with tf.variable_scope(name_scope) as scope:
|
66 |
+
input = tf.matmul(last_hidden, weights) + biases
|
67 |
+
if activation_function == None:
|
68 |
+
output = input
|
69 |
+
else:
|
70 |
+
output = activation_function(input, name='output')
|
71 |
+
self.inputs.append(input)
|
72 |
+
self.hidden.append(output)
|
73 |
+
return output
|
74 |
+
|
75 |
+
@property
|
76 |
+
def num_of_layers(self):
|
77 |
+
return self._num_of_layers
|
78 |
+
|
79 |
+
@property
|
80 |
+
def hidden_layers(self):
|
81 |
+
"""The hidden layers of the netowrk"""
|
82 |
+
if self.hidden is None:
|
83 |
+
self.hidden, self.inputs, self.weights_all, self.biases_all = [], [], [], []
|
84 |
+
last_hidden = self.x
|
85 |
+
if self.covnet == 1:
|
86 |
+
y_conv, self._drouput, self.hidden, self.inputs = deepnn(self.x)
|
87 |
+
elif self.covnet == 2:
|
88 |
+
y_c, self.hidden, self.inputs = multi_layer_perceptron(self.x, self.input_size, self.num_of_classes,
|
89 |
+
self.layerSize[0], self.layerSize[1])
|
90 |
+
else:
|
91 |
+
|
92 |
+
self._drouput = 'dr'
|
93 |
+
# self.hidden.append(self.x)
|
94 |
+
for i in range(1, len(self.all_layer_sizes)):
|
95 |
+
name_scope = 'hidden' + str(i - 1)
|
96 |
+
row_size, col_size = self.all_layer_sizes[i - 1], self.all_layer_sizes[i]
|
97 |
+
activation_function = self.activation_function
|
98 |
+
last_hidden = self.initilizae_layer(name_scope, row_size, col_size, activation_function,
|
99 |
+
last_hidden)
|
100 |
+
name_scope = 'final_layer'
|
101 |
+
row_size, col_size = self.layerSize[-1], self.num_of_classes
|
102 |
+
activation_function = tf.nn.softmax
|
103 |
+
last_hidden = self.initilizae_layer(name_scope, row_size, col_size, activation_function, last_hidden)
|
104 |
+
return self.hidden
|
105 |
+
|
106 |
+
@lazy_property
|
107 |
+
def prediction(self):
|
108 |
+
logits = self.hidden_layers[-1]
|
109 |
+
return logits
|
110 |
+
|
111 |
+
@lazy_property
|
112 |
+
def drouput(self):
|
113 |
+
return self._drouput
|
114 |
+
|
115 |
+
@property
|
116 |
+
def optimize(self):
|
117 |
+
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate_local).minimize(self.cross_entropy)
|
118 |
+
|
119 |
+
return optimizer
|
120 |
+
|
121 |
+
@lazy_property
|
122 |
+
def x(self):
|
123 |
+
return tf.placeholder(tf.float32, shape=[None, self.input_size], name='x')
|
124 |
+
|
125 |
+
@lazy_property
|
126 |
+
def labels(self):
|
127 |
+
return tf.placeholder(tf.float32, shape=[None, self.num_of_classes], name='y_true')
|
128 |
+
|
129 |
+
@lazy_property
|
130 |
+
def accuracy(self):
|
131 |
+
correct_prediction = tf.equal(tf.argmax(self.prediction, 1), tf.argmax(self.labels, 1))
|
132 |
+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
133 |
+
tf.summary.scalar('accuracy', accuracy)
|
134 |
+
return accuracy
|
135 |
+
|
136 |
+
@lazy_property
|
137 |
+
def cross_entropy(self):
|
138 |
+
cross_entropy = tf.reduce_mean(
|
139 |
+
-tf.reduce_sum(self.labels * tf.log(tf.clip_by_value(self.prediction, 1e-50, 1.0)), reduction_indices=[1]))
|
140 |
+
tf.summary.scalar('cross_entropy', cross_entropy)
|
141 |
+
return cross_entropy
|
142 |
+
|
143 |
+
@property
|
144 |
+
def save_file(self):
|
145 |
+
return self._save_file
|
146 |
+
|
147 |
+
def inference(self, data):
|
148 |
+
"""Return the predication of the network with the given data"""
|
149 |
+
with tf.Session() as sess:
|
150 |
+
self.saver.restore(sess, './' + self.save_file)
|
151 |
+
feed_dict = {self.x: data}
|
152 |
+
pred = sess.run(self.prediction, feed_dict=feed_dict)
|
153 |
+
return pred
|
154 |
+
|
155 |
+
def inference_default(self, data):
|
156 |
+
session = tf.get_default_session()
|
157 |
+
feed_dict = {self.x: data}
|
158 |
+
pred = session.run(self.prediction, feed_dict=feed_dict)
|
159 |
+
return pred
|
160 |
+
|
161 |
+
def get_layer_with_inference(self, data, layer_index, epoch_index):
|
162 |
+
"""Return the layer activation's values with the results of the network"""
|
163 |
+
with tf.Session() as sess:
|
164 |
+
self.savers[epoch_index].restore(sess, './' + self.save_file + str(epoch_index))
|
165 |
+
feed_dict = {self.hidden_layers[layer_index]: data[:, 0:self.hidden_layers[layer_index]._shape[1]]}
|
166 |
+
pred, layer_values = sess.run([self.prediction, self.hidden_layers[layer_index]], feed_dict=feed_dict)
|
167 |
+
return pred, layer_values
|
168 |
+
|
169 |
+
def calc_layer_values(self, X, layer_index):
|
170 |
+
"""Return the layer's values"""
|
171 |
+
with tf.Session() as sess:
|
172 |
+
self.savers[-1].restore(sess, './' + self.save_file)
|
173 |
+
feed_dict = {self.x: X}
|
174 |
+
layer_values = sess.run(self.hidden_layers[layer_index], feed_dict=feed_dict)
|
175 |
+
return layer_values
|
176 |
+
|
177 |
+
def update_weights_and_calc_values_temp(self, d_w_i_j, layer_to_perturbe, i, j, X):
|
178 |
+
"""Update the weights of the given layer cacl the output and return it to the original values"""
|
179 |
+
if layer_to_perturbe + 1 >= len(self.hidden_layers):
|
180 |
+
scope_name = 'softmax_linear'
|
181 |
+
else:
|
182 |
+
scope_name = "hidden" + str(layer_to_perturbe)
|
183 |
+
weights = get_scope_variable(scope_name, "weights", shape=None, initializer=None)
|
184 |
+
session = tf.get_default_session()
|
185 |
+
weights_values = weights.eval(session=session)
|
186 |
+
weights_values_pert = weights_values
|
187 |
+
weights_values_pert[i, j] += d_w_i_j
|
188 |
+
set_value(weights, weights_values_pert)
|
189 |
+
feed_dict = {self.x: X}
|
190 |
+
layer_values = session.run(self.hidden_layers[layer_to_perturbe], feed_dict=feed_dict)
|
191 |
+
set_value(weights, weights_values)
|
192 |
+
return layer_values
|
193 |
+
|
194 |
+
def update_weights(self, d_w0, layer_to_perturbe):
|
195 |
+
"""Update the weights' values of the given layer"""
|
196 |
+
weights = get_scope_variable("hidden" + str(layer_to_perturbe), "weights", shape=None, initializer=None)
|
197 |
+
session = tf.get_default_session()
|
198 |
+
weights_values = weights.eval(session=session)
|
199 |
+
set_value(weights, weights_values + d_w0)
|
200 |
+
|
201 |
+
def get_wights_size(self, layer_to_perturbe):
|
202 |
+
"""Return the size of the given layer"""
|
203 |
+
weights = get_scope_variable("hidden" + str(layer_to_perturbe), "weights", shape=None, initializer=None)
|
204 |
+
return weights._initial_value.shape[1].value, weights._initial_value.shape[0].value
|
205 |
+
|
206 |
+
def get_layer_input(self, layer_to_perturbe, X):
|
207 |
+
"""Return the input of the given layer for the given data"""
|
208 |
+
session = tf.get_default_session()
|
209 |
+
inputs = self.inputs[layer_to_perturbe]
|
210 |
+
feed_dict = {self.x: X}
|
211 |
+
layer_values = session.run(inputs, feed_dict=feed_dict)
|
212 |
+
return layer_values
|
idnns/networks/models.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from idnns.networks.ops import *
|
3 |
+
|
4 |
+
|
5 |
+
def multi_layer_perceptron(x, n_input, n_classes, n_hidden_1, n_hidden_2):
|
6 |
+
hidden = []
|
7 |
+
input = []
|
8 |
+
hidden.append(x)
|
9 |
+
# Network Parameters
|
10 |
+
# n_input = x.shape[0] # MNIST data input (img shape: 28*28)
|
11 |
+
# n_classes = 10 # MNIST total classes (0-9 digits)
|
12 |
+
|
13 |
+
weights = {
|
14 |
+
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
|
15 |
+
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
|
16 |
+
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
|
17 |
+
}
|
18 |
+
biases = {
|
19 |
+
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
|
20 |
+
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
|
21 |
+
'out': tf.Variable(tf.random_normal([n_classes]))
|
22 |
+
}
|
23 |
+
|
24 |
+
# Hidden layer with RELU activation
|
25 |
+
layer_1_input = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
|
26 |
+
layer_1 = tf.nn.relu(layer_1_input)
|
27 |
+
input.append(layer_1)
|
28 |
+
hidden.append(layer_1)
|
29 |
+
# Hidden layer with RELU activation
|
30 |
+
layer_2_input = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
|
31 |
+
layer_2 = tf.nn.relu(layer_2_input)
|
32 |
+
input.append(layer_2_input)
|
33 |
+
hidden.append(layer_2)
|
34 |
+
# Output layer with linear activation
|
35 |
+
input_y = tf.matmul(layer_2, weights['out']) + biases['out']
|
36 |
+
y_output = tf.nn.softmax(input_y)
|
37 |
+
input.append(y_output)
|
38 |
+
hidden.append(y_output)
|
39 |
+
return y_output, hidden, input
|
40 |
+
|
41 |
+
|
42 |
+
def deepnn(x):
|
43 |
+
"""deepnn builds the graph for a deep net for classifying digits.
|
44 |
+
Args:
|
45 |
+
x: an input tensor with the dimensions (N_examples, 784), where 784 is the
|
46 |
+
number of pixels in a standard MNIST image.
|
47 |
+
Returns:
|
48 |
+
A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values
|
49 |
+
equal to the logits of classifying the digit into one of 10 classes (the
|
50 |
+
digits 0-9). keep_prob is a scalar placeholder for the probability of
|
51 |
+
dropout.
|
52 |
+
"""
|
53 |
+
hidden = []
|
54 |
+
input = []
|
55 |
+
x_image = tf.reshape(x, [-1, 28, 28, 1])
|
56 |
+
hidden.append(x)
|
57 |
+
# First convolutional layer - maps one grayscale image to 32 feature maps.
|
58 |
+
with tf.name_scope('conv1'):
|
59 |
+
with tf.name_scope('weights'):
|
60 |
+
W_conv1 = weight_variable([5, 5, 1, 32])
|
61 |
+
variable_summaries(W_conv1)
|
62 |
+
with tf.name_scope('biases'):
|
63 |
+
b_conv1 = bias_variable([32])
|
64 |
+
variable_summaries(b_conv1)
|
65 |
+
with tf.name_scope('activation'):
|
66 |
+
input_con1 = conv2d(x_image, W_conv1) + b_conv1
|
67 |
+
h_conv1 = tf.nn.relu(input_con1)
|
68 |
+
tf.summary.histogram('activations', h_conv1)
|
69 |
+
with tf.name_scope('max_pol'):
|
70 |
+
# Pooling layer - downsamples by 2X.
|
71 |
+
h_pool1 = max_pool_2x2(h_conv1)
|
72 |
+
input.append(input_con1)
|
73 |
+
hidden.append(h_pool1)
|
74 |
+
with tf.name_scope('conv2'):
|
75 |
+
# Second convolutional layer -- maps 32 feature maps to 64.
|
76 |
+
with tf.name_scope('weights'):
|
77 |
+
W_conv2 = weight_variable([5, 5, 32, 64])
|
78 |
+
variable_summaries(W_conv2)
|
79 |
+
with tf.name_scope('biases'):
|
80 |
+
b_conv2 = bias_variable([64])
|
81 |
+
variable_summaries(b_conv2)
|
82 |
+
with tf.name_scope('activation'):
|
83 |
+
input_con2 = conv2d(h_pool1, W_conv2) + b_conv2
|
84 |
+
h_conv2 = tf.nn.relu(input_con2)
|
85 |
+
tf.summary.histogram('activations', h_conv2)
|
86 |
+
with tf.name_scope('max_pol'):
|
87 |
+
# Second pooling layer.
|
88 |
+
h_pool2 = max_pool_2x2(h_conv2)
|
89 |
+
input.append(input_con2)
|
90 |
+
hidden.append(h_pool2)
|
91 |
+
# Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image
|
92 |
+
# is down to 7x7x64 feature maps -- maps this to 1024 features.
|
93 |
+
with tf.name_scope('FC1'):
|
94 |
+
with tf.name_scope('weights'):
|
95 |
+
W_fc1 = weight_variable([7 * 7 * 64, 1024])
|
96 |
+
variable_summaries(W_fc1)
|
97 |
+
with tf.name_scope('biases'):
|
98 |
+
b_fc1 = bias_variable([1024])
|
99 |
+
variable_summaries(b_fc1)
|
100 |
+
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
|
101 |
+
with tf.name_scope('activation'):
|
102 |
+
input_fc1 = tf.matmul(h_pool2_flat, W_fc1) + b_fc1
|
103 |
+
h_fc1 = tf.nn.relu(input_fc1)
|
104 |
+
tf.summary.histogram('activations', h_fc1)
|
105 |
+
|
106 |
+
with tf.name_scope('drouput'):
|
107 |
+
keep_prob = tf.placeholder(tf.float32)
|
108 |
+
tf.summary.scalar('dropout_keep_probability', keep_prob)
|
109 |
+
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
|
110 |
+
input.append(input_fc1)
|
111 |
+
hidden.append(h_fc1_drop)
|
112 |
+
# Map the 1024 features to 10 classes, one for each digit
|
113 |
+
with tf.name_scope('FC2'):
|
114 |
+
with tf.name_scope('weights'):
|
115 |
+
W_fc2 = weight_variable([1024, 10])
|
116 |
+
variable_summaries(W_fc2)
|
117 |
+
with tf.name_scope('biases'):
|
118 |
+
b_fc2 = bias_variable([10])
|
119 |
+
variable_summaries(b_fc2)
|
120 |
+
|
121 |
+
input_y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
|
122 |
+
y_conv = tf.nn.softmax(input_y_conv)
|
123 |
+
input.append(input_y_conv)
|
124 |
+
hidden.append(y_conv)
|
125 |
+
return y_conv, keep_prob, hidden, input
|
idnns/networks/network.py
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Train and calculate the information of network"""
|
2 |
+
import multiprocessing
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
import warnings
|
6 |
+
import numpy as np
|
7 |
+
import tensorflow as tf
|
8 |
+
from idnns.information import information_process as inn
|
9 |
+
from idnns.networks.utils import data_shuffle
|
10 |
+
from idnns.networks import model as mo
|
11 |
+
import tensorflow.compat.v1 as tf
|
12 |
+
tf.disable_v2_behavior()
|
13 |
+
|
14 |
+
warnings.filterwarnings("ignore")
|
15 |
+
summaries_dir = 'summaries'
|
16 |
+
NUM_CORES = multiprocessing.cpu_count()
|
17 |
+
|
18 |
+
|
19 |
+
def build_model(activation_function, layerSize, input_size, num_of_classes, learning_rate_local, save_file, covn_net):
|
20 |
+
"""Bulid specipic model of the network
|
21 |
+
Return the network model
|
22 |
+
"""
|
23 |
+
model = mo.Model(input_size, layerSize, num_of_classes, learning_rate_local, save_file, int(activation_function),
|
24 |
+
cov_net=covn_net)
|
25 |
+
return model
|
26 |
+
|
27 |
+
|
28 |
+
def train_and_calc_inf_network(i, j, k, layerSize, num_of_ephocs, learning_rate_local, batch_size, indexes, save_grads,
|
29 |
+
data_sets_org,
|
30 |
+
model_type, percent_of_train, interval_accuracy_display, calc_information,
|
31 |
+
calc_information_last, num_of_bins,
|
32 |
+
interval_information_display, save_ws, rand_int, cov_net):
|
33 |
+
"""Train the network and calculate it's information"""
|
34 |
+
network_name = '{0}_{1}_{2}_{3}'.format(i, j, k, rand_int)
|
35 |
+
print ('Training network - {0}'.format(network_name))
|
36 |
+
network = train_network(layerSize, num_of_ephocs, learning_rate_local, batch_size, indexes, save_grads,
|
37 |
+
data_sets_org, model_type, percent_of_train, interval_accuracy_display, network_name,
|
38 |
+
cov_net)
|
39 |
+
network['information'] = []
|
40 |
+
if calc_information:
|
41 |
+
print ('Calculating the infomration')
|
42 |
+
infomration = np.array([inn.get_information(network['ws'], data_sets_org.data, data_sets_org.labels,
|
43 |
+
num_of_bins, interval_information_display, network['model'],
|
44 |
+
layerSize)])
|
45 |
+
network['information'] = infomration
|
46 |
+
elif calc_information_last:
|
47 |
+
print ('Calculating the infomration for the last epoch')
|
48 |
+
infomration = np.array([inn.get_information([network['ws'][-1]], data_sets_org.data, data_sets_org.labels,
|
49 |
+
num_of_bins, interval_information_display,
|
50 |
+
network['model'], layerSize)])
|
51 |
+
network['information'] = infomration
|
52 |
+
# If we dont want to save layer's output
|
53 |
+
if not save_ws:
|
54 |
+
network['weights'] = 0
|
55 |
+
return network
|
56 |
+
|
57 |
+
|
58 |
+
def exctract_activity(sess, batch_points_all, model, data_sets_org):
|
59 |
+
"""Get the activation values of the layers for the input"""
|
60 |
+
w_temp = []
|
61 |
+
for i in range(0, len(batch_points_all) - 1):
|
62 |
+
batch_xs = data_sets_org.data[batch_points_all[i]:batch_points_all[i + 1]]
|
63 |
+
batch_ys = data_sets_org.labels[batch_points_all[i]:batch_points_all[i + 1]]
|
64 |
+
feed_dict_temp = {model.x: batch_xs, model.labels: batch_ys}
|
65 |
+
w_temp_local = sess.run([model.hidden_layers],
|
66 |
+
feed_dict=feed_dict_temp)
|
67 |
+
for s in range(len(w_temp_local[0])):
|
68 |
+
if i == 0:
|
69 |
+
w_temp.append(w_temp_local[0][s])
|
70 |
+
else:
|
71 |
+
w_temp[s] = np.concatenate((w_temp[s], w_temp_local[0][s]), axis=0)
|
72 |
+
""""
|
73 |
+
infomration[k] = inn.calc_information_for_epoch(k, interval_information_display, ws_t, params['bins'],
|
74 |
+
params['unique_inverse_x'],
|
75 |
+
params['unique_inverse_y'],
|
76 |
+
params['label'], estimted_labels,
|
77 |
+
params['b'], params['b1'], params['len_unique_a'],
|
78 |
+
params['pys'], py_hats_temp, params['pxs'], params['py_x'],
|
79 |
+
params['pys1'])
|
80 |
+
|
81 |
+
"""
|
82 |
+
return w_temp
|
83 |
+
|
84 |
+
|
85 |
+
def print_accuracy(batch_points_test, data_sets, model, sess, j, acc_train_array):
|
86 |
+
"""Calc the test acc and print the train and test accuracy"""
|
87 |
+
acc_array = []
|
88 |
+
for i in range(0, len(batch_points_test) - 1):
|
89 |
+
batch_xs = data_sets.test.data[batch_points_test[i]:batch_points_test[i + 1]]
|
90 |
+
batch_ys = data_sets.test.labels[batch_points_test[i]:batch_points_test[i + 1]]
|
91 |
+
feed_dict_temp = {model.x: batch_xs, model.labels: batch_ys}
|
92 |
+
acc = sess.run([model.accuracy],
|
93 |
+
feed_dict=feed_dict_temp)
|
94 |
+
acc_array.append(acc)
|
95 |
+
print ('Epoch {0} - Test Accuracy: {1:.3f} Train Accuracy: {2:.3f}'.format(j, np.mean(np.array(acc_array)),
|
96 |
+
np.mean(np.array(acc_train_array))))
|
97 |
+
|
98 |
+
|
99 |
+
def train_network(layerSize, num_of_ephocs, learning_rate_local, batch_size, indexes, save_grads,
|
100 |
+
data_sets_org, model_type, percent_of_train, interval_accuracy_display,
|
101 |
+
name, covn_net):
|
102 |
+
"""Train the nework"""
|
103 |
+
tf.reset_default_graph()
|
104 |
+
data_sets = data_shuffle(data_sets_org, percent_of_train)
|
105 |
+
ws, estimted_label, gradients, infomration, models, weights = [[None] * len(indexes) for _ in range(6)]
|
106 |
+
loss_func_test, loss_func_train, test_prediction, train_prediction = [np.zeros((len(indexes))) for _ in range(4)]
|
107 |
+
input_size = data_sets_org.data.shape[1]
|
108 |
+
num_of_classes = data_sets_org.labels.shape[1]
|
109 |
+
batch_size = np.min([batch_size, data_sets.train.data.shape[0]])
|
110 |
+
batch_points = np.rint(np.arange(0, data_sets.train.data.shape[0] + 1, batch_size)).astype(dtype=np.int32)
|
111 |
+
batch_points_test = np.rint(np.arange(0, data_sets.test.data.shape[0] + 1, batch_size)).astype(dtype=np.int32)
|
112 |
+
batch_points_all = np.rint(np.arange(0, data_sets_org.data.shape[0] + 1, batch_size)).astype(dtype=np.int32)
|
113 |
+
if data_sets_org.data.shape[0] not in batch_points_all:
|
114 |
+
batch_points_all = np.append(batch_points_all, [data_sets_org.data.shape[0]])
|
115 |
+
if data_sets.train.data.shape[0] not in batch_points:
|
116 |
+
batch_points = np.append(batch_points, [data_sets.train.data.shape[0]])
|
117 |
+
if data_sets.test.data.shape[0] not in batch_points_test:
|
118 |
+
batch_points_test = np.append(batch_points_test, [data_sets.test.data.shape[0]])
|
119 |
+
# Build the network
|
120 |
+
model = build_model(model_type, layerSize, input_size, num_of_classes, learning_rate_local, name, covn_net)
|
121 |
+
optimizer = model.optimize
|
122 |
+
saver = tf.train.Saver(max_to_keep=0)
|
123 |
+
init = tf.global_variables_initializer()
|
124 |
+
grads = tf.gradients(model.cross_entropy, tf.trainable_variables())
|
125 |
+
# Train the network
|
126 |
+
with tf.Session() as sess:
|
127 |
+
sess.run(init)
|
128 |
+
# Go over the epochs
|
129 |
+
k = 0
|
130 |
+
acc_train_array = []
|
131 |
+
for j in range(0, num_of_ephocs):
|
132 |
+
epochs_grads = []
|
133 |
+
if j in indexes:
|
134 |
+
ws[k] = exctract_activity(sess, batch_points_all, model, data_sets_org)
|
135 |
+
# Print accuracy
|
136 |
+
if np.mod(j, interval_accuracy_display) == 1 or interval_accuracy_display == 1:
|
137 |
+
print_accuracy(batch_points_test, data_sets, model, sess, j, acc_train_array)
|
138 |
+
# Go over the batch_points
|
139 |
+
acc_train_array = []
|
140 |
+
current_weights = [[] for _ in range(len(model.weights_all))]
|
141 |
+
for i in range(0, len(batch_points) - 1):
|
142 |
+
batch_xs = data_sets.train.data[batch_points[i]:batch_points[i + 1]]
|
143 |
+
batch_ys = data_sets.train.labels[batch_points[i]:batch_points[i + 1]]
|
144 |
+
feed_dict = {model.x: batch_xs, model.labels: batch_ys}
|
145 |
+
_, tr_err = sess.run([optimizer, model.accuracy], feed_dict=feed_dict)
|
146 |
+
acc_train_array.append(tr_err)
|
147 |
+
if j in indexes:
|
148 |
+
epochs_grads_temp, loss_tr, weights_local = sess.run(
|
149 |
+
[grads, model.cross_entropy, model.weights_all],
|
150 |
+
feed_dict=feed_dict)
|
151 |
+
epochs_grads.append(epochs_grads_temp)
|
152 |
+
for ii in range(len(current_weights)):
|
153 |
+
current_weights[ii].append(weights_local[ii])
|
154 |
+
if j in indexes:
|
155 |
+
if save_grads:
|
156 |
+
gradients[k] = epochs_grads
|
157 |
+
current_weights_mean = []
|
158 |
+
for ii in range(len(current_weights)):
|
159 |
+
current_weights_mean.append(np.mean(np.array(current_weights[ii]), axis=0))
|
160 |
+
weights[k] = current_weights_mean
|
161 |
+
# Save the model
|
162 |
+
write_meta = True if k == 0 else False
|
163 |
+
# saver.save(sess, model.save_file, global_step=k, write_meta_graph=write_meta)
|
164 |
+
k += 1
|
165 |
+
network = {}
|
166 |
+
network['ws'] = ws
|
167 |
+
network['test_prediction'] = test_prediction
|
168 |
+
network['train_prediction'] = train_prediction
|
169 |
+
network['loss_test'] = loss_func_test
|
170 |
+
network['loss_train'] = loss_func_train
|
171 |
+
network['gradients'] = gradients
|
172 |
+
network['model'] = model
|
173 |
+
return network
|
idnns/networks/network_paramters.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import re
|
3 |
+
|
4 |
+
|
5 |
+
def str2bool(v):
|
6 |
+
if v.lower() in ('yes', 'true', 't', 'y', '1') or v == True:
|
7 |
+
return True
|
8 |
+
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
|
9 |
+
return False
|
10 |
+
else:
|
11 |
+
raise argparse.ArgumentTypeError('Boolean value expected.')
|
12 |
+
|
13 |
+
|
14 |
+
def get_default_parser(num_of_samples=None):
|
15 |
+
parser = argparse.ArgumentParser()
|
16 |
+
parser.add_argument('-start_samples',
|
17 |
+
'-ss', dest="start_samples", default=1,
|
18 |
+
type=int, help='The number of the first sample that we calculate the information')
|
19 |
+
|
20 |
+
parser.add_argument('-batch_size',
|
21 |
+
'-b', dest="batch_size", default=512,
|
22 |
+
type=int, help='The size of the batch')
|
23 |
+
|
24 |
+
parser.add_argument('-learning_rate',
|
25 |
+
'-l', dest="learning_rate", default=0.0004,
|
26 |
+
type=float,
|
27 |
+
help='The learning rate of the network')
|
28 |
+
|
29 |
+
parser.add_argument('-num_repeat',
|
30 |
+
'-r', dest="num_of_repeats", default=1,
|
31 |
+
type=int, help='The number of times to run the network')
|
32 |
+
|
33 |
+
parser.add_argument('-num_epochs',
|
34 |
+
'-e', dest="num_ephocs", default=8000,
|
35 |
+
type=int, help='max number of epochs')
|
36 |
+
|
37 |
+
parser.add_argument('-net',
|
38 |
+
'-n', dest="net_type", default='1',
|
39 |
+
help='The architecture of the networks')
|
40 |
+
|
41 |
+
parser.add_argument('-inds',
|
42 |
+
'-i', dest="inds", default='[80]',
|
43 |
+
help='The percent of the training data')
|
44 |
+
|
45 |
+
parser.add_argument('-name',
|
46 |
+
'-na', dest="name", default='net',
|
47 |
+
help='The name to save the results')
|
48 |
+
|
49 |
+
parser.add_argument('-d_name',
|
50 |
+
'-dna', dest="data_name", default='var_u',
|
51 |
+
help='The dataset that we want to run ')
|
52 |
+
|
53 |
+
parser.add_argument('-num_samples',
|
54 |
+
'-ns', dest="num_of_samples", default=400,
|
55 |
+
type=int,
|
56 |
+
help='The max number of indexes for calculate information')
|
57 |
+
|
58 |
+
parser.add_argument('-nDistSmpls',
|
59 |
+
'-nds', dest="nDistSmpls", default=1,
|
60 |
+
type=int, help='S')
|
61 |
+
|
62 |
+
parser.add_argument('-save_ws',
|
63 |
+
'-sws', dest="save_ws", type=str2bool, nargs='?', const=False, default=False,
|
64 |
+
help='if we want to save the output of the layers')
|
65 |
+
|
66 |
+
parser.add_argument('-calc_information',
|
67 |
+
'-cinf', dest="calc_information", type=str2bool, nargs='?', const=True, default=True,
|
68 |
+
help='if we want to calculate the MI in the network for all the epochs')
|
69 |
+
|
70 |
+
parser.add_argument('-calc_information_last',
|
71 |
+
'-cinfl', dest="calc_information_last", type=str2bool, nargs='?', const=False, default=False,
|
72 |
+
help='if we want to calculate the MI in the network only for the last epoch')
|
73 |
+
|
74 |
+
parser.add_argument('-save_grads',
|
75 |
+
'-sgrad', dest="save_grads", type=str2bool, nargs='?', const=False, default=False,
|
76 |
+
help='if we want to save the gradients in the network')
|
77 |
+
|
78 |
+
parser.add_argument('-run_in_parallel',
|
79 |
+
'-par', dest="run_in_parallel", type=str2bool, nargs='?', const=False, default=False,
|
80 |
+
help='If we want to run all the networks in parallel mode')
|
81 |
+
|
82 |
+
parser.add_argument('-num_of_bins',
|
83 |
+
'-nbins', dest="num_of_bins", default=30, type=int,
|
84 |
+
help='The number of bins that we divide the output of the neurons')
|
85 |
+
|
86 |
+
parser.add_argument('-activation_function',
|
87 |
+
'-af', dest="activation_function", default=0, type=int,
|
88 |
+
help='The activation function of the model 0 for thnh 1 for RelU')
|
89 |
+
|
90 |
+
parser.add_argument('-iad', dest="interval_accuracy_display", default=499, type=int,
|
91 |
+
help='The interval for display accuracy')
|
92 |
+
|
93 |
+
parser.add_argument('-interval_information_display',
|
94 |
+
'-iid', dest="interval_information_display", default=30, type=int,
|
95 |
+
help='The interval for display the information calculation')
|
96 |
+
|
97 |
+
parser.add_argument('-cov_net',
|
98 |
+
'-cov', dest="cov_net", type=int, default=0,
|
99 |
+
help='True if we want covnet')
|
100 |
+
|
101 |
+
parser.add_argument('-rl',
|
102 |
+
'-rand_labels', dest="random_labels", type=str2bool, nargs='?', const=False, default=False,
|
103 |
+
help='True if we want to set random labels')
|
104 |
+
parser.add_argument('-data_dir',
|
105 |
+
'-dd', dest="data_dir", default='data/',
|
106 |
+
help='The directory for finding the data')
|
107 |
+
args = parser.parse_args()
|
108 |
+
args.inds = [map(int, inner.split(',')) for inner in re.findall("\[(.*?)\]", args.inds)]
|
109 |
+
if num_of_samples != None:
|
110 |
+
args.inds = [[num_of_samples]]
|
111 |
+
return args
|
112 |
+
|
113 |
+
|
114 |
+
def select_network_arch(type_net):
|
115 |
+
"""Selcet the architectures of the networks according to their type
|
116 |
+
we can choose also costume network for example type_net=[size_1, size_2, size_3]"""
|
117 |
+
if type_net == '1':
|
118 |
+
layers_sizes = [[10, 7, 5, 4, 3]]
|
119 |
+
elif type_net == '1-2-3':
|
120 |
+
layers_sizes = [[10, 9, 7, 7, 3], [10, 9, 7, 5, 3], [10, 9, 7, 3, 3]]
|
121 |
+
elif type_net == '11':
|
122 |
+
layers_sizes = [[10, 7, 7, 4, 3]]
|
123 |
+
elif type_net == '2':
|
124 |
+
layers_sizes = [[10, 7, 5, 4]]
|
125 |
+
elif type_net == '3':
|
126 |
+
layers_sizes = [[10, 7, 5]]
|
127 |
+
elif type_net == '4':
|
128 |
+
layers_sizes = [[10, 7]]
|
129 |
+
elif type_net == '5':
|
130 |
+
layers_sizes = [[10]]
|
131 |
+
elif type_net == '6':
|
132 |
+
layers_sizes = [[1, 1, 1, 1]]
|
133 |
+
else:
|
134 |
+
# Custom network
|
135 |
+
layers_sizes = [map(int, inner.split(',')) for inner in re.findall("\[(.*?)\]", type_net)]
|
136 |
+
return layers_sizes
|
idnns/networks/ops.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
from idnns.networks.utils import _convert_string_dtype
|
4 |
+
import tensorflow.compat.v1 as tf
|
5 |
+
tf.disable_v2_behavior()
|
6 |
+
|
7 |
+
|
8 |
+
def conv2d(x, W):
|
9 |
+
"""conv2d returns a 2d convolution layer with full stride."""
|
10 |
+
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
|
11 |
+
|
12 |
+
|
13 |
+
def max_pool_2x2(x):
|
14 |
+
"""max_pool_2x2 downsamples a feature map by 2X."""
|
15 |
+
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
|
16 |
+
strides=[1, 2, 2, 1], padding='SAME')
|
17 |
+
|
18 |
+
|
19 |
+
def weight_variable(shape):
|
20 |
+
"""weight_variable generates a weight variable of a given shape."""
|
21 |
+
initial = tf.truncated_normal(shape, stddev=0.1)
|
22 |
+
return tf.Variable(initial)
|
23 |
+
|
24 |
+
|
25 |
+
def bias_variable(shape):
|
26 |
+
"""bias_variable generates a bias variable of a given shape."""
|
27 |
+
initial = tf.constant(0.1, shape=shape)
|
28 |
+
return tf.Variable(initial)
|
29 |
+
|
30 |
+
|
31 |
+
def set_value(x, value):
|
32 |
+
"""Sets the value of a variable, from a Numpy array.
|
33 |
+
# Arguments
|
34 |
+
x: Tensor to set to a new value.
|
35 |
+
value: Value to set the tensor to, as a Numpy array
|
36 |
+
(of the same shape).
|
37 |
+
"""
|
38 |
+
value = np.asarray(value)
|
39 |
+
tf_dtype = _convert_string_dtype(x.dtype.name.split('_')[0])
|
40 |
+
if hasattr(x, '_assign_placeholder'):
|
41 |
+
assign_placeholder = x._assign_placeholder
|
42 |
+
assign_op = x._assign_op
|
43 |
+
else:
|
44 |
+
assign_placeholder = tf.placeholder(tf_dtype, shape=value.shape)
|
45 |
+
assign_op = x.assign(assign_placeholder)
|
46 |
+
x._assign_placeholder = assign_placeholder
|
47 |
+
x._assign_op = assign_op
|
48 |
+
session = tf.get_default_session()
|
49 |
+
session.run(assign_op, feed_dict={assign_placeholder: value})
|
50 |
+
|
51 |
+
|
52 |
+
def variable_summaries(var):
|
53 |
+
"""Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
|
54 |
+
with tf.name_scope('summaries'):
|
55 |
+
mean = tf.reduce_mean(var)
|
56 |
+
tf.summary.scalar('mean', mean)
|
57 |
+
with tf.name_scope('stddev'):
|
58 |
+
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
|
59 |
+
tf.summary.scalar('stddev', stddev)
|
60 |
+
tf.summary.scalar('max', tf.reduce_max(var))
|
61 |
+
tf.summary.scalar('min', tf.reduce_min(var))
|
62 |
+
tf.summary.histogram('histogram', var)
|
63 |
+
|
64 |
+
|
65 |
+
def get_scope_variable(name_scope, var, shape=None, initializer=None):
|
66 |
+
with tf.variable_scope(name_scope) as scope:
|
67 |
+
try:
|
68 |
+
v = tf.get_variable(var, shape, initializer=initializer)
|
69 |
+
except ValueError:
|
70 |
+
scope.reuse_variables()
|
71 |
+
v = tf.get_variable(var)
|
72 |
+
return v
|
idnns/networks/utils.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import scipy.io as sio
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
import tensorflow as tf
|
6 |
+
import tensorflow.compat.v1 as tf
|
7 |
+
tf.disable_v2_behavior()
|
8 |
+
|
9 |
+
# TF2 doesn't include the mnist module in tensorflow.examples.tutorials
|
10 |
+
# Use tf.keras.datasets.mnist instead
|
11 |
+
def load_data(name, random_labels=False):
|
12 |
+
"""Load the data
|
13 |
+
name - the name of the dataset
|
14 |
+
random_labels - True if we want to return random labels to the dataset
|
15 |
+
return object with data and labels"""
|
16 |
+
print('Loading Data...')
|
17 |
+
C = type('type_C', (object,), {})
|
18 |
+
data_sets = C()
|
19 |
+
if name.split('/')[-1] == 'MNIST':
|
20 |
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
|
21 |
+
data_sets.data = np.concatenate((x_train, x_test), axis=0).reshape((-1, 28*28)) / 255.0 # Normalize
|
22 |
+
data_sets.labels = np.concatenate((tf.one_hot(y_train, depth=10), tf.one_hot(y_test, depth=10)), axis=0)
|
23 |
+
else:
|
24 |
+
d = sio.loadmat(os.path.join(os.path.dirname(sys.argv[0]), name + '.mat'))
|
25 |
+
F = d['F']
|
26 |
+
y = d['y']
|
27 |
+
data_sets = C()
|
28 |
+
data_sets.data = F
|
29 |
+
data_sets.labels = np.squeeze(np.concatenate((y[None, :], 1 - y[None, :]), axis=0).T)
|
30 |
+
# If we want to assign random labels to the data
|
31 |
+
if random_labels:
|
32 |
+
labels = np.zeros(data_sets.labels.shape)
|
33 |
+
labels_index = np.random.randint(low=0, high=labels.shape[1], size=labels.shape[0])
|
34 |
+
labels[np.arange(len(labels)), labels_index] = 1
|
35 |
+
data_sets.labels = labels
|
36 |
+
return data_sets
|
37 |
+
|
38 |
+
def shuffle_in_unison_inplace(a, b):
|
39 |
+
"""Shuffle the arrays randomly"""
|
40 |
+
assert len(a) == len(b)
|
41 |
+
p = np.random.permutation(len(a))
|
42 |
+
return a[p], b[p]
|
43 |
+
|
44 |
+
def data_shuffle(data_sets_org, percent_of_train, min_test_data=80, shuffle_data=False):
|
45 |
+
"""Divided the data to train and test and shuffle it"""
|
46 |
+
perc = lambda i, t: np.rint((i * t) / 100).astype(np.int32)
|
47 |
+
C = type('type_C', (object,), {})
|
48 |
+
data_sets = C()
|
49 |
+
stop_train_index = int(perc(percent_of_train, data_sets_org.data.shape[0]))
|
50 |
+
start_test_index = int(stop_train_index)
|
51 |
+
if percent_of_train > min_test_data:
|
52 |
+
start_test_index = int(perc(min_test_data, data_sets_org.data.shape[0]))
|
53 |
+
data_sets.train = C()
|
54 |
+
data_sets.test = C()
|
55 |
+
if shuffle_data:
|
56 |
+
shuffled_data, shuffled_labels = shuffle_in_unison_inplace(data_sets_org.data, data_sets_org.labels)
|
57 |
+
else:
|
58 |
+
shuffled_data, shuffled_labels = data_sets_org.data, data_sets_org.labels
|
59 |
+
data_sets.train.data = shuffled_data[:stop_train_index, :]
|
60 |
+
data_sets.train.labels = shuffled_labels[:stop_train_index, :]
|
61 |
+
data_sets.test.data = shuffled_data[start_test_index:, :]
|
62 |
+
data_sets.test.labels = shuffled_labels[start_test_index:, :]
|
63 |
+
return data_sets
|
64 |
+
|
65 |
+
# This function was used for dtype conversion, which might not be necessary in the simplified context
|
66 |
+
# However, if needed, TF2 supports these types directly without conversion
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
# import numpy as np
|
71 |
+
# from tensorflow.examples.tutorials.mnist import input_data
|
72 |
+
# import scipy.io as sio
|
73 |
+
# import os
|
74 |
+
# import sys
|
75 |
+
# import tensorflow as tf
|
76 |
+
|
77 |
+
|
78 |
+
# def load_data(name, random_labels=False):
|
79 |
+
# """Load the data
|
80 |
+
# name - the name of the dataset
|
81 |
+
# random_labels - True if we want to return random labels to the dataset
|
82 |
+
# return object with data and labels"""
|
83 |
+
# print ('Loading Data...')
|
84 |
+
# C = type('type_C', (object,), {})
|
85 |
+
# data_sets = C()
|
86 |
+
# if name.split('/')[-1] == 'MNIST':
|
87 |
+
# data_sets_temp = input_data.read_data_sets(os.path.dirname(sys.argv[0]) + "/data/MNIST_data/", one_hot=True)
|
88 |
+
# data_sets.data = np.concatenate((data_sets_temp.train.images, data_sets_temp.test.images), axis=0)
|
89 |
+
# data_sets.labels = np.concatenate((data_sets_temp.train.labels, data_sets_temp.test.labels), axis=0)
|
90 |
+
# else:
|
91 |
+
# d = sio.loadmat(os.path.join(os.path.dirname(sys.argv[0]), name + '.mat'))
|
92 |
+
# F = d['F']
|
93 |
+
# y = d['y']
|
94 |
+
# C = type('type_C', (object,), {})
|
95 |
+
# data_sets = C()
|
96 |
+
# data_sets.data = F
|
97 |
+
# data_sets.labels = np.squeeze(np.concatenate((y[None, :], 1 - y[None, :]), axis=0).T)
|
98 |
+
# # If we want to assign random labels to the data
|
99 |
+
# if random_labels:
|
100 |
+
# labels = np.zeros(data_sets.labels.shape)
|
101 |
+
# labels_index = np.random.randint(low=0, high=labels.shape[1], size=labels.shape[0])
|
102 |
+
# labels[np.arange(len(labels)), labels_index] = 1
|
103 |
+
# data_sets.labels = labels
|
104 |
+
# return data_sets
|
105 |
+
|
106 |
+
|
107 |
+
# def shuffle_in_unison_inplace(a, b):
|
108 |
+
# """Shuffle the arrays randomly"""
|
109 |
+
# assert len(a) == len(b)
|
110 |
+
# p = np.random.permutation(len(a))
|
111 |
+
# return a[p], b[p]
|
112 |
+
|
113 |
+
|
114 |
+
# def data_shuffle(data_sets_org, percent_of_train, min_test_data=80, shuffle_data=False):
|
115 |
+
# """Divided the data to train and test and shuffle it"""
|
116 |
+
# perc = lambda i, t: np.rint((i * t) / 100).astype(np.int32)
|
117 |
+
# C = type('type_C', (object,), {})
|
118 |
+
# data_sets = C()
|
119 |
+
# stop_train_index = perc(percent_of_train[0], data_sets_org.data.shape[0])
|
120 |
+
# start_test_index = stop_train_index
|
121 |
+
# if percent_of_train > min_test_data:
|
122 |
+
# start_test_index = perc(min_test_data, data_sets_org.data.shape[0])
|
123 |
+
# data_sets.train = C()
|
124 |
+
# data_sets.test = C()
|
125 |
+
# if shuffle_data:
|
126 |
+
# shuffled_data, shuffled_labels = shuffle_in_unison_inplace(data_sets_org.data, data_sets_org.labels)
|
127 |
+
# else:
|
128 |
+
# shuffled_data, shuffled_labels = data_sets_org.data, data_sets_org.labels
|
129 |
+
# data_sets.train.data = shuffled_data[:stop_train_index, :]
|
130 |
+
# data_sets.train.labels = shuffled_labels[:stop_train_index, :]
|
131 |
+
# data_sets.test.data = shuffled_data[start_test_index:, :]
|
132 |
+
# data_sets.test.labels = shuffled_labels[start_test_index:, :]
|
133 |
+
# return data_sets
|
134 |
+
|
135 |
+
|
136 |
+
def _convert_string_dtype(dtype):
|
137 |
+
if dtype == 'float16':
|
138 |
+
return tf.float16
|
139 |
+
if dtype == 'float32':
|
140 |
+
return tf.float32
|
141 |
+
elif dtype == 'float64':
|
142 |
+
return tf.float64
|
143 |
+
elif dtype == 'int16':
|
144 |
+
return tf.int16
|
145 |
+
elif dtype == 'int32':
|
146 |
+
return tf.int32
|
147 |
+
elif dtype == 'int64':
|
148 |
+
return tf.int64
|
149 |
+
elif dtype == 'uint8':
|
150 |
+
return tf.int8
|
151 |
+
elif dtype == 'uint16':
|
152 |
+
return tf.uint16
|
153 |
+
else:
|
154 |
+
raise ValueError('Unsupported dtype:', dtype)
|
idnns/plots/__init__.py
ADDED
File without changes
|
idnns/plots/ops.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
|
4 |
+
def sampleStandardDeviation(x):
|
5 |
+
"""calculates the sample standard deviation"""
|
6 |
+
sumv = 0.0
|
7 |
+
for i in x:
|
8 |
+
sumv += (i) ** 2
|
9 |
+
return math.sqrt(sumv / (len(x) - 1))
|
10 |
+
|
11 |
+
|
12 |
+
def pearson(x, y):
|
13 |
+
"""calculates the PCC"""
|
14 |
+
scorex, scorey = [], []
|
15 |
+
for i in x:
|
16 |
+
scorex.append((i) / sampleStandardDeviation(x))
|
17 |
+
for j in y:
|
18 |
+
scorey.append((j) / sampleStandardDeviation(y))
|
19 |
+
# multiplies both lists together into 1 list (hence zip) and sums the whole list
|
20 |
+
return (sum([i * j for i, j in zip(scorex, scorey)])) / (len(x) - 1)
|
idnns/plots/plot_figures.py
ADDED
@@ -0,0 +1,678 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Plot the networks in the information plane"""
|
2 |
+
import matplotlib
|
3 |
+
matplotlib.use("TkAgg")
|
4 |
+
import numpy as np
|
5 |
+
import _pickle as cPickle
|
6 |
+
# import cPickle
|
7 |
+
from scipy.interpolate import interp1d
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
from matplotlib.collections import LineCollection
|
10 |
+
from matplotlib.colors import ListedColormap, BoundaryNorm
|
11 |
+
import scipy.io as sio
|
12 |
+
import scipy.stats as sis
|
13 |
+
import os
|
14 |
+
import matplotlib.animation as animation
|
15 |
+
import math
|
16 |
+
import os.path
|
17 |
+
import idnns.plots.utils as utils
|
18 |
+
import tkinter as tk
|
19 |
+
from numpy import linalg as LA
|
20 |
+
|
21 |
+
from tkinter import filedialog
|
22 |
+
LAYERS_COLORS = ['red', 'blue', 'green', 'yellow', 'pink', 'orange']
|
23 |
+
|
24 |
+
def plot_all_epochs(gen_data, I_XT_array, I_TY_array, axes, epochsInds, f, index_i, index_j, size_ind,
|
25 |
+
font_size, y_ticks, x_ticks, colorbar_axis, title_str, axis_font, bar_font, save_name, plot_error = True,index_to_emphasis=1000):
|
26 |
+
"""Plot the infomration plane with the epochs in diffrnet colors """
|
27 |
+
#If we want to plot the train and test error
|
28 |
+
if plot_error:
|
29 |
+
fig_strs = ['train_error','test_error','loss_train','loss_test' ]
|
30 |
+
fig_data = [np.squeeze(gen_data[fig_str]) for fig_str in fig_strs]
|
31 |
+
f1 = plt.figure(figsize=(12, 8))
|
32 |
+
ax1 = f1.add_subplot(111)
|
33 |
+
mean_sample = False if len(fig_data[0].shape)==1 else True
|
34 |
+
if mean_sample:
|
35 |
+
fig_data = [ np.mean(fig_data_s, axis=0) for fig_data_s in fig_data]
|
36 |
+
for i in range(len(fig_data)):
|
37 |
+
ax1.plot(epochsInds, fig_data[i],':', linewidth = 3 , label = fig_strs[i])
|
38 |
+
ax1.legend(loc='best')
|
39 |
+
f = plt.figure(figsize=(12, 8))
|
40 |
+
axes = f.add_subplot(111)
|
41 |
+
axes = np.array([[axes]])
|
42 |
+
|
43 |
+
I_XT_array = np.squeeze(I_XT_array)
|
44 |
+
I_TY_array = np.squeeze(I_TY_array)
|
45 |
+
if len(I_TY_array[0].shape) >1:
|
46 |
+
I_XT_array = np.mean(I_XT_array, axis=0)
|
47 |
+
I_TY_array = np.mean(I_TY_array, axis=0)
|
48 |
+
max_index = size_ind if size_ind != -1 else I_XT_array.shape[0]
|
49 |
+
|
50 |
+
cmap = plt.get_cmap('gnuplot')
|
51 |
+
#For each epoch we have diffrenet color
|
52 |
+
colors = [cmap(i) for i in np.linspace(0, 1, epochsInds[max_index-1]+1)]
|
53 |
+
#Change this if we have more then one network arch
|
54 |
+
nums_arc= -1
|
55 |
+
#Go over all the epochs and plot then with the right color
|
56 |
+
for index_in_range in range(0, max_index):
|
57 |
+
XT = I_XT_array[index_in_range, :]
|
58 |
+
TY = I_TY_array[index_in_range, :]
|
59 |
+
#If this is the index that we want to emphsis
|
60 |
+
if epochsInds[index_in_range] ==index_to_emphasis:
|
61 |
+
axes[index_i, index_j].plot(XT, TY, marker='o', linestyle=None, markersize=19, markeredgewidth=0.04,
|
62 |
+
linewidth=2.1,
|
63 |
+
color='g',zorder=10)
|
64 |
+
else:
|
65 |
+
axes[index_i, index_j].plot(XT[:], TY[:], marker='o', linestyle='-', markersize=12, markeredgewidth=0.01, linewidth=0.2,
|
66 |
+
color=colors[int(epochsInds[index_in_range])])
|
67 |
+
utils.adjustAxes(axes[index_i, index_j], axis_font=axis_font, title_str=title_str, x_ticks=x_ticks,
|
68 |
+
y_ticks=y_ticks, x_lim=[0, 25.1], y_lim=None,
|
69 |
+
set_xlabel=index_i == axes.shape[0] - 1, set_ylabel=index_j == 0, x_label='$I(X;T)$',
|
70 |
+
y_label='$I(T;Y)$', set_xlim=False,
|
71 |
+
set_ylim=False, set_ticks=True, label_size=font_size)
|
72 |
+
#Save the figure and add color bar
|
73 |
+
if index_i ==axes.shape[0]-1 and index_j ==axes.shape[1]-1:
|
74 |
+
utils.create_color_bar(f, cmap, colorbar_axis, bar_font, epochsInds, title='Epochs')
|
75 |
+
f.savefig(save_name+'.jpg', dpi=500, format='jpg')
|
76 |
+
|
77 |
+
|
78 |
+
def plot_by_training_samples(I_XT_array, I_TY_array, axes, epochsInds, f, index_i, index_j, size_ind, font_size, y_ticks, x_ticks, colorbar_axis, title_str, axis_font, bar_font, save_name, samples_labels):
|
79 |
+
"""Print the final epoch of all the diffrenet training samples size """
|
80 |
+
max_index = size_ind if size_ind!=-1 else I_XT_array.shape[2]-1
|
81 |
+
cmap = plt.get_cmap('gnuplot')
|
82 |
+
colors = [cmap(i) for i in np.linspace(0, 1, max_index+1)]
|
83 |
+
#Print the final epoch
|
84 |
+
nums_epoch= -1
|
85 |
+
#Go over all the samples size and plot them with the right color
|
86 |
+
for index_in_range in range(0, max_index):
|
87 |
+
XT, TY = [], []
|
88 |
+
for layer_index in range(0, I_XT_array.shape[4]):
|
89 |
+
XT.append(np.mean(I_XT_array[:, -1, index_in_range, nums_epoch, layer_index], axis=0))
|
90 |
+
TY.append(np.mean(I_TY_array[:, -1, index_in_range,nums_epoch, layer_index], axis=0))
|
91 |
+
axes[index_i, index_j].plot(XT, TY, marker='o', linestyle='-', markersize=12, markeredgewidth=0.2, linewidth=0.5,
|
92 |
+
color=colors[index_in_range])
|
93 |
+
utils.adjustAxes(axes[index_i, index_j], axis_font=axis_font, title_str=title_str, x_ticks=x_ticks, y_ticks=y_ticks,
|
94 |
+
x_lim=None, y_lim=None,
|
95 |
+
set_xlabel=index_i == axes.shape[0] - 1, set_ylabel=index_j == 0, x_label='$I(X;T)$',
|
96 |
+
y_label='$I(T;Y)$', set_xlim=True,
|
97 |
+
set_ylim=True, set_ticks=True, label_size=font_size)
|
98 |
+
#Create color bar and save it
|
99 |
+
if index_i == axes.shape[0] - 1 and index_j == axes.shape[1] - 1:
|
100 |
+
utils.create_color_bar(f, cmap, colorbar_axis, bar_font, epochsInds, title='Training Data')
|
101 |
+
f.savefig(save_name + '.jpg', dpi=150, format='jpg')
|
102 |
+
|
103 |
+
def calc_velocity(data, epochs):
|
104 |
+
"""Calculate the velocity (both in X and Y) for each layer"""
|
105 |
+
vXs, vYs = [], []
|
106 |
+
for layer_index in range(data.shape[5]):
|
107 |
+
curernt_vXs = []
|
108 |
+
current_VYs = []
|
109 |
+
for epoch_index in range(len(epochs)-1):
|
110 |
+
vx = np.mean(data[0,:,-1, -1, epoch_index+1,layer_index], axis=0) - np.mean(data[0,:,-1, -1, epoch_index,layer_index], axis=0)
|
111 |
+
vx/= (epochs[epoch_index+1] - epochs[epoch_index])
|
112 |
+
vy = np.mean(data[1, :, -1, -1, epoch_index + 1, layer_index], axis=0) - np.mean(data[1, :, -1, -1, epoch_index, layer_index], axis=0)
|
113 |
+
vy /= (epochs[epoch_index + 1] - epochs[epoch_index])
|
114 |
+
current_VYs.append(vy)
|
115 |
+
curernt_vXs.append(vx)
|
116 |
+
vXs.append(curernt_vXs)
|
117 |
+
vYs.append(current_VYs)
|
118 |
+
return vXs,vYs
|
119 |
+
|
120 |
+
def update_line_specipic_points(nums, data, axes, to_do, font_size, axis_font):
|
121 |
+
"""Update the lines in the axes for snapshot of the whole process"""
|
122 |
+
colors =LAYERS_COLORS
|
123 |
+
x_ticks = [0, 2, 4, 6, 8, 10]
|
124 |
+
#Go over all the snapshot
|
125 |
+
for i in range(len(nums)):
|
126 |
+
num = nums[i]
|
127 |
+
#Plot the right layer
|
128 |
+
for layer_num in range(data.shape[3]):
|
129 |
+
axes[i].scatter(data[0, :, num, layer_num], data[1, :, num, layer_num], color = colors[layer_num], s = 105,edgecolors = 'black',alpha = 0.85)
|
130 |
+
utils.adjustAxes(axes[i], axis_font=axis_font, title_str='', x_ticks=x_ticks, y_ticks=[], x_lim=None,
|
131 |
+
y_lim=None,
|
132 |
+
set_xlabel=to_do[i][0], set_ylabel=to_do[i][1], x_label='$I(X;T)$', y_label='$I(T;Y)$',
|
133 |
+
set_xlim=True, set_ylim=True,
|
134 |
+
set_ticks=True, label_size=font_size)
|
135 |
+
|
136 |
+
def update_line_each_neuron(num, print_loss, Ix, axes, Iy, train_data, accuracy_test, epochs_bins, loss_train_data, loss_test_data, colors, epochsInds,
|
137 |
+
font_size = 18, axis_font = 16, x_lim = [0,12.2], y_lim=[0, 1.08],x_ticks = [], y_ticks = []):
|
138 |
+
"""Update the figure of the infomration plane for the movie"""
|
139 |
+
#Print the line between the points
|
140 |
+
axes[0].clear()
|
141 |
+
if len(axes)>1:
|
142 |
+
axes[1].clear()
|
143 |
+
#Print the points
|
144 |
+
for layer_num in range(Ix.shape[2]):
|
145 |
+
for net_ind in range(Ix.shape[0]):
|
146 |
+
axes[0].scatter(Ix[net_ind,num, layer_num], Iy[net_ind,num, layer_num], color = colors[layer_num], s = 35,edgecolors = 'black',alpha = 0.85)
|
147 |
+
title_str = 'Information Plane - Epoch number - ' + str(epochsInds[num])
|
148 |
+
utils.adjustAxes(axes[0], axis_font, title_str, x_ticks, y_ticks, x_lim, y_lim, set_xlabel=True, set_ylabel=True,
|
149 |
+
x_label='$I(X;T)$', y_label='$I(T;Y)$')
|
150 |
+
#Print the loss function and the error
|
151 |
+
if len(axes)>1:
|
152 |
+
axes[1].plot(epochsInds[:num], 1 - np.mean(accuracy_test[:, :num], axis=0), color='g')
|
153 |
+
if print_loss:
|
154 |
+
axes[1].plot(epochsInds[:num], np.mean(loss_test_data[:, :num], axis=0), color='y')
|
155 |
+
nereast_val = np.searchsorted(epochs_bins, epochsInds[num], side='right')
|
156 |
+
axes[1].set_xlim([0,epochs_bins[nereast_val]])
|
157 |
+
axes[1].legend(('Accuracy', 'Loss Function'), loc='best')
|
158 |
+
|
159 |
+
def update_line(num, print_loss, data, axes, epochsInds, test_error, test_data, epochs_bins, loss_train_data, loss_test_data, colors,
|
160 |
+
font_size = 18, axis_font=16, x_lim = [0,12.2], y_lim=[0, 1.08], x_ticks = [], y_ticks = []):
|
161 |
+
"""Update the figure of the infomration plane for the movie"""
|
162 |
+
#Print the line between the points
|
163 |
+
cmap = ListedColormap(LAYERS_COLORS)
|
164 |
+
segs = []
|
165 |
+
for i in range(0, data.shape[1]):
|
166 |
+
x = data[0, i, num, :]
|
167 |
+
y = data[1, i, num, :]
|
168 |
+
points = np.array([x, y]).T.reshape(-1, 1, 2)
|
169 |
+
segs.append(np.concatenate([points[:-1], points[1:]], axis=1))
|
170 |
+
segs = np.array(segs).reshape(-1, 2, 2)
|
171 |
+
axes[0].clear()
|
172 |
+
if len(axes)>1:
|
173 |
+
axes[1].clear()
|
174 |
+
lc = LineCollection(segs, cmap=cmap, linestyles='solid',linewidths = 0.3, alpha = 0.6)
|
175 |
+
lc.set_array(np.arange(0,5))
|
176 |
+
#Print the points
|
177 |
+
for layer_num in range(data.shape[3]):
|
178 |
+
axes[0].scatter(data[0, :, num, layer_num], data[1, :, num, layer_num], color = colors[layer_num], s = 35,edgecolors = 'black',alpha = 0.85)
|
179 |
+
axes[1].plot(epochsInds[:num], 1 - np.mean(test_error[:, :num], axis=0), color ='r')
|
180 |
+
|
181 |
+
title_str = 'Information Plane - Epoch number - ' + str(epochsInds[num])
|
182 |
+
utils.adjustAxes(axes[0], axis_font, title_str, x_ticks, y_ticks, x_lim, y_lim, set_xlabel=True, set_ylabel=True,
|
183 |
+
x_label='$I(X;T)$', y_label='$I(T;Y)$')
|
184 |
+
title_str = 'Precision as function of the epochs'
|
185 |
+
utils.adjustAxes(axes[1], axis_font, title_str, x_ticks, y_ticks, x_lim, y_lim, set_xlabel=True, set_ylabel=True,
|
186 |
+
x_label='# Epochs', y_label='Precision')
|
187 |
+
|
188 |
+
|
189 |
+
def plot_animation(name_s, save_name):
|
190 |
+
"""Plot the movie for all the networks in the information plane"""
|
191 |
+
# If we want to print the loss function also
|
192 |
+
print_loss = False
|
193 |
+
#The bins that we extened the x axis of the accuracy each time
|
194 |
+
epochs_bins = [0, 500, 1500, 3000, 6000, 10000, 20000]
|
195 |
+
|
196 |
+
data_array = utils.get_data(name_s[0][0])
|
197 |
+
data = data_array['infomration']
|
198 |
+
epochsInds = data_array['epochsInds']
|
199 |
+
loss_train_data = data_array['loss_train']
|
200 |
+
loss_test_data = data_array['loss_test_data']
|
201 |
+
f, (axes) = plt.subplots(2, 1)
|
202 |
+
f.subplots_adjust(left=0.14, bottom=0.1, right=.928, top=0.94, wspace=0.13, hspace=0.55)
|
203 |
+
colors = LAYERS_COLORS
|
204 |
+
#new/old version
|
205 |
+
if False:
|
206 |
+
Ix = np.squeeze(data[0,:,-1,-1, :, :])
|
207 |
+
Iy = np.squeeze(data[1,:,-1,-1, :, :])
|
208 |
+
else:
|
209 |
+
Ix = np.squeeze(data[0, :, -1, -1, :, :])[np.newaxis,:,:]
|
210 |
+
Iy = np.squeeze(data[1, :, -1, -1, :, :])[np.newaxis,:,:]
|
211 |
+
#Interploation of the samplings (because we don't cauclaute the infomration in each epoch)
|
212 |
+
interp_data_x = interp1d(epochsInds, Ix, axis=1)
|
213 |
+
interp_data_y = interp1d(epochsInds, Iy, axis=1)
|
214 |
+
new_x = np.arange(0,epochsInds[-1])
|
215 |
+
new_data = np.array([interp_data_x(new_x), interp_data_y(new_x)])
|
216 |
+
""""
|
217 |
+
train_data = interp1d(epochsInds, np.squeeze(train_data), axis=1)(new_x)
|
218 |
+
test_data = interp1d(epochsInds, np.squeeze(test_data), axis=1)(new_x)
|
219 |
+
"""
|
220 |
+
if print_loss:
|
221 |
+
loss_train_data = interp1d(epochsInds, np.squeeze(loss_train_data), axis=1)(new_x)
|
222 |
+
loss_test_data=interp1d(epochsInds, np.squeeze(loss_test_data), axis=1)(new_x)
|
223 |
+
line_ani = animation.FuncAnimation(f, update_line, len(new_x), repeat=False,
|
224 |
+
interval=1, blit=False, fargs=(print_loss, new_data, axes,new_x,train_data,test_data,epochs_bins, loss_train_data,loss_test_data, colors))
|
225 |
+
Writer = animation.writers['ffmpeg']
|
226 |
+
writer = Writer(fps=100)
|
227 |
+
#Save the movie
|
228 |
+
line_ani.save(save_name+'_movie2.mp4',writer=writer,dpi=250)
|
229 |
+
plt.show()
|
230 |
+
|
231 |
+
|
232 |
+
def plot_animation_each_neuron(name_s, save_name, print_loss=False):
|
233 |
+
"""Plot the movie for all the networks in the information plane"""
|
234 |
+
# If we want to print the loss function also
|
235 |
+
#The bins that we extened the x axis of the accuracy each time
|
236 |
+
epochs_bins = [0, 500, 1500, 3000, 6000, 10000, 20000]
|
237 |
+
data_array = utils.get_data(name_s[0][0])
|
238 |
+
data = np.squeeze(data_array['information'])
|
239 |
+
|
240 |
+
f, (axes) = plt.subplots(1, 1)
|
241 |
+
axes = [axes]
|
242 |
+
f.subplots_adjust(left=0.14, bottom=0.1, right=.928, top=0.94, wspace=0.13, hspace=0.55)
|
243 |
+
colors = LAYERS_COLORS
|
244 |
+
#new/old version
|
245 |
+
Ix = np.squeeze(data[0,:, :, :])
|
246 |
+
Iy = np.squeeze(data[1,:, :, :])
|
247 |
+
#Interploation of the samplings (because we don't cauclaute the infomration in each epoch)
|
248 |
+
#interp_data_x = interp1d(epochsInds, Ix, axis=1)
|
249 |
+
#interp_data_y = interp1d(epochsInds, Iy, axis=1)
|
250 |
+
#new_x = np.arange(0,epochsInds[-1])
|
251 |
+
#new_data = np.array([interp_data_x(new_x), interp_data_y(new_x)])
|
252 |
+
""""
|
253 |
+
train_data = interp1d(epochsInds, np.squeeze(train_data), axis=1)(new_x)
|
254 |
+
test_data = interp1d(epochsInds, np.squeeze(test_data), axis=1)(new_x)
|
255 |
+
|
256 |
+
if print_loss:
|
257 |
+
loss_train_data = interp1d(epochsInds, np.squeeze(loss_train_data), axis=1)(new_x)
|
258 |
+
loss_test_data=interp1d(epochsInds, np.squeeze(loss_test_data), axis=1)(new_x)
|
259 |
+
"""
|
260 |
+
line_ani = animation.FuncAnimation(f, update_line_each_neuron, Ix.shape[1], repeat=False,
|
261 |
+
interval=1, blit=False, fargs=(print_loss, Ix, axes,Iy,train_data,test_data,epochs_bins, loss_train_data,loss_test_data, colors,epochsInds))
|
262 |
+
Writer = animation.writers['ffmpeg']
|
263 |
+
writer = Writer(fps=100)
|
264 |
+
#Save the movie
|
265 |
+
line_ani.save(save_name+'_movie.mp4',writer=writer,dpi=250)
|
266 |
+
plt.show()
|
267 |
+
|
268 |
+
|
269 |
+
def plot_snapshots(name_s, save_name, i, time_stemps=[13, 180, 963],font_size = 36,axis_font = 28,fig_size = (14, 6)):
|
270 |
+
"""Plot snapshots of the given network"""
|
271 |
+
f, (axes) = plt.subplots(1, len(time_stemps), sharey=True, figsize=fig_size)
|
272 |
+
f.subplots_adjust(left=0.095, bottom=0.18, right=.99, top=0.97, wspace=0.03, hspace=0.03)
|
273 |
+
#Adding axes labels
|
274 |
+
to_do = [[True, True], [True, False], [True, False]]
|
275 |
+
data_array = utils.get_data(name_s)
|
276 |
+
data = np.squeeze(data_array['information'])
|
277 |
+
update_line_specipic_points(time_stemps, data, axes, to_do, font_size, axis_font)
|
278 |
+
f.savefig(save_name + '.jpg', dpi=200, format='jpg')
|
279 |
+
|
280 |
+
|
281 |
+
def load_figures(mode, str_names=None):
|
282 |
+
"""Creaet new figure based on the mode of it
|
283 |
+
This function is really messy and need to rewrite """
|
284 |
+
if mode == 0:
|
285 |
+
font_size = 34
|
286 |
+
axis_font = 28
|
287 |
+
bar_font = 28
|
288 |
+
fig_size = (14, 6.5)
|
289 |
+
title_strs = [['', '']]
|
290 |
+
f, (axes) = plt.subplots(1, 2, sharey=True, figsize=fig_size)
|
291 |
+
sizes = [[-1, -1]]
|
292 |
+
colorbar_axis = [0.935, 0.14, 0.027, 0.75]
|
293 |
+
axes = np.vstack(axes).T
|
294 |
+
f.subplots_adjust(left=0.09, bottom=0.15, right=.928, top=0.94, wspace=0.03, hspace=0.04)
|
295 |
+
yticks = [0, 1, 2, 3]
|
296 |
+
xticks = [3, 6, 9, 12, 15]
|
297 |
+
# for 3 rows with 2 colmus
|
298 |
+
if mode == 1:
|
299 |
+
font_size = 34
|
300 |
+
axis_font = 26
|
301 |
+
bar_font = 28
|
302 |
+
fig_size = (14, 19)
|
303 |
+
xticks = [1, 3, 5, 7, 9, 11]
|
304 |
+
yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
|
305 |
+
title_strs = [['One hidden layer', 'Two hidden layers'], ['Three hidden layers', 'Four hidden layers'],
|
306 |
+
['Five hidden layers', 'Six hidden layers']]
|
307 |
+
|
308 |
+
title_strs = [['5 bins', '12 bins'], ['18 bins', '25 bins'],
|
309 |
+
['35 bins', '50 bins']]
|
310 |
+
f, (axes) = plt.subplots(3, 2, sharex=True, sharey=True, figsize=fig_size)
|
311 |
+
f.subplots_adjust(left=0.09, bottom=0.08, right=.92, top=0.94, wspace=0.03, hspace=0.15)
|
312 |
+
colorbar_axis = [0.93, 0.1, 0.035, 0.76]
|
313 |
+
sizes = [[1010, 1010], [1017, 1020], [1700, 920]]
|
314 |
+
|
315 |
+
# for 2 rows with 3 colmus
|
316 |
+
if mode == 11:
|
317 |
+
font_size = 34
|
318 |
+
axis_font = 26
|
319 |
+
bar_font = 28
|
320 |
+
fig_size = (14, 9)
|
321 |
+
xticks = [1, 3, 5, 7, 9, 11]
|
322 |
+
yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
|
323 |
+
title_strs = [['One hidden layer', 'Two hidden layers','Three hidden layers'], ['Four hidden layers',
|
324 |
+
'Five hidden layers', 'Six hidden layers']]
|
325 |
+
|
326 |
+
title_strs = [['5 Bins', '10 Bins', '15 Bins'], ['20 Bins',
|
327 |
+
'25 Bins',
|
328 |
+
'35 Bins']]
|
329 |
+
f, (axes) = plt.subplots(2, 3, sharex=True, sharey=True, figsize=fig_size)
|
330 |
+
f.subplots_adjust(left=0.09, bottom=0.1, right=.92, top=0.94, wspace=0.03, hspace=0.15)
|
331 |
+
colorbar_axis = [0.93, 0.1, 0.035, 0.76]
|
332 |
+
sizes = [[1010, 1010, 1017], [1020, 1700, 920]]
|
333 |
+
# one figure
|
334 |
+
if mode == 2 or mode ==6:
|
335 |
+
axis_font = 28
|
336 |
+
bar_font = 28
|
337 |
+
fig_size = (14, 10)
|
338 |
+
font_size = 34
|
339 |
+
f, (axes) = plt.subplots(1, len(str_names), sharey=True, figsize=fig_size)
|
340 |
+
if len(str_names) == 1:
|
341 |
+
axes = np.vstack(np.array([axes]))
|
342 |
+
f.subplots_adjust(left=0.097, bottom=0.12, right=.87, top=0.99, wspace=0.03, hspace=0.03)
|
343 |
+
colorbar_axis = [0.905, 0.12, 0.03, 0.82]
|
344 |
+
xticks = [1, 3, 5, 7, 9, 11]
|
345 |
+
yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
|
346 |
+
|
347 |
+
#yticks = [0, 1, 2, 3, 3.5]
|
348 |
+
#xticks = [2, 5, 8, 11, 14, 17]
|
349 |
+
sizes = [[-1]]
|
350 |
+
title_strs = [['', '']]
|
351 |
+
# one figure with error bar
|
352 |
+
if mode == 3:
|
353 |
+
fig_size = (14, 10)
|
354 |
+
font_size = 36
|
355 |
+
axis_font = 28
|
356 |
+
bar_font = 25
|
357 |
+
title_strs = [['']]
|
358 |
+
f, (axes) = plt.subplots(1, len(str_names), sharey=True, figsize=fig_size)
|
359 |
+
if len(str_names) == 1:
|
360 |
+
axes = np.vstack(np.array([axes]))
|
361 |
+
f.subplots_adjust(left=0.097, bottom=0.12, right=.90, top=0.99, wspace=0.03, hspace=0.03)
|
362 |
+
sizes = [[-1]]
|
363 |
+
colorbar_axis = [0.933, 0.125, 0.03, 0.83]
|
364 |
+
xticks = [0, 2, 4, 6, 8, 10, 12]
|
365 |
+
yticks = [0.3, 0.4, 0.6, 0.8, 1]
|
366 |
+
# two figures second
|
367 |
+
if mode == 4:
|
368 |
+
font_size = 27
|
369 |
+
axis_font = 18
|
370 |
+
bar_font = 23
|
371 |
+
fig_size = (14, 6.5)
|
372 |
+
title_strs = [['', '']]
|
373 |
+
f, (axes) = plt.subplots(1, 2, figsize=fig_size)
|
374 |
+
sizes = [[-1, -1]]
|
375 |
+
colorbar_axis = [0.948, 0.08, 0.025, 0.81]
|
376 |
+
axes = np.vstack(axes).T
|
377 |
+
f.subplots_adjust(left=0.07, bottom=0.15, right=.933, top=0.94, wspace=0.12, hspace=0.04)
|
378 |
+
#yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
|
379 |
+
#xticks = [1, 3, 5, 7, 9, 11]
|
380 |
+
|
381 |
+
yticks = [0, 1, 2, 3, 3]
|
382 |
+
xticks = [2, 5, 8, 11,14, 17]
|
383 |
+
return font_size, axis_font, bar_font, colorbar_axis, sizes, yticks, xticks,title_strs, f, axes
|
384 |
+
|
385 |
+
|
386 |
+
def plot_figures(str_names, mode, save_name):
|
387 |
+
"""Plot the data in the given names with the given mode"""
|
388 |
+
[font_size, axis_font, bar_font, colorbar_axis, sizes, yticks, xticks,title_strs, f, axes] = load_figures(mode, str_names)
|
389 |
+
#Go over all the files
|
390 |
+
for i in range(len(str_names)):
|
391 |
+
for j in range(len(str_names[i])):
|
392 |
+
name_s = str_names[i][j]
|
393 |
+
#Load data for the given file
|
394 |
+
data_array = utils.get_data(name_s)
|
395 |
+
data = np.squeeze(np.array(data_array['information']))
|
396 |
+
I_XT_array = np.array(extract_array(data, 'local_IXT'))
|
397 |
+
I_TY_array = np.array(extract_array(data, 'local_ITY'))
|
398 |
+
#I_XT_array = np.array(extract_array(data, 'IXT_vartional'))
|
399 |
+
#I_TY_array = np.array(extract_array(data, 'ITY_vartional'))
|
400 |
+
epochsInds = data_array['params']['epochsInds']
|
401 |
+
#I_XT_array = np.squeeze(np.array(data))[:, :, 0]
|
402 |
+
#I_TY_array = np.squeeze(np.array(data))[:, :, 1]
|
403 |
+
#Plot it
|
404 |
+
if mode ==3:
|
405 |
+
plot_by_training_samples(I_XT_array, I_TY_array, axes, epochsInds, f, i, j, sizes[i][j], font_size, yticks, xticks, colorbar_axis, title_strs[i][j], axis_font, bar_font, save_name)
|
406 |
+
elif mode ==6:
|
407 |
+
plot_norms(axes, epochsInds,data_array['norms1'],data_array['norms2'])
|
408 |
+
else:
|
409 |
+
plot_all_epochs(data_array, I_XT_array, I_TY_array, axes, epochsInds, f, i, j, sizes[i][j], font_size, yticks, xticks,
|
410 |
+
colorbar_axis, title_strs[i][j], axis_font, bar_font, save_name)
|
411 |
+
plt.show()
|
412 |
+
|
413 |
+
|
414 |
+
def plot_norms(axes, epochsInds, norms1, norms2):
|
415 |
+
"""Plot the norm l1 and l2 of the given name"""
|
416 |
+
axes.plot(epochsInds, np.mean(norms1[:,0,0,:], axis=0), color='g')
|
417 |
+
axes.plot(epochsInds, np.mean(norms2[:,0,0,:], axis=0), color='b')
|
418 |
+
axes.legend(('L1 norm', 'L2 norm'))
|
419 |
+
axes.set_xlabel('Epochs')
|
420 |
+
|
421 |
+
def plot_pearson(name):
|
422 |
+
"""Plot the pearsin coeff of the neurons for each layer"""
|
423 |
+
data_array = utils.get_data(name)
|
424 |
+
ws = data_array['weights']
|
425 |
+
f = plt.figure(figsize=(12, 8))
|
426 |
+
axes = f.add_subplot(111)
|
427 |
+
#The number of neurons in each layer -
|
428 |
+
#TODO need to change it to be auto
|
429 |
+
sizes =[10,7, 5, 4,3,2 ]
|
430 |
+
#The mean of pearson coeffs of all the layers
|
431 |
+
pearson_mean =[]
|
432 |
+
#Go over all the layers
|
433 |
+
for layer in range(len(sizes)):
|
434 |
+
inner_pearson_mean =[]
|
435 |
+
#Go over all the weights in the layer
|
436 |
+
for k in range(len(ws)):
|
437 |
+
ws_current = np.squeeze(ws[k][0][0][-1])
|
438 |
+
#Go over the neurons
|
439 |
+
for neuron in range(len(ws_current[layer])):
|
440 |
+
person_t = []
|
441 |
+
#Go over the rest of the neurons
|
442 |
+
for neuron_second in range(neuron+1, len(ws_current[layer])):
|
443 |
+
pearson_c, p_val =sis.pearsonr(ws_current[layer][neuron], ws_current[layer][neuron_second])
|
444 |
+
person_t.append(pearson_c)
|
445 |
+
inner_pearson_mean.append(np.mean(person_t))
|
446 |
+
pearson_mean.append(np.mean(inner_pearson_mean))
|
447 |
+
#Plot the coeff
|
448 |
+
axes.bar(np.arange(1,7), np.abs(np.array(pearson_mean))*np.sqrt(sizes), align='center')
|
449 |
+
axes.set_xlabel('Layer')
|
450 |
+
axes.set_ylabel('Abs(Pearson)*sqrt(N_i)')
|
451 |
+
rects = axes.patches
|
452 |
+
# Now make some labels
|
453 |
+
labels = ["L%d (%d nuerons)" % (i, j) for i, j in zip(range(len(rects)), sizes)]
|
454 |
+
plt.xticks(np.arange(1,7), labels)
|
455 |
+
|
456 |
+
|
457 |
+
def update_axes(axes, xlabel, ylabel, xlim, ylim, title, xscale, yscale, x_ticks, y_ticks, p_0, p_1
|
458 |
+
,font_size = 30, axis_font = 25,legend_font = 16 ):
|
459 |
+
"""adjust the axes to the ight scale/ticks and labels"""
|
460 |
+
categories =6*['']
|
461 |
+
labels = ['$10^{-5}$', '$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '$10^0$', '$10^1$']
|
462 |
+
#The legents of the mean and the std
|
463 |
+
leg1 = plt.legend(p_0, categories, title=r'$\|Mean\left(\nabla{W_i}\right)\|$', loc='best',fontsize = legend_font,markerfirst = False, handlelength = 5)
|
464 |
+
leg2 = plt.legend(p_1, categories, title=r'$STD\left(\nabla{W_i}\right)$', loc='best',fontsize = legend_font ,markerfirst = False,handlelength = 5)
|
465 |
+
leg1.get_title().set_fontsize('21') # legend 'Title' fontsize
|
466 |
+
leg2.get_title().set_fontsize('21') # legend 'Title' fontsize
|
467 |
+
plt.gca().add_artist(leg1)
|
468 |
+
plt.gca().add_artist(leg2)
|
469 |
+
utils.adjustAxes(axes, axis_font=20, title_str='', x_ticks=x_ticks, y_ticks=y_ticks, x_lim=xlim, y_lim=ylim,
|
470 |
+
set_xlabel=True, set_ylabel=True, x_label=xlabel, y_label=ylabel, set_xlim=True, set_ylim=True,
|
471 |
+
set_ticks=True, label_size=font_size, set_yscale=True,
|
472 |
+
set_xscale=True, yscale=yscale, xscale=xscale, ytick_labels=labels, genreal_scaling=True)
|
473 |
+
|
474 |
+
|
475 |
+
|
476 |
+
def extract_array(data, name):
|
477 |
+
results = [[data[j,k][name] for k in range(data.shape[1])] for j in range(data.shape[0])]
|
478 |
+
return results
|
479 |
+
|
480 |
+
def update_bars_num_of_ts(num, p_ts, H_Xgt,DKL_YgX_YgT, axes, ind_array):
|
481 |
+
axes[1].clear()
|
482 |
+
axes[2].clear()
|
483 |
+
axes[0].clear()
|
484 |
+
current_pts =p_ts[num]
|
485 |
+
current_H_Xgt = H_Xgt[num]
|
486 |
+
current_DKL_YgX_YgT = DKL_YgX_YgT[num]
|
487 |
+
num_of_t = [c_pts.shape[0] for c_pts in current_pts]
|
488 |
+
x = range(len(num_of_t))
|
489 |
+
axes[0].bar(x, num_of_t)
|
490 |
+
axes[0].set_title('Number of Ts in every layer - Epoch number - {0}'.format(ind_array[num]))
|
491 |
+
axes[0].set_xlabel('Layer Number')
|
492 |
+
axes[0].set_ylabel('# of Ts')
|
493 |
+
axes[0].set_ylim([0, 800])
|
494 |
+
h_list, dkl_list = [], []
|
495 |
+
for i in range(len(current_pts)):
|
496 |
+
h_list.append(-np.dot(current_H_Xgt[i],current_pts[i]))
|
497 |
+
dkl_list.append(np.dot(current_DKL_YgX_YgT[i].T, current_pts[i]))
|
498 |
+
axes[1].bar(x,h_list)
|
499 |
+
axes[2].bar(x,dkl_list)
|
500 |
+
|
501 |
+
axes[2].bar(x, dkl_list)
|
502 |
+
axes[1].set_title('H(X|T)', title_size = 16)
|
503 |
+
axes[1].set_xlabel('Layer Number')
|
504 |
+
axes[1].set_ylabel('H(X|T)')
|
505 |
+
|
506 |
+
axes[2].set_title('DKL[p(y|x)||p(y|t)]',fontsize = 16)
|
507 |
+
axes[2].set_xlabel('Layer Number')
|
508 |
+
axes[2].set_ylabel('DKL[p(y|x)||p(y|t)]',fontsize = 16)
|
509 |
+
|
510 |
+
def update_bars_entropy(num, H_Xgt,DKL_YgX_YgT, axes, ind_array):
|
511 |
+
axes[0].clear()
|
512 |
+
current_H_Xgt =np.mean(H_Xgt[num], axis=0)
|
513 |
+
x = range(len(current_H_Xgt))
|
514 |
+
axes[0].bar(x, current_H_Xgt)
|
515 |
+
axes[0].set_title('H(X|T) in every layer - Epoch number - {0}'.format(ind_array[num]))
|
516 |
+
axes[0].set_xlabel('Layer Number')
|
517 |
+
axes[0].set_ylabel('# of Ts')
|
518 |
+
|
519 |
+
|
520 |
+
def plot_hist(str_name, save_name='dist'):
|
521 |
+
data_array = utils.get_data(str_name)
|
522 |
+
params = np.squeeze(np.array(data_array['information']))
|
523 |
+
ind_array = data_array['params']['epochsInds']
|
524 |
+
DKL_YgX_YgT = utils.extract_array(params, 'DKL_YgX_YgT')
|
525 |
+
p_ts = utils.extract_array(params, 'pts')
|
526 |
+
H_Xgt = utils.extract_array(params, 'H_Xgt')
|
527 |
+
|
528 |
+
f, (axes) = plt.subplots(3, 1)
|
529 |
+
#axes = [axes]
|
530 |
+
f.subplots_adjust(left=0.14, bottom=0.1, right=.928, top=0.94, wspace=0.13, hspace=0.55)
|
531 |
+
colors = LAYERS_COLORS
|
532 |
+
line_ani = animation.FuncAnimation(f, update_bars_num_of_ts, len(p_ts), repeat=False,
|
533 |
+
interval=1, blit=False, fargs=[p_ts,H_Xgt,DKL_YgX_YgT, axes,ind_array])
|
534 |
+
Writer = animation.writers['ffmpeg']
|
535 |
+
writer = Writer(fps=50)
|
536 |
+
#Save the movie
|
537 |
+
line_ani.save(save_name+'_movie.mp4',writer=writer,dpi=250)
|
538 |
+
plt.show()
|
539 |
+
|
540 |
+
def plot_alphas(str_name, save_name='dist'):
|
541 |
+
data_array = utils.get_data(str_name)
|
542 |
+
params = np.squeeze(np.array(data_array['information']))
|
543 |
+
I_XT_array = np.squeeze(np.array(extract_array(params, 'local_IXT')))
|
544 |
+
""""
|
545 |
+
for i in range(I_XT_array.shape[2]):
|
546 |
+
f1, axes1 = plt.subplots(1, 1)
|
547 |
+
|
548 |
+
axes1.plot(I_XT_array[:,:,i])
|
549 |
+
plt.show()
|
550 |
+
return
|
551 |
+
"""
|
552 |
+
I_XT_array_var = np.squeeze(np.array(extract_array(params, 'IXT_vartional')))
|
553 |
+
I_TY_array_var = np.squeeze(np.array(extract_array(params, 'ITY_vartional')))
|
554 |
+
|
555 |
+
I_TY_array = np.squeeze(np.array(extract_array(params, 'local_ITY')))
|
556 |
+
"""
|
557 |
+
f1, axes1 = plt.subplots(1, 1)
|
558 |
+
#axes1.plot(I_XT_array,I_TY_array)
|
559 |
+
f1, axes2 = plt.subplots(1, 1)
|
560 |
+
|
561 |
+
axes1.plot(I_XT_array ,I_TY_array_var)
|
562 |
+
axes2.plot(I_XT_array ,I_TY_array)
|
563 |
+
f1, axes1 = plt.subplots(1, 1)
|
564 |
+
axes1.plot(I_TY_array, I_TY_array_var)
|
565 |
+
axes1.plot([0, 1.1], [0, 1.1], transform=axes1.transAxes)
|
566 |
+
#axes1.set_title('Sigmma=' + str(sigmas[i]))
|
567 |
+
axes1.set_ylim([0, 1.1])
|
568 |
+
axes1.set_xlim([0, 1.1])
|
569 |
+
plt.show()
|
570 |
+
return
|
571 |
+
"""
|
572 |
+
#for i in range()
|
573 |
+
sigmas = np.linspace(0, 0.3, 20)
|
574 |
+
|
575 |
+
for i in range(0,20):
|
576 |
+
print (i, sigmas[i])
|
577 |
+
f1, axes1 = plt.subplots(1, 1)
|
578 |
+
axes1.plot(I_XT_array, I_XT_array_var[:,:,i], linewidth=5)
|
579 |
+
axes1.plot([0, 15.1], [0, 15.1], transform=axes1.transAxes)
|
580 |
+
axes1.set_title('Sigmma=' +str(sigmas[i]))
|
581 |
+
axes1.set_ylim([0,15.1])
|
582 |
+
axes1.set_xlim([0,15.1])
|
583 |
+
plt.show()
|
584 |
+
return
|
585 |
+
epochs_s = data_array['params']['epochsInds']
|
586 |
+
f, axes = plt.subplots(1, 1)
|
587 |
+
#epochs_s = []
|
588 |
+
colors = LAYERS_COLORS
|
589 |
+
linestyles = [ '--', '-.', '-','', ' ',':', '']
|
590 |
+
epochs_s =[0, -1]
|
591 |
+
for j in epochs_s:
|
592 |
+
for i in range(0, I_XT_array.shape[1]):
|
593 |
+
|
594 |
+
axes.plot(sigmas, I_XT_array_var[j,i,:],color = colors[i], linestyle = linestyles[j], label='Layer-'+str(i) +' Epoch - ' +str(epochs_s[j]))
|
595 |
+
title_str = 'I(X;T) for different layers as function of $\sigma$ (The width of the gaussian)'
|
596 |
+
x_label = '$\sigma$'
|
597 |
+
y_label = '$I(X;T)$'
|
598 |
+
x_lim = [0, 3]
|
599 |
+
utils.adjustAxes(axes, axis_font=20, title_str=title_str, x_ticks=[], y_ticks=[], x_lim=x_lim, y_lim=None,
|
600 |
+
set_xlabel=True, set_ylabel=True, x_label=x_label, y_label=y_label, set_xlim=True, set_ylim=False,
|
601 |
+
set_ticks=False,
|
602 |
+
label_size=20, set_yscale=False,
|
603 |
+
set_xscale=False, yscale=None, xscale=None, ytick_labels='', genreal_scaling=False)
|
604 |
+
axes.legend()
|
605 |
+
plt.show()
|
606 |
+
if __name__ == '__main__':
|
607 |
+
#The action the you want to plot
|
608 |
+
#Plot snapshots of all the networks
|
609 |
+
TIME_STEMPS = 'time-stemp'
|
610 |
+
#create movie of the networks
|
611 |
+
MOVIE = 'movie'
|
612 |
+
#plot networks with diffrenet number of layers
|
613 |
+
ALL_LAYERS = 'all_layers'
|
614 |
+
#plot networks with 5% of the data and with 80%
|
615 |
+
COMPRAED_PERCENT = 'compare_percent'
|
616 |
+
#plot the infomration curves for the networks with diffrenet percent of the data
|
617 |
+
ALL_SAMPLES = 'all_samples'
|
618 |
+
#Choose whice figure to plot
|
619 |
+
action = COMPRAED_PERCENT
|
620 |
+
prex = 'jobsFiles/'
|
621 |
+
sofix = '.pickle'
|
622 |
+
prex2 = '/Users/ravidziv/PycharmProjects/IDNNs/jobs/'
|
623 |
+
#plot above action, the norms, the gradients and the pearson coeffs
|
624 |
+
do_plot_action, do_plot_norms, do_plot_pearson = True, False, False
|
625 |
+
do_plot_eig = False
|
626 |
+
plot_movie = False
|
627 |
+
do_plot_time_stepms = False
|
628 |
+
#str_names = [[prex2+'fo_layerSizes=10,7,5,4,3_LastEpochsInds=9998_nRepeats=1_batch=3563_DataName=reg_1_nEpoch=10000_lr=0.0004_nEpochInds=964_samples=1_nDistSmpls=1/']]
|
629 |
+
if action == TIME_STEMPS or action == MOVIE:
|
630 |
+
index = 1
|
631 |
+
name_s = prex2+ 'g_layerSizes=10,7,5,4,3_LastEpochsInds=9998_nRepeats=40_batch=3563_DataName=var_u_nEpoch=10000_lr=0.0002_nEpochInds=964_samples=1_nDistSmpls=1/'
|
632 |
+
name_s = prex2 +'r_DataName=MNIST_sampleLen=1_layerSizes=400,200,150,60,50,40,30_lr=0.0002_nEpochInds=677_nRepeats=1_LastEpochsInds=1399_nDistSmpls=1_nEpoch=1400_batch=2544/'
|
633 |
+
if action ==TIME_STEMPS:
|
634 |
+
save_name = '3_time_series'
|
635 |
+
#plot_snapshots(name_s, save_name, index)
|
636 |
+
else:
|
637 |
+
save_name = 'genreal'
|
638 |
+
plot_animation(name_s, save_name)
|
639 |
+
else:
|
640 |
+
if action ==ALL_LAYERS:
|
641 |
+
mode =11
|
642 |
+
save_name = ALL_LAYERS
|
643 |
+
str_names = [[prex + 'ff3_5_198.pickle', prex+ 'ff3_4_198.pickle',prex + 'ff3_3_198.pickle'],[prex + 'ff3_2_198.pickle',prex + 'ff3_1_198.pickle',prex + 'ff4_1_10.pickle']]
|
644 |
+
str_names[1][2] = prex2+'g_layerSizes=10,7,5,4,4,3_LastEpochsInds=9998_nRepeats=20_batch=3563_DataName=var_u_nEpoch=10000_lr=0.0004_nEpochInds=964_samples=1_nDistSmpls=1/'
|
645 |
+
str_names = [[prex2 +'nbins8_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=964_nRepeats=5_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4096/',
|
646 |
+
prex2 +'nbins12_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=964_nRepeats=5_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4096/',
|
647 |
+
prex2 +'nbins18_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=964_nRepeats=5_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4096/']
|
648 |
+
,[prex2 +'nbins25_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=964_nRepeats=5_LastEpochsInds=9998_nDistrSmpls=1_nEpoch=10000_batch=4096/',
|
649 |
+
prex2 +'nbins35_DataName=var_u_sampleLen=1_layerSizes=10,7,5, 4,3_lr=0.0004_nEpochInds=964_nRepeats=5_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4096/',
|
650 |
+
prex2 + 'nbins50_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=964_nRepeats=5_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4096/' ]]
|
651 |
+
elif action == COMPRAED_PERCENT:
|
652 |
+
save_name = COMPRAED_PERCENT
|
653 |
+
#mode =0
|
654 |
+
mode = 2
|
655 |
+
str_names = [[prex + 'ff4_1_10.pickle', prex + 'ff3_1_198.pickle']]
|
656 |
+
elif action == ALL_SAMPLES:
|
657 |
+
save_name = ALL_SAMPLES
|
658 |
+
mode =3
|
659 |
+
str_names = [[prex+'t_32_1.pickle']]
|
660 |
+
root = tk.Tk()
|
661 |
+
root.withdraw()
|
662 |
+
file_path = filedialog.askopenfilename()
|
663 |
+
str_names = [[('/').join(file_path.split('/')[:-1]) + '/']]
|
664 |
+
if do_plot_action:
|
665 |
+
plot_figures(str_names, mode, save_name)
|
666 |
+
if do_plot_norms:
|
667 |
+
plot_norms(str_names)
|
668 |
+
if do_plot_pearson:
|
669 |
+
plot_pearson(str_names)
|
670 |
+
if plot_movie:
|
671 |
+
plot_animation_each_neuron(str_names, save_name)
|
672 |
+
if do_plot_eig:
|
673 |
+
pass
|
674 |
+
if do_plot_time_stepms:
|
675 |
+
plot_snapshots(str_names[0][0], save_name, 1)
|
676 |
+
|
677 |
+
#plot_eigs_movie(str_names)
|
678 |
+
plt.show()
|
idnns/plots/plot_gradients.py
ADDED
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'Calculate and plot the gradients (the mean and std of the mini-batch gradients) of the trained network'
|
2 |
+
import matplotlib
|
3 |
+
matplotlib.use("TkAgg")
|
4 |
+
import numpy as np
|
5 |
+
import idnns.plots.utils as plt_ut
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import tkinter as tk
|
8 |
+
from tkinter import filedialog
|
9 |
+
from numpy import linalg as LA
|
10 |
+
import os
|
11 |
+
import sys
|
12 |
+
import statsmodels
|
13 |
+
colors = ['red', 'c', 'blue', 'green', 'orange', 'purple']
|
14 |
+
|
15 |
+
|
16 |
+
def plot_gradients(name_s=None, data_array=None, figures_dir=''):
|
17 |
+
"""Plot the gradients and the means of the networks over the batches"""
|
18 |
+
if data_array == None:
|
19 |
+
data_array= plt_ut.get_data(name_s[0][0])
|
20 |
+
#plot_loss_figures(data_array, xlim = [0, 7000] )
|
21 |
+
#The gradients - the diemnstions are #epochs X #Batchs # Layers
|
22 |
+
conv_net = False
|
23 |
+
if conv_net:
|
24 |
+
gradients =data_array['var_grad_val'][0][0][0]
|
25 |
+
num_of_epochs = len(gradients)
|
26 |
+
num_of_batchs = len(gradients[0])
|
27 |
+
num_of_layers = len(gradients[0][0]) / 2
|
28 |
+
else:
|
29 |
+
gradients = np.squeeze(data_array['var_grad_val'])[:, :, :]
|
30 |
+
num_of_epochs,num_of_batchs, num_of_layers = gradients.shape
|
31 |
+
num_of_layers = int(num_of_layers / 2)
|
32 |
+
#The indxes where we sampled the network
|
33 |
+
print (np.squeeze(data_array['var_grad_val'])[0,0].shape)
|
34 |
+
epochsInds = (data_array['params']['epochsInds']).astype(np.int)
|
35 |
+
#The norms of the layers
|
36 |
+
#l2_norm = calc_weights_norms(data_array['ws_all'])
|
37 |
+
f_log, axes_log, f_norms, axes_norms, f_snr, axes_snr,axes_gaus, f_gaus = create_figs()
|
38 |
+
p_1, p_0, sum_y ,p_3, p_4= [], [], [], [], []
|
39 |
+
# Go over the layers
|
40 |
+
cov_traces_all,means_all = [],[]
|
41 |
+
all_gradients = np.empty(num_of_layers, dtype=np.object)
|
42 |
+
#print np.squeeze(data_array['var_grad_val']).shape
|
43 |
+
for layer in range(0,num_of_layers):
|
44 |
+
# The traces of the covarince and the means of the gradients for the current layer
|
45 |
+
# Go over all the epochs
|
46 |
+
cov_traces, means = [], []
|
47 |
+
gradinets_layer = []
|
48 |
+
for epoch_index in range(num_of_epochs):
|
49 |
+
# the gradients are dimensions of #batchs X # output weights - when output weights is the number of wieghts that go out from the layer
|
50 |
+
gradients_current_epoch_and_layer = flatted_graidnet(gradients, epoch_index, 2 * layer)
|
51 |
+
gradinets_layer.append(gradients_current_epoch_and_layer)
|
52 |
+
num_of_output_weights = gradients_current_epoch_and_layer.shape[1]
|
53 |
+
# the average vector over the batchs - this is vector in the size of #output weights
|
54 |
+
# We averged over the batchs - It's mean vector of the batchs!
|
55 |
+
average_vec = np.mean(gradients_current_epoch_and_layer, axis=0)
|
56 |
+
# The sqrt of the sum over all the weights of the squares of the gradinets - Sqrt of AA^T - This is a number
|
57 |
+
gradients_mean = LA.norm(average_vec)
|
58 |
+
# The covarince matrix is in the size of #output weights X #output weights
|
59 |
+
sum_covs_mat = np.zeros((average_vec.shape[0], average_vec.shape[0]))
|
60 |
+
# Go over all the vectors of batchs (each vector is the size of # output weights, reduce the mean (over the batchs)
|
61 |
+
# and calculate the covariance matrix
|
62 |
+
for batch_index in range(num_of_batchs):
|
63 |
+
# This is in the size of the #output weights
|
64 |
+
current_vec = gradients_current_epoch_and_layer[batch_index, :] - average_vec
|
65 |
+
# The outer product of the current gradinet of the weights (in this specipic batch) with the transpose of it -
|
66 |
+
# give a matrix in the size of # output weights X # output weights
|
67 |
+
current_cov_mat = np.einsum('i,j', current_vec, current_vec)
|
68 |
+
#current_cov_mat = np.dot(current_vec[:,None], current_vec[None,:])
|
69 |
+
# Sum the covarince matrixes over the batchs
|
70 |
+
sum_covs_mat+=current_cov_mat
|
71 |
+
#Take the mean of the cov matrix over the batchs - The size is #output weights X # output weights
|
72 |
+
mean_cov_mat = sum_covs_mat / num_of_batchs
|
73 |
+
#The trace of the mean of the cov matrix - a number
|
74 |
+
trac_cov = np.sqrt(np.trace(mean_cov_mat))
|
75 |
+
means.append(gradients_mean)
|
76 |
+
cov_traces.append(trac_cov)
|
77 |
+
"""
|
78 |
+
|
79 |
+
#cov_traces.append(np.mean(grad_norms))
|
80 |
+
#means.append(norm_mean)
|
81 |
+
c_var,c_mean,total_w = [], [],[]
|
82 |
+
|
83 |
+
for neuron in range(len(grad[epoch_number][0][layer])/10):
|
84 |
+
gradients_list = np.array([grad[epoch_number][i][layer][neuron] for i in range(len(grad[epoch_number]))])
|
85 |
+
total_w.extend(gradients_list.T)
|
86 |
+
grad_norms1 = np.std(gradients_list, axis=0)
|
87 |
+
mean_la = np.abs(np.mean(np.array(gradients_list), axis=0))
|
88 |
+
#mean_la = LA.norm(gradients_list, axis=0)
|
89 |
+
c_var.append(np.mean(grad_norms1))
|
90 |
+
c_mean.append(np.mean(mean_la))
|
91 |
+
#total_w is in size [num_of_total_weights, num of epochs]
|
92 |
+
total_w = np.array(total_w)
|
93 |
+
#c_var.append(np.sqrt(np.trace(np.cov(np.array(total_w).T)))/np.cov(np.array(total_w).T).shape[0])
|
94 |
+
#print np.mean(c_mean).shape
|
95 |
+
means.append(np.mean(c_mean))
|
96 |
+
cov_traces.append(np.mean(c_var))
|
97 |
+
"""
|
98 |
+
|
99 |
+
gradinets_layer = np.array(gradinets_layer)
|
100 |
+
all_gradients[layer]= gradinets_layer
|
101 |
+
cov_traces_all.append(np.array(cov_traces))
|
102 |
+
means_all.append(np.array(means))
|
103 |
+
#The cov_traces and the means are vectors with the dimension of # epochs
|
104 |
+
#y_var = np.array(cov_traces)
|
105 |
+
#y_mean = np.array(means)
|
106 |
+
y_var = np.sum(cov_traces_all, axis=0)
|
107 |
+
y_mean = np.sum(means_all, axis=0)
|
108 |
+
snr = y_mean**2 / y_var
|
109 |
+
#Plot the gradients and the means
|
110 |
+
c_p1, = axes_log.plot(epochsInds[:], np.sqrt(y_var),markersize = 4, linewidth = 4,color = colors[layer], linestyle=':', markeredgewidth=0.2, dashes = [4,4])
|
111 |
+
c_p0,= axes_log.plot(epochsInds[:], y_mean, linewidth = 2,color = colors[layer])
|
112 |
+
c_p3,= axes_snr.plot(epochsInds[:],snr, linewidth = 2,color = colors[layer])
|
113 |
+
c_p4,= axes_gaus.plot(epochsInds[:],np.log(1+snr), linewidth = 2,color = colors[layer])
|
114 |
+
#For the legend
|
115 |
+
p_0.append(c_p0), p_1.append(c_p1),sum_y.append(y_mean) , p_3.append(c_p3), p_4.append(c_p4)
|
116 |
+
plt_ut.adjust_axes(axes_log, axes_norms, p_0, p_1, f_log, f_norms, axes_snr, f_snr, p_3, axes_gaus, f_gaus, p_4,
|
117 |
+
directory_name=figures_dir)
|
118 |
+
plt.show()
|
119 |
+
|
120 |
+
|
121 |
+
def calc_mean_var_loss(epochsInds,loss_train):
|
122 |
+
#Loss train is in dimension # epochs X #batchs
|
123 |
+
num_of_epochs = loss_train.shape[0]
|
124 |
+
#Average over the batchs
|
125 |
+
loss_train_mean = np.mean(loss_train,1)
|
126 |
+
#The diff divided by the sampled indexes
|
127 |
+
d_mean_loss_to_dt = np.sqrt(np.abs(np.diff(loss_train_mean) / np.diff(epochsInds[:])))
|
128 |
+
var_loss = []
|
129 |
+
#Go over the epochs
|
130 |
+
for epoch_index in range(num_of_epochs):
|
131 |
+
#The loss for the specpic epoch
|
132 |
+
current_loss = loss_train[epoch_index, :]
|
133 |
+
#The derivative between the batchs
|
134 |
+
current_loss_dt = np.diff(current_loss)
|
135 |
+
#The mean of his derivative
|
136 |
+
average_loss = np.mean(current_loss_dt)
|
137 |
+
current_loss_minus_mean = current_loss_dt- average_loss
|
138 |
+
#The covarince between the batchs
|
139 |
+
cov_mat = np.dot(current_loss_minus_mean[:, None], current_loss_minus_mean[None, :])
|
140 |
+
# The trace of the cov matrix
|
141 |
+
trac_cov = np.trace(cov_mat)
|
142 |
+
var_loss.append(trac_cov)
|
143 |
+
return np.array(var_loss), d_mean_loss_to_dt
|
144 |
+
|
145 |
+
def plot_loss_figures(data_array, fig_size=(14, 10), xlim = None, y_lim = None):
|
146 |
+
epochsInds = (data_array['params']['epochsInds']).astype(np.int)
|
147 |
+
dif_var_loss, diff_mean_loss = calc_mean_var_loss(epochsInds, np.squeeze(data_array['loss_train']))
|
148 |
+
f_log1, (axes_log1) = plt.subplots(1, 1, figsize=fig_size)
|
149 |
+
axes_log1.set_title('The mean and the varince( between the batchs) of the derivative of the train error')
|
150 |
+
axes_log1.plot(epochsInds[1:], np.array(diff_mean_loss), color='green', label = 'Mean of the derivative of the error')
|
151 |
+
axes_log1.plot(epochsInds[:], (dif_var_loss), color='blue', label='Variance of the derivative of the error' )
|
152 |
+
axes_log1.set_xscale('log')
|
153 |
+
axes_log1.set_yscale('log')
|
154 |
+
axes_log1.set_xlabel('#Epochs')
|
155 |
+
axes_log1.legend()
|
156 |
+
|
157 |
+
f_log1, (axes_log1) = plt.subplots(1, 1, figsize=fig_size)
|
158 |
+
title = r'The SNR of the error derivatives'
|
159 |
+
|
160 |
+
p_5, =axes_log1.plot(epochsInds[1:], np.array(diff_mean_loss)/ np.sqrt(dif_var_loss[1:]), linewidth = 3, color='green',
|
161 |
+
)
|
162 |
+
plt_ut.update_axes(axes_log1, f_log1, '#Epochs', 'SNR',[0, 7000], [0.001, 1], title, 'log', 'log',
|
163 |
+
[1, 10, 100, 1000, 7000], [0.001, 0.01, 0.1, 1])
|
164 |
+
#axes_log1.plot(epochsInds[:], (dif_var_loss), color='blue', label='Variance of the derivative of the error')
|
165 |
+
axes_log1.legend([r'$\frac{|d Error|}{STD\left(Error)\right)}$'], loc= 'best',fontsize = 21)
|
166 |
+
|
167 |
+
def create_figs(fig_size = (14, 10)):
|
168 |
+
f_norms, (axes_norms) = plt.subplots(1, 1, figsize=fig_size)
|
169 |
+
f_log, (axes_log) = plt.subplots(1, 1, figsize=fig_size)
|
170 |
+
f_snr, (axes_snr) = plt.subplots(1, 1, figsize=fig_size)
|
171 |
+
f_gaus, (axes_gaus) = plt.subplots(1, 1, figsize=fig_size)
|
172 |
+
f_log.subplots_adjust(left=0.097, bottom=0.11, right=.95, top=0.95, wspace=0.03, hspace=0.03)
|
173 |
+
return f_log, axes_log, f_norms, axes_norms, f_snr, axes_snr,axes_gaus, f_gaus
|
174 |
+
|
175 |
+
|
176 |
+
def flatted_graidnet(gradients, epoch_number, layer):
|
177 |
+
gradients_list = []
|
178 |
+
# For each neuron in the current layer go over all the weights
|
179 |
+
for i in range(len(gradients[epoch_number])):
|
180 |
+
current_list_inner = []
|
181 |
+
for neuron in range(len(gradients[epoch_number][0][layer])):
|
182 |
+
c_n = gradients[epoch_number][i][layer][neuron]
|
183 |
+
current_list_inner.extend(c_n)
|
184 |
+
gradients_list.append(current_list_inner)
|
185 |
+
gradients_list = np.array(gradients_list)
|
186 |
+
gradients_list =np.reshape(gradients_list, (gradients_list.shape[0], -1))
|
187 |
+
|
188 |
+
return gradients_list
|
189 |
+
|
190 |
+
|
191 |
+
def calc_weights_norms(ws, num_of_layer = 6):
|
192 |
+
layer_l2_norm = []
|
193 |
+
for i in range(num_of_layer):
|
194 |
+
flatted_list = [1]
|
195 |
+
"""
|
196 |
+
if type(ws_in[epoch_number][layer_index]) is list:
|
197 |
+
flatted_list = [item for sublist in ws_in[epoch_number][layer_index] for item in sublist]
|
198 |
+
else:
|
199 |
+
flatted_list = ws_in[epoch_number][layer_index]
|
200 |
+
"""
|
201 |
+
layer_l2_norm.append(LA.norm(np.array(flatted_list)))
|
202 |
+
# plot the norms
|
203 |
+
#axes_norms.plot(epochsInds[:], np.array(layer_l2_norm), linewidth=2, color=colors[layer_index])
|
204 |
+
return layer_l2_norm
|
205 |
+
|
206 |
+
def extract_array(data, name):
|
207 |
+
results = [[data[j,k][name] for k in range(data.shape[1])] for j in range(data.shape[0])]
|
208 |
+
return results
|
209 |
+
|
210 |
+
|
211 |
+
def load_from_memory(data_array):
|
212 |
+
plot_gradients(data_array=data_array)
|
213 |
+
|
214 |
+
|
215 |
+
if __name__ == '__main__':
|
216 |
+
directory = './figures/'
|
217 |
+
if not os.path.exists(directory):
|
218 |
+
os.makedirs(directory)
|
219 |
+
root = tk.Tk()
|
220 |
+
root.withdraw()
|
221 |
+
file_path = filedialog.askopenfilename()
|
222 |
+
str_names = [[('/').join(file_path.split('/')[:-1]) + '/']]
|
223 |
+
plot_gradients(str_names, figures_dir=directory)
|
idnns/plots/utils.py
ADDED
@@ -0,0 +1,243 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib
|
2 |
+
|
3 |
+
matplotlib.use("TkAgg")
|
4 |
+
import scipy.io as sio
|
5 |
+
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import os
|
8 |
+
import numpy as np
|
9 |
+
import sys
|
10 |
+
|
11 |
+
if sys.version_info >= (3, 0):
|
12 |
+
import _pickle as cPickle
|
13 |
+
else:
|
14 |
+
import cPickle
|
15 |
+
|
16 |
+
|
17 |
+
def update_axes(axes, f, xlabel, ylabel, xlim, ylim, title='', xscale=None, yscale=None, x_ticks=None, y_ticks=None,
|
18 |
+
p_0=None, p_1=None, p_3=None, p_4=None,
|
19 |
+
p_5=None, title_size=22):
|
20 |
+
"""adjust the axes to the ight scale/ticks and labels"""
|
21 |
+
font_size = 30
|
22 |
+
axis_font = 25
|
23 |
+
legend_font = 16
|
24 |
+
categories = 6 * ['']
|
25 |
+
labels = ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '$10^0$', '$10^1$']
|
26 |
+
# If we want grey line in the midle
|
27 |
+
# axes.axvline(x=370, color='grey', linestyle=':', linewidth = 4)
|
28 |
+
# The legents of the mean and the std
|
29 |
+
"""
|
30 |
+
if p_0:
|
31 |
+
leg1 = f.legend([p_0[0],p_0[1],p_0[2],p_0[3],p_0[4], p_0[5]], categories, title=r'$\|Mean\left(\nabla{W_i}\right)\|$',bbox_to_anchor=(0.09, 0.95), loc=2,fontsize = legend_font,markerfirst = False, handlelength = 5)
|
32 |
+
leg1.get_title().set_fontsize('21') # legend 'Title' fontsize
|
33 |
+
axes.add_artist(leg1)
|
34 |
+
|
35 |
+
if p_1:
|
36 |
+
leg2 = f.legend([p_1[0],p_1[1],p_1[2],p_1[3],p_1[4], p_1[5]], categories, title=r'$Variance\left(\nabla{W_i}\right)$', loc=2,bbox_to_anchor=(0.25, 0.95), fontsize = legend_font ,markerfirst = False,handlelength = 5)
|
37 |
+
leg2.get_title().set_fontsize('21') # legend 'Title' fontsize
|
38 |
+
axes.add_artist(leg2)
|
39 |
+
if p_3:
|
40 |
+
leg2 = f.legend([p_3[0], p_3[1], p_3[2], p_3[3], p_3[4], p_3[5]], categories,
|
41 |
+
title=r'$SNR\left(\nabla{W_i}\right)$', loc=3, fontsize=legend_font,
|
42 |
+
markerfirst=False, handlelength=5,bbox_to_anchor=(0.15, 0.1))
|
43 |
+
leg2.get_title().set_fontsize('21') # legend 'Title'
|
44 |
+
|
45 |
+
if p_4:
|
46 |
+
leg2 = f.legend([p_4[0], p_4[1], p_4[2], p_4[3], p_4[4], p_4[5]], categories,
|
47 |
+
title=r'$\log\left(1+ SNR\left(\nabla{W_i}\right)\right)$', loc=3, fontsize=legend_font,
|
48 |
+
markerfirst=False, handlelength=5, bbox_to_anchor=(0.15, 0.1))
|
49 |
+
leg2.get_title().set_fontsize('21') # legend 'Title'
|
50 |
+
if p_5:
|
51 |
+
pass
|
52 |
+
#leg2 = axes.legend(handles=[r'$\frac{|d Error|}{STD\left(Error)\right)}$'], loc=3, fontsize=legend_font,
|
53 |
+
# bbox_to_anchor=(0.15, 0.1))
|
54 |
+
#leg2.get_title().set_fontsize('21')
|
55 |
+
"""
|
56 |
+
# plt.gca().add_artist(leg2)
|
57 |
+
axes.set_xscale(xscale)
|
58 |
+
axes.set_yscale(yscale)
|
59 |
+
axes.set_xlabel(xlabel, fontsize=font_size)
|
60 |
+
axes.set_ylabel(ylabel, fontsize=font_size)
|
61 |
+
axes.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
|
62 |
+
axes.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
|
63 |
+
if y_ticks:
|
64 |
+
axes.set_xticks(x_ticks)
|
65 |
+
axes.set_yticks(y_ticks)
|
66 |
+
axes.tick_params(axis='x', labelsize=axis_font)
|
67 |
+
axes.tick_params(axis='y', labelsize=axis_font)
|
68 |
+
|
69 |
+
axes.xaxis.major.formatter._useMathText = True
|
70 |
+
axes.set_yticklabels(labels, fontsize=font_size)
|
71 |
+
axes.set_title(title, fontsize=title_size)
|
72 |
+
axes.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useMathText=True))
|
73 |
+
axes.set_xlim(xlim)
|
74 |
+
if ylim:
|
75 |
+
axes.set_ylim(ylim)
|
76 |
+
|
77 |
+
|
78 |
+
def update_axes_norms(axes, xlabel, ylabel):
|
79 |
+
"""Adjust the axes of the norms figure with labels/ticks"""
|
80 |
+
font_size = 30
|
81 |
+
axis_font = 25
|
82 |
+
legend_font = 16
|
83 |
+
# the legends
|
84 |
+
categories = [r'$\|W_1\|$', r'$\|W_2\|$', r'$\|W_3\|$', r'$\|W_4\|$', r'$\|W_5\|$', r'$\|W_6\|$']
|
85 |
+
# Grey line in the middle
|
86 |
+
axes.axvline(x=370, color='grey', linestyle=':', linewidth=4)
|
87 |
+
axes.legend(categories, loc='best', fontsize=legend_font)
|
88 |
+
axes.set_xlabel(xlabel, fontsize=font_size)
|
89 |
+
axes.set_ylabel(ylabel, fontsize=font_size)
|
90 |
+
axes.tick_params(axis='x', labelsize=axis_font)
|
91 |
+
axes.tick_params(axis='y', labelsize=axis_font)
|
92 |
+
|
93 |
+
|
94 |
+
def update_axes_snr(axes, xlabel, ylabel):
|
95 |
+
"""Adjust the axes of the norms figure with labels/ticks"""
|
96 |
+
font_size = 30
|
97 |
+
axis_font = 25
|
98 |
+
legend_font = 16
|
99 |
+
# the legends
|
100 |
+
categories = [r'$W_1$', r'$W_2$', r'$W_3$', r'$W_4$', r'$W_5$', r'$W_6$']
|
101 |
+
# Grey line in the middle
|
102 |
+
axes.set_title('The SNR ($norm^2/variance$)')
|
103 |
+
# axes.axvline(x=370, color='grey', linestyle=':', linewidth=4)
|
104 |
+
axes.legend(categories, loc='best', fontsize=legend_font)
|
105 |
+
axes.set_xlabel(xlabel, fontsize=font_size)
|
106 |
+
axes.set_ylabel(ylabel, fontsize=font_size)
|
107 |
+
axes.tick_params(axis='x', labelsize=axis_font)
|
108 |
+
axes.tick_params(axis='y', labelsize=axis_font)
|
109 |
+
|
110 |
+
|
111 |
+
def adjust_axes(axes_log, axes_norms, p_0, p_1, f_log, f_norms, axes_snr=None, f_snr=None, p_3=None, axes_gaus=None,
|
112 |
+
f_gau=None, p_4=None, directory_name=''):
|
113 |
+
# adejust the figure according the specipic labels, scaling and legends
|
114 |
+
# Change the log and log to linear if you want linear scaling
|
115 |
+
# update_axes(reg_axes, '# Epochs', 'Normalized Mean and STD', [0, 10000], [0.000001, 10], '', 'log', 'log', [1, 10, 100, 1000, 10000], [0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10], p_0, p_1)
|
116 |
+
title = 'The Mean and std of the gradients of each layer'
|
117 |
+
update_axes(axes_log, f_log, '# Epochs', 'Mean and STD', [0, 7000], [0.001, 10], title, 'log', 'log',
|
118 |
+
[1, 10, 100, 1000, 7000], [0.001, 0.01, 0.1, 1, 10], p_0, p_1)
|
119 |
+
update_axes_norms(axes_norms, '# Epochs', '$L_2$')
|
120 |
+
if p_3:
|
121 |
+
title = r'SNR of the gradients ($\frac{norm^2}{variance}$)'
|
122 |
+
update_axes(axes_snr, f_snr, '# Epochs', 'SNR', [0, 7000], [0.0001, 10], title, 'log', 'log',
|
123 |
+
[1, 10, 100, 1000, 7000], [0.0001, 0.001, 0.01, 0.1, 1, 10], p_3=p_3)
|
124 |
+
if p_4:
|
125 |
+
title = r'Gaussian Channel bounds of the gradients ($\log\left(1+SNR\right)$)'
|
126 |
+
|
127 |
+
update_axes(axes_gaus, f_gau, '# Epochs', 'log(SNR+1)', [0, 7000], [0.0001, 10], title, 'log', 'log',
|
128 |
+
[1, 10, 100, 1000, 7000], [0.0001, 0.001, 0.01, 0.1, 1, 10], p_4=p_4)
|
129 |
+
# axes_log.plot(epochsInds[1:], np.abs(np.diff(np.squeeze(data_array['loss_train']))) / np.diff(epochsInds[:]), color='black', linewidth = 3)
|
130 |
+
|
131 |
+
# axes_log.plot(epochsInds[0:], np.sum(np.array(sum_y), axis=0), color='c', linewidth = 3)
|
132 |
+
# axes_log.plot(epochsInds[1:], diff_mean_loss, color='red', linewidth = 3)
|
133 |
+
# f_log1, (axes_log1) = plt.subplots(1, 1, figsize=fig_size)
|
134 |
+
|
135 |
+
# axes_log1.plot(epochsInds[1:], np.sum(np.array(sum_y), axis=0)[1:] / diff_mean_loss, color='c', linewidth=3)
|
136 |
+
|
137 |
+
# axes_log.set_xscale('log')
|
138 |
+
f_log.savefig(directory_name + 'log_gradient.svg', dpi=200, format='svg')
|
139 |
+
f_norms.savefig(directory_name + 'norms.jpg', dpi=200, format='jpg')
|
140 |
+
|
141 |
+
|
142 |
+
def adjustAxes(axes, axis_font=20, title_str='', x_ticks=[], y_ticks=[], x_lim=None, y_lim=None,
|
143 |
+
set_xlabel=True, set_ylabel=True, x_label='', y_label='', set_xlim=True, set_ylim=True, set_ticks=True,
|
144 |
+
label_size=20, set_yscale=False,
|
145 |
+
set_xscale=False, yscale=None, xscale=None, ytick_labels='', genreal_scaling=False):
|
146 |
+
"""Organize the axes of the given figure"""
|
147 |
+
if set_xscale:
|
148 |
+
axes.set_xscale(xscale)
|
149 |
+
if set_yscale:
|
150 |
+
axes.set_yscale(yscale)
|
151 |
+
if genreal_scaling:
|
152 |
+
axes.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
|
153 |
+
axes.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
|
154 |
+
axes.xaxis.major.formatter._useMathText = True
|
155 |
+
axes.set_yticklabels(ytick_labels)
|
156 |
+
axes.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useMathText=True))
|
157 |
+
if set_xlim:
|
158 |
+
axes.set_xlim(x_lim)
|
159 |
+
if set_ylim:
|
160 |
+
axes.set_ylim(y_lim)
|
161 |
+
axes.set_title(title_str, fontsize=axis_font + 2)
|
162 |
+
axes.tick_params(axis='y', labelsize=axis_font)
|
163 |
+
axes.tick_params(axis='x', labelsize=axis_font)
|
164 |
+
if set_ticks:
|
165 |
+
axes.set_xticks(x_ticks)
|
166 |
+
axes.set_yticks(y_ticks)
|
167 |
+
if set_xlabel:
|
168 |
+
axes.set_xlabel(x_label, fontsize=label_size)
|
169 |
+
if set_ylabel:
|
170 |
+
axes.set_ylabel(y_label, fontsize=label_size)
|
171 |
+
|
172 |
+
|
173 |
+
def create_color_bar(f, cmap, colorbar_axis, bar_font, epochsInds, title):
|
174 |
+
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=0, vmax=1))
|
175 |
+
sm._A = []
|
176 |
+
cbar_ax = f.add_axes(colorbar_axis)
|
177 |
+
cbar = f.colorbar(sm, ticks=[], cax=cbar_ax)
|
178 |
+
cbar.ax.tick_params(labelsize=bar_font)
|
179 |
+
cbar.set_label(title, size=bar_font)
|
180 |
+
cbar.ax.text(0.5, -0.01, epochsInds[0], transform=cbar.ax.transAxes,
|
181 |
+
va='top', ha='center', size=bar_font)
|
182 |
+
cbar.ax.text(0.5, 1.0, str(epochsInds[-1]), transform=cbar.ax.transAxes,
|
183 |
+
va='bottom', ha='center', size=bar_font)
|
184 |
+
|
185 |
+
|
186 |
+
def get_data(name):
|
187 |
+
"""Load data from the given name"""
|
188 |
+
gen_data = {}
|
189 |
+
# new version
|
190 |
+
if os.path.isfile(name + 'data.pickle'):
|
191 |
+
curent_f = open(name + 'data.pickle', 'rb')
|
192 |
+
d2 = cPickle.load(curent_f)
|
193 |
+
# Old version
|
194 |
+
else:
|
195 |
+
curent_f = open(name, 'rb')
|
196 |
+
d1 = cPickle.load(curent_f)
|
197 |
+
data1 = d1[0]
|
198 |
+
data = np.array([data1[:, :, :, :, :, 0], data1[:, :, :, :, :, 1]])
|
199 |
+
# Convert log e to log2
|
200 |
+
normalization_factor = 1 / np.log2(2.718281)
|
201 |
+
epochsInds = np.arange(0, data.shape[4])
|
202 |
+
d2 = {}
|
203 |
+
d2['epochsInds'] = epochsInds
|
204 |
+
d2['information'] = data / normalization_factor
|
205 |
+
return d2
|
206 |
+
|
207 |
+
|
208 |
+
def load_reverese_annealing_data(name, max_beta=300, min_beta=0.8, dt=0.1):
|
209 |
+
"""Load mat file of the reverse annealing data with the give params"""
|
210 |
+
with open(name + '.mat', 'rb') as handle:
|
211 |
+
d = sio.loadmat(name + '.mat')
|
212 |
+
F = d['F']
|
213 |
+
ys = d['y']
|
214 |
+
PXs = np.ones(len(F)) / len(F)
|
215 |
+
f_PYs = np.mean(ys)
|
216 |
+
PYs = np.array([f_PYs, 1 - f_PYs])
|
217 |
+
PYX = np.concatenate((np.array(ys)[None, :], 1 - np.array(ys)[None, :]))
|
218 |
+
mybetaS = 2 ** np.arange(np.log2(min_beta), np.log2(max_beta), dt)
|
219 |
+
mybetaS = mybetaS[::-1]
|
220 |
+
PTX0 = np.eye(PXs.shape[0])
|
221 |
+
return mybetaS, np.squeeze(PTX0), np.squeeze(PXs), np.squeeze(PYX), np.squeeze(PYs)
|
222 |
+
|
223 |
+
|
224 |
+
def get_data(name):
|
225 |
+
"""Load data from the given name"""
|
226 |
+
gen_data = {}
|
227 |
+
# new version
|
228 |
+
if os.path.isfile(name + 'data.pickle'):
|
229 |
+
curent_f = open(name + 'data.pickle', 'rb')
|
230 |
+
d2 = cPickle.load(curent_f)
|
231 |
+
# Old version
|
232 |
+
else:
|
233 |
+
curent_f = open(name, 'rb')
|
234 |
+
d1 = cPickle.load(curent_f)
|
235 |
+
data1 = d1[0]
|
236 |
+
data = np.array([data1[:, :, :, :, :, 0], data1[:, :, :, :, :, 1]])
|
237 |
+
# Convert log e to log2
|
238 |
+
normalization_factor = 1 / np.log2(2.718281)
|
239 |
+
epochsInds = np.arange(0, data.shape[4])
|
240 |
+
d2 = {}
|
241 |
+
d2['epochsInds'] = epochsInds
|
242 |
+
d2['information'] = data / normalization_factor
|
243 |
+
return d2
|
main.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Train % plot networks in the information plane
|
3 |
+
"""
|
4 |
+
|
5 |
+
import tensorflow.compat.v1 as tf
|
6 |
+
tf.disable_v2_behavior()
|
7 |
+
|
8 |
+
|
9 |
+
from idnns.networks import information_network as inet
|
10 |
+
def main():
|
11 |
+
#Build the network
|
12 |
+
print ('Building the network')
|
13 |
+
net = inet.informationNetwork()
|
14 |
+
net.print_information()
|
15 |
+
print ('Start running the network')
|
16 |
+
net.run_network()
|
17 |
+
print ('Saving data')
|
18 |
+
net.save_data()
|
19 |
+
print ('Ploting figures')
|
20 |
+
#Plot the newtork
|
21 |
+
net.plot_network()
|
22 |
+
if __name__ == '__main__':
|
23 |
+
main()
|
24 |
+
|
test.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import idnns.plots.plot_figures as plt_fig
|
2 |
+
if __name__ == '__main__':
|
3 |
+
|
4 |
+
str_name =[['jobs/usa22_DataName=MNIST_sampleLen=1_layerSizes=400,200,100_lr=0.002_nEpochInds=22_nRepeats=1_LastEpochsInds=299_nDistSmpls=1_nEpoch=300_batch=2560/']]
|
5 |
+
str_name = [['jobs/trails1_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=84_nRepeats=1_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4016/']]
|
6 |
+
str_name =[['jobs/trails1_DataName=g2_sampleLen=1_layerSizes=10,7,5,4,3_lr=1e-05_nEpochInds=75_nRepeats=1_LastEpochsInds=999_nDistSmpls=1_nEpoch=1000_batch=4016/']]
|
7 |
+
#str_name = [['jobs/trails2_DataName=var_u_sampleLen=1_layerSizes=10,7,5,4,3_lr=0.0004_nEpochInds=84_nRepeats=1_LastEpochsInds=9998_nDistSmpls=1_nEpoch=10000_batch=4016/']]
|
8 |
+
#plt_fig.plot_figures(str_name, 2, 'd')
|
9 |
+
plt_fig.plot_alphas(str_name[0][0])
|
10 |
+
mode = 2
|
11 |
+
save_name = 'figure'
|
12 |
+
#plt_fig.plot_figures(str_name, mode, save_name)
|
13 |
+
|
14 |
+
#plt_fig.plot_hist(str_name[0][0])
|