add initialization

This commit is contained in:
Joerg Franke 2018-06-19 22:42:23 +02:00
parent da7babc725
commit 14c60970b1
4 changed files with 77 additions and 6 deletions

25
adnc/utils/initialization.py Executable file
View File

@ -0,0 +1,25 @@
# Copyright 2018 Jörg Franke
#
# 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.
# ==============================================================================
import numpy as np
import tensorflow as tf
def unit_simplex_initialization(rng, batch_size, shape, dtype=tf.float32):
mat = []
for i in range(batch_size):
mat.append(rng.uniform(0, 1 / np.sum(shape), shape))
mat = np.array(mat)
return tf.constant(mat, dtype=dtype)

View File

@ -0,0 +1,46 @@
# Copyright 2018 Jörg Franke
#
# 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.
# ==============================================================================
import pytest
import numpy as np
import tensorflow as tf
from adnc.utils.initialization import unit_simplex_initialization
@pytest.fixture()
def session():
with tf.Session() as sess:
yield sess
tf.reset_default_graph()
@pytest.fixture()
def np_rng():
seed = np.random.randint(1, 999)
return np.random.RandomState(seed)
BATCH_SIZE = 4
SHAPE = [2, 3]
def test_unit_simplex_initialization(session, np_rng):
init_matrix = unit_simplex_initialization(np_rng, BATCH_SIZE, SHAPE, dtype=tf.float32)
np_init_matrix = init_matrix.eval()
for b in range(BATCH_SIZE):
tensor = np_init_matrix[b, :, :]
assert np.sum(tensor) <= 1
assert tensor.min() >= 0
assert tensor.max() <= 1

View File

@ -19,18 +19,16 @@ import tensorflow as tf
from adnc.utils.normalization import layer_norm
@pytest.fixture()
def np_rng():
seed = np.random.randint(1, 999)
return np.random.RandomState(seed)
@pytest.fixture()
def session():
with tf.Session() as sess:
yield sess
tf.reset_default_graph()
@pytest.fixture()
def np_rng():
seed = np.random.randint(1, 999)
return np.random.RandomState(seed)
def test_layer_norm(session, np_rng):
np_weights = np_rng.normal(0, 1, [64, 128])

View File

@ -18,12 +18,14 @@ import tensorflow as tf
from adnc.utils.tf_functions import oneplus
@pytest.fixture()
def session():
with tf.Session() as sess:
yield sess
tf.reset_default_graph()
def test_oneplus(session):
tf_x = tf.constant([1, 2, 3], dtype=tf.float32, )
tf_x_oneplus = oneplus(tf_x)