Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for common_layer #79

Merged
merged 3 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions delta/layers/common_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ def attention(inputs, attention_size, time_major=False, return_alphas=False):
hidden_size = inputs.shape[2].value # D value - hidden size of the RNN layer

# Trainable parameters
W_omega = tf.get_variable(
tf.random_normal([hidden_size, attention_size], stddev=0.1))
b_omega = tf.get_variable(tf.random_normal([attention_size], stddev=0.1))
u_omega = tf.get_variable(tf.random_normal([attention_size, 1], stddev=0.1))
W_omega = tf.get_variable(name='W_omega',
initializer=tf.random_normal([hidden_size, attention_size], stddev=0.1))
b_omega = tf.get_variable(name='b_omega',
initializer=tf.random_normal([attention_size], stddev=0.1))
u_omega = tf.get_variable(name='u_omega',
initializer=tf.random_normal([attention_size, 1], stddev=0.1))

# Applying fully connected layer with non-linear activation to each of the B*T timestamps;
# the shape of `v` is (B,T,D)*(D,A)=(B,T,A), where A=attention_size
Expand Down Expand Up @@ -252,8 +254,8 @@ def attention(inputs, attention_size, time_major=False, return_alphas=False):
def embedding_look_up(text_inputs, vocab_size, embedding_size):
"""Embedding layer."""
with tf.variable_scope("embedding"):
W = tf.get_variable(
tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), "W")
W = tf.get_variable(name='W',
initializer=tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0))
embedding_chars = tf.nn.embedding_lookup(W, text_inputs)
embedding_chars_expanded = tf.expand_dims(embedding_chars, -1)
return embedding_chars_expanded
Expand All @@ -272,8 +274,10 @@ def conv_pool(embedded_chars_expanded, filter_sizes, embedding_size,
with tf.variable_scope("conv-maxpool-%s" % filter_size):
# Convolution Layer
filter_shape = [filter_size, embedding_size, 1, num_filters]
W = tf.get_variable(tf.truncated_normal(filter_shape, stddev=0.1), "W")
b = tf.get_variable(tf.constant(0.1, shape=[num_filters]), "b")
W = tf.get_variable(name='W',
initializer=tf.truncated_normal(filter_shape, stddev=0.1))
b = tf.get_variable(name='b',
initializer=tf.constant(0.1, shape=[num_filters]))
conv = tf.nn.conv2d(
embedded_chars_expanded,
W,
Expand Down
152 changes: 152 additions & 0 deletions delta/layers/common_layers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright (C) 2017 Beijing Didi Infinity Technology and Development Co.,Ltd.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Common layers test."""

import tensorflow as tf
from absl import logging

import common_layers as cl

class LossUtilTest(tf.test.TestCase):
''' common layer unittest '''

def test_depthwise_separable_conv(self):
'''test depthwise separable conv'''
inputs = tf.random_uniform(shape=[1, 5, 5, 1], dtype=tf.float32, maxval=1.0)
num_pwc_filters = 3 #out filters num
width_multiplier = 1
scope = 'depthwise'
output = cl.depthwise_separable_conv(inputs,
num_pwc_filters,
width_multiplier,
scope)
shape = [1, 5, 5, 3]
self.assertAllEqual(tf.shape(output), shape)


def test_splice_layer(self):
'''test splice layer'''
inputs = tf.reshape(tf.range(15), shape=[1, 5, 3])
context = [1, 3]
output = cl.splice_layer(inputs, 'splice', context)
GaryGao99 marked this conversation as resolved.
Show resolved Hide resolved
output_true = tf.constant([[[3, 4, 5, 9, 10, 11],
[6, 7, 8, 12, 13, 14],
[9, 10, 11, 12, 13, 14],
[12, 13, 14, 12, 13, 14],
[12, 13, 14, 12, 13, 14]]])

self.assertAllEqual(output, output_true)

def test_tdnn(self):
'''test tdnn'''
#A 3D Tensor [batch, in_width, in_channels]
inputs = tf.random_uniform(shape=[2, 5, 3], dtype=tf.float32, maxval=1.0)
in_dim = inputs.get_shape().as_list()[2]
out_dim = 4
context = [-2, -1, 0, 1, 2]
output = cl.tdnn(inputs, 'test_tdnn0', in_dim, context, out_dim, method='splice_layer')
out_shape = [2, 5, 4]
self.assertAllEqual(tf.shape(output), out_shape)

context = 2
#output = cl.tdnn(inputs, 'test_tdnn1', in_dim, context, out_dim, method='splice_op')
#self.assertAllEqual(tf.shape(output), out_shape)

output = cl.tdnn(inputs, 'test_tdnn2', in_dim, context, out_dim, method='conv1d')
self.assertAllEqual(tf.shape(output), out_shape)

def test_conv2d(self):
'''test conv2d'''
inputs = tf.random_uniform(shape=[2, 5, 5, 3], dtype=tf.float32, maxval=1.0) #A 4D Tensor
filter_size = [3, 3]
in_channels = inputs.get_shape().as_list()[3]
out_channels = 4
strides = [1, 1]
output = cl.conv2d(inputs, 'test_conv2d', filter_size, in_channels, out_channels, strides)
output_shape = [2, 5, 5, 4]
self.assertAllEqual(tf.shape(output), output_shape)

def test_maxpool(self):
'''test maxpool'''
inputs = tf.reshape(tf.range(25), shape=[1, 5, 5, 1]) #A 4D tensor
ksize = [3, 3]
strides = [1, 1]
output = cl.max_pool(inputs, ksize, strides)
output_shape = [1, 3, 3, 1]
self.assertAllEqual(tf.shape(output), output_shape)

output_true = tf.constant([[[[12], [13], [14]],
[[17], [18], [19]],
[[22], [23], [24]]]])
self.assertAllEqual(output, output_true)

def test_linear(self):
'''test linear'''
inputs = tf.random_uniform(shape=[4, 5], dtype=tf.float32, maxval=1.0) # A 2D tensor
shape = [5, 4]
output = cl.linear(inputs, 'test_linear0', shape)
output_shape = [4, 4]
self.assertAllEqual(tf.shape(output), output_shape)

inputs = tf.random_uniform(shape=[2, 4, 5], dtype=tf.float32, maxval=1.0) # A 3D tensor
shape = [5, 4]
output = cl.linear(inputs, 'test_linear1', shape)
output_shape = [2, 4, 4]
self.assertAllEqual(tf.shape(output), output_shape)

# A 4D tensor [B, C, H, W]
inputs = tf.random_uniform(shape=[2, 3, 4, 5], dtype=tf.float32, maxval=1.0)
shape = [5, 4]
output = cl.linear(inputs, 'test_linear2', shape)
output_shape = [2, 3, 4, 4]
self.assertAllEqual(tf.shape(output), output_shape)

def test_attention(self):
'''test attention'''
# A 3D tensor [B, T, D]
inputs = tf.random_uniform(shape=[2, 100, 512], dtype=tf.float32, maxval=1.0)
attention_size = 256
output, alpha = cl.attention(inputs, attention_size, return_alphas=True)
output_shape = [2, 512]
alpha_shape = [2, 100, 1]
self.assertAllEqual(tf.shape(output), output_shape)
self.assertAllEqual(tf.shape(alpha), alpha_shape)

def test_embedding_look_up(self):
'''test embedding look up'''
text_inputs = [0, 1, 2]
vocab_size = 3
embedding_size = 512
output = cl.embedding_look_up(text_inputs, vocab_size, embedding_size)
output_shape = [3, 512, 1]
self.assertAllEqual(tf.shape(output), output_shape)

def test_conv_pool(self):
'''test conv pool'''
# A 4D tensor [B, H, W, C]
embedded_chars_expanded = tf.random_uniform(shape=[2, 7, 7, 1], dtype=tf.float32, maxval=1.0)
filter_sizes = [3, 5]
embedding_size = 3
num_filters = 3
sequence_length = 5
output = cl.conv_pool(embedded_chars_expanded, filter_sizes, embedding_size,
num_filters, sequence_length)
output_shape = [30, 6]
self.assertAllEqual(tf.shape(output), output_shape)

if __name__ == '__main__':
logging.set_verbosity(logging.INFO)
tf.test.main()