Skip to content

Commit 41500d7

Browse files
committed
update backend
1 parent f23ad19 commit 41500d7

File tree

8 files changed

+59
-24
lines changed

8 files changed

+59
-24
lines changed

docs/modules/cost.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ we can. So we encourage you to use TensorFlow's function, , see `TensorFlow API
1111

1212
.. autosummary::
1313

14-
cross_entropy
14+
softmax_cross_entropy_with_logits
1515
sigmoid_cross_entropy
1616
binary_cross_entropy
1717
mean_squared_error
@@ -28,12 +28,11 @@ we can. So we encourage you to use TensorFlow's function, , see `TensorFlow API
2828
maxnorm_regularizer
2929
maxnorm_o_regularizer
3030
maxnorm_i_regularizer
31-
huber_loss
3231

3332

3433
Softmax cross entropy
3534
----------------------
36-
.. autofunction:: cross_entropy
35+
.. autofunction:: softmax_cross_entropy_with_logits
3736

3837
Sigmoid cross entropy
3938
----------------------
@@ -94,7 +93,3 @@ Special
9493
.. autofunction:: lo_regularizer
9594
.. autofunction:: maxnorm_o_regularizer
9695
.. autofunction:: maxnorm_i_regularizer
97-
98-
Huber Loss
99-
^^^^^^^^^^
100-
.. autofunction:: huber_loss

docs/user/get_start_model.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Switching train/test modes
120120
... # testing code here
121121
122122
# method 2: Using packaged training modules
123-
model = tl.models.Model(network=MLP, loss_fn=tl.cost.cross_entropy, optimizer=optimizer)
123+
model = tl.models.Model(network=MLP, loss_fn=tl.cost.softmax_cross_entropy_with_logits, optimizer=optimizer)
124124
model.train(n_epoch=n_epoch, train_dataset=train_ds)
125125
126126
Reuse weights
@@ -192,13 +192,13 @@ Save weights only
192192
MLP.save_weights('./model_weights.npz') # by default, file will be in hdf5 format
193193
MLP.load_weights('./model_weights.npz')
194194
195-
Save model architecture and weights (optional)
195+
Save model weights (optional)
196196
-----------------------------------------------
197197

198198
.. code-block:: python
199199
200200
# When using packaged training modules. Saving and loading the model can be done as follows
201-
model = tl.models.Model(network=MLP, loss_fn=tl.cost.cross_entropy, optimizer=optimizer)
201+
model = tl.models.Model(network=MLP, loss_fn=tl.cost.softmax_cross_entropy_with_logits, optimizer=optimizer)
202202
model.train(n_epoch=n_epoch, train_dataset=train_ds)
203203
model.save_weights('./model.npz', format='npz_dict')
204204
model.load_weights('./model.npz', format='npz_dict')

tensorlayer/backend/ops/load_backend.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
BACKEND = 'tensorflow'
99
# BACKEND = 'mindspore'
10+
# BACKEND = 'paddle'
1011

1112
# Check for backend.json files
1213
tl_backend_dir = os.path.expanduser('~')

tensorlayer/backend/ops/paddle_nn.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,22 @@ class Conv2D(object):
431431

432432
def __init__(self, strides, padding, data_format='NHWC', dilations=None, out_channel=None, k_size=None):
433433
self.data_format, self.padding = preprocess_2d_format(data_format, padding)
434-
self.ksize = k_size[0]
435434
if self.data_format is 'NHWC':
436-
self.dg_stride = strides[1]
437-
self.dg_dilation = dilations[1]
435+
self._stride = (strides[1], strides[2])
436+
self._dilation = (dilations[1], dilations[2])
438437
elif self.data_format is 'NCHW':
439-
self.dg_stride = strides[2]
440-
self.dg_dilation = dilations[2]
438+
self._stride = (strides[2], strides[3])
439+
self._dilation = (dilations[2], dilations[3])
440+
441441

442442
def __call__(self, inputs, filters):
443-
raise NotImplementedError
443+
outputs = F.conv2d(x=inputs,
444+
weight=filters,
445+
stride=self._stride,
446+
dilation=self._dilation,
447+
padding=self.padding,
448+
data_format=self.data_format)
449+
return outputs
444450

445451

446452
def conv2d(input, filters, strides, padding, data_format='NCHW', dilations=None):
@@ -468,7 +474,20 @@ def conv2d(input, filters, strides, padding, data_format='NCHW', dilations=None)
468474
-------
469475
A Tensor. Has the same type as input.
470476
"""
471-
raise NotImplementedError
477+
data_format, padding = preprocess_2d_format(data_format, padding)
478+
if data_format is 'NHWC':
479+
_stride = (strides[1], strides[2])
480+
_dilation = (dilations[1], dilations[2])
481+
elif data_format is 'NCHW':
482+
_stride = (strides[2], strides[3])
483+
_dilation = (dilations[2], dilations[3])
484+
outputs = F.conv2d(x=input,
485+
weight=filters,
486+
stride=_stride,
487+
dilation=_dilation,
488+
padding=padding,
489+
data_format=data_format)
490+
return outputs
472491

473492

474493
class Conv3D(object):
@@ -577,10 +596,18 @@ class MaxPool(object):
577596
def __init__(self, ksize, strides, padding, data_format=None):
578597
self.data_format, self.padding = preprocess_2d_format(data_format, padding)
579598
self.ksize = ksize
580-
self.strides = strides
599+
if self.data_format is 'NHWC':
600+
self._stride = (strides[1], strides[2])
601+
elif self.data_format is 'NCHW':
602+
self._stride = (strides[2], strides[3])
581603

582604
def __call__(self, inputs):
583-
raise NotImplementedError
605+
outputs = F.max_pool2d(x=inputs,
606+
kernel_size=self.ksize,
607+
stride=self._stride,
608+
padding=self.padding,
609+
data_format=self.data_format)
610+
return outputs
584611

585612

586613
def max_pool(input, ksize, strides, padding, data_format=None):
@@ -951,7 +978,7 @@ def __init__(self):
951978
pass
952979

953980
def __call__(self, *args, **kwargs):
954-
raise NotImplementedError
981+
pd.nn.BatchNorm2D
955982

956983

957984
class GroupConv2D(object):

tensorlayer/initializers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# 'Initializer', 'Zeros', 'Ones', 'Constant', 'RandomUniform', 'RandomNormal', 'TruncatedNormal',
66
# 'deconv2d_bilinear_upsampling_initializer', 'He_Normal'
77
# ]
8-
8+
from .load_initializers_backend import Initializer
99
from .load_initializers_backend import Zeros
1010
from .load_initializers_backend import Ones
1111
from .load_initializers_backend import Constant

tensorlayer/layers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .padding import *
1919
from .pooling import *
2020
from .quantize import *
21-
# from .recurrent import *
21+
from .recurrent import *
2222
from .scale import *
2323
from .shape import *
2424
from .spatial_transformer import *

tensorlayer/layers/convolution/simplified_conv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Conv2d(Module):
188188
--------
189189
With TensorLayer
190190
191-
>>> net = tl.layers.Input([8, 3, 400, 400], name='input')
191+
>>> net = tl.layers.Input([8, 400, 400, 3], name='input')
192192
>>> conv2d = tl.layers.Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), b_init=None, in_channels=3, name='conv2d_1')
193193
>>> print(conv2d)
194194
>>> tensor = tl.layers.Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tl.ReLU, name='conv2d_2')(net)
@@ -630,7 +630,7 @@ class DeConv2d(Module):
630630
--------
631631
With TensorLayer
632632
633-
>>> net = tl.layers.Input([8, 3, 400, 400], name='input')
633+
>>> net = tl.layers.Input([8, 400, 400, 3], name='input')
634634
>>> conv2d_transpose = tl.layers.DeConv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), b_init=None, in_channels=3, name='conv2d_transpose_1')
635635
>>> print(conv2d_transpose)
636636
>>> tensor = tl.layers.DeConv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tl.ReLU, name='conv2d_transpose_2')(net)

tensorlayer/layers/core/core_paddle.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ def __call__(self, *inputs, **kwargs):
191191
return outputs
192192

193193
def _get_weights(self, var_name, shape, init=None, trainable=True, transposed=None):
194+
# TODO 2D mindspore weights shape : [out_channel, in_channel, kernel_h, kernel_w]
195+
# TODO 2D mindspore transposed shape [in_channel, out_channel, kernel_h, kernel_w]
196+
if len(shape) == 3:
197+
shape = shape[::-1]
198+
if len(shape) == 4:
199+
if not transposed and self.data_format == 'NHWC':
200+
shape = (shape[3], shape[0], shape[1], shape[2])
201+
else:
202+
shape = (shape[3], shape[2], shape[0], shape[1])
203+
if len(shape) == 5:
204+
shape = (shape[4], shape[3], shape[0], shape[1], shape[2])
205+
194206
if var_name in ["filters", "weights"]:
195207
w_tmp = self.create_parameter(shape=shape, attr=init, is_bias=False)
196208
elif var_name in ["biases"]:

0 commit comments

Comments
 (0)