0

I'm using MatrixToolkitsJava for a Neural Network project for my thesis, and in it I need to make the weights matrices larger, and later potentially smaller. The way I'm currently doing this now, creating a new matrix and copying the existing values while instantiating the new values, is extremely inefficient, taking up the vast majority of the time. Is there an efficient way to add rows and columns to existing matrices?

Beez
  • 391
  • 5
  • 6
  • 16
  • There is no way to change the size of an array, which is why they invented something called lists. You can perhaps make an arraylist of arraylists. – RaminS Jun 09 '16 at 00:01
  • I'm not using an array, I'm using a Matrix from MatrixToolkitsJava. I'm just wondering if there's a more computationally-efficient way of resizing the Matrix, or even of copying the values within the Matrix. – Beez Jun 09 '16 at 00:09

1 Answers1

0

As stated you can use lists, but if you are dead set on using an array, you could create a method to create a new array, copy over the data, and then replace the reference that you are storing.

If you use System.arrayCopy(Object src, int srcPos, Object dest, int destPos, int length) it will fast enough if you aren't using a giant array.

EDIT: I can't believe I did not think of this earlier. Just use matrix multiplication to copy the data. For example, to make a 2 by 2 a 3 by 3, just do [2,2] x [(1,0), (0,1), (0,0)] = [2,3] => [(1,0,0), (0,1,0)] x [2,3] = [3,3]

Jedi_Maseter_Sam
  • 753
  • 4
  • 24
  • They're not arrays, they're Matrices from MTJ for the computationally-efficient linear algebra required. And they're getting pretty big, which is the problem. They easily reach thousands of rows/columns. That is what I'm doing currently, copying the data I have and adding in the new data, but as I said it's pretty inefficient and tends to slow things down. I doubt arrayCopy would work for MTJ Matrices though. – Beez Jun 09 '16 at 03:43
  • Is there anyway to figure out how big you need it beforehand? Otherwise I'd suggest making it much larger than you think you will need it. – Jedi_Maseter_Sam Jun 09 '16 at 03:53
  • There's not, unfortunately. The goal here is to make a Growing Neural Network, which would hopefully find its appropriate size as it goes. Another problem with making the Matrices larger than they need to be is that the larger they are the more the program slows down, by a significant amount. So making them extremely large and filling them would result in a slower program than growing them as I go does. – Beez Jun 09 '16 at 03:58
  • Is is part of the project specs to use MTJ? If not maybe try a different library with a copy ability. Otherwise, you can use Java Native Access and use C++ to do a quick copy (that is what arrayCopy is doing) – Jedi_Maseter_Sam Jun 09 '16 at 04:02