You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by mo...@apache.org on 2018/05/18 04:52:12 UTC
[06/14] incubator-singa git commit: SINGA-349 Create layer operations
for autograd
SINGA-349 Create layer operations for autograd
Modified the design of convolution operation to let it trainable.
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/51c242b5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/51c242b5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/51c242b5
Branch: refs/heads/master
Commit: 51c242b5fbaeddb48b49a325e814608ff9c1a10d
Parents: 195b4d4
Author: xuewanqi <36...@users.noreply.github.com>
Authored: Tue May 1 21:16:06 2018 +0800
Committer: Wang Wei <dc...@nus.edu.sg>
Committed: Thu May 17 21:19:06 2018 +0800
----------------------------------------------------------------------
python/singa/convolution_operation.py | 57 ++++++++++--------------------
1 file changed, 19 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/51c242b5/python/singa/convolution_operation.py
----------------------------------------------------------------------
diff --git a/python/singa/convolution_operation.py b/python/singa/convolution_operation.py
index dcab2d4..8475c21 100644
--- a/python/singa/convolution_operation.py
+++ b/python/singa/convolution_operation.py
@@ -34,20 +34,24 @@ class Convolution2D(tensor.Operation):
data_format=data_format, use_bias=use_bias, W_specs=W_specs, b_specs=b_specs,
pad=pad, input_sample_shape=input_sample_shape)
+
def __call__(self, x):
if not self.PyLayer.has_setup:
self.PyLayer.setup(x.shape[1:])
param_data = self.PyLayer.layer.param_values()
+
+ if not hasattr(self, 'w'):
+ self.w = tensor.Tensor(data=param_data[0], requires_grad=True, stores_grad=True)
+ self.w.gaussian(0.0, 0.1) # TODO realize other initialization method according to W_specs
+
+ xs = [x, self.w]
+
if len(param_data) == 2:
- w = tensor.Tensor(data=param_data[0], requires_grad=True, stores_grad=True)
- b = tensor.Tensor(data=param_data[1], requires_grad=True, stores_grad=True)
- w.gaussian(0.0, 0.1)
- b.set_value(0.0)
- xs = tuple([x, w, b])
- else:
- w = tensor.Tensor(data=param_data[0], requires_grad=True, stores_grad=True)
- w.gaussian(0.0, 0.1)
- xs = tuple([x, w])
+ self.b = tensor.Tensor(data=param_data[1], requires_grad=True, stores_grad=True)
+ self.b.set_value(0.0) # TODO realize other initialization method according to b_specs
+ xs.append(self.b)
+
+ xs = tuple(xs)
return self._do_forward(*xs)
def forward(self, *xs):
@@ -58,14 +62,6 @@ class Convolution2D(tensor.Operation):
return (ret[0],)+ret[1]
-x = tensor.Tensor(shape=(1, 1, 3, 3), requires_grad=True, stores_grad=True)
-x.gaussian(1,0.0)
-layer_1= Convolution2D('conv1',4)
-y= layer_1(x)[0]
-z= layer_1._do_backward(y.data)
-a=ctensor2numpy(y.data)
-
-
class MaxPooling2D(tensor.Operation):
def __init__(self, name, kernel=3, stride=2, border_mode='same', pad=None,
data_format='NCHW', input_sample_shape=None):
@@ -85,13 +81,6 @@ class MaxPooling2D(tensor.Operation):
def backward(self, dy):
return self.PyLayer.layer.Backward(True, dy)[0] # how backward() return?
-x = tensor.Tensor(shape=(1, 1, 3, 3), requires_grad=True, stores_grad=True)
-x.gaussian(1,0.0)
-layer_1= MaxPooling2D('pooling1')
-y= layer_1(x)[0]
-z= layer_1._do_backward(y.data)
-a=ctensor2numpy(y.data)
-
class Activation(tensor.Operation):
def __init__(self,name, mode='relu',input_sample_shape=None):
@@ -109,14 +98,6 @@ class Activation(tensor.Operation):
return self.PyLayer.layer.Backward(True, dy)[0]
-x = tensor.Tensor(shape=(1, 1, 3, 3), requires_grad=True, stores_grad=True)
-x.gaussian(-1,0.0)
-layer_1= Activation('relu1')
-y= layer_1(x)[0]
-z= layer_1._do_backward(y.data)
-a=ctensor2numpy(y.data)
-
-
class Flatten(tensor.Operation):
def __init__(self, name, axis=1, input_sample_shape=None):
self.PyLayer = layer.Flatten(name, axis, input_sample_shape)
@@ -132,12 +113,12 @@ class Flatten(tensor.Operation):
def backward(self, dy):
return self.PyLayer.layer.Backward(True, dy)[0]
-x = tensor.Tensor(shape=(1, 1, 3, 3), requires_grad=True, stores_grad=True)
-x.gaussian(-1,0.0)
-layer_1= Flatten('flatten')
-y= layer_1(x)[0]
-z= layer_1._do_backward(y.data)
-a=ctensor2numpy(y.data)
+
+class Dense(tensor.Operation):
+ '''
+ Need to implemented?
+ '''
+ pass
inputs=tensor.Tensor(shape=(10, 2, 3, 3), requires_grad=False, stores_grad=False)