Tensorboard- superimpose 2 plots - neural-network

In tensorboard, I would like to superimpose 2 plots on the same graph (training and validation losses of a neural network).
I can see 2 separate plots, but not one plot with 2 superimposed curves. Otherwise, I get one plot in zigzag.
How can I do?

It is possible to superimpose two plots in Tensorboard. You'll have to satisfy both of the following:
Create two separate tf.train.SummaryWriter objects such that it outputs in two folders.
Create two summaries (e.g. tf.scalar_summary) with the same name.
For example to plot training and validation loss:
# Before training
train_summary = tf.scalar_summary('Loss', train_loss)
vali_summary = tf.scalar_summary('Loss', vali_loss)
train_writer = tf.train.SummaryWriter('/tmp/train'), sess.graph)
vali_writer = tf.train.SummaryWriter('/tmp/vali'), sess.graph)
# And then later
train_writer.add_summary(...)
vali_writer.add_summary(...)

If you point tensorboard at the directory containing the tf events for both runs, you should be able to see them. (if you have them in subdirectories, point it to the parent of both).

Related

How to plot histograms of two different classes in a single plot?

I have two draw histogram of two clases in to single plot. One class is of distance of authenticated users and others if of distance of intruduers achieved by L2-NORM.
intruder_dist =[0.02, 0.05,0.03......] and another is its corresponding number. intruder_num = [10,2,40,..........]
Same is with authencticated users distances.
intruder_dist =[0,0.002,0.001......] and another is its corresponding number. intruder_num = [30,50,70,..........]
Below is the histogram that I want to achieve. How to plot it in matlab.
If I understand correctly, you just need hold on between the two calls to histogram/ hist. Here's an example:
histogram(10+2*randn(1,1e4), 21)
hold on
histogram(2+.3*randn(1,1e4), 11)

Plotting K-means results in Matlab

I have 3 sets of signals, each containing 4 distinct operational states, and I have to classify the states in each signal using K-means in Matlab. The classification is done after I have smoothened the original signal using a filter. My output should be a plot of the smoothened signal with each part of the signal in a different color to denote the different operational state.
I am very new to Matlab, and this is what I have for the classification part.
numClusters = 4;
idx_1 = kmeans([X_1 smoothY_1],numClusters,'Replicates', 5);
[numDataPoints,numDimensions] = size(smoothY_1);
Colors = hsv(numClusters);
for i = 1 : numDataPoints
plot(X_1(i),smoothY_1(i),'.','Color',Colors(idx_1(i),:))
hold on
end
I have a few questions.
1) It appears to me that the kmeans function in Matlab will return a set of arbitrary cluster index in every run. For example, running the code above on the same signal twice may give me the cluster index (for 10 data points) [4 4 2 2 2 1 1 3 3 3] and [2 2 1 1 1 4 4 3 3 3], resulting in arbitrary colors denoting each state. Ideally, I would like the indices to be (somewhat) ordered and the colors to be the same for corresponding states, so that it makes sense to say "Red means Operational State 1, blue means State 2, etc". How can I synchronize this?
I have 2 pictures to illustrate this.
Set 1 and 2 are two of the datasets. Each stage of the signal is in a different color. I would like, for example, the first segment to be red, second in cyan, third in green, fourth in purple.
2) I can't seem to plot the graph using the specifier '-'. There is no output when I tried to do that, so I'm forced to use '.', which isn't what i want. How can I plot a continuous curve here?
3) Right now, I'm running K-means independently on all 3 sets of data, so there's no concept of training/test datasets. I would like to use one dataset for training and the other 2 for testing, but I don't know how to do that using K-means in Matlab. How can I do that?
ETA: I noticed that my smoothed plots are all about half the heights of my plots of the original data, e.g. the highest point in my original signal is y = 22, while the highest point in my smoothed signal is y = 11, although the shape remains the same. Is this correct?
ETA2: I realized that it seems as if what the K-means clustering did was simply divide the graph into numClusters segments (based on X_1 values) and that's it. I've tried with different values of numClusters and each gave me equally divided segments. Surely this can't be right? For instance, isn't it more likely that the long segment after the biggest spike belong to the same cluster, rather than 3 clusters? Should I be using K-means at all?
For the first question:
You can reorder your vector with
[~,~,a] = unique(a,'stable');
For the second question:
You can find all the information about the LineSpec here:
LineSpec
If you don't add a LineSpec the default option is a continuous line, as you want.
For the third question:
I don't think that you can train your kmean algorithm (due to the method) as it could be possible with an SVM, but i'm waiting for an expert opinion.

Detection of groups

After having used bwconncomp and regionprops to detect the number and place of connected domains within my picture. I now want to figure out how much space (size of a convex domain) the objects fill out.
If all of the objects are evenly distributed or are in one big group, this is not a problem as I can just use convhull to calculate the area.
The problem is now if I have several groups then I want several convex areas - one for each group, the number of groups are on beforehand unknown.
See for instance:
(source: jasonyianakis.co.nz)
Note that it is just the grouping I am interested in, the detection as a single element is already functioning.
You can use 'ConvexArea' property in regionprops
For example
img = imread('http://jasonyianakis.co.nz/wp-ontent/uploads/2012/08/different-people-groups.jpg');
img = im2double(img); %// convert to double
bw = max(bsxfun(#rdivide,img,sum(img,3)),[], 3 )>.4; %// get a binary mask
The resulting binary mask:
Label each component, and get the 'ConvexArea':
lb = bwlabel(bw);
st = regionprops(lb,'ConvexArea');
cxArea = [st.ConvexArea];
Discard too small regions
cxArea( cxArea < 100 ) = [];
Now you have the convex area of the components:
cxArea =
474813 2054497 451879
Try to use the kmeans function of Matlab. This funtion is used to divide data into some clusters. Also, Gaussian mixture models are frequently used for your purpose (they give the best results).There are lot of information in web about them and several functions implemented in Matlab.
PS: Determine the number of clusters is a very difficult task. clusterdata and similar functions can do it for you but it does not mean that it will be the best result. It is usual to try several different number of groups and see what fits best , the Gaussians can help, with its weights . If one has a very low weight it is more likely than it is not significant and can be deleted.

Extracting data that meet a threshold in another matrix

I'm having some trouble in Matlab, and this might be more of a methodology/logic question.
So I have three netCDF files that I am loading into three separate matrices. Two of these matrices are values of a test run for two different experiments (all three files are gridded), and the other is the p-value of the data between the two experiments.
I want to create a new netCDF file that has the values of the difference between the two experiments if that particular grid has a pvalue <= 0.05.
I thought this would be fairly easy. Here is what I implemented:
%find the difference between experiments, averaged over the 3rd dimension
yr_diff = mean(mat_2012-mat_1980,3);
yr_diff(pvalue >= 0.05) = NaN;
final_grid = yr_diff;
And then I used a fairly straight forward netCDF exporting scheme. To my knowledge, the mat_2012 and mat_1980 matrices are organized in the same as the pvalue matrix.
This produces a sort of weird grid that doesn't really show anything. The first image below shows filled contours of the differences, with black contour lines where the areas are significant. The second plot shows filled contours of the netCDF file created with this logic, with the same significant contour lines.
So my question: Is the logic incorrect, and should I take a new direction? Is this something related to my exporting scheme? Or is this something else completely?

Handles for individual matrix columns/rows/elements -- Matlab

I'm currently using Matlab and I am plotting the contents of the rows of a matrix, where each column is an independent data set. As the matrix is large I don't want to have to go through the tedius task of writing up the plot labels for each data set individually, so I was wondering if there is a specific way to include a handle/name for each column in such a way that it will automatically apply the plot label, and will adjust accordingly if columns are added or removed from the matrix?
Thanks!
Specifics, if they help:
Amplified spontaneous emission (ASE) in an optical fibre amplifier. Rows act as storage for a discretised ASE spectrum, columns are a given position along the fibre amplifier (it is this position -- the distance along the fibre corresponding to the column -- which I want to use as the label) and each element contains power information. The plot gives spectral power of ASE in the fibre for different positions along its length.
If by labels you mean the plot legend, you can do that by using cells. Consider matrix A
A = repmat([1:3], 3, 1)
A =
1 2 3
1 2 3
1 2 3
You can call plot to plot the columns of the matrix
plot(A);
Here, you will get 3 horizontal lines at y=1, 2 and 3. You can create your legend as follows
l{1} = 'dataset1';
l{2} = 'dataset2';
l{3} = 'dataset3';
Then you type
legend(l)
to show the legend. However, no one will create the legend for you, so you must create the cell array yourself. You can do it automatically, of course, e.g. the above legend can be created by a simple loop
for i=1:size(A, 2)
l{i} = ['dataset' num2str(i)];
end