Neural networks and Python - java

I'm trying to study about neural networks, following a great guide:
http://neuralnetworksanddeeplearning.com/chap1.html
Currently I've reached this code snippet which I'm trying to understand and write in Java:
class Network(object):
def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]
I managed to figure out what everything means except for the last line:
[np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]
As far as I can understand: create a matrix with y rows and x columns, for each pair x,y which can be found in the matrix zip which is created by the merging of the two "sizes" arrays. I understand that sizes[1:] means taking all elements from sizes starting from index 1, but sizes[:-1] makes no sense to me.
I read online that s[::-1] means getting the reverse of the array, but in the above case we only have one colon, while in the formula for the reverse array there seems to be two colons.
Sadly, I have no idea how Python works and I got pretty far along with the online book to give it up now (I also truly like it), so can someone say if I'm right until now, correct me if needed, or straight out explaining that final line?

sizes[:-1] is a list slice which returns a copy of the sizes list but without the last item.

Related

Java - Old array always is equal to new array?

I'm not very good at programming but right now, I need to do program a little Java application and I'm running into a weird problem. I've been trying to solve this for hours now.
Here's the problem:
I'm saving a small amount of my screen (20x20) to my program. I do this by looping through every pixel, saving it's RGB into an array via Java.awt.robot. With the following function, the program should save the copied image to 3 arrays (R, G and B) before getting the new area of the screen (I want to compare them later and look for changes). The one damn thing: The old arrays in which I save the data before overwriting the main arrays are always overwriting without me telling them to.
private void fillArrayData(){
oldDataR = dataR; <----- The problem is here. These arrays are now overwritten with the
oldDataG = dataG; <----- current data, just before I write stuff to dataR, G and B.
oldDataB = dataB; <----- As you see, I don't modify oldDataR, G, B later on.
scanArea.x = MouseInfo.getPointerInfo().getLocation().x;
scanArea.y = MouseInfo.getPointerInfo().getLocation().y;
for(int i = 0; i<scanSize; i++){
for(int n = 0; n<scanSize; n++){
dataR[i][n] = (rbt.getPixelColor(scanArea.x+i, scanArea.y+n)).getRed();
dataG[i][n] = (rbt.getPixelColor(scanArea.x+i, scanArea.y+n)).getGreen();
dataB[i][n] = (rbt.getPixelColor(scanArea.x+i, scanArea.y+n)).getBlue();
}
}
}
Even though I never access oldDataR, oldDataG and oldDataB later on, it everytime is equal to the dataR, dataG and dataB after this void finishes. That doesn't make sense as I'm writing new data to the three main arrays (dataR, dataG, dataB) AFTER I saved them to the oldData-Arrays. And yes, I made sure that the data which is received by rbt.getPixelColor is not the same as before.
Please help me, I'm really frustrated by now but need to keep going.
That is happening because oldDataR (and the rest) is just another variable pointing to the same array, if you want to keep the old values in the array separately, and modify the original one, you need to copy it.
This post can be helpful for copying two-dimensional arrays:
copy a 2d array in java
You are passing your arrays as references, so, in the end it is the same array in the pointers of the two variables.
Have you tried array copy?
System.arraycopy()
or
Arrays.copyOf()
Your code would look like:
oldDataR = Arrays.copyOf(dataR);
Edit
I missed the multi dimension of the arrays, just follow #khachik link suggestion so you can handle the two dimensions.

Insertion in 3D arraylist

I have created 3D ArrayList. I know initialization is done using this code
ArrayList<ArrayList<ArrayList>> ll=new ArrayList<ArrayList<ArrayList>>();
for(int j=0;j<n;j++){
ll.add(new ArrayList(new ArrayList()));
}
But I dont know how insert values in it.
I am familiar with 2D arraylist in which inserting value b at a index is done using
ll.get(a).add(b);
but I dont know how to insert value into 3D arrayList.
I am solving question http://www.spoj.com/problems/BENEFACT/
in which I think longest distance in tree is the answer. I used 3D ArrayList
here, 2dimension to store connection of places and 3rd dimension to store distance
Is this correct approach? Is Any other approach possible in which solution is obtained easier than using 3d ArrayList?
I think if you want to access an object at index i,j,k you can do it by
ll.get(i).get(j).get(k)
Or
ll.get(i).get(j).add(k,newObject)
Depending on your need, you probably don't want that. I didn't read the full description of the problem to solve (the link), but you just need 6 points in 3D space.
I'd suggest creating a Point3D class with x, y, and z, and then just a simple List<Point3D>.

OpenMaple Java Interface

I am trying to use OpenMaple with Java interface to simply turn a matrix into reduced row echelon form. Ultimately I want to find the inverse of a matrix. I am not sure how to take a 2-d array in my program and make it interact with engine objects.
Some basic examples are here.http://www.maplesoft.com/support/help/Maple/view.aspx?path=OpenMaple/Java/Engine/evaluate
I basically want this: t.evaluate( "with(LinearAlgebra): A:=<<2,4,8>|<8,2,4>|<4,8,2>>; ReducedRowEchelonForm(A);" );
Problem 1: getting a 2-d matrix I made into that statement.
Problem 2: somehow making it output into another 2-d array as a result.
I don't know much about OpenMaple, therefore I come here. Thanks!

Implemeting a File Character Analysis

I'm stuck on my assignment and wanted some pointers on an algorithm.
I am presented with text files that represent different images. blank space is where no pixels are on and '&' represents an on pixel.
The aim is i am given a 100 x 100 image text file to analyse and work out the probability that the object is there and then the co-ordinate of where it is on the file.
I know that i have to use character analysis of some sort but i feel that i have to check for example 10x10 grids at a time, analyse how many pixels are on and work out the certainty that the object is there. (This is because more or less pixels can be on and the object still present)
Thanks for your help.
I think I understand your question correctly. One thing that will change the answer is whether or not you know the object beforehand. If you are looking for an arbitrary pattern, it is somewhat more difficult, but still feasible. To find an object that you know what it will look like will come down to nested for loops and a solid understanding of 2D arrays. You can pull in each line of the text file and look for an '&'. If it finds one, it begins looking for the rest of the pattern based on the location relative to that first '&'.
For example, if you are looking for a diagonal line from top left to bottom right, you would continue along until you came to the first '&'. After that, you would look at the cell one column over and 1 row down. If that is an ampersand as well, you know that you have a diagonal line. If not, just keep going along after the first '&'.
for (int c = 0; c < textArray.length; c++)
{
for (int i = 0; i < textArray[c].length; i++)
{
Look at the character
If it is '&'
Look for the next character and so forth
If the pattern is there
return true
}
}
See if that helps get your algorithm rolling. You will need to make sure to check for legal bounds in your arrays in order to combat out of bounds exceptions.

Fastest way to access a table of data Java

Basically I am amidst a friendly code optimisation battle (to get the fastest program), I am trying to find a way that is faster to access a dictionary of hard coded data than a multidimensional array.
e.g to get the value for x:
int x = array[v1][v2][v3] ;
I have read that nested switch statements in a custom array may possibly be faster. Or is there a way I can possibly access memory more directly similar to pointers in C. Any ideas appreciated!
My 'competitor' is using a truth table and idea is to find something faster!
Many Thanks
Sam
If the array is regular in shape (i.e. MxNxK for some fixed M, N and K), you could try flattening it to achieve better locality of reference:
int array[] = new int[M*N*K];
...
int x = array[v1*N*K + v2*K + v3];
Also, if the entire array doesn't fit in the CPU cache, you might want to examine the patterns in which the array is accessed, to perhaps re-order the indices or change your code to make better use of the caches.

Categories