def get_pos_enc(pos_id_q, pos_id_k, d_model, dropout, is_training, clamp_len=-1, dtype=tf.float32): """Create inputs related to relative position encoding.""" pos_id_q = tf.cast(pos_id_q, dtype) pos_id_k = tf.cast(pos_id_k, dtype) if clamp_len > 0: pos_id_q = tf.clamp(pos_id_q, -clamp_len, clamp_len) pos_id_k = tf.clamp(pos_id_k, -clamp_len, clamp_len) d_model_half = d_model // 2 freq_seq = tf.cast(tf.range(0, d_model_half, 1.0), dtype=dtype) inv_freq = 1 / (10000 ** (freq_seq / d_model_half)) sinusoid_q = tf.einsum("...i,d->...id", pos_id_q, inv_freq) sinusoid_k = tf.einsum("...i,d->...id", pos_id_k, inv_freq) sin_enc_q = tf.sin(sinusoid_q) cos_enc_q = tf.cos(sinusoid_q) sin_enc_q = dropout_op(sin_enc_q, dropout, training=is_training) cos_enc_q = dropout_op(cos_enc_q, dropout, training=is_training) sin_enc_k = tf.sin(sinusoid_k) cos_enc_k = tf.cos(sinusoid_k) enc_q_1 = tf.concat([sin_enc_q, sin_enc_q], axis=-1) enc_k_1 = tf.concat([cos_enc_k, sin_enc_k], axis=-1) enc_q_2 = tf.concat([cos_enc_q, cos_enc_q], axis=-1) enc_k_2 = tf.concat([-sin_enc_k, cos_enc_k], axis=-1) return [enc_q_1, enc_q_2, enc_k_1, enc_k_2]
def get_filters(R, filter_size, P=None, n_rings=None): """Perform single-frequency DFT on each ring of a polar-resampled patch""" k = filter_size filters = {} N = n_samples(k) from scipy.linalg import dft for m, r in R.items(): rsh = r.get_shape().as_list() # Get the basis matrices weights = get_interpolation_weights(k, m, n_rings=n_rings) DFT = dft(N)[m, :] LPF = np.dot(DFT, weights).T cosine = np.real(LPF).astype(np.float32) sine = np.imag(LPF).astype(np.float32) # Reshape for multiplication with radial profile cosine = tf.constant(cosine) sine = tf.constant(sine) # Project taps on to rotational basis r = tf.reshape(r, tf.stack([rsh[0], rsh[1] * rsh[2]])) ucos = tf.reshape(tf.matmul(cosine, r), tf.stack([k, k, rsh[1], rsh[2]])) usin = tf.reshape(tf.matmul(sine, r), tf.stack([k, k, rsh[1], rsh[2]])) if P is not None: # Rotate basis matrices ucos_ = tf.cos(P[m]) * ucos + tf.sin(P[m]) * usin usin = -tf.sin(P[m]) * ucos + tf.cos(P[m]) * usin ucos = ucos_ filters[m] = (ucos, usin) print(ucos) return filters
def _compute_sdf(self, x, translations, blend_terms, points): """Compute signed distances between query points and hyperplanes.""" n_parts = tf.shape(x)[1] n_planes = tf.shape(x)[2] norm_logit = x[..., 0:self._dims - 1] offset = (-(tf.nn.sigmoid(x[..., self._dims - 1:self._dims]) * self._offset_scale + self._offset_lbound)) blend_planes = ( tf.nn.sigmoid(blend_terms[..., :n_parts]) * self._blend_scale + self._blend_lbound) # Norm of the boundary line norm_rad = tf.tanh(norm_logit) * np.pi # [..., (azimuth, altitude)] if self._dims == 3: norm = tf.stack([ tf.sin(norm_rad[..., 1]) * tf.cos(norm_rad[..., 0]), tf.sin(norm_rad[..., 1]) * tf.sin(norm_rad[..., 0]), tf.cos(norm_rad[..., 1]) ], axis=-1) else: norm = tf.concat([tf.cos(norm_rad), tf.sin(norm_rad)], axis=-1) # Calculate signed distances to hyperplanes. points = (tf.expand_dims(points, axis=1) - tf.expand_dims(translations, axis=2)) points = tf.expand_dims(points, axis=2) points = tf.tile(points, [1, 1, n_planes, 1, 1]) signed_dis = tf.matmul(points, tf.expand_dims(norm, axis=-1)) signed_dis = signed_dis + tf.expand_dims(offset, axis=-2) return signed_dis, translations, blend_planes, offset
def tf_rotation_around_grid_centroid(view_params, shapenet_viewer=False): #This function returns a rotation matrix around a center with y-axis being the up vector, and a scale matrix. #It first rotates the matrix by the azimuth angle (theta) around y, then around X-axis by elevation angle (gamma) #return a rotation matrix in homogenous coordinate #The default Open GL camera is to looking towards the negative Z direction #This function is suitable when the silhoutte projection is done along the Z direction batch_size = tf.shape( view_params )[0] # shape: (batch_size * 6) 6: azimuth, elevation, scale, translation_x ~ z azimuth = tf.reshape(view_params[:, 0], (batch_size, 1, 1)) elevation = tf.reshape(view_params[:, 1], (batch_size, 1, 1)) # azimuth = azimuth # why this is necessary? if shapenet_viewer == False: azimuth = (azimuth - tf.constant(math.pi * 0.5)) #======================================================== #Because tensorflow does not allow tensor item replacement #A new matrix needs to be created from scratch by concatenating different vectors into rows and stacking them up #Batch Rotation Y matrixes ones = tf.ones_like(azimuth) # shape: (batch_size * 1 * 1) zeros = tf.zeros_like(azimuth) batch_Rot_Y = tf.concat([ tf.concat([tf.cos(azimuth), zeros, -tf.sin(azimuth), zeros], axis=2), tf.concat([zeros, ones, zeros, zeros], axis=2), tf.concat([tf.sin(azimuth), zeros, tf.cos(azimuth), zeros], axis=2), tf.concat([zeros, zeros, zeros, ones], axis=2) ], axis=1) #Batch Rotation Z matrixes  Why you rotates the matrix around X-axis? It is different to your annotation before batch_Rot_Z = tf.concat([ tf.concat([tf.cos(elevation), tf.sin(elevation), zeros, zeros], axis=2), tf.concat([-tf.sin(elevation), tf.cos(elevation), zeros, zeros], axis=2), tf.concat([zeros, zeros, ones, zeros], axis=2), tf.concat([zeros, zeros, zeros, ones], axis=2) ], axis=1) transformation_matrix = tf.matmul(batch_Rot_Z, batch_Rot_Y) if tf.shape(view_params)[1] == 2: return transformation_matrix else: #Batch Scale matrixes: scale = tf.reshape(view_params[:, 2], (batch_size, 1, 1)) batch_Scale = tf.concat([ tf.concat([scale, zeros, zeros, zeros], axis=2), tf.concat([zeros, scale, zeros, zeros], axis=2), tf.concat([zeros, zeros, scale, zeros], axis=2), tf.concat([zeros, zeros, zeros, ones], axis=2) ], axis=1) return transformation_matrix, batch_Scale
def generate_heatmap_target_sigmas_rotation(heatmap_size, landmarks, sigmas, rotation, scale=1.0, normalize=False, data_format='channels_first'): """ Generates heatmap images for the given parameters. :param heatmap_size: The image size of a single heatmap. :param landmarks: The list of landmarks. For each landmark, a heatmap on the given coordinate will be generated. If landmark.is_valid is False, then the heatmap will be empty. :param sigmas: The sigmas for the individual heatmaps. May be either fixed, or trainable. :param rotation: The rotation of the heatmap. May be either fixed, or trainable. :param scale: The scale factor for each heatmap. Each pixel value will be multiplied by this value. :param normalize: If true, each heatmap value will be multiplied by the normalization factor of the gaussian. :param data_format: The data format of the resulting tensor of heatmap images. :return: The tensor of heatmap images. """ landmarks_shape = landmarks.get_shape().as_list() sigmas_shape = sigmas.get_shape().as_list() batch_size = landmarks_shape[0] num_landmarks = landmarks_shape[1] dim = landmarks_shape[2] - 1 assert dim == 2, 'Currently only dim == 2 is supported.' assert len(heatmap_size) == dim, 'Dimensions do not match.' assert sigmas_shape[0] == num_landmarks, 'Number of sigmas does not match.' rotation_matrix = tf.stack([tf.stack([tf.cos(rotation), -tf.sin(rotation)], axis=-1), tf.stack([tf.sin(rotation), tf.cos(rotation)], axis=-1)], axis=-1) rotation_matrix_t = tf.stack([tf.stack([tf.cos(rotation), tf.sin(rotation)], axis=-1), tf.stack([-tf.sin(rotation), tf.cos(rotation)], axis=-1)], axis=-1) det_covariances = tf.reduce_prod(sigmas, axis=-1) sigmas_inv_eye = tf.eye(dim, dim, batch_shape=[num_landmarks]) * tf.expand_dims(1.0 / sigmas, -1) inv_covariances = tf.matmul(tf.matmul(rotation_matrix, sigmas_inv_eye), rotation_matrix_t) if data_format == 'channels_first': heatmap_axis = 1 landmarks_reshaped = tf.reshape(landmarks[..., 1:], [batch_size, num_landmarks] + [1] * dim + [dim]) is_valid_reshaped = tf.reshape(landmarks[..., 0], [batch_size, num_landmarks] + [1] * dim) det_covariances_reshaped = tf.reshape(det_covariances, [1, num_landmarks] + [1] * dim) inv_covariances_reshaped = tf.reshape(inv_covariances, [1, num_landmarks] + [1] * dim + [dim, dim]) else: heatmap_axis = dim + 1 landmarks_reshaped = tf.reshape(landmarks[..., 1:], [batch_size] + [1] * dim + [num_landmarks, dim]) is_valid_reshaped = tf.reshape(landmarks[..., 0], [batch_size] + [1] * dim + [num_landmarks]) det_covariances_reshaped = tf.reshape(det_covariances, [1] + [1] * dim + [num_landmarks]) inv_covariances_reshaped = tf.reshape(inv_covariances, [1] + [1] * dim + [num_landmarks, dim, dim]) aranges = [np.arange(s) for s in heatmap_size] grid = tf.meshgrid(*aranges, indexing='ij') grid_stacked = tf.stack(grid, axis=dim) grid_stacked = tf.cast(grid_stacked, tf.float32) grid_stacked = tf.stack([grid_stacked] * batch_size, axis=0) grid_stacked = tf.stack([grid_stacked] * num_landmarks, axis=heatmap_axis) if normalize: scale /= tf.sqrt(tf.pow(2 * np.pi, dim) * det_covariances_reshaped) x_minus_mu = grid_stacked - landmarks_reshaped exp_factor = tf.reduce_sum(tf.reduce_sum(tf.expand_dims(x_minus_mu, -1) * inv_covariances_reshaped * tf.expand_dims(x_minus_mu, -2), axis=-1), axis=-1) heatmap = scale * tf.exp(-0.5 * exp_factor) heatmap_or_zeros = tf.where((is_valid_reshaped + tf.zeros_like(heatmap)) > 0, heatmap, tf.zeros_like(heatmap)) return heatmap_or_zeros
def fingauss(kfield, R, kx, ky, kz, nc, bs): kny = 1 * np.pi * nc / bs kx = tf.reshape(kx, [-1, 1, 1]) * nc / bs ky = tf.reshape(ky, [1, -1, 1]) * nc / bs kz = tf.reshape(kz, [1, 1, -1]) * nc / bs kk = tf.sqrt((2 * kny / np.pi * tf.sin(kx * np.pi / (2 * kny)))**2 + (2 * kny / np.pi * tf.sin(ky * np.pi / (2 * kny)))**2 + (2 * kny / np.pi * tf.sin(kz * np.pi / (2 * kny)))**2) wts = tf.exp(-0.5 * R**2 * kk**2) return kfield * tf.cast(wts, kfield.dtype)
def _get_rot_mat_z_hom(angle): """ Returns a 3D rotation matrix in homogeneous coords. """ one_vec = tf.ones_like(angle) zero_vec = one_vec * 0.0 trafo_matrix = _stitch_mat_from_vecs([ tf.cos(angle), -tf.sin(angle), zero_vec, zero_vec, tf.sin(angle), tf.cos(angle), zero_vec, zero_vec, zero_vec, zero_vec, one_vec, zero_vec, zero_vec, zero_vec, zero_vec, one_vec ]) return trafo_matrix
def Question2(optim): x_data = np.load('x_hw.npy') y_data = np.load('y_hw.npy') #Uncomment to plot the raw data Answer to question 1 #plt.plot(x_data,y_data) #plt.show() #Initialize the waves to separate values or else they will train symmetrically A1 = tf.Variable(tf.ones([1])) f1 = tf.Variable(tf.ones([1])) A2 = tf.Variable(tf.zeros([1])) f2 = tf.Variable(tf.ones([1])) #This could be a more efficent method for the hypothesis, but it's so small who cares. #FASTER: y = add(multiply(A1,sin(multiply(f1,x_data))), multiply(A2,sin(multiply(f2,x_data)))) #Answer to Question 2 y = A1*tf.sin(f1*x_data) + A2*tf.sin(f2*x_data) loss = tf.reduce_mean(tf.square(y - y_data)) #Could realistically get away with a training step as large as .2 or higher, #but I'll do this to show I understand the danger of large training steps. #FASTER: optimizer = tf.train.AdamOptimizer(0.2) optimizer = optim train_step = optimizer.minimize(loss) session = tf.InteractiveSession() tf.global_variables_initializer().run() #Tensorboard setup tf.summary.scalar("LOSS", loss) merged = tf.summary.merge_all() writer = tf.summary.FileWriter('./logs/2/train') writer.add_graph(session.graph) #FASTER: optimizer = N= 200 N= 10000 for k in range(N): session.run(train_step) s=session.run(merged) writer.add_summary(s, k) if k%200 == 0: print("k =",k, "A1 =", session.run(A1[0]), "A2 =", session.run(A2[0]),"f1 =", session.run(f1[0]), "f2 =", session.run(f2[0]), "loss =",session.run(loss) ) AA1,ff1,AA2,ff2 = session.run([A1,f1,A2,f2]) AA1 = AA1[0] AA2 = AA2[0] ff1 = ff1[0] ff2 = ff2[0] MSE = mean_squared_error(AA1*np.sin(ff1*x_data) + AA2*np.sin(ff2*x_data),y_data) return MSE
def euler2mat(z, y, x): """Converts euler angles to rotation matrix TODO: remove the dimension for 'N' (deprecated for converting all source poses altogether) Reference: https://github.com/pulkitag/pycaffe-utils/blob/master/rot_utils.py#L174 Args: z: rotation angle along z axis (in radians) -- size = [B, N] y: rotation angle along y axis (in radians) -- size = [B, N] x: rotation angle along x axis (in radians) -- size = [B, N] Returns: Rotation matrix corresponding to the euler angles -- size = [B, N, 3, 3] """ B = tf.shape(z)[0] N = 1 z = tf.clip_by_value(z, -np.pi, np.pi) y = tf.clip_by_value(y, -np.pi, np.pi) x = tf.clip_by_value(x, -np.pi, np.pi) # Expand to B x N x 1 x 1 z = tf.expand_dims(tf.expand_dims(z, -1), -1) y = tf.expand_dims(tf.expand_dims(y, -1), -1) x = tf.expand_dims(tf.expand_dims(x, -1), -1) zeros = tf.zeros([B, N, 1, 1]) ones = tf.ones([B, N, 1, 1]) cosz = tf.cos(z) sinz = tf.sin(z) rotz_1 = tf.concat([cosz, -sinz, zeros], axis=3) rotz_2 = tf.concat([sinz, cosz, zeros], axis=3) rotz_3 = tf.concat([zeros, zeros, ones], axis=3) zmat = tf.concat([rotz_1, rotz_2, rotz_3], axis=2) cosy = tf.cos(y) siny = tf.sin(y) roty_1 = tf.concat([cosy, zeros, siny], axis=3) roty_2 = tf.concat([zeros, ones, zeros], axis=3) roty_3 = tf.concat([-siny, zeros, cosy], axis=3) ymat = tf.concat([roty_1, roty_2, roty_3], axis=2) cosx = tf.cos(x) sinx = tf.sin(x) rotx_1 = tf.concat([ones, zeros, zeros], axis=3) rotx_2 = tf.concat([zeros, cosx, -sinx], axis=3) rotx_3 = tf.concat([zeros, sinx, cosx], axis=3) xmat = tf.concat([rotx_1, rotx_2, rotx_3], axis=2) rotMat = tf.matmul(tf.matmul(xmat, ymat), zmat) return rotMat
def oscillator_bank(frequency_envelopes: tf.Tensor, amplitude_envelopes: tf.Tensor, sample_rate: int = 16000) -> tf.Tensor: """Generates audio from sample-wise frequencies for a bank of oscillators. Args: frequency_envelopes: Sample-wise oscillator frequencies (Hz). Shape [batch_size, n_samples, n_sinusoids]. amplitude_envelopes: Sample-wise oscillator amplitude. Shape [batch_size, n_samples, n_sinusoids]. sample_rate: Sample rate in samples per a second. Returns: wav: Sample-wise audio. Shape [batch_size, n_samples, n_sinusoids]. """ frequency_envelopes = tf_float32(frequency_envelopes) amplitude_envelopes = tf_float32(amplitude_envelopes) # Don't exceed Nyquist. amplitude_envelopes = remove_above_nyquist(frequency_envelopes, amplitude_envelopes, sample_rate) # Change Hz to radians per sample. omegas = frequency_envelopes * (2.0 * np.pi) # rad / sec omegas = omegas / float(sample_rate) # rad / sample # Accumulate phase and synthesize. phases = cumsum(omegas, axis=1) wavs = tf.sin(phases) harmonic_audio = amplitude_envelopes * wavs # [mb, n_samples, n_sinusoids] audio = tf.reduce_sum(harmonic_audio, axis=-1) # [mb, n_samples] return audio
def unstacked_matrix_from_angles(rx, ry, rz, name=None): """Create an unstacked rotation matrix from rotation angles. Args: rx: A tf.Tensor of rotation angles abound x, of any shape. ry: A tf.Tensor of rotation angles abound y (of the same shape as x). rz: A tf.Tensor of rotation angles abound z (of the same shape as x). name: A string, name for the op. Returns: A 3-tuple of 3-tuple of tf.Tensors of the same shape as x, representing the respective rotation matrix. The small 3x3 dimensions are unstacked into a tuple to avoid tensors with small dimensions, which bloat the TPU HBM memory. Unstacking is one of the recommended methods for resolving the problem. """ with tf.name_scope(name, 'BuildUnstackedRotationMatrix', [rx, ry, rz]): angles = [-rx, -ry, -rz] sx, sy, sz = [tf.sin(a) for a in angles] cx, cy, cz = [tf.cos(a) for a in angles] m00 = cy * cz m10 = (sx * sy * cz) - (cx * sz) m20 = (cx * sy * cz) + (sx * sz) m01 = cy * sz m11 = (sx * sy * sz) + (cx * cz) m21 = (cx * sy * sz) - (sx * cz) m02 = -sy m12 = sx * cy m22 = cx * cy return ((m00, m01, m02), (m10, m11, m12), (m20, m21, m22))
def get_pos_enc_gpu(rel_pos_id, d_model, dropout, is_training, clamp_len=-1, dtype=tf.float32): """Create inputs related to relative position encoding.""" rel_pos_id = tf.cast(rel_pos_id, dtype) if clamp_len > 0: rel_pos_id = tf.clamp(rel_pos_id, -clamp_len, clamp_len) d_model_half = d_model // 2 freq_seq = tf.cast(tf.range(0, d_model_half, 1.0), dtype=dtype) inv_freq = 1 / (10000**(freq_seq / d_model_half)) sinusoid = tf.einsum("...i,d->...id", rel_pos_id, inv_freq) print((rel_pos_id).get_shape(), (inv_freq).get_shape(), "==get_pos_enc shape==", sinusoid.get_shape()) sin_enc = tf.sin(sinusoid) cos_enc = tf.cos(sinusoid) sin_enc = dropout_op(sin_enc, dropout, training=is_training) cos_enc = dropout_op(cos_enc, dropout, training=is_training) pos_enc = tf.concat([sin_enc, cos_enc], axis=-1) return pos_enc
def test_MpiAdam(): np.random.seed(0) tf.set_random_seed(0) a = tf.Variable(np.random.randn(3).astype('float32')) b = tf.Variable(np.random.randn(2,5).astype('float32')) loss = tf.reduce_sum(tf.square(a)) + tf.reduce_sum(tf.sin(b)) stepsize = 1e-2 update_op = tf.train.AdamOptimizer(stepsize).minimize(loss) do_update = U.function([], loss, updates=[update_op]) tf.get_default_session().run(tf.global_variables_initializer()) for i in range(10): print(i,do_update()) tf.set_random_seed(0) tf.get_default_session().run(tf.global_variables_initializer()) var_list = [a,b] lossandgrad = U.function([], [loss, U.flatgrad(loss, var_list)], updates=[update_op]) adam = MpiAdam(var_list) for i in range(10): l,g = lossandgrad() adam.update(g, stepsize) print(i,l)
def positional_embedding(pos_seq, inv_freq, bsz=None): sinusoid_inp = tf.einsum('i,j->ij', pos_seq, inv_freq) pos_emb = tf.concat([tf.sin(sinusoid_inp), tf.cos(sinusoid_inp)], -1) if bsz is not None: return tf.tile(pos_emb[:, None, :], [1, bsz, 1]) else: return pos_emb[:, None, :]
def testIntegratedGradientsGetMask(self): with tf.Graph().as_default() as graph: x = tf.placeholder(shape=[None, 3], dtype=tf.float32) y = 5 * x[:, 0] + x[:, 0] * x[:, 1] + tf.sin(x[:, 2]) with tf.Session() as sess: # Calculate the value of `y` at the baseline. x_baseline_val = np.array([[0.5, 0.8, 1.0]], dtype=np.float) y_baseline_val = sess.run(y, feed_dict={x: x_baseline_val}) # Calculate the value of `y` at the input. x_input_val = np.array([[1.0, 2.0, 3.0]], dtype=np.float) y_input_val = sess.run(y, feed_dict={x: x_input_val}) # Due to mathematical properties of the integrated gradients, # the expected IG value is equal to the difference between # the `y` value at the input and the `y` value at the baseline. expected_val = y_input_val[0] - y_baseline_val[0] # Calculate the integrated gradients attribution of the input. ig = integrated_gradients.IntegratedGradients( graph, sess, y[0], x) mask = ig.GetMask(x_value=x_input_val[0], feed_dict={}, x_baseline=x_baseline_val[0], x_steps=1000) # Verify the result. self.assertAlmostEqual(expected_val, mask.sum(), places=3)
def _get_rot_mat(self, ux_b, uy_b, uz_b): """ Returns a rotation matrix from axis and (encoded) angle.""" with tf.name_scope('get_rot_mat'): u_norm = tf.sqrt( tf.square(ux_b) + tf.square(uy_b) + tf.square(uz_b) + 1e-8) theta = u_norm # some tmp vars st_b = tf.sin(theta) ct_b = tf.cos(theta) one_ct_b = 1.0 - tf.cos(theta) st = st_b[:, 0] ct = ct_b[:, 0] one_ct = one_ct_b[:, 0] norm_fac = 1.0 / u_norm[:, 0] ux = ux_b[:, 0] * norm_fac uy = uy_b[:, 0] * norm_fac uz = uz_b[:, 0] * norm_fac trafo_matrix = self._stitch_mat_from_vecs([ ct + ux * ux * one_ct, ux * uy * one_ct - uz * st, ux * uz * one_ct + uy * st, uy * ux * one_ct + uz * st, ct + uy * uy * one_ct, uy * uz * one_ct - ux * st, uz * ux * one_ct - uy * st, uz * uy * one_ct + ux * st, ct + uz * uz * one_ct ]) return trafo_matrix
def get_timing_signal_1d_given_position(channels, position, min_timescale=1.0, max_timescale=1.0e4): """Get sinusoids of diff frequencies, with timing position given. Adapted from add_timing_signal_1d_given_position in //third_party/py/tensor2tensor/layers/common_attention.py Args: channels: scalar, size of timing embeddings to create. The number of different timescales is equal to channels / 2. position: a Tensor with shape [batch, seq_len] min_timescale: a float max_timescale: a float Returns: a Tensor of timing signals [batch, seq_len, channels] """ num_timescales = channels // 2 log_timescale_increment = ( math.log(float(max_timescale) / float(min_timescale)) / (tf.to_float(num_timescales) - 1)) inv_timescales = min_timescale * tf.exp( tf.to_float(tf.range(num_timescales)) * -log_timescale_increment) scaled_time = (tf.expand_dims(tf.to_float(position), 2) * tf.expand_dims(tf.expand_dims(inv_timescales, 0), 0)) signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=2) signal = tf.pad(signal, [[0, 0], [0, 0], [0, tf.mod(channels, 2)]]) return signal
def __call__(self, inputs, start_index=None): dtype = inputs.dtype inputs_shape = tf.shape(inputs) batch_size = inputs_shape[0] length = inputs_shape[1] channels = inputs_shape[2] if start_index is None: start_index = tf.zeros((batch_size, 1), tf.int32) position = tf.expand_dims(tf.range(length), 0) position = tf.tile(position, [batch_size, 1]) + start_index position = tf.cast(position, dtype) num_timescales = channels // 2 log_timescale_increment = ( math.log(_MAX_TIMESCALE / _MIN_TIMESCALE) / tf.maximum(tf.cast(num_timescales, dtype) - 1, 1)) inv_timescales = _MIN_TIMESCALE * tf.exp( tf.cast(tf.range(num_timescales), dtype) * -log_timescale_increment) scaled_time = tf.expand_dims(position, 2) * tf.reshape( inv_timescales, [1, 1, -1]) signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=2) signal = tf.pad(signal, [[0, 0], [0, 0], [0, tf.mod(channels, 2)]]) signal = tf.reshape(signal, [-1, length, channels]) return inputs + signal
def setUp(self): super().setUp() with tf.Graph().as_default() as graph: x = tf.placeholder(shape=[None, 3], dtype=tf.float32) contrib = [5 * x[:, 0], x[:, 1] * x[:, 1], tf.sin(x[:, 2])] y = contrib[0] + contrib[1] + contrib[2] sess = tf.Session(graph=graph) self.sess_spy = mock.MagicMock(wraps=sess) self.x_baseline_val = np.array([[0.5, 0.8, 1.0]], dtype=float) self.x_input_val = np.array([[1.0, 2.0, 3.0]], dtype=float) # Calculate the value of `contrib` at the baseline and input. `y` is # the sum of contrib and each variable is independent, so the expected # contribution is equal to the difference between the baseline and input # contribution for each. contrib_baseline_val = sess.run(contrib, feed_dict={x: self.x_baseline_val}) contrib_input_val = sess.run(contrib, feed_dict={x: self.x_input_val}) self.expected_val = np.array(contrib_input_val) - np.array( contrib_baseline_val) self.expected_val = self.expected_val.flatten() self.ig_instance = integrated_gradients.IntegratedGradients( graph, self.sess_spy, y, x)
def rodrigues(r): """ Rodrigues' rotation formula that turns axis-angle tensor into rotation matrix in a batch-ed manner. Parameter: ---------- r: Axis-angle rotation tensor of shape [batch_size, 1, 3]. Return: ------- Rotation matrix of shape [batch_size, 3, 3]. """ theta = tf.norm(r + tf.random_normal(r.shape, 0, 1e-8, dtype=tf.float64), axis=(1, 2), keepdims=True) # avoid divide by zero r_hat = r / theta cos = tf.cos(theta) z_stick = tf.zeros(theta.get_shape().as_list()[0], dtype=tf.float64) m = tf.stack( (z_stick, -r_hat[:, 0, 2], r_hat[:, 0, 1], r_hat[:, 0, 2], z_stick, -r_hat[:, 0, 0], -r_hat[:, 0, 1], r_hat[:, 0, 0], z_stick), axis=1) m = tf.reshape(m, (-1, 3, 3)) i_cube = tf.expand_dims(tf.eye(3, dtype=tf.float64), axis=0) + tf.zeros( (theta.get_shape().as_list()[0], 3, 3), dtype=tf.float64) A = tf.transpose(r_hat, (0, 2, 1)) B = r_hat dot = tf.matmul(A, B) R = cos * i_cube + (1 - cos) * dot + tf.sin(theta) * m return R
def rotation_matrix(self, plane): i = plane.first_axis j = plane.second_axis theta = plane.theta cos_theta = theta sin_thesta = tf.stop_gradient(tf.sin(tf.acos(theta))) # rotation = np.eye(self.dim).tolist() # rotation[i][i] = cos_theta # rotation[i][j] = -sin_thesta # rotation[j][i] = sin_thesta # rotation[j][j] = cos_theta # rotation = tf.stack(rotation) idx = [] values = [] for k in range(self.dim): idx.append([k, k]) if k == i or k == j: values.append(cos_theta) else: values.append(1) idx.append([i, j]) values.append(-sin_thesta) idx.append([j, i]) values.append(sin_thesta) rotation = tf.SparseTensor(idx, values=tf.stack(values), dense_shape=[self.dim, self.dim]) return rotation
def create_2x2_rotation_matrix(radians): """Creates a 2D rotation matrix. For an angle a this is [[cos(a), -sin(a)], [sin(a), cos(a)]] Args: radians: rotation angle in radians. Returns: 2-D Tensor with 2x2 elements, the rotation matrix. """ rotation = [[tf.cos(radians), -tf.sin(radians)], [tf.sin(radians), tf.cos(radians)]] rotation = tf.convert_to_tensor(rotation, name="rotation_matrix") return rotation
def gpt(y, h, normalize=False): # implementation of Eq.(9) if normalize: h = gproj(y, h) [u, unorm] = unit(h) return (u * tf.cos(unorm) - y * tf.sin(unorm)) * unorm
def gexp(y, h, normalize=False): # implementation of Eq.(7) if normalize: y, _ = unit(y) h = gproj(y, h) u, hnorm = unit(h) return y * tf.cos(hnorm) + u * tf.sin(hnorm)
def get_random_rotation(max_angle, rotation_center, image_width): """ Arguments: max_angle: an integer, angle in degrees. rotation_center: a float tensor with shape [1, 2]. image_width: a float tensor with shape []. Returns: a float tensor with shape [2, 2]. """ PI = 3.141592653589793 max_angle_radians = max_angle * (PI/180.0) # x-coordinate of the rotation center cx = rotation_center[0, 1] # relative distance between centers distance_to_image_center = tf.abs(cx - 0.5 * image_width) distance_to_image_center /= image_width # if the center is too near to the borders then # reduce the maximal rotation angle decay = (0.6 - 2.0 * distance_to_image_center)/0.6 decay = tf.maximum(decay, 0.0) max_angle_radians *= decay # decay is in [0, 1] range, # decay = 1 if cx = 0.5 * image_width, # decay = 0 if cx = 0.2 * image_width # get a random angle theta = tf.random_uniform( [], minval=-max_angle_radians, maxval=max_angle_radians, dtype=tf.float32 ) rotation = tf.stack([ tf.cos(theta), tf.sin(theta), -tf.sin(theta), tf.cos(theta) ], axis=0) rotation = tf.reshape(rotation, [2, 2]) return rotation
def geometric_transform(pose_tensor, similarity=False, nonlinear=True, as_matrix=False): """Convers paramer tensor into an affine or similarity transform. Args: pose_tensor: [..., 6] tensor. similarity: bool. nonlinear: bool; applies nonlinearities to pose params if True. as_matrix: bool; convers the transform to a matrix if True. Returns: [..., 3, 3] tensor if `as_matrix` else [..., 6] tensor. """ scale_x, scale_y, theta, shear, trans_x, trans_y = tf.split( pose_tensor, 6, -1) if nonlinear: scale_x, scale_y = (tf.nn.sigmoid(i) + 1e-2 for i in (scale_x, scale_y)) trans_x, trans_y, shear = (tf.nn.tanh(i * 5.) for i in (trans_x, trans_y, shear)) theta *= 2. * math.pi else: scale_x, scale_y = (abs(i) + 1e-2 for i in (scale_x, scale_y)) c, s = tf.cos(theta), tf.sin(theta) if similarity: scale = scale_x pose = [scale * c, -scale * s, trans_x, scale * s, scale * c, trans_y] else: pose = [ scale_x * c + shear * scale_y * s, -scale_x * s + shear * scale_y * c, trans_x, scale_y * s, scale_y * c, trans_y ] pose = tf.concat(pose, -1) # convert to a matrix if as_matrix: shape = pose.shape[:-1].as_list() shape += [2, 3] pose = tf.reshape(pose, shape) zeros = tf.zeros_like(pose[Ellipsis, :1, 0]) last = tf.stack([zeros, zeros, zeros + 1], -1) pose = tf.concat([pose, last], -2) return pose
def random_vector_on_sphere(batch, limits): """Randomly sample a point on a unit sphere within a range.""" min_y, max_y = limits[0][0], limits[0][1] min_theta, max_theta = limits[1][0], limits[1][1] y = tf.random_uniform([batch, 1], minval=min_y, maxval=max_y) theta = tf.random_uniform([batch, 1], minval=min_theta, maxval=max_theta) cos_phi = tf.sqrt(1 - tf.square(y)) x = cos_phi * tf.cos(theta) z = -cos_phi * tf.sin(theta) return tf.concat([x, y, z], -1)
def position_encoding(inputs): T = tf.shape(inputs)[1] repr_dim = inputs.get_shape()[-1].value pos = tf.reshape(tf.range(0.0, tf.to_float(T), dtype=tf.float32), [-1, 1]) i = np.arange(0, repr_dim, 2, np.float32) denom = np.reshape(np.power(10000.0, i / repr_dim), [1, -1]) enc = tf.expand_dims( tf.concat( [tf.sin(pos / denom), tf.cos(pos / denom)], 1), 0) return tf.tile(enc, [tf.shape(inputs)[0], 1, 1])
def lookup_all(self, h, r, t): re_head = tf.nn.embedding_lookup(self.re_ent_embeds, h) re_tail = tf.nn.embedding_lookup(self.re_ent_embeds, t) im_head = tf.nn.embedding_lookup(self.im_ent_embeds, h) im_tail = tf.nn.embedding_lookup(self.im_ent_embeds, t) relation = tf.nn.embedding_lookup(self.rel_embeds, r) phase_relation = relation / (self.embedding_range / self.pi) re_relation = tf.cos(phase_relation) im_relation = tf.sin(phase_relation) return re_head, re_relation, re_tail, im_head, im_relation, im_tail
def rot_matrix(s): i = tf.cast((s+1)*4.5, 'int32') vp = tf.constant([0, np.pi/4.0, np.pi/2.0, 3*np.pi/4.0, np.pi, 5*np.pi/4.0, 3*np.pi/2.0, 7*np.pi/4.0, np.pi/2.0]) hp = tf.constant([0, 0, 0, 0, 0, 0, 0, 0, np.pi/2.0]) theta = tf.gather(vp, i) phi = tf.gather(hp, i) #theta = (s[0] + 1.0) * np.pi #phi = s[1] * np.pi/2.0 sin_theta = tf.sin(theta) cos_theta = tf.cos(theta) sin_phi = tf.sin(phi) cos_phi = tf.cos(phi) ry = [[cos_theta, 0, sin_theta], [0, 1, 0], [-sin_theta, 0, cos_theta]] rx = [[1, 0, 0], [0, cos_phi, -sin_phi], [0, sin_phi, cos_phi]] return tf.matmul(rx, ry)