What should the results of a convolution be? - neural-network

I'm using convnetjs to build an interactive tutorial (as I learn it myself). I have a simple 9x9 image of an 'X' and a convolutional layer with one of the filters as a 3x3 '\'...
I expected the results to be different. I expected the circled result on the right to be (-1+1+1+1+1+1+1+1+1)/9 = 0.77 instead of 7.1.
What else is happening that results in 7.1? Is this due to biases? I also expected the whole result to show highest numbers along the '\' diagonal, since the filter is that shape which would match the '\' part of the 'X'.
UPDATE: I expected the results to be the following. The biases appear to be an array [0.1,0.1,0.1]. What is the calculation that yeilds the above results (for at least the upper left pixel), instead of the below?
<html>
<head>
<script src="http://cs.stanford.edu/people/karpathy/convnetjs/build/convnet-min.js"></script>
</head>
<body>
<script>
// Initialize an input that is 9x9 and initialized with zeroes.
let inputVol = new convnetjs.Vol(9, 9, 1, 0.0);
// Manually set the input weights from zeroes to a 'X'...
inputVol.w = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
// Define the layers
let layers = [];
layers.push({
type: 'input',
out_sx: 9,
out_sy: 9,
out_depth: 1
});
layers.push({
type: 'conv',
sx: 3,
pad: 0,
filters: 3,
stride: 1,
activation: 'relu'
});
let net = new convnetjs.Net();
net.makeLayers(layers);
let convLayer = net.layers[1];
let convLayerFilters = convLayer.filters;
// Set filters manually
// looks like a '\'
convLayerFilters[0].w = [1, -1, -1, -1, 1, -1, -1, -1, 1];
// looks like a 'X'
convLayerFilters[1].w = [1, -1, 1, -1, 1, -1, 1, -1, 1];
// looks like a '/'
convLayerFilters[2].w = [-1, -1, 1, -1, 1, -1, 1, -1, -1];
// Run the net
net.forward(inputVol);
// Prints '7.1' instead of '0.77'. Why???
console.log(net.layers[1].out_act.w[0]);
</script>
</body>
</html>

Yes it is happening because of biasing so that it stays in the range defined.
Bias is the value that you can add to each element in a convolution result to add additional influence from neighbouring pixels. Since with certain convolutions it is possible to get negative numbers (which are not representable in a 0–255 format), bias prevents the signal from drifting out of range. You can choose to add a bias of 127 or 128 to allow some negative numbers to be representable (with an implicit +127 or +128 in their value).

Related

HLSL Array Indexing Returning Unexpected Values

I am executing some HLSL code on the GPU in Unity, but I am having issues with getting values out of an array. Here is my simplified code example.
C#
ComputeBuffer meshVerticesBuffer = new ComputeBuffer(
15 * 1,
sizeof(float) * 3
);
marchingCubesShader.SetBuffer(0, "MeshVertices", meshVerticesBuffer);
marchingCubesShader.Dispatch(0, 1, 1, 1);
Vector3[] meshVertices = new Vector3[15 * 1];
meshVerticesBuffer.GetData(meshVertices);
meshVerticesBuffer.Release();
HLSL
#pragma kernel ApplyMarchingCubes
int EDGE_TABLE[][15] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
...255 more irrelevant entries
};
RWStructuredBuffer<float3> MeshVertices;
[numthreads(4, 4, 4)]
void ApplyMarchingCubes(uint3 id : SV_DispatchThreadID)
{
MeshVertices[0] = float3(0, 0, EDGE_TABLE[0][0]);
}
I am watching meshVertices on the C# side through the debugger, and the first item is always a Vector3(0, 0, 0). I am expecting a result of Vector3(0, 0, -1). What am I doing wrong?
I figured out why the array was not putting out the right values.
In HLSL, when declaring and initializing an array in the same line, you must include the static keyword.
My HLSL code should have been:
#pragma kernel ApplyMarchingCubes
static int EDGE_TABLE[][15] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
...255 more irrelevant entries
};
RWStructuredBuffer<float3> MeshVertices;
[numthreads(4, 4, 4)]
void ApplyMarchingCubes(uint3 id : SV_DispatchThreadID)
{
MeshVertices[0] = float3(0, 0, EDGE_TABLE[0][0]);
}
(Notice static on the third line, it was not there before).

Distribute elements evenly (adding ints values to array of int values)

Say I have an array of Ints and all elements equal zero. It'd look something like this:
let arr: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
There are 11 elements in this array in total. I want three of the elements in this array to be the number one. I want these one values to be distributed evenly throughout the array so that it looks something like this:
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
I want to be able to add however many one's and distribute them evenly (or as close to evenly as possible) no matter how many total elements there are. How could I do this?
Note: For anyone wondering why I need this, I have a collection of strings that when joined together make up a large body of text. Think of the zeroes as the pieces of text and think of the ones as advertisements I am adding in between the text. I wanted to distribute these ads as evenly as possible. I figured this would be a simple way of expressing what I needed.
Maybe you can try this.
var arr: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let distribution = arr.count / 3 // 3 is the number of 1s
for (index, value) in arr.enumerated() {
arr[index] = (index + 1) % distribution == 0 ? 1 : value
}
print(arr) // [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
Assuming that the value distribution > 1

Using textscan in Matlab to handle data not properly formatted data

I'm using textscan to import data. I can get to it successfully import properly formatted data. I can't get it to properly handle data that isn't properly formatted. Below is the format of the data.
JeB2021Da 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 1, 3, 1, 99, 0, 0, 0,
JoB2021Ha 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 99, 2, 1, 0,
JoP2021Co 12-13 and stuff, not enough samples
MaA2021Be 12-13 and stuff, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 99, 1, 0, 0,
MaA2021Ma 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 99, 1, 0, 0,
How would I handle the data that is, not enough samples? Because currently the data structures don't line up. The data structures that are being produced are 17 x 1 and 16 x 14. I'd like to import the string as it is in the data. So not enough samples would be imported. Below is the code that I'm using.
fid = fopen('./file.txt','r');
fmt = ['%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d'];
d = textscan(fid, fmt, 'CollectOutput', 1,'Delimiter',',','headerLines', 1, 'EmptyValue', 0);
I'm trying to handle it with the EmptyValue flag but it's not working. Any help is greatly appreciated.
I am not sure what exactly you mean by I'd like to import the string as it is in the data, or more exactly where you would like to have that string stored.
But about just reading your data as a whole you can use the 'TreatAsEmpty' argument:
d = textscan(fid, fmt, 'CollectOutput', 1,'Delimiter',',','headerLines', 1,'TreatAsEmpty','not enough samples');
Then you can modify the input further by looking for the rows in the imported data array that solely consist of zeros.

Autohotkey Most of numpad keys don't work

Here is my script. When I try it, only the NumpadClear (Num5) key works. I use Windows 7 64bit.
#SingleInstance force
#UseHook
#InstallKeybdHook
*~NumpadIns::MouseMove, 0, 1, 0, R
*~NumpadClear::MouseMove, 0, -1, 0, R
*~NumpadEnd::MouseMove, -1, 0, 0, R
*~NumpadPgDn::MouseMove, 1, 0, 0, R
*~NumpadDown::Click
*~NumpadEnter::Click Right
This AutoHotkey script should achieve what you're looking for.
It will work whether NumLock is turned off or on.
#SingleInstance force
#UseHook
#InstallKeybdHook
NumpadIns::MouseMove, 0, 1, 0, R
NumpadClear::MouseMove, 0, -1, 0, R
NumpadEnd::MouseMove, -1, 0, 0, R
NumpadPgDn::MouseMove, 1, 0, 0, R
NumpadDown::Click
NumpadEnter::Click Right
Numpad0::MouseMove, 0, 1, 0, R
Numpad5::MouseMove, 0, -1, 0, R
Numpad1::MouseMove, -1, 0, 0, R
Numpad3::MouseMove, 1, 0, 0, R
Numpad2::Click
;NumpadEnter::Click Right

Postive/Negative Chart in Google Visualization API

I need to generate a chart like this one:
Specifically, I want to show both a positive value and a negative value for a time period (could be an hour, minute, etc.) and display it like this.
I could have sworn I saw something like this on the Google Visualization API Gallery the other day, but I can't find it now, and am not even sure what this kind of chart is called.
First, do you know what this kind of chart is called so I can possibly find documentation? Second, is there any way to implement such a chart with the Google Visualization API? If not, is there another common charting solution for web that I can achieve this with?
Thank you for your time.
This is called a "Stacked Bar Chart", and can indeed be created with the Google Visualisation API.
Simply use the "isStacked" property (described here; http://code.google.com/apis/visualization/documentation/gallery/barchart.html).
Here's some sample code (based off the default bar chart example provided by Google and updated to show the use of isStacked and some sample data from your example);
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('number');
data.addColumn('number');
data.addRows(12);
data.setCell(0, 0, 'January');
data.setCell(1, 0, 'February');
data.setCell(2, 0, 'March');
data.setCell(3, 0, 'April');
data.setCell(4, 0, 'May');
data.setCell(5, 0, 'June');
data.setCell(6, 0, 'July');
data.setCell(7, 0, 'August');
data.setCell(8, 0, 'September');
data.setCell(9, 0, 'October');
data.setCell(10, 0, 'November');
data.setCell(11, 0, 'December');
data.setCell(0, 1, 19);
data.setCell(1, 1, 18);
data.setCell(2, 1, 20);
data.setCell(3, 1, 19);
data.setCell(4, 1, 18);
data.setCell(5, 1, 20);
data.setCell(6, 1, 19);
data.setCell(7, 1, 18);
data.setCell(8, 1, 20);
data.setCell(9, 1, 19);
data.setCell(10, 1, 18);
data.setCell(11, 1, 20);
data.setCell(0, 2, -12);
data.setCell(1, 2, -13);
data.setCell(2, 2, -11);
data.setCell(3, 2, -12);
data.setCell(4, 2, -13);
data.setCell(5, 2, -11);
data.setCell(6, 2, -12);
data.setCell(7, 2, -13);
data.setCell(8, 2, -11);
data.setCell(9, 2, -12);
data.setCell(10, 2, -13);
data.setCell(11, 2, -11);
data.setCell(0, 2, -12);
data.setCell(1, 2, -13);
data.setCell(2, 2, -11);
// Create and draw the visualization.
new google.visualization.ColumnChart(document.getElementById('visualization')).
draw(data,
{title:"S&P 500 Up/Down Performance Since 1980",
width:600, height:400,
isStacked:"true",
legend:"none" }
);
}
And the results...
Use ColumnChart instead of BarChart:
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
https://jsfiddle.net/0rrar9oq/16