Git Product home page Git Product logo

Comments (8)

charlesmartin14 avatar charlesmartin14 commented on August 16, 2024

Thanks for submitting

The MP solver has some issues that sometimes cause the residual to off for the first case

I will try to fix in the next minor release, 0.4.8

from weightwatcher.

charlesmartin14 avatar charlesmartin14 commented on August 16, 2024

This is a scaling issue

From what I can tell, different frameworks set the normalization of W differently, and I have not figured out a way to automatically detect and fix this yet

In RMT (random matrix theory), we expect W to say a Gaussian random matrix, with variance 1.0, and normalized by 1/N. But the frameworks don't actually do this--they might use Glorot normalization (which is not 100% the same ) , and they frequently rescale W by say 1/(N+M), or sqrt(2/(N+M)), rather than 1/N. So when we initialize the DNN layers, we need to figure out what was done, and then move back to this normalization (see below)

My best idea right now is to simply let the user enter their own normalization function, i.e:
watcher.analyze(..., normalizer=(lambda N, M, rf: (N/(N+M))))
So that in the code we can, for every layer, we can fix the normalization using:
W = W*normalized(N,M,rf) I think this will work for the Transformers (not sure about the Conv2D layers though)


Here is an footnote from our long paper which discusses this
(its buried in a footnote because it is a minor tweak to the theory--but for for the MP fits to work , its critical to get this right)
image

from weightwatcher.

nsfzyzz avatar nsfzyzz commented on August 16, 2024

The following seems to help in my case.

First, change the range of sigma in the following function.

def fit_density_with_range(evals, Q, bw=0.1, sigma_range=(slice(0.1, 1.25, 0.01),) ):

Add the following two lines.
sigma_scaling_factor = calc_sigma(Q, evals)
sigma_range = (slice(0.05 * sigma_scaling_factor, 2.0 * sigma_scaling_factor, 0.01 * sigma_scaling_factor),)

Second, change the following condition.

if (b > max(xde)) | (a > xde[np.where(yde == max(yde))][0]) | (sigma > 1):

Change this condition to the following.
if (b < xde[np.where(yde == max(yde))][0] ) | (b > max(xde)) | (a > xde[np.where(yde == max(yde))][0]):

Third, change the following normalization code.

Wscale = np.sqrt(N*rf)/Wnorm

I am not sure why the original code uses N. But I used the following so that the eigenvalues are normalized to have an average value of 1.
Wscale = np.sqrt(to_plot.shape[0]*rf)/Wnorm

I test locally on a bunch of NLP models with only linear layers. The results seem to be fine.

image

There are still some cases that I do not know how to solve. For example, for CV models, it is possible that the eigenvalues do not follow the quarter circle function. In some other cases, the ESD is not very similar to any MP distribution or QC distribution, such as the following figure.

image

from weightwatcher.

charlesmartin14 avatar charlesmartin14 commented on August 16, 2024

I have implemented your fix, but the VGG model does not look correct
Can you please confirm that the fix works on all plots in the VGG notebooks provided
WeightWatcher-VGG.ipynb
WeightWatcher-VGG-Random.ipynb
and the GPT notebooks
The fix appears to break the QC plots, when N=M, Q=1 for VGG
Screen Shot 2021-08-07 at 10 50 04 AM

But the GPT models seems ok when Q=1
Screen Shot 2021-08-07 at 11 24 18 AM

from weightwatcher.

charlesmartin14 avatar charlesmartin14 commented on August 16, 2024

For example, for CV models, it is possible that the eigenvalues do not follow the quarter circle function.
If the matrix is randomized, it will mostly follow the QC law
I suspect this is a scaling issue

from weightwatcher.

nsfzyzz avatar nsfzyzz commented on August 16, 2024

Ok, so I think the previous fix still has a problem. On my side, I tried to change

Wscale = np.sqrt(N*rf)/Wnorm

to Wscale = np.sqrt(to_plot.shape[0])/Wnorm.

Previously, I suggested Wscale = np.sqrt(to_plot.shape[0]*rf)/Wnorm. I did not understand the meaning of rf and assumed that is always 1.

Now, the VGG seems to give a proper result.

image

Could you see if that works?

from weightwatcher.

nsfzyzz avatar nsfzyzz commented on August 16, 2024

Another potential issue is that Q can be < 1.0 in the following line.

I think one should add an if-condition to check if N and M should be flipped.

from weightwatcher.

charlesmartin14 avatar charlesmartin14 commented on August 16, 2024

I think this is now fixed, see #60
closing

from weightwatcher.

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.