def test_batch_dot_shape(self): x_batch = KTF.ones(shape=(32, 20)) y_batch = KTF.ones(shape=(32, 20)) xy_batch_dot = KTF.batch_dot(x_batch, y_batch, axes=1) assert_allclose(KTF.eval(xy_batch_dot), np.ones((32, 1)) * 20, atol=1e-05) xy_batch_dot = KTF.batch_dot(x_batch, y_batch, axes=0) assert_allclose(KTF.eval(xy_batch_dot), np.ones((20, 1)) * 32, atol=1e-05) # making sure swapping axes when ndim == 2 works x_batch = KTF.ones(shape=(32, 20)) y_batch = KTF.ones(shape=(20, 32)) xy_batch_dot = KTF.batch_dot(x_batch, y_batch, axes=(0, 1)) assert_allclose(KTF.eval(xy_batch_dot), np.ones((20, 1)) * 32, atol=1e-05) xy_batch_dot = KTF.batch_dot(x_batch, y_batch, axes=(1, 0)) assert_allclose(KTF.eval(xy_batch_dot), np.ones((32, 1)) * 20, atol=1e-05)
def get_weightnorm_params_and_grads(p, g): ps = K.get_variable_shape(p) # construct weight scaler: V_scaler = g/||V|| V_scaler_shape = (ps[-1], ) # assumes we're using tensorflow! V_scaler = K.ones( V_scaler_shape) # init to ones, so effective parameters don't change # get V parameters = ||V||/g * W norm_axes = [i for i in range(len(ps) - 1)] V = p / tf.reshape(V_scaler, [1] * len(norm_axes) + [-1]) # split V_scaler into ||V|| and g parameters V_norm = tf.sqrt(tf.reduce_sum(tf.square(V), norm_axes)) g_param = V_scaler * V_norm # get grad in V,g parameters grad_g = tf.reduce_sum(g * V, norm_axes) / V_norm grad_V = tf.reshape(V_scaler, [1] * len(norm_axes) + [-1]) * \ (g - tf.reshape(grad_g / V_norm, [1] * len(norm_axes) + [-1]) * V) return V, V_norm, V_scaler, g_param, grad_g, grad_V
def test_batch_dot_shape(self): with pytest.raises(ValueError): x_batch = KTF.ones(shape=(32, 20)) y_batch = KTF.ones(shape=(32, 20)) xy_batch_dot = KTF.batch_dot(x_batch, y_batch, axes=1)