https://github.com/GPflow/GPflow
Tip revision: 2a755d547574a9cd4bc813ab147cc57a30808737 authored by Alexander G. de G. Matthews on 04 November 2016, 14:23:00 UTC
Incrementing minor version number to 0.3.4 (#262)
Incrementing minor version number to 0.3.4 (#262)
Tip revision: 2a755d5
tf_wraps.py
# Copyright 2016 James Hensman, alexggmatthews
#
# 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.
"""
A collection of wrappers and extensions for tensorflow.
"""
import os
import tensorflow as tf
from ._settings import settings
def eye(N):
"""
An identitiy matrix
"""
return tf.diag(tf.ones(tf.pack([N, ]), dtype=settings.dtypes.float_type))
_custom_op_module = tf.load_op_library(os.path.join(os.path.dirname(__file__), 'tfops', 'matpackops.so'))
vec_to_tri = _custom_op_module.vec_to_tri
tri_to_vec = _custom_op_module.tri_to_vec
@tf.RegisterGradient("VecToTri")
def _vec_to_tri_grad(op, grad):
return [tri_to_vec(grad)]
@tf.RegisterShape("VecToTri")
def _vec_to_tri_shape(op):
in_shape = op.inputs[0].get_shape().with_rank(2)
M = in_shape[1].value
if M is None:
k = None
else:
k = int((M * 8 + 1) ** 0.5 / 2.0 - 0.5)
shape = tf.TensorShape([in_shape[0], k, k])
return [shape]