ioam / topographica

A general-purpose neural simulator focusing on topographic maps.
topographica.org
BSD 3-Clause "New" or "Revised" License
53 stars 32 forks source link

viewing connection fields of sparce/gpu models #635

Open mjabri opened 8 years ago

mjabri commented 8 years ago

I now can train some sparse/gpu models and wanted to look at the results. What's the best way to view connection fields of sparce/gpu models? If nothing in place yet, any hints on how to get back the connection fields from the cusparse weight array?

philippjfr commented 8 years ago

I remember briefly looking into this but I don't recall much so I can only comment generally on how the sparse representation is stored. Basically all the weights in a projection are stored in a single large sparse matrix, where each column represents one CF. So to view a single CF you should slice into the large matrix and then reshape the column to the size of the source sheet. Let me know if you run into any issues.

mjabri commented 8 years ago

Thanks. I will try this. BTW I am finding the interactivity of the GUI (can run and watch and interrupt at same time) and the way .ty files can be ingested in notebooks simply too difficult to replace with the notebook. So if the problem of viewing legacy models (non-sparse) in latest topographica on github is not solvable, I may try to transplant the sparse/gpu parts into a version where the GUI is still working.

mjabri commented 8 years ago

Was the idea of copying the connection fields weights from GPU back to host at end of simulation (mentioned in @Tasignotas thesis) implemented? I can see there the fields in GPUSparseCFProjection for (sparse) 'weights' and (cusparse.CSR) 'weights_gpu' and I am assuming the weights are still in weights_gpu after each run. So I am wondering whether a function for copying from weights_gpu to weights exists somewhether. Looked into gpu folder and could not find anything...

jbednar commented 8 years ago

I don't think that they were ever copied out, but I could be mistaken.

Jim

On Tue, Oct 13, 2015 at 3:41 PM, M. Jabri notifications@github.com wrote:

Was the idea of copying the connection fields weights from GPU back to host at end of simulation (mentioned in @Tasignotas https://github.com/Tasignotas thesis) implemented? I can see there the fields in GPUSparseCFProjection for (sparse) 'weights' and (cusparse.CSR) 'weights_gpu' and I am assuming the weights are still in weights_gpu after each run. So I am wondering whether a function for copying from weights_gpu to weights exists somewhether. Looked into gpu folder and could not find anything...

— Reply to this email directly or view it on GitHub https://github.com/ioam/topographica/issues/635#issuecomment-147846376.

mjabri commented 8 years ago

Ok, but how the receptive fields as produced on GPU would've been verified? I can see the activities with the gcal_sparse example with artificial patterns, but these response activities do not validate the learning is as expected.

philippjfr commented 8 years ago

If the activities match by the end of the simulation then that verifies the learning by proxy. The chance of getting the same activity with different weights might as well be zero.

mjabri commented 8 years ago

This is good for validation.

mjabri commented 8 years ago

So seems reasonable to add a function to copy weights_gpu to weights for GPUSparseCFProjection which would be used when need to view and save the weights.

mjabri commented 8 years ago

ok, here is a function that copies weights from gpu to cfs. Tested this on my old fork of topographica where i have transplanted the sparce/gpu stuff and it seems to work. I am not sure whether this is the most efficient way though.

from math import sqrt
from nmutils import info
import numpy as np

def cfs2cpu(in_conns):
    for c in in_conns:
        try:
            gw = c.weights_gpu
            m = gw.todense(to_cpu=True)
            if m is None:
                info('Could not get the dense matrix for connection {0}. Ignoring...'.format(c.name))
                continue
            rows,cols = m.shape
        except:
            import sys
            e = sys.exc_info()[0]
            info("Exception: \'{0}\' while coppying GPU weights of connection {1}. Ignoring.".\
                 format(e, c.name))
            continue
        info('Copying weights all {0} GPU weights of connection {1} to CF weights in flatcfs.'.format(rows,c.name))
        for i in xrange(rows):
            row = m[i]
            (s,) = row.shape
            s=sqrt(s)
            cf = row.reshape((s,s))
            r1,r2,c1,c2 = c.flatcfs[i].input_sheet_slice
            # Note below will not work as __set_weights of SparseConnectionField.
            # np.copyto(c.flatcfs[i].weights, cf[r1:r2,c1:c2])
            c.flatcfs[i].weights = cf[r1:r2,c1:c2].copy()
        del m