You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@singa.apache.org by GitBox <gi...@apache.org> on 2020/05/18 08:37:35 UTC

[GitHub] [singa] dcslin commented on a change in pull request #697: New Model Layer Operator API

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



##########
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:
       @XJDKC do you know why this still trigger `Check failed: initialized_ Must initialize data before reading it`?




----------------------------------------------------------------
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