Giter VIP home page Giter VIP logo

Comments (6)

nudles avatar nudles commented on July 23, 2024

@dcslin can you help check this issue?

from singa.

dcslin avatar dcslin commented on July 23, 2024

hi @Shashankwer I am looking into this.

from singa.

dcslin avatar dcslin commented on July 23, 2024

Hi @Shashankwer , Understand that the error code is not clear enough, but I could not replicate the error without further details(inputs, outputs), would you like to refer to following working example transformed from your code to help you debugging?

from singa import autograd
from singa import module
from singa import opt
from singa import tensor

class MLP():
    def __init__(self):
        self.linear1 = autograd.Linear(3,4)
        self.linear2 = autograd.Linear(4,3)
    def forward(self,x):
        y = self.linear1(x)
        return self.linear2(y)
    def loss(self, out, ty):
        return autograd.softmax_cross_entropy(out, ty)
    def optim(self, loss):
        self.optimizer.backward_and_update(loss)
    def set_optimizer(self, optimizer):
        self.optimizer = optimizer


if __name__ == '__main__':
    x=tensor.Tensor((3,3)).gaussian(1,1)
    y=tensor.Tensor((3,3)).gaussian(1,1)

    autograd.training = True
    m = MLP()
    sgd = opt.SGD()
    m.set_optimizer(sgd)
    out = m.forward(x)
    loss = m.loss(out, y)
    m.optim(loss)
    print(loss)

from singa.

Shashankwer avatar Shashankwer commented on July 23, 2024

Hi,

Issue reported here is for handling the error on the python API side and is particularly noticed for autograd.backward function.

Consider the below example

from singa import autograd
from singa import module
from singa import opt
from singa import tensor
from singa import device

class MLP():
    def __init__(self):
        self.linear1 = autograd.Linear(3, 4)
        self.linear2 = autograd.Linear(4, 3)
    def forward(self,x):
        y = self.linear1(x)
        return self.linear2(y)
    def loss(self, out, ty):
        return autograd.softmax_cross_entropy(out, ty)
    def optim(self, loss):
        self.optimizer.backward_and_update(loss)
    def set_optimizer(self, optimizer):
        self.optimizer = optimizer

def train(model, x, t, dev=device.get_default_device(), epochs=100):
    for i in range(epochs):
        y = model.forward(x)
        loss = autograd.mse_loss(y, t)
        print("loss: ", loss)
        sgd = opt.SGD()
        for p, gp in autograd.backward(loss):
            sgd.update(p, gp)
        sgd.step()


if __name__ == '__main__':
    x=tensor.Tensor((3,3)).gaussian(1,1)
    y=tensor.Tensor((3,3)).gaussian(1,1)
    
    autograd.training = True
    m = MLP()
    sgd = opt.SGD()
    m.set_optimizer(sgd)
    out = m.forward(x)
    loss = m.loss(out, y)
    m.optim(loss)
    print(loss)
    train(m,x,y)

The above code will execute without any issues. However if we change the dimension of output tensor such that it does not match the model constructed, the error is noticed. For example

from singa import autograd
from singa import module
from singa import opt
from singa import tensor
from singa import device

class MLP():
    def __init__(self):
        self.linear1 = autograd.Linear(3, 4)
        self.linear2 = autograd.Linear(4, 3)
    def forward(self,x):
        y = self.linear1(x)
        return self.linear2(y)
    def loss(self, out, ty):
        return autograd.softmax_cross_entropy(out, ty)
    def optim(self, loss):
        self.optimizer.backward_and_update(loss)
    def set_optimizer(self, optimizer):
        self.optimizer = optimizer

def train(model, x, t, dev=device.get_default_device(), epochs=100):
    for i in range(epochs):
        y = model.forward(x)
        loss = autograd.mse_loss(y, t)
        print("loss: ", loss)
        sgd = opt.SGD()
        for p, gp in autograd.backward(loss):
            sgd.update(p, gp)
        sgd.step()


if __name__ == '__main__':
    x=tensor.Tensor((3,3)).gaussian(1,1)
    y=tensor.Tensor((3,4)).gaussian(1,1)
    
    autograd.training = True
    m = MLP()
    sgd = opt.SGD()
    m.set_optimizer(sgd)
    out = m.forward(x)
    loss = m.loss(out, y)
    m.optim(loss)
    print(loss)
    train(m,x,y)

from singa.

dcslin avatar dcslin commented on July 23, 2024

Yes We should add input shape check all necessary operators in autograd.py
for example, we should raise exception if input shapes are different:

autograd.softmax_cross_entropy(tx, ty)
autograd.mse_loss(tx, ty)
autograd.equal(tx,ty)

from singa.

dcslin avatar dcslin commented on July 23, 2024

addressed in pr #751

from singa.

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.