|
- import os
- os.environ['TL_BACKEND'] = 'tensorflow'
- # os.environ['TL_BACKEND'] = 'mindspore'
-
- import tensorlayerx as tlx
- from tensorlayerx.nn import Module
- from tensorlayerx.nn import Linear
- import numpy as np
-
- def test_conv1d():
- input_layer = tlx.nn.Input([8, 100, 1])
- conv1dlayer1 = tlx.nn.Conv1d(in_channels=1, out_channels=32, kernel_size=5, stride=2, data_format='channels_last')
- n1 = conv1dlayer1(input_layer)
- print("Conv1D channels last: ", n1.shape)
-
- def test_conv2d():
- input_layer = tlx.nn.Input([5, 100, 100, 3])
- conv2dlayer1 = tlx.nn.Conv2d(
- out_channels=32, in_channels=3, stride=(2, 2), kernel_size=(5, 5), padding='SAME',
- b_init=tlx.nn.initializers.truncated_normal(0.01), name='conv2dlayer'
- )
- n1 = conv2dlayer1(input_layer)
- print("Conv2d", n1.shape)
-
- input_layer = tlx.nn.Input([5, 100, 100, 3])
- conv2dlayer2 = tlx.nn.Conv2d(
- out_channels=32, in_channels=3, kernel_size=(3, 3), stride=(2, 2), act=None, name='conv2d'
- )
- n2 = conv2dlayer2(input_layer)
- print("Conv2d", n2.shape)
-
- input_layer = tlx.nn.Input([5, 100, 100, 32])
- conv2dlayer3 = tlx.nn.Conv2d(
- in_channels=32, out_channels=32, kernel_size=(3, 3), stride=(2, 2), act=tlx.ReLU, b_init=None,
- name='conv2d_no_bias'
- )
- n3 = conv2dlayer3(input_layer)
- print("Conv2d", n3.shape)
-
-
- def test_pooling():
- # 2d pool
- x_2_input_shape = [3, 100, 100, 3]
- nin_2 = tlx.nn.Input(x_2_input_shape, name='test_in2')
-
- n6 = tlx.nn.Conv2d(out_channels=32, kernel_size=(3, 3), stride=(2, 2), name='test_conv2d')(nin_2)
- n7 = tlx.nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding='SAME',
- name='test_maxpool2d')(n6)
-
- n9 = tlx.nn.GlobalMaxPool2d(name='test_maxpool2d')(n6)
- n10 = tlx.nn.GlobalAvgPool2d(name='test_Avgpool2d')(n6)
- n6_shape = tlx.get_tensor_shape(n6)
- n7_shape = tlx.get_tensor_shape(n7)
- n9_shape = tlx.get_tensor_shape(n9)
- n10_shape = tlx.get_tensor_shape(n10)
-
- print("2d pooling", n6_shape, n7_shape, n9_shape, n10_shape)
-
-
- def test_dense():
- input_layer = tlx.nn.Input([10, 30])
- n1 = tlx.nn.Linear(out_features=100, in_features=30, b_init=tlx.initializers.truncated_normal())(input_layer)
- n2 = tlx.nn.Linear(out_features=10, name='none inchannels')(input_layer)
- print("Dense :", n1.shape, n2.shape)
-
-
- def test_rnn():
- rnncell_input = tlx.nn.Input([4, 16], name='input')
- rnncell_prev_h = tlx.nn.Input([4, 32])
- rnncell = tlx.nn.RNNCell(input_size=16, hidden_size=32, bias=True, act='tanh', name='rnncell_1')
- rnncell_out, _ = rnncell(rnncell_input, rnncell_prev_h)
- print("rnncell out", rnncell_out.shape)
-
- rnn_input = tlx.nn.Input([23, 32, 16], name='input1')
- rnn_prev_h = tlx.nn.Input([4, 32, 32])
- rnn = tlx.nn.RNN(
- input_size=16, hidden_size=32, bias=True, num_layers=2, bidirectional=True, act='tanh',
- batch_first=False, dropout=0, name='rnn_1')
-
- rnn_out, _ = rnn(rnn_input, rnn_prev_h)
- print("rnn out", rnn_out.shape)
-
- lstmcell_input = tlx.nn.Input([4, 16], name='input')
- lstmcell_prev_h = tlx.nn.Input([4, 32])
- lstmcell_prev_c = tlx.nn.Input([4, 32])
- lstmcell = tlx.nn.LSTMCell(input_size=16, hidden_size=32, bias=True, name='lstmcell_1')
- lstmcell_out, (h, c) = lstmcell(lstmcell_input, (lstmcell_prev_h, lstmcell_prev_c))
- print("lstmcell out", lstmcell_out.shape)
-
- lstm_input = tlx.nn.Input([23, 32, 16], name='input')
- lstm_prev_h = tlx.nn.Input([4, 32, 32])
- lstm_prev_c = tlx.nn.Input([4, 32, 32])
- lstm = tlx.nn.LSTM(input_size=16, hidden_size=32, bias=True, num_layers=2, bidirectional=True,
- batch_first=False, dropout=0, name='lstm_1')
- lstm_out, (h, c) = lstm(lstm_input, (lstm_prev_h, lstm_prev_c))
- print("lstm out", lstm_out.shape)
-
- grucell_input = tlx.nn.Input([4, 16], name='input')
- grucell_prev_h = tlx.nn.Input([4, 32])
- grucell = tlx.nn.GRUCell(input_size=16, hidden_size=32, bias=True, name='grucell_1')
- grucell_out, h = grucell(grucell_input, grucell_prev_h)
- print("grucell_out out", grucell_out.shape)
-
- gru_input = tlx.nn.Input([23, 32, 16], name='input')
- gru_prev_h = tlx.nn.Input([4, 32, 32])
- gru = tlx.nn.GRU(input_size=16, hidden_size=32, bias=True, num_layers=2, bidirectional=True,
- batch_first=False, dropout=0, name='GRU_1')
- gru_out, h = gru(gru_input, gru_prev_h)
- print("gru_out out", gru_out.shape)
-
-
-
- if __name__ == '__main__':
- test_conv1d()
- test_conv2d()
- test_pooling()
- test_dense()
- test_rnn()
|