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.iteritems(): 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) return filters
def testVonMisesSampleMoments(self): locs_v = np.array([-2., -1., 0.3, 2.3]) concentrations_v = np.array([0.1, 1.0, 2.0, 10.0]) von_mises = tfd.VonMises( self.make_tensor(locs_v), self.make_tensor(concentrations_v)) n = 10000 samples = von_mises.sample(n, seed=12345) expected_mean = von_mises.mean() actual_mean = tf.atan2( tf.reduce_mean(tf.sin(samples), 0), tf.reduce_mean(tf.cos(samples), 0)) expected_variance = von_mises.variance() standardized_samples = samples - tf.expand_dims(von_mises.mean(), 0) actual_variance = 1. - tf.reduce_mean(tf.cos(standardized_samples), axis=0) [ expected_mean_val, expected_variance_val, actual_mean_val, actual_variance_val ] = self.evaluate( [expected_mean, expected_variance, actual_mean, actual_variance]) self.assertAllClose(expected_mean_val, actual_mean_val, rtol=0.1) self.assertAllClose(expected_variance_val, actual_variance_val, rtol=0.1)
def call(self, inputs): k1 = tf.matmul(tf.cos(inputs), self.k1 * tf.cos(self.mu)) k2 = tf.matmul(tf.sin(inputs), self.k2 * tf.sin(self.mu)) # Defines the two model formulations: "glm" vs "gvm". if self.model_type == 'glm': return tf.exp(k1 + k2 + self.k0) else: return tf.nn.softplus(self.b) + self.g * tf.exp(k1 + k2)
def _euler2mat(z, y, x): """Converts euler angles to rotation matrix. From: https://github.com/pulkitag/pycaffe-utils/blob/master/rot_utils.py#L174 TODO: Remove the dimension for 'N' (deprecated for converting all source poses altogether). 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, with shape [B, n, 3, 3]. """ batch_size = 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([batch_size, n, 1, 1]) ones = tf.ones([batch_size, 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) return tf.matmul(tf.matmul(xmat, ymat), zmat)
def _J(self, theta): """ Implements the order dependent family of functions defined in equations 4 to 7 in the reference paper. """ if self.order == 0: return np.pi - theta elif self.order == 1: return tf.sin(theta) + (np.pi - theta) * tf.cos(theta) elif self.order == 2: return 3. * tf.sin(theta) * tf.cos(theta) + \ (np.pi - theta) * (1. + 2. * tf.cos(theta) ** 2)
def mmd_fourier(x1, x2, bandwidth=2., dim_r=500): """ Approximate RBF kernel by random features Notes: Reimplementation in tensorflow of the Variational Fair Autoencoder https://arxiv.org/abs/1511.00830 """ d = x1.get_shape().as_list()[1] rW_n = tf.sqrt(2. / bandwidth) * tf.random_normal([d, dim_r]) / np.sqrt(d) rb_u = 2 * np.pi * tf.random_uniform([dim_r]) rf0 = tf.sqrt(2. / dim_r) * tf.cos(tf.matmul(x1, rW_n) + rb_u) rf1 = tf.sqrt(2. / dim_r) * tf.cos(tf.matmul(x2, rW_n) + rb_u) result = tf.reduce_sum((tf.reduce_mean(rf0, axis=0) - tf.reduce_mean(rf1, axis=0))**2) return tf.sqrt(result)
def test_cwise_unary_grad(self): """ Ensure that all component-wise unary functions in the math op library yield an identical gradient to tensorflow """ test_config = tf.ConfigProto(allow_soft_placement=False) test_config.graph_options.optimizer_options.opt_level = -1 with tf.Session(config=test_config) as s: arg_np = np.random.random(100) grad_above = tf.constant(np.random.random(100)) arg = tf.constant(arg_np) def test_grad(fcn, tf_fcn): ovl_out = as_tensorflow(fcn(arg)) tf_out = tf_fcn(arg) ovl_grad = tf.gradients(ovl_out, arg, grad_above)[0] tf_grad = tf.gradients(tf_out, arg, grad_above)[0] ovl_out, tf_out, ovl_grad, tf_grad = s.run([ovl_out, tf_out, ovl_grad, tf_grad]) assert np.allclose(ovl_out, tf_out) assert np.allclose(ovl_grad, tf_grad) test_grad(lambda x: neg(x), lambda x: tf.neg(x)) test_grad(lambda x: tanh(x), lambda x: tf.tanh(x)) test_grad(lambda x: sin(x), lambda x: tf.sin(x)) test_grad(lambda x: cos(x), lambda x: tf.cos(x)) test_grad(lambda x: tan(x), lambda x: tf.tan(x)) test_grad(lambda x: sigmoid(x), lambda x: tf.sigmoid(x))
def loss(y_true_cls, y_pred_cls, y_true_geo, y_pred_geo, training_mask): ''' define the loss used for training, contraning two part, the first part we use dice loss instead of weighted logloss, the second part is the iou loss defined in the paper :param y_true_cls: ground truth of text :param y_pred_cls: prediction os text :param y_true_geo: ground truth of geometry :param y_pred_geo: prediction of geometry :param training_mask: mask used in training, to ignore some text annotated by ### :return: ''' classification_loss = dice_coefficient(y_true_cls, y_pred_cls, training_mask) # scale classification loss to match the iou loss part classification_loss *= 0.01 # d1 -> top, d2->right, d3->bottom, d4->left d1_gt, d2_gt, d3_gt, d4_gt, theta_gt = tf.split(value=y_true_geo, num_or_size_splits=5, axis=3) d1_pred, d2_pred, d3_pred, d4_pred, theta_pred = tf.split(value=y_pred_geo, num_or_size_splits=5, axis=3) area_gt = (d1_gt + d3_gt) * (d2_gt + d4_gt) area_pred = (d1_pred + d3_pred) * (d2_pred + d4_pred) w_union = tf.minimum(d2_gt, d2_pred) + tf.minimum(d4_gt, d4_pred) h_union = tf.minimum(d1_gt, d1_pred) + tf.minimum(d3_gt, d3_pred) area_intersect = w_union * h_union area_union = area_gt + area_pred - area_intersect L_AABB = -tf.log((area_intersect + 1.0)/(area_union + 1.0)) L_theta = 1 - tf.cos(theta_pred - theta_gt) tf.summary.scalar('geometry_AABB', tf.reduce_mean(L_AABB * y_true_cls * training_mask)) tf.summary.scalar('geometry_theta', tf.reduce_mean(L_theta * y_true_cls * training_mask)) L_g = L_AABB + 20 * L_theta return tf.reduce_mean(L_g * y_true_cls * training_mask) + classification_loss
def phigrad(X, omegas, D): Z = tf.matmul(X, omegas) Zc = tf.cos(Z) Zs = tf.sin(Z) phiX = tf.concat([Zc, Zs], 1) / np.sqrt(D) phiXg = tf.concat([-omegas * Zs, omegas * Zc], 1) / np.sqrt(D) return phiX, phiXg
def get_box3d_corners_helper(centers, headings, sizes): """ TF layer. Input: (N,3), (N,), (N,3), Output: (N,8,3) """ #print '-----', centers N = centers.get_shape()[0].value l = tf.slice(sizes, [0,0], [-1,1]) # (N,1) w = tf.slice(sizes, [0,1], [-1,1]) # (N,1) h = tf.slice(sizes, [0,2], [-1,1]) # (N,1) #print l,w,h x_corners = tf.concat([l/2,l/2,-l/2,-l/2,l/2,l/2,-l/2,-l/2], axis=1) # (N,8) y_corners = tf.concat([h/2,h/2,h/2,h/2,-h/2,-h/2,-h/2,-h/2], axis=1) # (N,8) z_corners = tf.concat([w/2,-w/2,-w/2,w/2,w/2,-w/2,-w/2,w/2], axis=1) # (N,8) corners = tf.concat([tf.expand_dims(x_corners,1), tf.expand_dims(y_corners,1), tf.expand_dims(z_corners,1)], axis=1) # (N,3,8) #print x_corners, y_corners, z_corners c = tf.cos(headings) s = tf.sin(headings) ones = tf.ones([N], dtype=tf.float32) zeros = tf.zeros([N], dtype=tf.float32) row1 = tf.stack([c,zeros,s], axis=1) # (N,3) row2 = tf.stack([zeros,ones,zeros], axis=1) row3 = tf.stack([-s,zeros,c], axis=1) R = tf.concat([tf.expand_dims(row1,1), tf.expand_dims(row2,1), tf.expand_dims(row3,1)], axis=1) # (N,3,3) #print row1, row2, row3, R, N corners_3d = tf.matmul(R, corners) # (N,3,8) corners_3d += tf.tile(tf.expand_dims(centers,2), [1,1,8]) # (N,3,8) corners_3d = tf.transpose(corners_3d, perm=[0,2,1]) # (N,8,3) return corners_3d
def get_position_encoding( length, hidden_size, min_timescale=1.0, max_timescale=1.0e4): """Return positional encoding. Calculates the position encoding as a mix of sine and cosine functions with geometrically increasing wavelengths. Defined and formulized in Attention is All You Need, section 3.5. Args: length: Sequence length. hidden_size: Size of the min_timescale: Minimum scale that will be applied at each position max_timescale: Maximum scale that will be applied at each position Returns: Tensor with shape [length, hidden_size] """ position = tf.to_float(tf.range(length)) num_timescales = hidden_size // 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(position, 1) * tf.expand_dims(inv_timescales, 0) signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1) return signal
def tf_cheating_contcartpole(state, action): gravity = 9.8 masscart = 1.0 masspole = 0.1 total_mass = (masspole + masscart) length = 0.5 # actually half the pole's length polemass_length = (masspole * length) force_mag = 10.0 tau = 0.02 # seconds between state updates # Angle at which to fail the episode theta_threshold_radians = 12 * 2 * math.pi / 360 x_threshold = 2.4 x, x_dot, theta, theta_dot = tf.split(state, 4, axis=-1) done = tf.logical_or(x < -x_threshold, tf.logical_or(x > x_threshold, tf.logical_or(theta < -theta_threshold_radians, theta > theta_threshold_radians))) force = force_mag * action costheta = tf.cos(theta) sintheta = tf.sin(theta) temp = old_div((force + polemass_length * theta_dot * theta_dot * sintheta), total_mass) thetaacc = old_div((gravity * sintheta - costheta* temp), (length * (old_div(4.0,3.0) - masspole * costheta * costheta / total_mass))) xacc = temp - polemass_length * thetaacc * costheta / total_mass x = x + tau * x_dot x_dot = x_dot + tau * xacc theta = theta + tau * theta_dot theta_dot = theta_dot + tau * thetaacc state = tf.concat([x,x_dot,theta,theta_dot], -1) done = tf.squeeze(tf.cast(done, tf.float32), -1) reward = 1.0 - done done *= 0. return state, reward, done
def objective(self, params, data, labels=None): radius = tf.sqrt(tf.reduce_sum(params[0]**2)) rad_loss = tf.reduce_sum(1. / (radius + 1e-6) * data[:, 0]) sin_dist = params[0][1:] - tf.cos(params[0][:-1]) * np.pi sin_loss = tf.reduce_sum((sin_dist * data[:, 1:])**2) return rad_loss + sin_loss
def FormLStack(omega_output, deltat): # encoded_layer is [None, 2] # omega_output is [None, 1] if omega_output.shape[1] == 1: entry11 = tf.cos(omega_output*deltat) entry12 = tf.sin(omega_output*deltat) row1 = tf.concat([entry11, -entry12], axis=1) # [None, 2] row2 = tf.concat([entry12, entry11], axis=1) # [None, 2] elif omega_output.shape[1] == 2: scale = tf.exp(omega_output[:,1] * deltat) entry11 = tf.multiply(scale, tf.cos(omega_output[:,0]*deltat)) entry12 = tf.multiply(scale, tf.sin(omega_output[:,0]*deltat)) row1 = tf.stack([entry11, -entry12], axis=1) # [None, 2] row2 = tf.stack([entry12, entry11], axis=1) # [None, 2] Lstack = tf.stack([row1, row2], axis=2) # [None, 2, 2] put one row below other return Lstack
def distance_cutoff(self, d, cutoff, flags): """ Generate distance matrix with trainable cutoff """ # Cutoff with threshold Rc d_flag = flags * tf.sign(cutoff - d) d_flag = tf.nn.relu(d_flag) d_flag = d_flag * tf.expand_dims((1 - tf.eye(self.max_atoms)), 0) d = 0.5 * (tf.cos(np.pi * d / cutoff) + 1) return d * d_flag
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ Generate Angular Symmetry Function """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() max_atoms = self.max_atoms d_cutoff = in_layers[0].out_tensor d = in_layers[1].out_tensor atom_coordinates = in_layers[2].out_tensor if self.atomic_number_differentiated: atom_numbers = in_layers[3].out_tensor atom_number_embedded = tf.nn.embedding_lookup(self.atom_number_embedding, atom_numbers) vector_distances = tf.tile(tf.expand_dims(atom_coordinates, axis=2), (1, 1, max_atoms, 1)) - \ tf.tile(tf.expand_dims(atom_coordinates, axis=1), (1, max_atoms, 1, 1)) R_ij = tf.tile(tf.expand_dims(d, axis=3), (1, 1, 1, max_atoms)) R_ik = tf.tile(tf.expand_dims(d, axis=2), (1, 1, max_atoms, 1)) f_R_ij = tf.tile(tf.expand_dims(d_cutoff, axis=3), (1, 1, 1, max_atoms)) f_R_ik = tf.tile(tf.expand_dims(d_cutoff, axis=2), (1, 1, max_atoms, 1)) # Define angle theta = R_ij(Vector) dot R_ik(Vector)/R_ij(distance)/R_ik(distance) theta = tf.reduce_sum(tf.tile(tf.expand_dims(vector_distances, axis=3), (1, 1, 1, max_atoms, 1)) * \ tf.tile(tf.expand_dims(vector_distances, axis=2), (1, 1, max_atoms, 1, 1)), axis=4) theta = tf.div(theta, R_ij * R_ik + 1e-5) R_ij = tf.stack([R_ij] * self.length, axis=4) R_ik = tf.stack([R_ik] * self.length, axis=4) f_R_ij = tf.stack([f_R_ij] * self.length, axis=4) f_R_ik = tf.stack([f_R_ik] * self.length, axis=4) theta = tf.stack([theta] * self.length, axis=4) lambd = tf.reshape(self.lambd, (1, 1, 1, 1, -1)) zeta = tf.reshape(self.zeta, (1, 1, 1, 1, -1)) ita = tf.reshape(self.ita, (1, 1, 1, 1, -1)) Rs = tf.reshape(self.Rs, (1, 1, 1, 1, -1)) thetas = tf.reshape(self.thetas, (1, 1, 1, 1, -1)) out_tensor = tf.pow(1 + lambd * tf.cos(theta - thetas), zeta) * \ tf.exp(-ita * tf.square((R_ij + R_ik) / 2 - Rs)) * \ f_R_ij * f_R_ik * tf.pow(tf.constant(2.), 1 - zeta) if self.atomic_number_differentiated: out_tensors = [] for atom_type_j in self.atom_number_cases: for atom_type_k in self.atom_number_cases: selected_atoms = tf.stack([atom_number_embedded[:, :, atom_type_j]] * max_atoms, axis=2) * \ tf.stack([atom_number_embedded[:, :, atom_type_k]] * max_atoms, axis=1) selected_atoms = tf.expand_dims( tf.expand_dims(selected_atoms, axis=1), axis=4) out_tensors.append( tf.reduce_sum(out_tensor * selected_atoms, axis=[2, 3])) self.out_tensor = tf.concat(out_tensors, axis=2) else: self.out_tensor = tf.reduce_sum(out_tensor, axis=[2, 3])
def _network_template(self, state, num_quantiles): r"""Builds an Implicit Quantile ConvNet. Takes state and quantile as inputs and outputs state-action quantile values. Args: state: A `tf.placeholder` for the RL state. num_quantiles: int, number of quantile inputs. Returns: _network_type object containing quantile value outputs of the network. """ weights_initializer = slim.variance_scaling_initializer( factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True) state_net = tf.cast(state, tf.float32) state_net = tf.div(state_net, 255.) state_net = slim.conv2d( state_net, 32, [8, 8], stride=4, weights_initializer=weights_initializer) state_net = slim.conv2d( state_net, 64, [4, 4], stride=2, weights_initializer=weights_initializer) state_net = slim.conv2d( state_net, 64, [3, 3], stride=1, weights_initializer=weights_initializer) state_net = slim.flatten(state_net) state_net_size = state_net.get_shape().as_list()[-1] state_net_tiled = tf.tile(state_net, [num_quantiles, 1]) batch_size = state_net.get_shape().as_list()[0] quantiles_shape = [num_quantiles * batch_size, 1] quantiles = tf.random_uniform( quantiles_shape, minval=0, maxval=1, dtype=tf.float32) quantile_net = tf.tile(quantiles, [1, self.quantile_embedding_dim]) pi = tf.constant(math.pi) quantile_net = tf.cast(tf.range( 1, self.quantile_embedding_dim + 1, 1), tf.float32) * pi * quantile_net quantile_net = tf.cos(quantile_net) quantile_net = slim.fully_connected(quantile_net, state_net_size, weights_initializer=weights_initializer) # Hadamard product. net = tf.multiply(state_net_tiled, quantile_net) net = slim.fully_connected( net, 512, weights_initializer=weights_initializer) quantile_values = slim.fully_connected( net, self.num_actions, activation_fn=None, weights_initializer=weights_initializer) return self._get_network_type()(quantile_values=quantile_values, quantiles=quantiles)
def get_timing_signal_1d(self, length, channels): position = tf.to_float(tf.range(length)) num_timescales = channels // 2 log_timescale_increment = (math.log(float(self.max_timescale) / float(self.min_timescale)) / (tf.to_float(num_timescales) - 1)) inv_timescales = self.min_timescale * tf.exp(tf.to_float(tf.range(num_timescales)) * -log_timescale_increment) scaled_time = tf.expand_dims(position, 1) * tf.expand_dims(inv_timescales, 0) signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1) signal = tf.pad(signal, [[0, 0], [0, tf.mod(channels, 2)]]) signal = tf.reshape(signal, [1, length, channels]) return signal
def angular_symmetry(self, d_cutoff, d, atom_numbers, coordinates): """ Angular Symmetry Function """ max_atoms = self.max_atoms embedding = tf.eye(np.max(self.atom_cases) + 1) atom_numbers_embedded = tf.nn.embedding_lookup(embedding, atom_numbers) Rs = np.linspace(0., self.angular_cutoff, self.angular_length) ita = 3 / (Rs[1] - Rs[0])**2 thetas = np.linspace(0., np.pi, self.angular_length) zeta = float(self.angular_length**2) ita, zeta, Rs, thetas = np.meshgrid(ita, zeta, Rs, thetas) zeta = tf.cast(np.reshape(zeta, (1, 1, 1, 1, -1)), tf.float32) ita = tf.cast(np.reshape(ita, (1, 1, 1, 1, -1)), tf.float32) Rs = tf.cast(np.reshape(Rs, (1, 1, 1, 1, -1)), tf.float32) thetas = tf.cast(np.reshape(thetas, (1, 1, 1, 1, -1)), tf.float32) length = zeta.get_shape().as_list()[-1] vector_distances = tf.stack([coordinates] * max_atoms, 1) - tf.stack( [coordinates] * max_atoms, 2) R_ij = tf.stack([d] * max_atoms, axis=3) R_ik = tf.stack([d] * max_atoms, axis=2) f_R_ij = tf.stack([d_cutoff] * max_atoms, axis=3) f_R_ik = tf.stack([d_cutoff] * max_atoms, axis=2) # Define angle theta = arccos(R_ij(Vector) dot R_ik(Vector)/R_ij(distance)/R_ik(distance)) vector_mul = tf.reduce_sum(tf.stack([vector_distances] * max_atoms, axis=3) * \ tf.stack([vector_distances] * max_atoms, axis=2), axis=4) vector_mul = vector_mul * tf.sign(f_R_ij) * tf.sign(f_R_ik) theta = tf.acos(tf.math.divide(vector_mul, R_ij * R_ik + 1e-5)) R_ij = tf.stack([R_ij] * length, axis=4) R_ik = tf.stack([R_ik] * length, axis=4) f_R_ij = tf.stack([f_R_ij] * length, axis=4) f_R_ik = tf.stack([f_R_ik] * length, axis=4) theta = tf.stack([theta] * length, axis=4) out_tensor = tf.pow((1. + tf.cos(theta - thetas)) / 2., zeta) * \ tf.exp(-ita * tf.square((R_ij + R_ik) / 2. - Rs)) * f_R_ij * f_R_ik * 2 if self.atomic_number_differentiated: out_tensors = [] for id_j, atom_type_j in enumerate(self.atom_cases): for atom_type_k in self.atom_cases[id_j:]: selected_atoms = tf.stack([atom_numbers_embedded[:, :, atom_type_j]] * max_atoms, axis=2) * \ tf.stack([atom_numbers_embedded[:, :, atom_type_k]] * max_atoms, axis=1) selected_atoms = tf.expand_dims( tf.expand_dims(selected_atoms, axis=1), axis=4) out_tensors.append( tf.reduce_sum(out_tensor * selected_atoms, axis=(2, 3))) return tf.concat(out_tensors, axis=2) else: return tf.reduce_sum(out_tensor, axis=(2, 3))
def times_diag_tf(input_matrix, n_hidden, diag): input_re = input_matrix[:, :n_hidden] #okay so the first left half of the matrix is real numbers input_im = input_matrix[:, n_hidden:] #the right half is the imaginary numbers that correspond Re = tf.diag(tf.cos(diag)) Im = tf.diag(tf.sin(diag)) input_re_times_Re = tf.matmul(input_re, Re) #matmul is the equivalent of dot input_re_times_Im = tf.matmul(input_re, Im) input_im_times_Re = tf.matmul(input_im, Re) input_im_times_Im = tf.matmul(input_im, Im) return tf.concat(1, [input_re_times_Re - input_im_times_Im, input_re_times_Im + input_im_times_Re]) #this will combine two matrixes
def cosine_decay_with_warmup(global_step, learning_rate_base, total_steps, warmup_learning_rate=0.0, warmup_steps=0, hold_base_rate_steps=0): """Cosine decay schedule with warm up period. Cosine annealing learning rate as described in: Loshchilov and Hutter, SGDR: Stochastic Gradient Descent with Warm Restarts. ICLR 2017. https://arxiv.org/abs/1608.03983 In this schedule, the learning rate grows linearly from warmup_learning_rate to learning_rate_base for warmup_steps, then transitions to a cosine decay schedule. Args: global_step: int64 (scalar) tensor representing global step. learning_rate_base: base learning rate. total_steps: total number of training steps. warmup_learning_rate: initial learning rate for warm up. warmup_steps: number of warmup steps. hold_base_rate_steps: Optional number of steps to hold base learning rate before decaying. Returns: a (scalar) float tensor representing learning rate. Raises: ValueError: if warmup_learning_rate is larger than learning_rate_base, or if warmup_steps is larger than total_steps. """ if total_steps < warmup_steps: raise ValueError('total_steps must be larger or equal to ' 'warmup_steps.') learning_rate = 0.5 * learning_rate_base * (1 + tf.cos( np.pi * (tf.cast(global_step, tf.float32) - warmup_steps - hold_base_rate_steps ) / float(total_steps - warmup_steps - hold_base_rate_steps))) if hold_base_rate_steps > 0: learning_rate = tf.where(global_step > warmup_steps + hold_base_rate_steps, learning_rate, learning_rate_base) if warmup_steps > 0: if learning_rate_base < warmup_learning_rate: raise ValueError('learning_rate_base must be larger or equal to ' 'warmup_learning_rate.') slope = (learning_rate_base - warmup_learning_rate) / warmup_steps warmup_rate = slope * tf.cast(global_step, tf.float32) + warmup_learning_rate learning_rate = tf.where(global_step < warmup_steps, warmup_rate, learning_rate) return tf.where(global_step > total_steps, 0.0, learning_rate, name='learning_rate')
def Position_Embedding(inputs, position_size): batch_size,seq_len = tf.shape(inputs)[0],tf.shape(inputs)[1] position_j = 1. / tf.pow(10000., \ 2 * tf.range(position_size / 2, dtype=tf.float32 \ ) / position_size) position_j = tf.expand_dims(position_j, 0) position_i = tf.range(tf.cast(seq_len, tf.float32), dtype=tf.float32) position_i = tf.expand_dims(position_i, 1) position_ij = tf.matmul(position_i, position_j) position_ij = tf.concat([tf.cos(position_ij), tf.sin(position_ij)], 1) position_embedding = tf.expand_dims(position_ij, 0) \ + tf.zeros((batch_size, seq_len, position_size)) return position_embedding
def get_transformation_matrix(transform): """Converts [tx, ty, tz, rx, ry, rz] to a transform matrix.""" rx = transform[3] ry = transform[4] rz = transform[5] rz = tf.clip_by_value(rz, -np.pi, np.pi) ry = tf.clip_by_value(ry, -np.pi, np.pi) rx = tf.clip_by_value(rx, -np.pi, np.pi) cos_rx = tf.cos(rx) sin_rx = tf.sin(rx) rotx_1 = tf.stack([1.0, 0.0, 0.0]) rotx_2 = tf.stack([0.0, cos_rx, -sin_rx]) rotx_3 = tf.stack([0.0, sin_rx, cos_rx]) xmat = tf.stack([rotx_1, rotx_2, rotx_3]) cos_ry = tf.cos(ry) sin_ry = tf.sin(ry) roty_1 = tf.stack([cos_ry, 0.0, sin_ry]) roty_2 = tf.stack([0.0, 1.0, 0.0]) roty_3 = tf.stack([-sin_ry, 0.0, cos_ry]) ymat = tf.stack([roty_1, roty_2, roty_3]) cos_rz = tf.cos(rz) sin_rz = tf.sin(rz) rotz_1 = tf.stack([cos_rz, -sin_rz, 0.0]) rotz_2 = tf.stack([sin_rz, cos_rz, 0.0]) rotz_3 = tf.stack([0.0, 0.0, 1.0]) zmat = tf.stack([rotz_1, rotz_2, rotz_3]) rotate = tf.matmul(tf.matmul(xmat, ymat), zmat) translate = transform[:3] mat = tf.concat([rotate, tf.expand_dims(translate, 1)], axis=1) hom_filler = tf.constant([0.0, 0.0, 0.0, 1.0], shape=[1, 4], dtype=tf.float32) mat = tf.concat([mat, hom_filler], axis=0) return mat
def testVonMisesSampleVarianceUniform(self): von_mises = tfd.VonMises(self.make_tensor(1.0), self.make_tensor(0.0)) n = 10000 samples = von_mises.sample(n, seed=12345) # For circular uniform distribution, the mean is not well-defined, # so only checking the variance. expected_variance = 1. standardized_samples = samples - tf.expand_dims(von_mises.mean(), 0) actual_variance = 1. - tf.reduce_mean(tf.cos(standardized_samples), axis=0) self.assertAllClose( expected_variance, self.evaluate(actual_variance), rtol=0.1)
def objective(self, params, data=None, labels=None): x, y = tf.split(params[0], 2, axis=0) # Define some constants. a = 1. b = 5.1 / (4. * np.pi ** 2) c = 5 / np.pi r = 6. s = 10. t = 1 / (8. * np.pi) # Evaluate the function. obj = a * (y - b * x ** 2 + c * x - r) ** 2 + s * (1 - t) * tf.cos(x) + s return tf.squeeze(obj)
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ Generate distance matrix for BPSymmetryFunction with trainable cutoff """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() d = in_layers[0].out_tensor d_flag = in_layers[1].out_tensor # Cutoff with threshold Rc d_flag = d_flag * tf.nn.relu(tf.sign(self.Rc - d)) d = 0.5 * (tf.cos(np.pi * d / self.Rc) + 1) out_tensor = d * d_flag out_tensor = out_tensor * tf.expand_dims((1 - tf.eye(self.max_atoms)), 0) self.out_tensor = out_tensor
def rotate_points(orig_points, angle, w, h): """Return rotated points Args: orig_points: 'Tensor' with shape [N,2], each entry is point (x,y) angle: rotate radians Returns: 'Tensor' with shape [N,2], with rotated points """ # rotation rotate_mat = tf.stack([[tf.cos(angle) / w, tf.sin(angle) / h], [-tf.sin(angle) / w, tf.cos(angle) / h]]) # shift coord orig_points = tf.subtract(orig_points, 0.5) orig_points = tf.stack([orig_points[:, 0] * w, orig_points[:, 1] * h], axis=1) print(orig_points) rotated_points = tf.matmul(orig_points, rotate_mat) + 0.5 return rotated_points
def create_tensor(self, in_layers=None, set_tensors=True, **kwargs): """ Generate Angular Symmetry Function """ if in_layers is None: in_layers = self.in_layers in_layers = convert_to_layers(in_layers) self.build() max_atoms = self.max_atoms d_cutoff = in_layers[0].out_tensor d = in_layers[1].out_tensor atom_coordinates = in_layers[2].out_tensor vector_distances = tf.tile(tf.expand_dims(atom_coordinates, axis=2), (1, 1, max_atoms, 1)) - \ tf.tile(tf.expand_dims(atom_coordinates, axis=1), (1, max_atoms, 1, 1)) R_ij = tf.tile(tf.expand_dims(d, axis=3), (1, 1, 1, max_atoms)) R_ik = tf.tile(tf.expand_dims(d, axis=2), (1, 1, max_atoms, 1)) R_jk = tf.tile(tf.expand_dims(d, axis=1), (1, max_atoms, 1, 1)) f_R_ij = tf.tile(tf.expand_dims(d_cutoff, axis=3), (1, 1, 1, max_atoms)) f_R_ik = tf.tile(tf.expand_dims(d_cutoff, axis=2), (1, 1, max_atoms, 1)) f_R_jk = tf.tile(tf.expand_dims(d_cutoff, axis=1), (1, max_atoms, 1, 1)) # Define angle theta = R_ij(Vector) dot R_ik(Vector)/R_ij(distance)/R_ik(distance) theta = tf.reduce_sum(tf.tile(tf.expand_dims(vector_distances, axis=3), (1, 1, 1, max_atoms, 1)) * \ tf.tile(tf.expand_dims(vector_distances, axis=2), (1, 1, max_atoms, 1, 1)), axis=4) theta = tf.math.divide(theta, R_ij * R_ik + 1e-5) R_ij = tf.stack([R_ij] * self.length, axis=4) R_ik = tf.stack([R_ik] * self.length, axis=4) R_jk = tf.stack([R_jk] * self.length, axis=4) f_R_ij = tf.stack([f_R_ij] * self.length, axis=4) f_R_ik = tf.stack([f_R_ik] * self.length, axis=4) f_R_jk = tf.stack([f_R_jk] * self.length, axis=4) theta = tf.stack([theta] * self.length, axis=4) lambd = tf.reshape(self.lambd, (1, 1, 1, 1, -1)) zeta = tf.reshape(self.zeta, (1, 1, 1, 1, -1)) ita = tf.reshape(self.ita, (1, 1, 1, 1, -1)) out_tensor = tf.pow(1 + lambd * tf.cos(theta), zeta) * \ tf.exp(-ita * (tf.square(R_ij) + tf.square(R_ik) + tf.square(R_jk))) * \ f_R_ij * f_R_ik * f_R_jk out_tensor = tf.reduce_sum(out_tensor, axis=[2, 3]) * \ tf.pow(tf.constant(2.), 1 - tf.reshape(self.zeta, (1, 1, -1))) if set_tensors: self.out_tensor = out_tensor return out_tensor
def gaussian(config, gan, net): z_dim = net.get_shape().as_list()[-1] net = (net + 1) / 2 if len(gan.ops.shape(net)) == 4: za = tf.slice(net, [0,0,0,0], [gan.batch_size(), -1, -1, z_dim//2]) zb = tf.slice(net, [0,0,0,z_dim//2], [gan.batch_size(), -1, -1, z_dim//2]) else: za = tf.slice(net, [0,0], [gan.batch_size(), z_dim//2]) zb = tf.slice(net, [0,z_dim//2], [gan.batch_size(), z_dim//2]) pi = np.pi ra = tf.sqrt(-2 * tf.log(za+TINY))*tf.cos(2*pi*zb) rb = tf.sqrt(-2 * tf.log(za+TINY))*tf.sin(2*pi*zb) return tf.reshape(tf.concat(axis=len(net.get_shape())-1, values=[ra, rb]), net.get_shape())
def get_flow(t, theta, map_size, name_scope='gen_flow'): """ Rotates the map by theta and translates the rotated map by t. Assume that the robot rotates by an angle theta and then moves forward by translation t. This function returns the flow field field. For every pixel in the new image it tells us which pixel in the original image it came from: NewI(x, y) = OldI(flow_x(x,y), flow_y(x,y)). Assume there is a point p in the original image. Robot rotates by R and moves forward by t. p1 = Rt*p; p2 = p1 - t; (the world moves in opposite direction. So, p2 = Rt*p - t, thus p2 came from R*(p2+t), which is what this function calculates. t: ... x 2 (translation for B batches of N motions each). theta: ... x 1 (rotation for B batches of N motions each). Output: ... x map_size x map_size x 2 """ with tf.name_scope(name_scope): tx, ty = tf.unstack(tf.reshape(t, shape=[-1, 1, 1, 1, 2]), axis=4) theta = tf.reshape(theta, shape=[-1, 1, 1, 1]) c = tf.constant((map_size-1.)/2., dtype=tf.float32) x, y = np.meshgrid(np.arange(map_size), np.arange(map_size)) x = tf.constant(x[np.newaxis, :, :, np.newaxis], dtype=tf.float32, name='x', shape=[1, map_size, map_size, 1]) y = tf.constant(y[np.newaxis, :, :, np.newaxis], dtype=tf.float32, name='y', shape=[1,map_size, map_size, 1]) x = x-(-tx+c) y = y-(-ty+c) sin_theta = tf.sin(theta) cos_theta = tf.cos(theta) xr = cos_theta*x - sin_theta*y yr = sin_theta*x + cos_theta*y xr = xr + c yr = yr + c flow = tf.stack([xr, yr], axis=-1) sh = tf.unstack(tf.shape(t), axis=0) sh = tf.stack(sh[:-1]+[tf.constant(_, dtype=tf.int32) for _ in [map_size, map_size, 2]]) flow = tf.reshape(flow, shape=sh) return flow
def sample_homography(shape, perspective=True, scaling=True, rotation=True, translation=True, n_scales=5, n_angles=25, scaling_amplitude=0.1, perspective_amplitude_x=0.1, perspective_amplitude_y=0.1, patch_ratio=0.5, max_angle=pi / 2, allow_artifacts=False, translation_overflow=0.): """Sample a random valid homography. Computes the homography transformation between a random patch in the original image and a warped projection with the same image size. As in `tf.contrib.image.transform`, it maps the output point (warped patch) to a transformed input point (original patch). The original patch, which is initialized with a simple half-size centered crop, is iteratively projected, scaled, rotated and translated. Arguments: shape: A rank-2 `Tensor` specifying the height and width of the original image. perspective: A boolean that enables the perspective and affine transformations. scaling: A boolean that enables the random scaling of the patch. rotation: A boolean that enables the random rotation of the patch. translation: A boolean that enables the random translation of the patch. n_scales: The number of tentative scales that are sampled when scaling. n_angles: The number of tentatives angles that are sampled when rotating. scaling_amplitude: Controls the amount of scale. perspective_amplitude_x: Controls the perspective effect in x direction. perspective_amplitude_y: Controls the perspective effect in y direction. patch_ratio: Controls the size of the patches used to create the homography. max_angle: Maximum angle used in rotations. allow_artifacts: A boolean that enables artifacts when applying the homography. translation_overflow: Amount of border artifacts caused by translation. Returns: A `Tensor` of shape `[1, 8]` corresponding to the flattened homography transform. """ # Corners of the output image pts1 = tf.stack([[0., 0.], [0., 1.], [1., 1.], [1., 0.]], axis=0) # Corners of the input patch margin = (1 - patch_ratio) / 2 pts2 = margin + tf.constant([[0, 0], [0, patch_ratio], [patch_ratio, patch_ratio], [patch_ratio, 0]], tf.float32) # Random perspective and affine perturbations if perspective: if not allow_artifacts: perspective_amplitude_x = min(perspective_amplitude_x, margin) perspective_amplitude_y = min(perspective_amplitude_y, margin) perspective_displacement = tf.truncated_normal([1], 0., perspective_amplitude_y) h_displacement_left = tf.truncated_normal([1], 0., perspective_amplitude_x) h_displacement_right = tf.truncated_normal([1], 0., perspective_amplitude_x) pts2 += tf.stack([ tf.concat([h_displacement_left, perspective_displacement], 0), tf.concat([h_displacement_left, -perspective_displacement], 0), tf.concat([h_displacement_right, perspective_displacement], 0), tf.concat([h_displacement_right, -perspective_displacement], 0) ]) # Random scaling # sample several scales, check collision with borders, randomly pick a valid one if scaling: scales = tf.concat( [[1.], tf.truncated_normal([n_scales], 1, scaling_amplitude / 2)], 0) center = tf.reduce_mean(pts2, axis=0, keepdims=True) scaled = tf.expand_dims(pts2 - center, axis=0) * tf.expand_dims( tf.expand_dims(scales, 1), 1) + center if allow_artifacts: valid = tf.range(n_scales) # all scales are valid except scale=1 else: valid = tf.where( tf.reduce_all((scaled >= 0.) & (scaled < 1.), [1, 2]))[:, 0] idx = valid[tf.random_uniform((), maxval=tf.shape(valid)[0], dtype=tf.int32)] pts2 = scaled[idx] # Random translation if translation: t_min, t_max = tf.reduce_min(pts2, axis=0), tf.reduce_min(1 - pts2, axis=0) if allow_artifacts: t_min += translation_overflow t_max += translation_overflow pts2 += tf.expand_dims(tf.stack([ tf.random_uniform((), -t_min[0], t_max[0]), tf.random_uniform((), -t_min[1], t_max[1]) ]), axis=0) # Random rotation # sample several rotations, check collision with borders, randomly pick a valid one if rotation: angles = tf.lin_space(tf.constant(-max_angle), tf.constant(max_angle), n_angles) angles = tf.concat([angles, [0.]], axis=0) # in case no rotation is valid center = tf.reduce_mean(pts2, axis=0, keepdims=True) rot_mat = tf.reshape( tf.stack([ tf.cos(angles), -tf.sin(angles), tf.sin(angles), tf.cos(angles) ], axis=1), [-1, 2, 2]) rotated = tf.matmul( tf.tile(tf.expand_dims(pts2 - center, axis=0), [n_angles + 1, 1, 1]), rot_mat) + center if allow_artifacts: valid = tf.range(n_angles) # all angles are valid, except angle=0 else: valid = tf.where( tf.reduce_all((rotated >= 0.) & (rotated < 1.), axis=[1, 2]))[:, 0] idx = valid[tf.random_uniform((), maxval=tf.shape(valid)[0], dtype=tf.int32)] pts2 = rotated[idx] # Rescale to actual size shape = tf.to_float(shape[::-1]) # different convention [y, x] pts1 *= tf.expand_dims(shape, axis=0) pts2 *= tf.expand_dims(shape, axis=0) def ax(p, q): return [p[0], p[1], 1, 0, 0, 0, -p[0] * q[0], -p[1] * q[0]] def ay(p, q): return [0, 0, 0, p[0], p[1], 1, -p[0] * q[1], -p[1] * q[1]] a_mat = tf.stack([f(pts1[i], pts2[i]) for i in range(4) for f in (ax, ay)], axis=0) p_mat = tf.transpose( tf.stack([[pts2[i][j] for i in range(4) for j in range(2)]], axis=0)) homography = tf.transpose(tf.matrix_solve_ls(a_mat, p_mat, fast=True)) return homography
def mesh(): vertices, faces = make_cylinder(0.2, 0.75, 0.1, 0., 10) vertices = np.float32( np.concatenate([vertices, np.ones([len(vertices), 1])], axis=1)) w = 48 h = 36 rotation_xy = tf.placeholder(tf.float32) view_matrix_1 = tf.convert_to_tensor( [[0.5 * tf.cos(rotation_xy), 0.5 * -tf.sin(rotation_xy), 0., 0.], [0.5 * tf.sin(rotation_xy), 0.5 * tf.cos(rotation_xy), 0., 0.], [0., 0., 0.5, 0.], [0., 0., 0., 1.]]) translation = tf.placeholder(tf.float32) view_matrix_2 = tf.stack([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], tf.concat([translation, [1.]], axis=0)]) if True: # use splitting vertex_count = len(faces) * 3 vertices, faces = dirt.lighting.split_vertices_by_face(vertices, faces) else: vertex_count = len(vertices) projection_matrix = dirt.matrices.perspective_projection( 0.1, 20., 0.2, float(h) / w) projected_vertices = tf.matmul( tf.matmul(tf.matmul(vertices, view_matrix_1), view_matrix_2), projection_matrix) bgcolor = tf.placeholder(tf.float32, [3]) vertex_color = tf.placeholder(tf.float32, [3]) vertex_colors = tf.concat([ tf.tile(vertex_color[np.newaxis, :], [75, 1]), np.random.uniform(size=[vertex_count - 75, 3]) ], axis=0) im = dirt.rasterise_ops.rasterise(tf.concat([ tf.tile(bgcolor[np.newaxis, np.newaxis, :], [h // 2, w, 1]), tf.ones([h // 2, w, 3]) ], axis=0), projected_vertices, vertex_colors, faces, height=h, width=w, channels=3) ims = dirt.rasterise_ops.rasterise_batch( tf.tile( tf.constant([[0., 0., 0.], [0., 0., 1.]])[:, np.newaxis, np.newaxis, :], [1, h, w, 1]), tf.tile(projected_vertices[np.newaxis, ...], [2, 1, 1]), np.random.uniform(size=[2, vertex_count, 3]), tf.tile(faces[np.newaxis, ...], [2, 1, 1]), height=h, width=w, channels=3) d_loss_by_pixels = tf.placeholder(tf.float32, [h, w, 3]) [gt, gr, gb, gc] = tf.gradients(im, [translation, rotation_xy, bgcolor, vertex_color], d_loss_by_pixels) ds_loss_by_pixels = tf.placeholder(tf.float32, [2, h, w, 3]) [gst, gsr] = tf.gradients(ims, [translation, rotation_xy], ds_loss_by_pixels) session = tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions( allow_growth=True))) with session.as_default(): tf.global_variables_initializer().run() gx_im = np.empty([h, w, 3], dtype=np.float32) gy_im = np.empty([h, w, 3], dtype=np.float32) gz_im = np.empty([h, w, 3], dtype=np.float32) gr_im = np.empty([h, w, 3], dtype=np.float32) gb_im = np.empty([h, w, 3], dtype=np.float32) gc_im = np.empty([h, w, 3], dtype=np.float32) for y in range(h): for x in range(w): for c in range(3): pixel_indicator = np.zeros([h, w, 3], dtype=np.float32) pixel_indicator[y, x, c] = 1 [[gx_im[y, x, c], gy_im[y, x, c], gz_im[y, x, c]], gr_im[y, x, c], [gb_im[y, x, c], _, _], [gc_im[y, x, c], _, _]] = \ session.run([gt, gr, gb, gc], {d_loss_by_pixels: pixel_indicator, translation: [0., 0., -0.25], rotation_xy: 0., bgcolor: [0.4, 0.2, 0.2], vertex_color: [0.7, 0.3, 0.6]}) print('.', end='') print() gsx_im = np.empty([2, h, w, 3], dtype=np.float32) gsy_im = np.empty([2, h, w, 3], dtype=np.float32) gsz_im = np.empty([2, h, w, 3], dtype=np.float32) gsr_im = np.empty([2, h, w, 3], dtype=np.float32) for iib in range(2): print(iib + 1) for y in range(h): for x in range(w): for c in range(3): pixel_indicator = np.zeros([2, h, w, 3], dtype=np.float32) pixel_indicator[iib, y, x, c] = 1 [[ gsx_im[iib, y, x, c], gsy_im[iib, y, x, c], gsz_im[iib, y, x, c] ], gsr_im[iib, y, x, c]] = session.run( [gst, gsr], { ds_loss_by_pixels: pixel_indicator, translation: [0., 0., -1.], rotation_xy: 0.5 }) print('.', end='') print() cv2.imshow( 'im', im.eval({ translation: [0., 0., -0.25], rotation_xy: 0., bgcolor: [0.6, 0.2, 0.2], vertex_color: [0.7, 0.3, 0.6] })) cv2.imshow( 'ims', np.concatenate(ims.eval({ translation: [0., 0., -0.25], rotation_xy: 0.5 }), axis=1)) g_im = np.concatenate( [gx_im, gy_im, gz_im, gr_im * 3., gb_im * 30., gc_im * 50.], axis=1) g_im = (g_im - np.min(g_im)) / (np.max(g_im) - np.min(g_im)) cv2.imshow('grad', g_im) gs_im = np.concatenate(np.concatenate( [gsx_im, gsy_im, gsz_im, gsr_im * 3.], axis=2), axis=0) gs_im = (gs_im - np.min(gs_im)) / (np.max(gs_im) - np.min(gs_im)) cv2.imshow('grads', gs_im) cv2.waitKey(0)
def loss(self, results, inputs): scores, bboxes, dirs = results gt_labels = inputs['labels'] gt_bboxes = inputs['bboxes'] # generate and filter bboxes target_bboxes, target_idx, pos_idx, neg_idx = self.bbox_head.assign_bboxes( bboxes, gt_bboxes) avg_factor = pos_idx.shape[0] # classification loss scores = tf.reshape(tf.transpose(scores, (0, 2, 3, 1)), (-1, self.bbox_head.num_classes)) target_labels = tf.fill((scores.shape[0],), tf.constant(self.bbox_head.num_classes, dtype=gt_labels.dtype)) gt_label = tf.gather(gt_labels, target_idx) target_labels = tf.tensor_scatter_nd_update( target_labels, tf.expand_dims(pos_idx, axis=-1), gt_label) loss_cls = self.loss_cls( tf.gather(scores, tf.concat([pos_idx, neg_idx], axis=0)), tf.gather(target_labels, tf.concat([pos_idx, neg_idx], axis=0)), avg_factor=avg_factor) # remove invalid labels cond = (gt_label >= 0) & (gt_label < self.bbox_head.num_classes) pos_idx = tf.boolean_mask(pos_idx, cond) target_idx = tf.boolean_mask(target_idx, cond) target_bboxes = tf.boolean_mask(target_bboxes, cond) bboxes = tf.reshape(tf.transpose(bboxes, (0, 2, 3, 1)), (-1, self.bbox_head.box_code_size)) bboxes = tf.gather(bboxes, pos_idx) dirs = tf.reshape(tf.transpose(dirs, (0, 2, 3, 1)), (-1, 2)) dirs = tf.gather(dirs, pos_idx) if len(pos_idx) > 0: # direction classification loss # to discrete bins target_dirs = tf.gather(gt_bboxes, target_idx)[:, -1] target_dirs = limit_period(target_dirs, 0, 2 * np.pi) target_dirs = tf.cast(target_dirs / np.pi, tf.int32) % 2 loss_dir = self.loss_dir(dirs, target_dirs, avg_factor=avg_factor) # bbox loss # sinus difference transformation r0 = tf.sin(bboxes[:, -1:]) * tf.cos(target_bboxes[:, -1:]) r1 = tf.cos(bboxes[:, -1:]) * tf.sin(target_bboxes[:, -1:]) bboxes = tf.concat([bboxes[:, :-1], r0], axis=-1) target_bboxes = tf.concat([target_bboxes[:, :-1], r1], axis=-1) loss_bbox = self.loss_bbox(bboxes, target_bboxes, avg_factor=avg_factor) else: loss_bbox = tf.reduce_sum(bboxes) loss_dir = tf.reduce_sum(dirs) return { 'loss_cls': loss_cls, 'loss_bbox': loss_bbox, 'loss_dir': loss_dir }
# 浮点除 show_values(tf.truediv(3, 4), "tf.truediv(3,4)") # 浮点取整除 show_values(tf.floordiv(3.0, 4.0), "tf.floordiv(3.0,4.0)") # 取模 show_values(tf.mod(22.0, 5.0), "tf.mod(22.0,5.0)") # 张量点积--Compute the pairwise cross product,必须是三维向量 # 两个向量的叉乘,又叫向量积、外积、叉积,叉乘的运算结果是一个向量而不是一个标量。 # 两个向量的叉积与这两个向量组成的坐标平面垂直。 show_values(tf.cross([1., 0., 0.], [0., 1., 0.]), "tf.cross([1., 0., 0.], [0., 1., 0.])") # show_values("tf.cross([1., 0., 0.,0.], [0., 1., 0.,0.])", tf.cross([1., 0., 0., 0.], [0., 1., 0., 0.])) # P11,数学函数列表 show_values(tf.div(tf.sin(3.1416 / 4.), tf.cos(3.1416 / 4.)), "tan(pi/4) = 1 = tf.div(tf.sin(3.1416/4.),tf.cos(3.1416/4.))") test_nums = range(15) # 自定义函数 # 3x^2-x+10,x=11,=> def custom_polynomial(value): return (tf.subtract(3 * tf.square(value), value) + 10) show_values(custom_polynomial(11), "3x^2-x+10,x=11=>") # What should we get with list comprehension expected_output = [3 * x * x - x + 10 for x in test_nums]
def _angle_to_unit_vector(angle): return tf.stack([tf.cos(angle), tf.sin(angle)], axis=-1)
def __init__(self): self.__anchor_per_scale = cfg.ANCHOR_PER_SCALE self.__classes = cfg.CLASSES self.__num_classes = len(self.__classes) self.__learn_rate_init = cfg.LEARN_RATE_INIT self.__learn_rate_end = cfg.LEARN_RATE_END self.__max_periods = cfg.MAX_PERIODS self.__periods_for_step0 = cfg.PERIODS_FOR_STEP0 self.__warmup_periods = cfg.WARMUP_PERIODS self.__weights_dir = cfg.WEIGHTS_DIR self.__weights_file = cfg.WEIGHTS_FILE self.__time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) self.__log_dir = os.path.join(cfg.LOG_DIR, 'train', self.__time) self.__moving_ave_decay = cfg.MOVING_AVE_DECAY self.__max_bbox_per_scale = cfg.MAX_BBOX_PER_SCALE self.__train_data = Data('train') self.__test_data = Data('test') self.__steps_per_period = len(self.__train_data) with tf.name_scope('input'): self.__input_data = tf.placeholder(dtype=tf.float32, name='input_data') self.__label_sbbox = tf.placeholder(dtype=tf.float32, name='label_sbbox') self.__label_mbbox = tf.placeholder(dtype=tf.float32, name='label_mbbox') self.__label_lbbox = tf.placeholder(dtype=tf.float32, name='label_lbbox') self.__sbboxes = tf.placeholder(dtype=tf.float32, name='sbboxes') self.__mbboxes = tf.placeholder(dtype=tf.float32, name='mbboxes') self.__lbboxes = tf.placeholder(dtype=tf.float32, name='lbboxes') self.__training = tf.placeholder(dtype=tf.bool, name='training') self.__yolo = YOLO_V3(self.__training) self.__conv_sbbox, self.__conv_mbbox, self.__conv_lbbox, self.__pred_sbbox, \ self.__pred_mbbox, self.__pred_lbbox = self.__yolo.build_nework(self.__input_data) self.__net_var = tf.global_variables() logging.info('Load Weight:') for var in self.__net_var: logging.info(var.op.name) self.__loss = self.__yolo.loss(self.__conv_sbbox, self.__conv_mbbox, self.__conv_lbbox, self.__pred_sbbox, self.__pred_mbbox, self.__pred_lbbox, self.__label_sbbox, self.__label_mbbox, self.__label_lbbox, self.__sbboxes, self.__mbboxes, self.__lbboxes) with tf.name_scope('optimize'): with tf.name_scope('learning_rate'): self.__global_step = tf.Variable(1.0, dtype=tf.float64, trainable=False, name='global_step') warmup_steps = tf.constant(self.__warmup_periods * self.__steps_per_period, dtype=tf.float64, name='warmup_steps') train_steps = tf.constant(self.__max_periods * self.__steps_per_period, dtype=tf.float64, name='train_steps') self.__learning_rate = tf.cond( pred=self.__global_step < warmup_steps, true_fn=lambda: self.__global_step / warmup_steps * self. __learn_rate_init, false_fn=lambda: self.__learn_rate_end + 0.5 * (self.__learn_rate_init - self.__learn_rate_end) * (1 + tf.cos((self.__global_step - warmup_steps) / (train_steps - warmup_steps) * np.pi))) global_step_update = tf.assign_add(self.__global_step, 1.0) moving_ave = tf.train.ExponentialMovingAverage( self.__moving_ave_decay).apply(tf.trainable_variables()) self.__trainable_var_list = [] for var in tf.trainable_variables(): var_name = var.op.name var_name_mess = str(var_name).split('/') if var_name_mess[0] in [ 'conv_sbbox', 'conv_mbbox', 'conv_lbbox' ]: self.__trainable_var_list.append(var) optimize0 = tf.train.GradientDescentOptimizer(self.__learning_rate).\ minimize(self.__loss, var_list=self.__trainable_var_list) with tf.control_dependencies([optimize0, global_step_update]): with tf.control_dependencies([moving_ave]): self.__train_op_with_frozen_variables = tf.no_op() #tf.no_op()表示执行完moving_ave之后什么也不执行 optimize1 = tf.train.AdamOptimizer(self.__learning_rate). \ minimize(self.__loss, var_list=tf.trainable_variables()) with tf.control_dependencies([optimize1, global_step_update]): with tf.control_dependencies([moving_ave]): self.__train_op_with_all_variables = tf.no_op() self.__train_op = self.__train_op_with_frozen_variables logging.info( 'Default trian step0 is freeze the weight of darknet') for var in self.__trainable_var_list: logging.info('\t' + str(var.op.name).ljust(50) + str(var.shape)) with tf.name_scope('load_save'): net_vars = tf.global_variables() self.__load = tf.train.Saver(self.__net_var) self.__save = tf.train.Saver(var_list=net_vars, max_to_keep=self.__max_periods) with tf.name_scope('summary'): self.__loss_ave = tf.Variable(0, dtype=tf.float32, trainable=False) tf.summary.scalar('loss_ave', self.__loss_ave) tf.summary.scalar('learn_rate', self.__learning_rate) self.__summary_op = tf.summary.merge_all() self.__summary_writer = tf.summary.FileWriter(self.__log_dir) self.__summary_writer.add_graph(tf.get_default_graph()) self.__sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True))
def get_input(): files = tf.train.match_filenames_once(DATA_PATH) filename_queue = tf.train.string_input_producer(files, shuffle=False) #reader = tf.TFRecordReader() #_,serialized_example = reader.read(filename_queue) num_readers = 12 examples_queue = tf.RandomShuffleQueue(capacity=10000 + 3 * BATCH_SIZE, min_after_dequeue=10000, dtypes=[tf.string]) enqueue_ops = [] for _ in range(num_readers): reader = tf.TFRecordReader() _, value = reader.read(filename_queue) enqueue_ops.append(examples_queue.enqueue([value])) tf.train.queue_runner.add_queue_runner( tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops)) serialized_example = examples_queue.dequeue() images_and_labels = [] num_preprocess_threads = 12 for thread_id in range(num_preprocess_threads): features = tf.parse_single_example(serialized_example, features={ 'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64) }) decoded_image = tf.decode_raw(features['image'], tf.uint8) reshaped_image = tf.reshape(decoded_image, [227, 227, 3]) retyped_image = tf.image.convert_image_dtype(reshaped_image, tf.float32) retyped_image = tf.subtract(retyped_image, 0.5) retyped_image = tf.multiply(retyped_image, 2.0) theta = tf.cast(features['label'], tf.float32) label = tf.stack( [tf.sin(theta * np.pi / 180.), tf.cos(theta * np.pi / 180.)]) images_and_labels.append([retyped_image, label]) min_after_dequeue = 10000 capacity = min_after_dequeue + 2 * num_preprocess_threads * BATCH_SIZE image_batch, label_batch = tf.train.shuffle_batch_join( images_and_labels, batch_size=BATCH_SIZE, capacity=capacity, min_after_dequeue=min_after_dequeue) return image_batch, label_batch
def test_Cos(self): t = tf.cos(self.random(4, 3)) self.check(t)
def tf_reward(self, obs, acts, next_obs): x = obs[:, 0] theta = obs[:, 2] return tf.cos(theta) - 0.01 * tf.square(x)
x = tf.placeholder(tf.float32,[None,in_units]) keep_prob = tf.placeholder(tf.float32) hidden1 = tf.nn.relu(tf.matmul(x,W1)+ b1)#激活函数 hidden1_drop = tf.nn.dropout(hidden1,keep_prob)#正则化,神经元失活 hidden2 = tf.nn.softmax(tf.matmul(hidden1_drop,W2)+b2) y_pre = tf.matmul(x,W3) #训练部分 y_ture = tf.placeholder(tf.float32, [None, 18]) #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ture * tf.log(y_pre), reduction_indices=[1]))#损失函数 #cross_entropy = tf.nn.softmax_cross_entropy_with_logits(y_ture,y_pre) #cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_ture,logits=y_pre)) cross_entropy = tf.sqrt(tf.add(tf.square(tf.matmul(y_pre[0]-y_ture[0],tf.cos(tf.matmul(0.5,y_pre[1]-y_ture[1])))),tf.square(y_pre[1]-y_ture[1]))) train_step = tf.train.GradientDescentOptimizer(0.3).minimize(cross_entropy)#更新网络 #定义一个InteractiveSession会话并初始化全部变量 correct_prediction = tf.equal(tf.arg_max(y_pre, 1), tf.arg_max(y_ture, 1))#Returns the truth value of (x == y) element-wise. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#cast,转化类型;reduce_mean,计算平均值 sess = tf.InteractiveSession() tf.global_variables_initializer().run() for i in range(10): avg_cost=0 # batch_xs, batch_ys = mnist.train.next_batch(100) for j in range(1,7): for z in range(100): X_train=features[(j-1)*1000:j*1000] Y_train=label[(j-1)*1000:j*1000] # print(Y_train)
def preprocess_egomotion(locs, thetas): with tf.name_scope('pre_ego'): pre_ego = tf.concat_v2([locs, tf.sin(thetas), tf.cos(thetas)], 2) sh = pre_ego.get_shape().as_list() pre_ego = tf.reshape(pre_ego, [-1, sh[-1]]) return pre_ego
def custom_activation(x): return (1 / np.sqrt(h_size)) * tf.cos(x / 0.02)
def _eunn_param(hidden_size, capacity=2, fft=False, comp=True): """ Create parameters and do the initial preparations """ theta_phi_initializer = tf.random_uniform_initializer(-np.pi, np.pi) if fft: capacity = int(np.ceil(np.log2(hidden_size))) diag_list_0 = [] off_list_0 = [] varsize = 0 for i in range(capacity): size = capacity - i normal_size = (hidden_size // (2**size)) * (2**(size - 1)) extra_size = max(0, (hidden_size % (2**size)) - (2**(size - 1))) varsize += normal_size + extra_size params_theta = vs.get_variable("theta_0", [varsize], initializer=theta_phi_initializer) cos_theta = tf.cos(params_theta) sin_theta = tf.sin(params_theta) if comp: params_phi = vs.get_variable("phi_0", [varsize], initializer=theta_phi_initializer) cos_phi = tf.cos(params_phi) sin_phi = tf.sin(params_phi) cos_list_0 = tf.complex(cos_theta, tf.zeros_like(cos_theta)) cos_list_1 = tf.complex(tf.multiply(cos_theta, cos_phi), tf.multiply(cos_theta, sin_phi)) sin_list_0 = tf.complex(sin_theta, tf.zeros_like(sin_theta)) sin_list_1 = tf.complex(-tf.multiply(sin_theta, cos_phi), -tf.multiply(sin_theta, sin_phi)) last = 0 for i in range(capacity): size = capacity - i normal_size = (hidden_size // (2**size)) * (2**(size - 1)) extra_size = max(0, (hidden_size % (2**size)) - (2**(size - 1))) if comp: cos_list_normal = tf.concat([ tf.slice(cos_list_0, [last], [normal_size]), tf.slice(cos_list_1, [last], [normal_size]) ], 0) sin_list_normal = tf.concat([ tf.slice(sin_list_0, [last], [normal_size]), -tf.slice(sin_list_1, [last], [normal_size]) ], 0) last += normal_size cos_list_extra = tf.concat([ tf.slice(cos_list_0, [last], [extra_size]), tf.complex( tf.ones( [hidden_size - 2 * normal_size - 2 * extra_size]), tf.zeros( [hidden_size - 2 * normal_size - 2 * extra_size])), tf.slice(cos_list_1, [last], [extra_size]) ], 0) sin_list_extra = tf.concat([ tf.slice(sin_list_0, [last], [extra_size]), tf.complex( tf.zeros( [hidden_size - 2 * normal_size - 2 * extra_size]), tf.zeros([ hidden_size - 2 * normal_size - 2 * extra_size ])), -tf.slice(sin_list_1, [last], [extra_size]) ], 0) last += extra_size else: cos_list_normal = tf.slice(cos_theta, [last], [normal_size]) cos_list_normal = tf.concat([cos_list_normal, cos_list_normal], 0) cos_list_extra = tf.slice(cos_theta, [last + normal_size], [extra_size]) cos_list_extra = tf.concat([ cos_list_extra, tf.ones([hidden_size - 2 * normal_size - 2 * extra_size]), cos_list_extra ], 0) sin_list_normal = tf.slice(sin_theta, [last], [normal_size]) sin_list_normal = tf.concat( [sin_list_normal, -sin_list_normal], 0) sin_list_extra = tf.slice(sin_theta, [last + normal_size], [extra_size]) sin_list_extra = tf.concat([ sin_list_extra, tf.zeros([hidden_size - 2 * normal_size - 2 * extra_size]), -sin_list_extra ], 0) last += normal_size + extra_size if normal_size != 0: cos_list_normal = tf.reshape( tf.transpose( tf.reshape(cos_list_normal, [-1, 2 * normal_size // (2**size)])), [-1]) sin_list_normal = tf.reshape( tf.transpose( tf.reshape(sin_list_normal, [-1, 2 * normal_size // (2**size)])), [-1]) cos_list = tf.concat([cos_list_normal, cos_list_extra], 0) sin_list = tf.concat([sin_list_normal, sin_list_extra], 0) diag_list_0.append(cos_list) off_list_0.append(sin_list) diag_vec = tf.stack(diag_list_0, 0) off_vec = tf.stack(off_list_0, 0) else: capacity_b = capacity // 2 capacity_a = capacity - capacity_b hidden_size_a = hidden_size // 2 hidden_size_b = (hidden_size - 1) // 2 params_theta_0 = vs.get_variable("theta_0", [capacity_a, hidden_size_a], initializer=theta_phi_initializer) cos_theta_0 = tf.reshape(tf.cos(params_theta_0), [capacity_a, -1, 1]) sin_theta_0 = tf.reshape(tf.sin(params_theta_0), [capacity_a, -1, 1]) params_theta_1 = vs.get_variable("theta_1", [capacity_b, hidden_size_b], initializer=theta_phi_initializer) cos_theta_1 = tf.reshape(tf.cos(params_theta_1), [capacity_b, -1, 1]) sin_theta_1 = tf.reshape(tf.sin(params_theta_1), [capacity_b, -1, 1]) if comp: params_phi_0 = vs.get_variable("phi_0", [capacity_a, hidden_size_a], initializer=theta_phi_initializer) cos_phi_0 = tf.reshape(tf.cos(params_phi_0), [capacity_a, -1, 1]) sin_phi_0 = tf.reshape(tf.sin(params_phi_0), [capacity_a, -1, 1]) cos_list_0_re = tf.reshape( tf.concat([cos_theta_0, tf.multiply(cos_theta_0, cos_phi_0)], 2), [capacity_a, -1]) cos_list_0_im = tf.reshape( tf.concat([ tf.zeros_like(cos_theta_0), tf.multiply(cos_theta_0, sin_phi_0) ], 2), [capacity_a, -1]) if hidden_size_a * 2 != hidden_size: cos_list_0_re = tf.concat( [cos_list_0_re, tf.ones([capacity_a, 1])], 1) cos_list_0_im = tf.concat( [cos_list_0_im, tf.zeros([capacity_a, 1])], 1) cos_list_0 = tf.complex(cos_list_0_re, cos_list_0_im) sin_list_0_re = tf.reshape( tf.concat([sin_theta_0, -tf.multiply(sin_theta_0, cos_phi_0)], 2), [capacity_a, -1]) sin_list_0_im = tf.reshape( tf.concat([ tf.zeros_like(sin_theta_0), -tf.multiply(sin_theta_0, sin_phi_0) ], 2), [capacity_a, -1]) if hidden_size_a * 2 != hidden_size: sin_list_0_re = tf.concat( [sin_list_0_re, tf.zeros([capacity_a, 1])], 1) sin_list_0_im = tf.concat( [sin_list_0_im, tf.zeros([capacity_a, 1])], 1) sin_list_0 = tf.complex(sin_list_0_re, sin_list_0_im) params_phi_1 = vs.get_variable("phi_1", [capacity_b, hidden_size_b], initializer=theta_phi_initializer) cos_phi_1 = tf.reshape(tf.cos(params_phi_1), [capacity_b, -1, 1]) sin_phi_1 = tf.reshape(tf.sin(params_phi_1), [capacity_b, -1, 1]) cos_list_1_re = tf.reshape( tf.concat([cos_theta_1, tf.multiply(cos_theta_1, cos_phi_1)], 2), [capacity_b, -1]) cos_list_1_re = tf.concat( [tf.ones((capacity_b, 1)), cos_list_1_re], 1) cos_list_1_im = tf.reshape( tf.concat([ tf.zeros_like(cos_theta_1), tf.multiply(cos_theta_1, sin_phi_1) ], 2), [capacity_b, -1]) cos_list_1_im = tf.concat( [tf.zeros((capacity_b, 1)), cos_list_1_im], 1) if hidden_size_b * 2 != hidden_size - 1: cos_list_1_re = tf.concat( [cos_list_1_re, tf.ones([capacity_b, 1])], 1) cos_list_1_im = tf.concat( [cos_list_1_im, tf.zeros([capacity_b, 1])], 1) cos_list_1 = tf.complex(cos_list_1_re, cos_list_1_im) sin_list_1_re = tf.reshape( tf.concat([sin_theta_1, -tf.multiply(sin_theta_1, cos_phi_1)], 2), [capacity_b, -1]) sin_list_1_re = tf.concat( [tf.zeros((capacity_b, 1)), sin_list_1_re], 1) sin_list_1_im = tf.reshape( tf.concat([ tf.zeros_like(sin_theta_1), -tf.multiply(sin_theta_1, sin_phi_1) ], 2), [capacity_b, -1]) sin_list_1_im = tf.concat( [tf.zeros((capacity_b, 1)), sin_list_1_im], 1) if hidden_size_b * 2 != hidden_size - 1: sin_list_1_re = tf.concat( [sin_list_1_re, tf.zeros([capacity_b, 1])], 1) sin_list_1_im = tf.concat( [sin_list_1_im, tf.zeros([capacity_b, 1])], 1) sin_list_1 = tf.complex(sin_list_1_re, sin_list_1_im) else: cos_list_0 = tf.reshape(tf.concat([cos_theta_0, cos_theta_0], 2), [capacity_a, -1]) sin_list_0 = tf.reshape(tf.concat([sin_theta_0, -sin_theta_0], 2), [capacity_a, -1]) if hidden_size_a * 2 != hidden_size: cos_list_0 = tf.concat( [cos_list_0, tf.ones([capacity_a, 1])], 1) sin_list_0 = tf.concat( [sin_list_0, tf.zeros([capacity_a, 1])], 1) cos_list_1 = tf.reshape(tf.concat([cos_theta_1, cos_theta_1], 2), [capacity_b, -1]) cos_list_1 = tf.concat([tf.ones((capacity_b, 1)), cos_list_1], 1) sin_list_1 = tf.reshape(tf.concat([sin_theta_1, -sin_theta_1], 2), [capacity_b, -1]) sin_list_1 = tf.concat([tf.zeros((capacity_b, 1)), sin_list_1], 1) if hidden_size_b * 2 != hidden_size - 1: cos_list_1 = tf.concat( [cos_list_1, tf.zeros([capacity_b, 1])], 1) sin_list_1 = tf.concat( [sin_list_1, tf.zeros([capacity_b, 1])], 1) if capacity_b != capacity_a: if comp: cos_list_1 = tf.concat([ cos_list_1, tf.complex(tf.zeros([1, hidden_size]), tf.zeros([1, hidden_size])) ], 0) sin_list_1 = tf.concat([ sin_list_1, tf.complex(tf.zeros([1, hidden_size]), tf.zeros([1, hidden_size])) ], 0) else: cos_list_1 = tf.concat( [cos_list_1, tf.zeros([1, hidden_size])], 0) sin_list_1 = tf.concat( [sin_list_1, tf.zeros([1, hidden_size])], 0) diag_vec = tf.reshape(tf.concat([cos_list_0, cos_list_1], 1), [capacity_a * 2, hidden_size]) off_vec = tf.reshape(tf.concat([sin_list_0, sin_list_1], 1), [capacity_a * 2, hidden_size]) if capacity_b != capacity_a: diag_vec = tf.slice(diag_vec, [0, 0], [capacity, hidden_size]) off_vec = tf.slice(off_vec, [0, 0], [capacity, hidden_size]) def _toTensorArray(elems): elems = tf.convert_to_tensor(elems) n = tf.shape(elems)[0] elems_ta = tf.TensorArray(dtype=elems.dtype, size=n, dynamic_size=False, infer_shape=True, clear_after_read=False) elems_ta = elems_ta.unstack(elems) return elems_ta diag_vec = _toTensorArray(diag_vec) off_vec = _toTensorArray(off_vec) if comp: omega = vs.get_variable("omega", [hidden_size], initializer=theta_phi_initializer) diag = tf.complex(tf.cos(omega), tf.sin(omega)) else: diag = None return diag_vec, off_vec, diag, capacity
for epoch in range(n_epochs): if epoch % 100 == 0: print("Epoch", epoch, "MSE =", mse.eval()) sess.run(training_op) best_theta = theta.eval() # Partial Derivatives reset_graph() a = tf.Variable(0.2, name="a") b = tf.Variable(0.3, name="b") z = tf.constant(0.0, name="z0") for i in range(100): z = a * tf.cos(z + i) + z * tf.sin(b - i) grads = tf.gradients(z, [a, b]) init = tf.global_variables_initializer() with tf.Session() as sess: init.run() print(z.eval()) print(sess.run(grads)) # Using function reset_graph() dz = partial_derivatives(0.2, 0.3) # Using a gradient descent optimizer reset_graph()
def sin_and_cos(x, name="ignored"): return tf.concat(len(x.get_shape()) - 1, [tf.sin(x), tf.cos(x)])
def _alpha(self, a): return 1 - tf.cos(a * np.pi / 2)
def create_IQ_pwc(self, instr: Instruction, chan: str) -> dict: """ Construct the in-phase (I) and quadrature (Q) components of the signal. These are universal to either experiment or simulation. In the xperiment these will be routed to AWG and mixer electronics, while in the simulation they provide the shapes of the instruction fields to be added to the Hamiltonian. Parameters ---------- channel : str Identifier for the selected drive line. components : dict Separate signals to be combined onto this drive line. t_start : float Beginning of the signal. t_end : float End of the signal. Returns ------- dict Waveforms as I and Q components. """ ts = self.create_ts(instr.t_start, instr.t_end, centered=True) components = instr.comps self.ts = ts # dt = ts[1] - ts[0] amp_tot_sq = 0.0 inphase_comps = [] quadrature_comps = [] for comp in components[chan].values(): if isinstance(comp, Envelope): amp_tot_sq += 1 if comp.shape is None: inphase = comp.params["inphase"].get_value() quadrature = comp.params["quadrature"].get_value() else: shape = comp.get_shape_values(ts) inphase = tf.math.real(shape) quadrature = tf.math.imag(shape) xy_angle = comp.params["xy_angle"].get_value() freq_offset = comp.params["freq_offset"].get_value() phase = xy_angle + freq_offset * ts if len(inphase) != len(quadrature): raise ValueError( "inphase and quadrature are of different lengths.") elif len(inphase) < len(ts): zeros = tf.constant(np.zeros(len(ts) - len(inphase)), dtype=inphase.dtype) inphase = tf.concat([inphase, zeros], axis=0) quadrature = tf.concat([quadrature, zeros], axis=0) inphase_comps.append(inphase * tf.cos(phase) + quadrature * tf.sin(phase)) quadrature_comps.append(quadrature * tf.cos(phase) - inphase * tf.sin(phase)) norm = tf.sqrt(tf.cast(amp_tot_sq, tf.float64)) inphase = tf.add_n(inphase_comps, name="inphase") quadrature = tf.add_n(quadrature_comps, name="quadrature") self.amp_tot = norm self.signal[chan] = { "inphase": inphase, "quadrature": quadrature, "ts": ts } return {"inphase": inphase, "quadrature": quadrature, "ts": ts}
def _alpha(self, a): return (1 - tf.cos(a * np.pi)) / 2
def rotate(self, rotAng, center): ''' Rotate image and KPs about center by rotAng radians :param img: :param kps: :param rotAng: rotation angle :param center: center of rotation :return: ''' # rotAng = tf.constant([0.0]) if center.shape == (2, ): center = tf.expand_dims(center, 1) assert center.shape == (2, 1) # mean pix value (to be used later) meanPixVal = tf.reduce_mean(tf.reshape(tf.to_float(self.img), [-1, 3]), axis=0) self.meanPixVal = tf.cast(meanPixVal, tf.uint8) # get transorm matrix from output to input (as required by tf.contrib.image.transform) rotMat = tf.stack([(tf.cos(rotAng), -tf.sin(rotAng)), (tf.sin(rotAng), tf.cos(rotAng))], axis=0)[:, :, 0] transVec = center - tf.matmul(tf.transpose(rotMat), center) transormMat = tf.concat([tf.transpose(rotMat), transVec], axis=1) transormMat = tf.concat( [transormMat, np.array([[0., 0., 1.]])], axis=0) # transform from input to output transormMatInv = tf.linalg.inv(transormMat) if self.validKps: # get homogeneous kps kpsHomo = tf.concat([ self.kps2D[:, :2], tf.ones((self.kps2D.shape[0], 1), dtype=tf.float32) ], axis=1) # transorm the keypoints kpsTrans = tf.matmul(kpsHomo, tf.transpose(transormMatInv)) # transform the 3d keypoints kps3DOld = tf.identity(self.kps3D) self.kps3D = tf.matmul(kpsTrans * self.kps3D[:, 2:3], tf.transpose(tf.linalg.inv(self.camMat))) # debug # self.kps3D = tf.Print(self.kps3D, [self.kps3D, kps3DOld]) # reproj = tf.matmul(self.kps3D, tf.transpose(self.camMat)) # reproj = reproj[:,:2]/reproj[:,2:] # self.kps3D = tf.Print(self.kps3D, [kpsTrans[0], reproj[0]]) # add the valid col back if self.kpsValidCol: self.kps2D = tf.concat([kpsTrans[:, :2], self.kps2D[:, 2:]], axis=1) else: self.kps2D = kpsTrans # rotate the image transformVec = tf.reshape(transormMat, [-1])[:-1] self.img = tf.contrib.image.transform(tf.expand_dims(self.img, 0), tf.expand_dims(transformVec, 0), interpolation='BILINEAR')[0] # rotate the seg if self.validSeg: self.seg = tf.contrib.image.transform(tf.expand_dims(self.seg, 0), tf.expand_dims( transformVec, 0), interpolation='NEAREST')[0] # set all black pixels (out of boundary) to mean pixel value self.img = tf.cond(self.setOOBPix, lambda: setBlackPixels(self.img, self.meanPixVal), lambda: self.img) # if self.setOOBPix: # self.img = setBlackPixels(self.img, self.meanPixVal) # plt.imshow(mask.numpy() * 255) # plt.imshow(self.img.numpy()[:, :, [2, 1, 0]]) return
def rcwindow(N): x = tf.linspace(0., N, N + 1)[:-1] rcw = .5 - .5 * tf.cos(2. * np.pi * (x + .5) / N) rcw = tf.reshape(rcw, (N, 1)) * tf.reshape(rcw, (1, N)) return rcw
def onn_layer_for_testing(X,N, phase): theta = phase #theta = tf.Variable(tf.constant(np.pi/2,shape=[1,N**2],dtype=tf.float64),name='theta') phase_mod = tf.complex(tf.cos(theta),tf.sin(theta),name='phase_mod') X=X*phase_mod return X
test_session += 1 # Create the model x = tf.placeholder(tf.float32, [None, 700, train_set[0].shape[-1]], name="x") # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 700, values_to_predict], name="labels") y_nn, regularizers = convnn(x, channels_num, layers_num, window_size) prediction = y_nn with tf.name_scope("loss"): deviations = tf.subtract(prediction, y_) ae = tf.abs(deviations) mae = tf.reduce_mean(ae) atan2 = tf.atan2(tf.sin(deviations), tf.cos(deviations)) loss = tf.square(atan2, name="loss") mean_loss = tf.reduce_mean(loss) loss_summary = tf.summary.scalar("loss", mean_loss) with tf.name_scope("loss2"): # print(tf.shape(prediction)) # print(tf.shape(y_)) phi = prediction[:, :, 0] phi0 = y_[:, :, 0] psi = prediction[:, :, 1] psi0 = y_[:,:, 1] # cos_phi_diff = tf.square(tf.subtract(tf.cos(phi), tf.cos(phi0))) # sin_phi_diff = tf.square(tf.subtract(tf.sin(phi), tf.sin(phi0))) # cos_psi_diff = tf.square(tf.subtract(tf.cos(psi), tf.cos(psi0))) # sin_psi_diff = tf.square(tf.subtract(tf.sin(psi), tf.sin(psi0)))
def func_cosxy2_tf(x, y): return tf.cos(x * y ** 2)
log_parent_dir = './train_log' log_dir = '' # tf Graph input # X = tf.placeholder(shape=[None, numInput], dtype=tf.float64) X = tf.placeholder(shape=[None, dof], dtype=tf.float64) # L = tf.placeholder(shape=[None, numInput], dtype=tf.float64) Y = tf.placeholder(shape=[None, numOutput], dtype=tf.float64) # Construct model # h_jointAngles = layers.fully_connected(inputs=X, num_outputs=hiddenSize, biases_initializer=tf.zeros_initializer(), activation_fn=tf.sin) # Change sigmoid (relu, cos/sin) | is there bias ?? inputCombinations = tf.matmul(X, combinationMatrix) modifiedInputs = tf.concat( [tf.sin(inputCombinations), tf.cos(inputCombinations)], 1) # encodingLayer1 = layers.fully_connected(inputs=modifiedInputs, num_outputs=32, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu) # encodingLayer2 = layers.fully_connected(inputs=encodingLayer1, num_outputs=16, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu) # layer3 = layers.fully_connected(inputs=layer2, num_outputs=hiddenSize, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu) # layer4 = layers.fully_connected(inputs=layer3, num_outputs=hiddenSize, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu6) encodedValues = layers.fully_connected( inputs=modifiedInputs, num_outputs=numEncoders, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu) # decodingLayer1 = layers.fully_connected(inputs=encodedValues, num_outputs=16, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu) # decodingLayer2 = layers.fully_connected(inputs=encodedValues, num_outputs=32, biases_initializer=tf.zeros_initializer(), activation_fn=tf.nn.relu) outputs = layers.fully_connected(inputs=encodedValues, num_outputs=numInput, biases_initializer=tf.zeros_initializer(), activation_fn=None)
def conv(a: CosArrow, args: TensorVarList, state) -> Sequence[Tensor]: return [tf.cos(*args)]
def warmup_cosine(x, warmup=0.002): s = tf.cast(x <= warmup, tf.float32) return s*(x/warmup) + (1-s)*(0.5 * (1 + tf.cos(math.pi * x)))
def execute_cos(self): return tf.cos(self.a, name="cos" + str(self.node_num))
def run(): """Run custom scalar demo and generate event files.""" step = tf.compat.v1.placeholder(tf.float32, shape=[]) with tf.name_scope('loss'): # Specify 2 different loss values, each tagged differently. summary_lib.scalar('foo', tf.pow(0.9, step)) summary_lib.scalar('bar', tf.pow(0.85, step + 2)) # Log metric baz as well as upper and lower bounds for a margin chart. middle_baz_value = step + 4 * tf.random.uniform([]) - 2 summary_lib.scalar('baz', middle_baz_value) summary_lib.scalar('baz_lower', middle_baz_value - 6.42 - tf.random.uniform([])) summary_lib.scalar('baz_upper', middle_baz_value + 6.42 + tf.random.uniform([])) with tf.name_scope('trigFunctions'): summary_lib.scalar('cosine', tf.cos(step)) summary_lib.scalar('sine', tf.sin(step)) summary_lib.scalar('tangent', tf.tan(step)) merged_summary = tf.compat.v1.summary.merge_all() with tf.compat.v1.Session() as sess, tf.summary.FileWriter( LOGDIR) as writer: # We only need to specify the layout once (instead of per step). layout_summary = summary_lib.custom_scalar_pb( layout_pb2.Layout(category=[ layout_pb2.Category( title='losses', chart=[ layout_pb2.Chart( title='losses', multiline=layout_pb2.MultilineChartContent( tag=[r'loss(?!.*margin.*)'], )), layout_pb2.Chart( title='baz', margin=layout_pb2.MarginChartContent(series=[ layout_pb2.MarginChartContent.Series( value='loss/baz/scalar_summary', lower='loss/baz_lower/scalar_summary', upper='loss/baz_upper/scalar_summary'), ], )), ]), layout_pb2.Category( title='trig functions', chart=[ layout_pb2.Chart( title='wave trig functions', multiline=layout_pb2.MultilineChartContent(tag=[ r'trigFunctions/cosine', r'trigFunctions/sine' ], )), # The range of tangent is different. Give it its own chart. layout_pb2.Chart( title='tan', multiline=layout_pb2.MultilineChartContent( tag=[r'trigFunctions/tangent'], )), ], # This category we care less about. Make it initially closed. closed=True), ])) writer.add_summary(layout_summary) for i in xrange(42): summary = sess.run(merged_summary, feed_dict={step: i}) writer.add_summary(summary, global_step=i)
def mixture_loss(y_true, logit, mask): """ Args: - y_true: [B, L, out_dim] - logit: [B, L, 3 * out_dim * num_mix + num_mix] - mask: [B, L] Return: - loss """ batch_size, time_step, _ = tf.shape(y_true) mean, logit_kappa, logit_lambda, logit_pi = tf.split( logit, num_or_size_splits=[ out_dim * num_mix, out_dim * num_mix, out_dim * num_mix, num_mix ], axis=-1, name='mix_vm_coeff_split') y_true = tf.reshape(y_true, [-1, 1, out_dim]) # [B*L, 1, out_dim] mask = tf.reshape(mask, [-1]) # [B*L] mean = tf.reshape(mean, [-1, num_mix, out_dim]) # [B*L, num_mix, out_dim] logit_kappa = tf.reshape( logit_kappa, [-1, num_mix, out_dim]) # [B*L, num_mix, out_dim] logit_lambda = tf.reshape(logit_lambda, [-1, num_mix, out_dim]) logit_pi = tf.reshape(logit_pi, [-1, num_mix]) # [B*L, num_mix] # rescale parameters kappa = tf.math.softplus(logit_kappa) # lambda_ = tf.math.softplus(logit_lambda) lambda_ = logit_lambda sin_diff = tf.sin(y_true - mean) # [B*L, num_mix, out_dim] cos_diff = tf.cos(y_true - mean) # NOTE: indexing the last dimension using 0:1 and the like only to maintain the resultant # matrix with shape=[B*l, num_mix, 1] phi_0 = lambda_[:, :, 0:1] * sin_diff[:, :, 1:2] + lambda_[:, :, 1:2] * sin_diff[:, :, 2:] phi_1 = lambda_[:, :, 0:1] * sin_diff[:, :, 0:1] + lambda_[:, :, 2:] * sin_diff[:, :, 2:] phi_2 = lambda_[:, :, 1:2] * sin_diff[:, :, 0:1] + lambda_[:, :, 2:] * sin_diff[:, :, 1:2] phi = tf.concat([phi_0, phi_1, phi_2], axis=-1) # [B*L, num_mix, out_dim] k_neg = tf.sqrt(kappa * kappa + phi * phi) log_probs = tf.reduce_sum( #-LOGTWOPI - tf.math.log(tf.math.bessel_i0(k_neg)) + cos_diff * kappa + sin_diff * phi, -LOGTWOPI - (tf.math.log(tf.math.bessel_i0e(k_neg)) + k_neg) + cos_diff * kappa + sin_diff * phi, axis=-1) mixed_log_probs = log_probs + tf.nn.log_softmax( logit_pi, axis=-1) # [B*L, num_mix] loss = -tf.reduce_logsumexp(mixed_log_probs, axis=-1) # [B*L] loss = tf.multiply(loss, mask, name='masking') if reduce: return tf.reduce_sum(loss) else: return tf.reshape(loss, [batch_size, time_step])
def cifar10_model_fn(features, labels, mode, params): """Model function for CIFAR-10.""" tf.summary.image('images', features, max_outputs=6) network = model.build_model(params) inputs = tf.reshape(features, [-1, _HEIGHT, _WIDTH, _DEPTH]) logits = network(inputs, mode == tf.estimator.ModeKeys.TRAIN) predictions = { 'classes': tf.argmax(logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate loss, which includes softmax cross entropy and L2 regularization. cross_entropy = tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=labels) # Create a tensor named cross_entropy for logging purposes. tf.identity(cross_entropy, name='cross_entropy') tf.summary.scalar('cross_entropy', cross_entropy) # Add weight decay to the loss. loss = cross_entropy + _WEIGHT_DECAY * tf.add_n( [tf.nn.l2_loss(v) for v in tf.trainable_variables()]) _log_variable_sizes(tf.trainable_variables(), "Trainable Variables") if mode == tf.estimator.ModeKeys.TRAIN: # Scale the learning rate linearly with the batch size. When the batch size # is 128, the learning rate should be 0.1. lr_max = params['lr_max'] lr_min = params['lr_min'] T_0 = tf.constant(params['T_0'], dtype=tf.float32) T_mul = tf.constant(params['T_mul'], dtype=tf.float32) batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() cur_epoch = tf.cast(global_step, dtype=tf.float32) / batches_per_epoch + 1.0 cur_i = tf.ceil( tf.log((T_mul - 1.0) * (cur_epoch / T_0 + 1.0)) / tf.log(2.0) - 1.0) T_beg = T_0 * (tf.pow(T_mul, cur_i) - 1.0) / (T_mul - 1.0) T_i = T_0 * tf.pow(T_mul, cur_i) T_cur = cur_epoch - T_beg learning_rate = lr_min + 0.5 * (lr_max - lr_min) * ( 1.0 + tf.cos(T_cur / T_i * np.pi)) """ initial_learning_rate = 0.1 * params['batch_size'] / 128 batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() # Multiply the learning rate by 0.1 at 100, 150, and 200 epochs. boundaries = [int(batches_per_epoch * epoch) for epoch in [100, 150, 200]] values = [initial_learning_rate * decay for decay in [1, 0.1, 0.01, 0.001]] learning_rate = tf.train.piecewise_constant( tf.cast(global_step, tf.int32), boundaries, values) """ # Create a tensor named learning_rate for logging purposes tf.identity(learning_rate, name='learning_rate') tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=_MOMENTUM) # Batch norm requires update ops to be added as a dependency to the train_op update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step) else: train_op = None accuracy = tf.metrics.accuracy(tf.argmax(labels, axis=1), predictions['classes']) metrics = {'accuracy': accuracy} # Create a tensor named train_accuracy for logging purposes tf.identity(accuracy[1], name='train_accuracy') tf.summary.scalar('train_accuracy', accuracy[1]) return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=metrics)