Skip to content

Commit de963e2

Browse files
committed
update examples
1 parent 2006b6c commit de963e2

8 files changed

+21
-40
lines changed

examples/basic_tutorials/tutorial_SequentialLayer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
3+
import os
4+
os.environ['TL_BACKEND'] = 'tensorflow'
35

46
from tensorlayer.layers import SequentialLayer
57
from tensorlayer.layers import Dense

examples/basic_tutorials/tutorial_automatic_inference_input _shape.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
3+
import os
4+
os.environ['TL_BACKEND'] = 'tensorflow'
35

46
import numpy as np
57
import time

examples/basic_tutorials/tutorial_cifar10_cnn_tensorflow_backend.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
33
# The tensorlayer and tensorflow operators can be mixed
4+
import os
5+
os.environ['TL_BACKEND'] = 'tensorflow'
46

57
import time
68
import numpy as np

examples/basic_tutorials/tutorial_mnist_mlp_mindspore_backend.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
3+
import os
4+
os.environ['TL_BACKEND'] = 'mindspore'
35

46
import mindspore.ops.operations as P
57
from mindspore.ops import composite as C

examples/basic_tutorials/tutorial_mnist_mlp_tensorflow_backend.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
33
# The tensorlayer and tensorflow operators can be mixed
4+
import os
5+
os.environ['TL_BACKEND'] = 'tensorflow'
46

57
import numpy as np
68
import time

examples/basic_tutorials/tutorial_mnist_simple.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,27 @@
33

44
# The same set of code can switch the backend with one line
55
import os
6-
os.environ['TL_BACKEND'] = 'tensorflow'
6+
# os.environ['TL_BACKEND'] = 'tensorflow'
77
# os.environ['TL_BACKEND'] = 'mindspore'
8-
# os.environ['TL_BACKEND'] = 'paddle'
8+
os.environ['TL_BACKEND'] = 'paddle'
99

1010
import tensorlayer as tl
1111
from tensorlayer.layers import Module
1212
from tensorlayer.layers import Dense, Dropout, Flatten
1313
from tensorlayer.dataflow import Dataset
14-
from tensorlayer.vision.transforms import Normalize, Compose
1514

16-
X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))
1715

18-
transform = Compose([Normalize(mean=[127.5/255.], std=[127.5/255.], data_format='HWC')])
16+
X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 784))
17+
1918

2019
class mnistdataset(Dataset):
2120

22-
def __init__(self, data = X_train, label = y_train ,transform = transform):
21+
def __init__(self, data = X_train, label = y_train):
2322
self.data = data
2423
self.label = label
25-
self.transform = transform
2624

2725
def __getitem__(self, index):
2826
data = self.data[index].astype('float32')
29-
data = self.transform(data)
3027
label = self.label[index].astype('int64')
3128

3229
return data, label
@@ -46,11 +43,9 @@ def __init__(self):
4643
self.dense2 = Dense(n_units=800, act=tl.ReLU, in_channels=800)
4744
self.dropout3 = Dropout(keep=0.8)
4845
self.dense3 = Dense(n_units=10, act=tl.ReLU, in_channels=800)
49-
self.flatten = Flatten()
5046

5147
def forward(self, x, foo=None):
52-
z = self.flatten(x)
53-
z = self.dropout1(z)
48+
z = self.dropout1(x)
5449
z = self.dense1(z)
5550
z = self.dropout2(z)
5651
z = self.dense2(z)
@@ -68,9 +63,9 @@ def forward(self, x, foo=None):
6863

6964

7065
train_weights = MLP.trainable_weights
71-
optimizer = tl.optimizers.Momentum(0.001, 0.9)
66+
optimizer = tl.optimizers.Momentum(0.05, 0.9)
7267
metric = tl.metric.Accuracy()
73-
train_dataset = mnistdataset(data = X_train, label = y_train ,transform = transform)
68+
train_dataset = mnistdataset(data = X_train, label = y_train)
7469
train_dataset = tl.dataflow.FromGenerator(train_dataset, output_types=[tl.float32, tl.int64], column_names=['data', 'label'])
7570
train_loader = tl.dataflow.Dataloader(train_dataset, batch_size=batch_size, shuffle=True)
7671

examples/basic_tutorials/tutorial_nested_usage_of_Layer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
3+
import os
4+
os.environ['TL_BACKEND'] = 'tensorflow'
35

46
import time
57
import numpy as np

tensorlayer/cost/mindspore_cost.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,7 @@
2626
]
2727

2828

29-
def softmax_cross_entropy_with_logits(output, target):
30-
"""Softmax cross-entropy operation, returns the TensorFlow expression of cross-entropy for two distributions,
31-
it implements softmax internally. See ``tf.ops.sparse_softmax_cross_entropy_with_logits``.
32-
33-
Parameters
34-
----------
35-
output : Tensor
36-
A batch of distribution with shape: [batch_size, num of classes].
37-
target : Tensor
38-
A batch of index with shape: [batch_size, ].
39-
name : string
40-
Name of this loss.
41-
42-
Examples
43-
--------
44-
>>> import tensorlayer as tl
45-
>>> ce = tl.cost.softmax_cross_entropy_with_logits(y_logits, y_target_logits, 'my_loss')
46-
47-
References
48-
-----------
49-
- About cross-entropy: `<https://en.wikipedia.org/wiki/Cross_entropy>`__.
50-
- The code is borrowed from: `<https://en.wikipedia.org/wiki/Cross_entropy>`__.
51-
52-
"""
53-
54-
outputs = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')(output, target)
55-
return outputs
29+
softmax_cross_entropy_with_logits = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
5630

5731

5832
def sigmoid_cross_entropy(output, target, name=None):

0 commit comments

Comments
 (0)