Giter VIP home page Giter VIP logo

Comments (8)

drasmuss avatar drasmuss commented on July 20, 2024 1

The trainable flag sets the parameters associated with that object to be (non)trainable, but you still need to backpropagate the gradient through those objects (to get the gradient for any preceding objects, which may still be trainable). So in your example, even if you set x to be not trainable, you still get nans when you propagate the gradient through x, which results in nans on the connection weights from stim -> x. In the second case (configure_settings(trainable=False)) you're also setting that stim->x connection to be non-trainable, so you don't end up with nan weights.

from nengo-dl.

arvoelke avatar arvoelke commented on July 20, 2024 1

Cool, that clears it all up. Thanks for the quick replies. Feel free to close whenever.

from nengo-dl.

drasmuss avatar drasmuss commented on July 20, 2024

Definitely something weird going on here, I'll dig into it.

from nengo-dl.

drasmuss avatar drasmuss commented on July 20, 2024

Can you try out #27 and see if it works for you?

The problem was that you were getting nans when computing the gradient of LIFRate, which TensorFlow was then silently converting into zero output for some reason. Although LIFRate isn't technically differentiable, we should still be able to compute the gradient without getting nans, which is what #27 does. But just for your own future work, I'd recommend using nengo_dl.SoftLIFRate instead, it'll work better during training.

from nengo-dl.

arvoelke avatar arvoelke commented on July 20, 2024

Thanks! This makes sense and works for me. For some reason I had it in the back of my head that nengo_dl would automatically switch the LIFRate for SoftLIFRate to do the optimization and then switch it back. Maybe a way to do this would be useful. If not, then a warning and/or some clip at a maximum value would help this from recurring.

Relatedly... I think I'm misunderstanding how the trainable config works. I was using this to disable the LIFRate populations, but that doesn't seem to prevent them from creating problems.

import numpy as np
import matplotlib.pyplot as plt

import nengo
import nengo_dl
import tensorflow as tf

u = np.random.randn(100, 1, 1)
dt = 0.001

with nengo.Network() as model:
    stim = nengo.Node(output=nengo.processes.PresentInput(
        u, presentation_time=dt))
    x = nengo.Ensemble(100, 1, neuron_type=nengo.LIFRate())
    y = nengo.Ensemble(100, 1, neuron_type=nengo_dl.SoftLIFRate())
    
    nengo_dl.configure_settings(trainable=True)
    model.config[x].trainable = False
    
    #nengo_dl.configure_settings(trainable=False)
    #model.config[y].trainable = True
    
    nengo.Connection(stim, x, synapse=None)
    nengo.Connection(x, y, synapse=None)
    p = nengo.Probe(y, synapse=None)

inputs = {stim: u}
targets = {p: u}
opt = tf.train.MomentumOptimizer(
    learning_rate=1e-13, momentum=0.1, use_nesterov=True)
    
with nengo_dl.Simulator(model, minibatch_size=1, dt=dt) as sim:
    sim.train(inputs, targets, opt, n_epochs=1)
    sim.run(len(u)*dt)

plt.figure()
plt.plot(sim.trange(), sim.data[p].squeeze())
plt.plot(sim.trange(), u.squeeze(), linestyle='--')
plt.show()

If you switch the two trainable lines that are commented, it goes from being broken (on master, without #27) to working. But I thought that neither approach would need to differentiate the tuning curves for the x population.

from nengo-dl.

drasmuss avatar drasmuss commented on July 20, 2024

For some reason I had it in the back of my head that nengo_dl would automatically switch the LIFRate for SoftLIFRate to do the optimization and then switch it back. Maybe a way to do this would be useful.

Yeah that should be possible. I probably wouldn't make it automatic by default, but could probably work up a flag or helper function that'd make that happen.

from nengo-dl.

arvoelke avatar arvoelke commented on July 20, 2024

Thanks for the explanation. To check my understanding, backprop is modifying the scalar transform from stim -> x to 0? Or could it even be modifying some other weights (e.g., gains, biases, encoders of x)?

from nengo-dl.

drasmuss avatar drasmuss commented on July 20, 2024

It's actually setting the transform to nan in this case (so the output of the network is nan, but I guess TensorFlow has some logic to convert that to zero).

The gains/biases/encoders are all associated with the trainability of x, so if you set x to be non-trainable then they will be fixed. You can use x.neurons to separately control the trainability of the biases, but you can't separate gains and encoders since those are actually just combined into one parameter in Nengo, the scaled_encoders. More details in the documentation (in case anyone comes across this discussion in the future).

from nengo-dl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.