singa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [singa] nudles commented on a change in pull request #697: New Model Layer Operator API
Date Mon, 18 May 2020 09:38:37 GMT

nudles commented on a change in pull request #697:
URL: https://github.com/apache/singa/pull/697#discussion_r426497119



##########
File path: new_api.py
##########
@@ -0,0 +1,42 @@
+from singa import singa_wrap, autograd, tensor, device, module, opt
+
+class MyModel(module.Module):
+    def __init__(self):
+        super(MyModel, self).__init__()
+        self.l1 = autograd.Linear(3,2)
+        self.optimizer = opt.SGD()
+
+    def forward(self, x):
+        y = self.l1(x)
+        return y
+
+    def train_one_batch(self, x, y):
+        y_ = self.forward(x)
+        l = self.loss(y_, y)
+        print("loss",l)
+        self.optim(l)
+        return y_, l
+
+    def loss(self, out, ty):
+        return autograd.softmax_cross_entropy(out, ty)
+
+    def optim(self, loss):
+        self.optimizer.backward_and_update(loss)
+
+if __name__ == "__main__":
+    PlaceHolder = tensor.Tensor
+    dev = device.create_cuda_gpu()
+    x = PlaceHolder((2,3), device=dev)
+
+    m = MyModel()
+    m.on_device(dev)
+    # m.compile([x], is_train=True, use_graph=True, sequential=True)
+    # m.compile([x], is_train=True, use_graph=True, sequential=False)
+    m.compile([x], is_train=True, use_graph=False, sequential=False)
+
+    print("compile done")
+    cx = PlaceHolder((2,3), device=dev).gaussian(1,1)
+    cy = PlaceHolder((2,2), device=dev).gaussian(1,1)
+
+    m.train_one_batch(cx,cy)

Review comment:
       We can merge model.on_device() into model.compile(inputs, dev...)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message