Need help to improve graphviz rendering of a basic neural network - neural-network

I try to use Graphviz to get a picture of the state of a basic neural network. The input layer has 14 neurons, and the output layer is only one neuron. One can choose the number of hidden layers as well as the number of neurons that lies within each layer. Usually, there is only one hidden layer. The number of neurons within this layer can be rather big though (like 1000). But, for demonstration purposes, I only need like 5 neurons (I'm aware I'll never get useful picture for hundreds of neurons). I'd like to:
Write the weight of the edge (connection) between each neuron
Modify the thickness of the edge in accordance to its weight
Maybe label each layer
About the thickness, I know I can use "penwidth". I also know about clusters, that can help bounding each layer and label them. My main problem, so far, is that there are too many edges, between the input layer (14 neurons) and the hidden layer, for Graphiz to produce a clear picture. If I add labels to display the weights of connections, it's even worse. I think putting more space between my input and hidden layer could improve the readability of the rendering, but I didn't find how to do that, despite long researches.
Here is how looks like for the moment:
Current rendering of the neural network, without weights
Here is the automatically generated dot code that is behind this picture:
digraph graphname {
0.0 -> 1.0;
0.1 -> 1.0;
0.2 -> 1.0;
0.3 -> 1.0;
0.4 -> 1.0;
0.5 -> 1.0;
0.6 -> 1.0;
0.7 -> 1.0;
0.8 -> 1.0;
0.9 -> 1.0;
0.10 -> 1.0;
0.11 -> 1.0;
0.12 -> 1.0;
0.13 -> 1.0;
0.0 -> 1.1;
0.1 -> 1.1;
0.2 -> 1.1;
0.3 -> 1.1;
0.4 -> 1.1;
0.5 -> 1.1;
0.6 -> 1.1;
0.7 -> 1.1;
0.8 -> 1.1;
0.9 -> 1.1;
0.10 -> 1.1;
0.11 -> 1.1;
0.12 -> 1.1;
0.13 -> 1.1;
0.0 -> 1.2;
0.1 -> 1.2;
0.2 -> 1.2;
0.3 -> 1.2;
0.4 -> 1.2;
0.5 -> 1.2;
0.6 -> 1.2;
0.7 -> 1.2;
0.8 -> 1.2;
0.9 -> 1.2;
0.10 -> 1.2;
0.11 -> 1.2;
0.12 -> 1.2;
0.13 -> 1.2;
0.0 -> 1.3;
0.1 -> 1.3;
0.2 -> 1.3;
0.3 -> 1.3;
0.4 -> 1.3;
0.5 -> 1.3;
0.6 -> 1.3;
0.7 -> 1.3;
0.8 -> 1.3;
0.9 -> 1.3;
0.10 -> 1.3;
0.11 -> 1.3;
0.12 -> 1.3;
0.13 -> 1.3;
0.0 -> 1.4;
0.1 -> 1.4;
0.2 -> 1.4;
0.3 -> 1.4;
0.4 -> 1.4;
0.5 -> 1.4;
0.6 -> 1.4;
0.7 -> 1.4;
0.8 -> 1.4;
0.9 -> 1.4;
0.10 -> 1.4;
0.11 -> 1.4;
0.12 -> 1.4;
0.13 -> 1.4;
1.0 -> 2.0;
1.1 -> 2.0;
1.2 -> 2.0;
1.3 -> 2.0;
1.4 -> 2.0;
}
In this code, the neurons are numbered this way: LAYER_NUMBER.NEURON_NUMBER.
Notice that neurons do not appear in the right order in the picture, problem that I didn't solve either...
Thanks for you help.

Here is a graph containing a few different ideas:
ranksep increases the distance between "ranks".
margin makes nodes bigger. (There are probably better ways to do this.)
Color each node and have each edges from that node all use the same hue. For readability, you can make the nodes lighter and the edges darker versions of that hue. Don't worry about using color names, because it supports #rrggbb notation.
I've found using color like this greatly adds to visual clarity for complicated graphs. It is much easier to follow an edge when it is colored, and if something has low weight, it could be made either barely visible or completely invisible.
Another added benefit of this colorization is that it can echo the approach of staining real neurons. See this for inspiration http://neuroimages.tumblr.com
digraph graphname {
graph [rankdir="LR" ranksep="3.0"]
node [style=filled];
edge [penwidth="0.5" color="#e0e0e0"];
0.1 [fillcolor="cadetblue"];
0.2 [fillcolor="coral"];
0.3 [fillcolor="green"];
0.4 [fillcolor="gold"];
0.6 [fillcolor="cyan"];
1.0 [margin=0.4];
1.1 [margin=0.4];
1.2 [margin=0.4];
1.3 [margin=0.4];
1.4 [margin=0.4];
0.0 -> 1.0;
0.1 -> 1.0 [color="cadetblue1" penwidth="1"];
0.2 -> 1.0 [color="coral2" penwidth="2"];
0.3 -> 1.0 [color="green3" penwidth="6"];
0.4 -> 1.0 [color="gold4" penwidth="8"];
0.5 -> 1.0;
0.6 -> 1.0;
0.7 -> 1.0;
0.8 -> 1.0;
0.9 -> 1.0;
0.10 -> 1.0;
0.11 -> 1.0;
0.12 -> 1.0;
0.13 -> 1.0;
0.0 -> 1.1;
0.1 -> 1.1 [color="cadetblue2" penwidth="2"];
0.2 -> 1.1 [color="coral3" penwidth="5"];
0.3 -> 1.1 [color="green4" penwidth="8"];
0.4 -> 1.1;
0.5 -> 1.1 [color="cyan1" penwidth="1"];
0.6 -> 1.1;
0.7 -> 1.1;
0.8 -> 1.1;
0.9 -> 1.1;
0.10 -> 1.1;
0.11 -> 1.1;
0.12 -> 1.1;
0.13 -> 1.1;
0.0 -> 1.2;
0.1 -> 1.2 [color="cadetblue3" penwidth="3"];
0.2 -> 1.2 [color="coral4" penwidth="6"];
0.3 -> 1.2;
0.4 -> 1.2 [color="gold1" penwidth="1"];
0.5 -> 1.2 [color="cyan2" penwidth="2"];
0.6 -> 1.2;
0.7 -> 1.2;
0.8 -> 1.2;
0.9 -> 1.2;
0.10 -> 1.2;
0.11 -> 1.2;
0.12 -> 1.2;
0.13 -> 1.2;
0.0 -> 1.3;
0.1 -> 1.3 [color="cadetblue4" penwidth="4"];
0.2 -> 1.3;
0.3 -> 1.3 [color="green1" penwidth="1"];
0.4 -> 1.3 [color="gold2" penwidth="2"];
0.5 -> 1.3 [color="cyan3" penwidth="3"];
0.6 -> 1.3;
0.7 -> 1.3;
0.8 -> 1.3;
0.9 -> 1.3;
0.10 -> 1.3;
0.11 -> 1.3;
0.12 -> 1.3;
0.13 -> 1.3;
0.0 -> 1.4;
0.1 -> 1.4 [color="cadetblue4" penwidth="2"];
0.2 -> 1.4 [color="coral4" penwidth="2"];
0.3 -> 1.4 [color="green4" penwidth="2"];
0.4 -> 1.4 [color="gold4" penwidth="2"];
0.5 -> 1.4 [color="cyan4" penwidth="2"];
0.6 -> 1.4;
0.7 -> 1.4;
0.8 -> 1.4;
0.9 -> 1.4;
0.10 -> 1.4;
0.11 -> 1.4;
0.12 -> 1.4;
0.13 -> 1.4;
1.0 -> 2.0;
1.1 -> 2.0;
1.2 -> 2.0;
1.3 -> 2.0;
1.4 -> 2.0;
}

Related

find maximum of 3 other rows in same array using 'for' for each value

I have 3 results for the same problems in the following array:
(0.0 0.0 0.2
0.8 0.0 1.2
2.9 1.6 4.3
0.0 1.5 0.2
0.0 0.0 0.4
0.0 2.4 0.3
0.4 0.3 6.2
0.9 0.0 0.4
0.3 0.0 1.2)
For example, one problem has the results 0.0 0.0 0.4 in positions (1,1) (4,1) (7,1). I am trying to find the maximum result in order to have only a 3x3 array:
for m=1:3
for n=1:3
sy(m,n)=max(sy(m,n), sy(m+3,n), sy(m+3*2,n));
end
end
sy=imresize(sy, [3 3]);
And I get MAX with two matrices to compare and a working dimension is
not supported.
Assuming that your matrix is set up as follows:
sy =[0.0 0.0 0.2; 0.8 0.0 1.2; 2.9 1.6 4.3; 0.0 1.5 0.2 ;...
0.0 0.0 0.4; 0.0 2.4 0.3; 0.4 0.3 6.2; 0.9 0.0 0.4; 0.3 0.0 1.2];
Then I think:
sy = max(reshape(sy',3,3,3),[],3)';
Achieves your desired result.

How to put a border around a tikz figure with tcbox?

The following code produces the figure I want but only when I remove the \tcbox. I'd like a border around the figure. What I have done below works with other tikz figures. The problem seems to be with the table data. Can anyone please advise?
\begin{figure}
\tcbox{
\begin{tikzpicture}
\begin{axis}[
legend pos=south east,
xlabel=Variable 1, % label x axis
ylabel=Variable 2, % label y axis
]
\addplot[
scatter, only marks,
scatter/classes={
a={mark=square*,blue},
b={mark=triangle*,red}
}
]
table[x=x,y=y,meta=label]{
x y label
0.1 0.35 a
0.2 0.4 a
0.25 0.35 a
0.3 0.4 a
0.3 0.35 a
0.4 0.3 a
0.45 0.3 a
0.4 0.4 a
0.6 0.7 b
0.65 0.55 b
0.65 0.55 b
0.7 0.6 b
0.75 0.65 b
0.8 0.75 b
0.9 0.6 b
0.7 0.6 b
0.5 0.7 b
0.5 0.55 b
0.6 0.8 b
};
\legend{}
\end{axis}
\end{tikzpicture}
}
\caption{CAP HERE}
\label{statsexample}
\end{figure}
The issue is with your row separator. You can use row sep=crcr instead:
\begin{figure}
\tcbox{
\begin{tikzpicture}
\begin{axis}[
legend pos=south east,
xlabel=Variable 1, % label x axis
ylabel=Variable 2, % label y axis
]
\addplot[
scatter, only marks,
scatter/classes={
a={mark=square*,blue},
b={mark=triangle*,red}
}
]
table[x=x,y=y,meta=label,row sep=crcr]{
x y label\\
0.1 0.35 a\\
0.2 0.4 a\\
0.25 0.35 a\\
0.3 0.4 a\\
0.3 0.35 a\\
0.4 0.3 a\\
0.45 0.3 a\\
0.4 0.4 a\\
0.6 0.7 b\\
0.65 0.55 b\\
0.65 0.55 b\\
0.7 0.6 b\\
0.75 0.65 b\\
0.8 0.75 b\\
0.9 0.6 b\\
0.7 0.6 b\\
0.5 0.7 b\\
0.5 0.55 b\\
0.6 0.8 b\\
};
\end{axis}
\end{tikzpicture}
}
\caption{CAP HERE}
\label{statsexample}
\end{figure}

How to select the average value from 3 matrices

I am new to MATLAB and I need help. I have 3 matrices (A, B, and C) and I want to create a new matrix average_ABC that contains average values.
A = [ 0.3 0.5 0.9
0.14 0.36 0.1
0.9 0.5 0.14]
B = [ 0.8 0.9 0.14
0.1 0.25 0.4
0.8 0.14 0.25]
C = [0.25 0.3 0.47
0.12 0.3 0.2
0.14 0.56 0.9]
The resulting matrix will be
average_matrix = [ 0.3 0.5 0.47
0.12 0.25 0.2
0.8 0.5 0.25]
Please, any suggestion, how can I do it?
You can first concatenate your matrices along the third dimension (using cat) and then compute whatever you want using the dim parameter that is available for most functions to specify that you want to perform that operation along the third dimension.
Also you've stated that you want the average (mean), but based on your example you actually want the median. Either way, we can compute them using this method.
data = cat(3, A, B, C);
% Compute the mean
mean(data, 3)
% 0.45 0.56667 0.50333
% 0.12 0.30333 0.23333
% 0.61333 0.4 0.43
% Compute the median (which seems to be what you actually want)
median(data, 3)
% 0.3 0.5 0.47
% 0.12 0.3 0.2
% 0.8 0.5 0.25
I hope this will work
average_matrix=(A+B+C)/3.;

how can transform a matrix matlab into file .txt?

I have a matrix proba (size :10 * 5).
proba=[0.5 0.3 0.8 0.9 0.8;
0.50 0.36 0.58 0.58 0.98;
0.1 0.25 0.6 0.8 0.9;
0.5 0.3 0.8 0.9 0.8;
0.2 0.9 0.58 0.58 0.69;
0.58 0.14 0.1 0.2 0.3;
0.25 0.9 0.8 0.7 0.5;
0.58 0.69 0.25 0.1 0.1;
0.1 0.25 0.36 0.2 0.3;
0.5 0.3 0.8 0.9 0.8 ];
I want to transform this matrix into a text file (proba.txt) with which the index column is written and the value of the column for each line as follows :
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
1 0.50 2 0.36 3 0.58 4 0.58 5 0.98
.
.
.
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
Please I need help, how can I do it?thanks in advance
You can use this function, it is useful for every matrix.
function data = addIndex(X)
[r, c] = size(X);
index = ones(r, 1);
data = zeros(r, 2 * c);
for i = 1:c
data(:, 2 * i - 1) = i .* index;
data(:, 2 * i) = X(:, i);
end
dlmwrite('proba.txt', data, '\t')
end
you can easily do this using dlmwrite, but first you want to add the column of indexes in front of your matrix:
function result = writematrix(proba)
rowind = 1:size(proba,2);
for t = 1:size(proba,1);
C(t,:,:) = [rowind',proba(t,:)']';
D(t,:) = C(t(:),:);
end
dlmwrite('filename.txt',D,'\t') %//I assume you want tab delimiter, if you want space, it is ' ' instead
%//dlmwrite('filename.txt',D,' ')
end
Note that this will write the text file into your local directory, and that it only works for numerical values, not strings, for strings, it is better to use csvwrite.
EDIT : Ops, didn't read the question fully, this should now work fine.

Plotting surface in Octave results in non-uniform surface?

I'm trying to plot the attached data in octave. The 1st column is the X values, the 2nd is the Y value, and the last is the Z values.
I'm running this script:
xVec = reshape(mat(:,1),25,9);
yVec = reshape(mat(:,2),25,9);
zVec = reshape(mat(:,3),25,9);
surf(xVec,yVec,zVec);
axis([0.15 0.85 0.15 0.85]);
set(gca, 'XTick',0.20:0.05:0.80);
set(gca, 'YTick',0.20:0.05:0.80);
But I keep getting an uneven surface which is very hard to understand. Why is that? What am I missing?
The data:
0.15 0.15 40.802
0.15 0.2 40.673
0.15 0.25 40.526
0.15 0.3 40.83
0.15 0.35 40.862
0.15 0.4 40.652
0.15 0.45 40.924
0.15 0.5 40.774
0.15 0.55 41.088
0.15 0.6 40.749
0.15 0.65 41.099
0.15 0.7 41.753
0.15 0.75 41.607
0.15 0.8 41.911
0.15 0.85 41.537
0.2 0.15 39.809
0.2 0.2 39.884
0.2 0.25 40.595
0.2 0.3 40.497
0.2 0.35 40.863
0.2 0.4 41.325
0.2 0.45 40.916
0.2 0.5 40.431
0.2 0.55 40.583
0.2 0.6 40.858
0.2 0.65 40.548
0.2 0.7 41.668
0.2 0.75 41.863
0.2 0.8 41.499
0.2 0.85 41.903
0.25 0.15 39.894
0.25 0.2 39.686
0.25 0.25 40.227
0.25 0.3 40.625
0.25 0.35 40.572
0.25 0.4 41.034
0.25 0.45 40.828
0.25 0.5 40.802
0.25 0.55 40.196
0.25 0.6 40.493
0.25 0.65 41.265
0.25 0.7 40.963
0.25 0.75 41.023
0.25 0.8 41.396
0.25 0.85 41.596
0.3 0.15 39.546
0.3 0.2 40.216
0.3 0.25 39.535
0.3 0.3 39.945
0.3 0.35 40.108
0.3 0.4 40.726
0.3 0.45 40.187
0.3 0.5 41.279
0.3 0.55 40.747
0.3 0.6 41.122
0.3 0.65 40.91
0.3 0.7 40.292
0.3 0.75 41.04
0.3 0.8 41.287
0.3 0.85 42.023
0.35 0.15 38.693
0.35 0.2 40.269
0.35 0.25 40.561
0.35 0.3 40.536
0.35 0.35 40.268
0.35 0.4 39.947
0.35 0.45 40.259
0.35 0.5 40.146
0.35 0.55 41.048
0.35 0.6 40.263
0.35 0.65 40.875
0.35 0.7 41.281
0.35 0.75 40.836
0.35 0.8 41.322
0.35 0.85 41.734
0.4 0.15 39.321
0.4 0.2 39.103
0.4 0.25 39.694
0.4 0.3 40.529
0.4 0.35 40.272
0.4 0.4 39.826
0.4 0.45 40.224
0.4 0.5 40.808
0.4 0.55 40.66
0.4 0.6 40.003
0.4 0.65 41.626
0.4 0.7 41.549
0.4 0.75 41.523
0.4 0.8 41.408
0.4 0.85 41.545
0.45 0.15 39.428
0.45 0.2 39.53
0.45 0.25 39.988
0.45 0.3 40.039
0.45 0.35 40.075
0.45 0.4 40.206
0.45 0.45 40.946
0.45 0.5 41.027
0.45 0.55 41.214
0.45 0.6 40.98
0.45 0.65 40.874
0.45 0.7 41.483
0.45 0.75 41.151
0.45 0.8 41.123
0.45 0.85 40.528
0.5 0.15 39.477
0.5 0.2 39.62
0.5 0.25 40.265
0.5 0.3 39.61
0.5 0.35 40.109
0.5 0.4 40.232
0.5 0.45 40.212
0.5 0.5 40.861
0.5 0.55 39.665
0.5 0.6 41.225
0.5 0.65 40.577
0.5 0.7 40.62
0.5 0.75 41.244
0.5 0.8 40.977
0.5 0.85 41.753
0.55 0.15 39.033
0.55 0.2 39.769
0.55 0.25 40.164
0.55 0.3 40.351
0.55 0.35 40.592
0.55 0.4 40.227
0.55 0.45 40.14
0.55 0.5 40.734
0.55 0.55 40.429
0.55 0.6 40.701
0.55 0.65 40.849
0.55 0.7 40.596
0.55 0.75 41.481
0.55 0.8 41.27
0.55 0.85 40.755
0.6 0.15 38.944
0.6 0.2 39.76
0.6 0.25 39.051
0.6 0.3 40.009
0.6 0.35 39.84
0.6 0.4 40.072
0.6 0.45 41.282
0.6 0.5 40.606
0.6 0.55 40.98
0.6 0.6 41.141
0.6 0.65 40.111
0.6 0.7 41.627
0.6 0.75 41.798
0.6 0.8 41.196
0.6 0.85 41.35
0.65 0.15 39.457
0.65 0.2 39.38
0.65 0.25 40.26
0.65 0.3 40.142
0.65 0.35 39.935
0.65 0.4 40.496
0.65 0.45 39.862
0.65 0.5 40.665
0.65 0.55 40.187
0.65 0.6 40.955
0.65 0.65 39.834
0.65 0.7 40.641
0.65 0.75 41.162
0.65 0.8 41.028
0.65 0.85 41.54
0.7 0.15 38.938
0.7 0.2 39.803
0.7 0.25 39.485
0.7 0.3 39.8
0.7 0.35 39.459
0.7 0.4 39.895
0.7 0.45 40.203
0.7 0.5 40.222
0.7 0.55 40.176
0.7 0.6 41.01
0.7 0.65 41.433
0.7 0.7 41.651
0.7 0.75 41.018
0.7 0.8 41.185
0.7 0.85 41.216
0.75 0.15 39.182
0.75 0.2 38.856
0.75 0.25 39.992
0.75 0.3 40.005
0.75 0.35 39.613
0.75 0.4 39.526
0.75 0.45 40.232
0.75 0.5 40.45
0.75 0.55 41.157
0.75 0.6 40.578
0.75 0.65 41.106
0.75 0.7 41.252
0.75 0.75 40.773
0.75 0.8 41.207
0.75 0.85 42.219
0.8 0.15 39.33
0.8 0.2 39.463
0.8 0.25 39.435
0.8 0.3 40.252
0.8 0.35 39.819
0.8 0.4 39.826
0.8 0.45 40.506
0.8 0.5 41.031
0.8 0.55 40.666
0.8 0.6 41.306
0.8 0.65 40.674
0.8 0.7 41.489
0.8 0.75 40.956
0.8 0.8 41.61
0.8 0.85 41.099
0.85 0.15 39.247
0.85 0.2 38.983
0.85 0.25 39.051
0.85 0.3 39.098
0.85 0.35 39.617
0.85 0.4 40.541
0.85 0.45 40.091
0.85 0.5 40.435
0.85 0.55 40.55
0.85 0.6 40.204
0.85 0.65 40.682
0.85 0.7 40.965
0.85 0.75 41.063
0.85 0.8 41.113
0.85 0.85 41.577
Well, as far as I can see in your data X values change every 15 points, so do Y and Z.
It seems you are reshaping incorrectly.
Try the next really small changes to your code:
xVec = reshape(data(:,1),15,15);
yVec = reshape(data(:,2),15,15);
zVec = reshape(data(:,3),15,15);
surf(xVec,yVec,zVec);
axis([0.15 0.85 0.15 0.85]);
set(gca, 'XTick',0.20:0.05:0.80);
set(gca, 'YTick',0.20:0.05:0.80);
Output:
(This is only true for matlab, so I don't know if octav is any different, but I don't think so.)
The problem is that surf needs a very specific input format, it does not work with vectors.
the function you would want is griddata which interpolates any 3D or 4D input data on an even grid (which would be the needed input of surf).
It works somewhat like this:
xVec=mat(:,1);
yVec=mat(:,2);
zVec=mat(:,3);
gridpoints = 100;
[xi, yi] = meshgrid(linspace(min(xVec),max(xVec),gridpoints),linspace(min(xVec),max(xVec),gridpoints));
zi = griddata(xVec,yVec,zVec,xi,yi);
surf(xi,yi,zi);