def call(self, x): y = x[1] x_normalize = tf.math.l2_normalize(x[0]) k_normalize = tf.math.l2_normalize(self.kernel) cos_m = K.cos(self.m) sin_m = K.sin(self.m) th = K.cos(np.pi - self.m) mm = K.sin(np.pi - self.m) * self.m cosine = K.dot(x_normalize, k_normalize) sine = K.sqrt(1.0 - K.square(cosine)) phi = cosine * cos_m - sine * sin_m if self.easy_margin: phi = tf.where(cosine > 0, phi, cosine) else: phi = tf.where(cosine > th, phi, cosine - mm) output = (y * phi) + ((1.0 - y) * cosine) output *= self.s return output
def convert_gaze_2d_3d(angles): x = -cos(angles[:, 0]) * sin(angles[:, 1]) y = -sin(angles[:, 0]) z = -cos(angles[:, 1]) * cos(angles[:, 1]) norm = sqrt(x**2 + y**2 + z**2) x /= norm y /= norm z /= norm return x, y, z
def noised(): Ni = K.shape(x)[0] #This is the number in the batch #get an angle to shift each image in the batch angles = K.clip( self.amount*K.random_normal((Ni,)), self.lower, self.upper) #We are going to post multiply the vector (x'=xR) with the matrix #rather than the normal way (x'=Rx) #so we use the transpose of what is shown in literature for R R = K.stack( (K.stack((K.cos(angles),K.sin(angles)),axis=1) , K.stack((-K.sin(angles),K.cos(angles)),axis=1)) , axis=1) return tf.matmul(x,R)
def loss_function(y_true, y_pred): """ :param gamma: focal parameter controls degree of down-weighting of easy examples delta: controls weight given to false positive and false negatives. this equates to the Tversky index when delta = 0.7 smooth: smoothing constant to prevent division by zero errors """ delta = 0.7 smooth = 0.000001 axis = identify_axis(y_true.get_shape()) # Calculate true positives (tp), false negatives (fn), false positives (fp) and # true negatives (tn) tp = K.sum(y_true * y_pred, axis=axis) fn = K.sum(y_true * (1-y_pred), axis=axis) fp = K.sum((1-y_true) * y_pred, axis=axis) tn = K.sum((1-y_true) * (1-y_pred), axis=axis) tversky_class = (tp + smooth)/(tp + delta*fn + (1-delta)*fp + smooth) # Clip Tversky values between 0 and 1 to prevent division by zero error tversky_class= K.clip(tversky_class, 0., 1.) # Calculate Cosine Tversky loss per class cosine_tversky = (K.cos(tversky_class * math.pi))**gamma # Sum across all classes cosine_tversky_loss = K.sum(1-cosine_tversky,axis=[-1]) # adjusts loss to account for number of classes num_classes = K.cast(K.shape(y_true)[-1],'float32') cosine_tversky_loss = cosine_tversky_loss / num_classes return cosine_tversky_loss
def positional_signal(hidden_size: int, length: int, min_timescale: float = 1.0, max_timescale: float = 1e4): """ Helper function, constructing positional encodings as described in "Attention is All You Need" (https://arxiv.org/abs/1706.03762) The implementation was taken from https://github.com/kpot/keras-transformer """ if hidden_size % 2 != 0: raise ValueError( f"The hidden dimension of the model must be divisible by 2. " f"Currently it is {hidden_size}") position = K.arange(0, length, dtype=K.floatx()) num_timescales = hidden_size // 2 log_timescale_increment = K.constant( (np.log(float(max_timescale) / float(min_timescale)) / (num_timescales - 1)), dtype=K.floatx()) inv_timescales = ( min_timescale * K.exp(K.arange(num_timescales, dtype=K.floatx()) * -log_timescale_increment)) scaled_time = K.expand_dims(position, 1) * K.expand_dims(inv_timescales, 0) signal = K.concatenate([K.sin(scaled_time), K.cos(scaled_time)], axis=1) return K.expand_dims(signal, axis=0)
def positional_signal(hidden_size: int, length: int, min_timescale: float = 1.0, max_timescale: float = 1e4): """ Helper function, constructing basic positional encoding. The code is partially based on implementation from Tensor2Tensor library https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/common_attention.py """ if hidden_size % 2 != 0: raise ValueError( f"The hidden dimension of the model must be divisible by 2." f"Currently it is {hidden_size}") position = K.arange(0, length, dtype=K.floatx()) num_timescales = hidden_size // 2 log_timescale_increment = K.constant( (np.log(float(max_timescale) / float(min_timescale)) / (num_timescales - 1)), dtype=K.floatx()) inv_timescales = (min_timescale * K.exp( K.arange(num_timescales, dtype=K.floatx()) * -log_timescale_increment)) scaled_time = K.expand_dims(position, 1) * K.expand_dims(inv_timescales, 0) signal = K.concatenate([K.sin(scaled_time), K.cos(scaled_time)], axis=1) return K.expand_dims(signal, axis=0)
def cartpole_next_state_fn(input, 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 x = input[:, 0] x_dot = input[:, 1] theta = input[:, 2] theta_dot = input[:, 3] costheta = K.cos(theta) sintheta = K.sin(theta) force = force_mag if action == 1 else -force_mag temp = (force + polemass_length * theta_dot * theta_dot * sintheta) / total_mass thetaacc = (gravity * sintheta - costheta * temp) / ( length * (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 return K.stack([x, x_dot, theta, theta_dot], axis=1)
def mass_layer(self, tau_4vec): import tensorflow as tf from tensorflow.keras.layers import Concatenate from tensorflow.keras import backend as K tau_4vec = K.reshape(tau_4vec, (-1, self._njets, self._n_features)) pt = K.exp(K.clip(tau_4vec[:, :, 0], -7., 7.)) - 0.1 eta = tau_4vec[:, :, 1] phi = tau_4vec[:, :, 2] mass = 1.777 px = pt * K.cos(phi) py = pt * K.sin(phi) pz = pt * tf.math.sinh(K.clip(eta, -5, 5)) epsilon = 0.1 # avoid nan when e=0. sqrt(x)^' = -1/2 * 1/sqrt(x) e = K.sqrt(epsilon + px**2 + py**2 + pz**2 + mass**2) px = K.reshape(px, (-1, self._njets, 1)) py = K.reshape(py, (-1, self._njets, 1)) pz = K.reshape(pz, (-1, self._njets, 1)) e = K.reshape(e, (-1, self._njets, 1)) tau_4vec = Concatenate(axis=2)([px, py, pz, e]) tau_4vec = K.sum(tau_4vec, axis=1) px = tau_4vec[:, 0] py = tau_4vec[:, 1] pz = tau_4vec[:, 2] e = tau_4vec[:, 3] masssq = e**2 - (px**2 + py**2 + pz**2) mass = K.sqrt(epsilon + masssq) mass = K.reshape(mass, [-1, 1]) return mass
def call(self, inputs): """如果custom_position_ids,那么第二个输入为自定义的位置id """ if self.custom_position_ids: seq_len = K.shape(inputs)[1] inputs, position_ids = inputs if 'float' not in K.dtype(position_ids): position_ids = K.cast(position_ids, K.floatx()) else: input_shape = K.shape(inputs) batch_size, seq_len = input_shape[0], input_shape[1] position_ids = K.arange(0, seq_len, dtype=K.floatx())[None] indices = K.arange(0, self.output_dim // 2, dtype=K.floatx()) indices = K.pow(10000.0, -2 * indices / self.output_dim) embeddings = tf.einsum('bn,d->bnd', position_ids, indices) embeddings = K.stack([K.sin(embeddings), K.cos(embeddings)], axis=-1) embeddings = K.reshape(embeddings, (-1, seq_len, self.output_dim)) if self.merge_mode == 'add': return inputs + embeddings elif self.merge_mode == 'mul': return inputs * embeddings else: if not self.custom_position_ids: embeddings = K.tile(embeddings, [batch_size, 1, 1]) return K.concatenate([inputs, embeddings])
def __init__(self, N_filt=256, Filt_dim=2501, fs=22050): self.N_filt = N_filt self.Filt_dim = Filt_dim self.fs = fs super(MusicSinc1D, self).__init__() # The filters are trainable parameters. # Mel Initialization of the filterbanks low_freq_mel = 80 high_freq_mel = 2595 * np.log10( 1 + (self.fs / 2) / 700) # Convert Hz to Mel mel_points = np.linspace(low_freq_mel, high_freq_mel, self.N_filt) # Equally spaced in Mel scale f_cos = 700 * (10**(mel_points / 2595) - 1) # Convert Mel to Hz b1 = np.roll(f_cos, 1) b2 = np.roll(f_cos, -1) b1[0] = 30 b2[-1] = (self.fs / 2) - 100 self.freq_scale = self.fs * 10 self.filt_b1 = K.variable(b1 / self.freq_scale) self.filt_band = K.variable((b2 - b1) / self.freq_scale) # Get beginning and end frequencies of the filters. min_freq = 50.0 min_band = 50.0 self.filt_beg_freq = K.abs(self.filt_b1) + min_freq / self.freq_scale self.filt_end_freq = self.filt_beg_freq + (K.abs(self.filt_band) + min_band / self.freq_scale) # Filter window (hamming). n = np.linspace(0, self.Filt_dim, self.Filt_dim) window = 0.54 - 0.46 * K.cos(2 * math.pi * n / self.Filt_dim) window = K.cast(window, "float32") self.window = K.variable(window) # TODO what is this? t_right_linspace = np.linspace(1, (self.Filt_dim - 1) / 2, int((self.Filt_dim - 1) / 2)) self.t_right = K.variable(t_right_linspace / self.fs) # Compute the filters. output_list = [] for i in range(self.N_filt): low_pass1 = ( 2 * self.filt_beg_freq[i] * sinc(self.filt_beg_freq[i] * self.freq_scale, self.t_right)) low_pass2 = ( 2 * self.filt_end_freq[i] * sinc(self.filt_end_freq[i] * self.freq_scale, self.t_right)) band_pass = low_pass2 - low_pass1 band_pass = band_pass / K.max(band_pass) output_list.append(band_pass * self.window) self.filters = K.stack(output_list) # (80, 251) self.filters = K.transpose(self.filters) # (251, 80) self.filters = K.reshape( self.filters, (self.Filt_dim, 1, self.N_filt) ) # (251,1,80) in TF: (filter_width, in_channels, out_channels) in
def call(self, x): y_embed = K.cumsum(K.ones_like(x[:, :, :, 0]), 1) x_embed = K.cumsum(K.ones_like(x[:, :, :, 0]), 2) if self.normalize: eps = 1e-6 y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale dim_t = K.arange(self.num_pos_feats, dtype='float') dim = self.temperature ** (2 * (dim_t // 2) /self.num_pos_feats) pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t pos_x = K.stack((K.sin(pos_x[:, :, :, 0::2]), K.cos(pos_x[:, :, :, 1::2])), axis=4) b, h, w, d, c = K.int_shape(pos_x) pos_x = K.reshape(pos_x, (-1, h, w, d*c)) pos_y = K.stack((K.sin(pos_y[:, :, :, 0::2]), K.cos(pos_y[:, :, :, 1::2])), axis=4) pos_y = K.reshape(pos_y, (-1, h, w, d*c)) pos = K.permute_dimensions(K.concatenate((pos_y, pos_x), axis=3), (0, 3, 1, 2)) return pos
def build(self, input_shape): self.pos_encoding = self.add_weight(shape=(input_shape[0],self.d_model), initializer=tf.keras.initializers.Zeros(), name='pos_encoding', trainable=False) self.position = K.expand_dims(K.arange(0,self.max_len,dtype=tf.float32),1) self.div_term = K.exp(K.arange(0,self.d_model, 2,dtype='float32') * (np.log(10000.0) / self.d_model)) self.pos_encoding[:,0::2] = K.sin(self.position * self.div_term) self.pos_encoding[:,1::2] = K.cos(self.position * self.div_term) self.pos_encoding = K.transpose(K.expand_dims(self.pos_encoding,0))
def call(self, x, **kwargs): mask = K.expand_dims(K.cast(K.arange(start=0, stop=K.shape(x)[1] + 1), 'float32'), axis=-1) bins = K.expand_dims(K.cast(K.arange(self.embedding_size // 2) * 2, 'float32'), axis=0) evens = K.dot(mask, 1.0 / K.pow(10000.0, bins / self.embedding_size)) odds = tf.identity(evens) evens = K.sin(evens)[1:, :] odds = K.cos(odds)[1:, :] pos = K.reshape(K.stack([evens, odds], axis=2), (-1, K.shape(x)[1], self.embedding_size)) return pos
def seasonality_model(thetas, backcast_length, forecast_length, is_forecast): p = thetas.get_shape().as_list()[-1] p1, p2 = (p // 2, p // 2) if p % 2 == 0 else (p // 2, p // 2 + 1) t = linear_space(backcast_length, forecast_length, fwd_looking=is_forecast) s1 = K.stack([K.cos(2 * np.pi * i * t) for i in range(p1)], axis=0) s2 = K.stack([K.sin(2 * np.pi * i * t) for i in range(p2)], axis=0) if p == 1: s = s2 else: s = K.concatenate([s1, s2], axis=0) s = K.cast(s, np.float32) return K.dot(thetas, s)
def idx2pos(self, pid): pid = K.cast(pid, 'float32') pid = K.expand_dims(pid, 2) pj = 1. / K.pow( 10000., 2. / self.v_dim * K.arange(self.v_dim // 2, dtype='float32')) pj = K.expand_dims(pj, 0) pv = K.dot(pid, pj) pv1, pv2 = K.sin(pv), K.cos(pv) pv1, pv2 = K.expand_dims(pv1, 3), K.expand_dims(pv2, 3) pv = K.concatenate([pv1, pv2], 3) return K.reshape(pv, (K.shape(pv)[0], K.shape(pv)[1], self.v_dim))
def build(self, input_shape): # The filters are trainable parameters. self.filt_b1 = self.add_weight( name="filt_b1", shape=(self.N_filt, ), initializer="he_uniform", trainable=True, ) self.filt_band = self.add_weight( name="filt_band", shape=(self.N_filt, ), initializer="he_uniform", trainable=True, ) # Mel Initialization of the filterbanks low_freq_mel = 80 high_freq_mel = 2595 * np.log10( 1 + (self.fs / 2) / 700) # Convert Hz to Mel mel_points = np.linspace(low_freq_mel, high_freq_mel, self.N_filt) # Equally spaced in Mel scale f_cos = 700 * (10**(mel_points / 2595) - 1) # Convert Mel to Hz b1 = np.roll(f_cos, 1) b2 = np.roll(f_cos, -1) b1[0] = 30 b2[-1] = (self.fs / 2) - 100 self.freq_scale = self.fs * 1.0 self.set_weights([b1 / self.freq_scale, (b2 - b1) / self.freq_scale]) # Get beginning and end frequencies of the filters. min_freq = 50.0 min_band = 50.0 self.filt_beg_freq = K.abs(self.filt_b1) + min_freq / self.freq_scale self.filt_end_freq = self.filt_beg_freq + (K.abs(self.filt_band) + min_band / self.freq_scale) # Filter window (hamming). n = np.linspace(0, self.Filt_dim, self.Filt_dim) window = 0.54 - 0.46 * K.cos(2 * math.pi * n / self.Filt_dim) window = K.cast(window, "float32") self.window = K.variable(window) debug_print(" window", self.window.shape) # TODO what is this? t_right_linspace = np.linspace(1, (self.Filt_dim - 1) / 2, int((self.Filt_dim - 1) / 2)) self.t_right = K.variable(t_right_linspace / self.fs) debug_print(" t_right", self.t_right) super(MusicSinc1D, self).build(input_shape) # Be sure to call this at the end
def call(self, x): if (self.size == None) or (self.mode == 'sum'): self.size = int(x.shape[-1]) position_j = 1. / K.pow( 10000., 2 * K.arange(self.size / 2, dtype='float32') / self.size ) position_j = K.expand_dims(position_j, 0) #按照x的1维度累计求和,与arange一样,生成序列。只不过按照x的实际长度来 position_i = tf.cumsum(K.ones_like(x[:,:,0]), 1)-1 position_i = K.expand_dims(position_i, 2) position_ij = K.dot(position_i, position_j) position_ij = K.concatenate([K.cos(position_ij), K.sin(position_ij)], 2) if self.mode == 'sum': return position_ij + x elif self.mode == 'concat': return K.concatenate([position_ij, x], 2)
def call(self, x): if (self.size == None) or (self.mode == 'sum'): self.size = int(x.shape[-1]) batch_size,seq_len = K.shape(x)[0],K.shape(x)[1] position_j = 1. / K.pow(10000., \ 2 * K.arange(self.size / 2, dtype='float32' \ ) / self.size) position_j = K.expand_dims(position_j, 0) position_i = K.cumsum(K.ones_like(x[:,:,0]), 1)-1 #K.arange不支持变长,只好用这种方法生成 position_i = K.expand_dims(position_i, 2) position_ij = K.dot(position_i, position_j) position_ij = K.concatenate([K.cos(position_ij), K.sin(position_ij)], 2) if self.mode == 'sum': return position_ij + x elif self.mode == 'concat': return K.concatenate([position_ij, x], 2)
def call(self, inputs, **kwargs): ''' :param inputs: A Tensor with shape (batch_size, feature_size, 1) :param kwargs: :return: A Tensor with shape (batch_size, feature_size, length of time vector representation + 1) ''' bias = self.wb * inputs + self.bb if self.p_activation.startswith('sin'): wgts = K.sin(K.dot(inputs, self.wa) + self.ba) elif self.p_activation.startswith('cos'): wgts = K.cos(K.dot(inputs, self.wa) + self.ba) else: raise NotImplementedError( 'Neither sine or cosine periodic activation be selected.') return K.concatenate([bias, wgts], -1)
def call(self, inputs: tensorflow.Tensor, mask: Optional[tensorflow.Tensor] = None) -> tensorflow.Tensor: input_shape = K.shape(inputs) if self.mode == self.MODE_ADD: batch_size, seq_len, output_dim = input_shape[0], input_shape[ 1], input_shape[2] pos_input = K.tile(K.expand_dims(K.arange(seq_len), axis=0), [batch_size, 1]) elif self.mode == self.MODE_CONCAT: batch_size, seq_len, output_dim = input_shape[0], input_shape[ 1], self.output_dim pos_input = K.tile(K.expand_dims(K.arange(seq_len), axis=0), [batch_size, 1]) else: output_dim = self.output_dim pos_input = inputs if K.dtype(pos_input) != K.floatx(): pos_input = K.cast(pos_input, K.floatx()) evens = K.arange(output_dim // 2) * 2 odds = K.arange(output_dim // 2) * 2 + 1 even_embd = K.sin( K.dot( K.expand_dims(pos_input, -1), K.expand_dims( 1.0 / K.pow( 10000.0, K.cast(evens, K.floatx()) / K.cast(output_dim, K.floatx())), 0))) odd_embd = K.cos( K.dot( K.expand_dims(pos_input, -1), K.expand_dims( 1.0 / K.pow( 10000.0, K.cast((odds - 1), K.floatx()) / K.cast(output_dim, K.floatx())), 0))) embd = K.stack([even_embd, odd_embd], axis=-1) output = K.reshape(embd, [-1, K.shape(inputs)[1], output_dim]) if self.mode == self.MODE_CONCAT: output = K.concatenate([inputs, output], axis=-1) if self.mode == self.MODE_ADD: output += inputs return output
def call(self, inputs, **kwargs): q_len, m_len = K.shape(inputs[0])[1], K.shape(inputs[1])[1] k_len = q_len + m_len start, stop = k_len, -1 if not self.directional: stop = -q_len inputs = K.tile( K.expand_dims(K.arange(start, stop, -1, dtype=K.floatx()), axis=0), [K.shape(inputs[0])[0], 1], ) if self.clamp_len is not None: inputs = K.clip(inputs, min_value=0, max_value=self.clamp_len) inputs = K.expand_dims(inputs, axis=-1) output_dim = K.cast(self.output_dim, K.floatx()) ranges = K.expand_dims(K.arange(0.0, self.output_dim, 2.0), axis=0) / output_dim inverse = 1.0 / K.pow(10000.0, ranges) positions = inputs * inverse return K.concatenate([K.sin(positions), K.cos(positions)], axis=-1)
def call(self, x, mask=None): if (self.size == None) or (self.mode == 'sum'): self.size = int(x.shape[-1]) position_j = 1. / \ K.pow(10000., 2 * K.arange(self.size / 2, dtype='float32') / self.size) position_j = K.expand_dims(position_j, 0) position_i = tf.cumsum(K.ones_like(x[:, :, 0]), 1) - 1 position_i = K.expand_dims(position_i, 2) position_ij = K.dot(position_i, position_j) outputs = K.concatenate([K.cos(position_ij), K.sin(position_ij)], 2) if self.mode == 'sum': if self.scale: outputs = outputs * self.size**0.5 return x + outputs elif self.mode == 'concat': return K.concatenate([outputs, x], 2)
def mountaincar_next_state_fn(input, action): min_position = -1.2 max_position = 0.6 max_speed = 0.07 goal_position = 0.5 position = input[:, 0] velocity = input[:, 1] velocity += (action - 1) * 0.001 + K.cos(3 * position) * (-0.0025) velocity = K.clip(velocity, -max_speed, max_speed) position += velocity position = K.clip(position, min_position, max_position) #if (position==min_position and velocity<0): velocity = 0 #print(position, position.shape) #print(velocity, velocity.shape) velocity = K.switch(K.equal(position, min_position), K.switch(velocity < 0, velocity * 0, velocity), velocity) # return K.stack([position, velocity], axis=1)
def generate_filters(self): #filters = K.zeros(shape=(N_filt, Filt_dim)) # Get beginning and end frequencies of the filters. min_freq = 50.0 min_band = 50.0 filt_beg_freq = K.abs(self.filt_b1) + min_freq / self.freq_scale filt_end_freq = filt_beg_freq + (K.abs(self.filt_band) + min_band / self.freq_scale) # Filter window (hamming). n = np.linspace(0, self.Filt_dim, self.Filt_dim) window = 0.54 - 0.46 * K.cos(2 * math.pi * n / self.Filt_dim) window = K.cast(window, "float32") # window = K.variable(window) # TODO what is this? t_right_linspace = np.linspace(1, (self.Filt_dim - 1) / 2, int((self.Filt_dim - 1) / 2)) # t_right = K.variable(t_right_linspace / self.fs) # this line doesn't work in tf edge mode t_right = t_right_linspace / self.fs # Compute the filters. output_list = [] for i in range(self.N_filt): low_pass1 = 2 * filt_beg_freq[i] * sinc( filt_beg_freq[i] * self.freq_scale, t_right) low_pass2 = 2 * filt_end_freq[i] * sinc( filt_end_freq[i] * self.freq_scale, t_right) band_pass = (low_pass2 - low_pass1) band_pass = band_pass / K.max(band_pass) output_list.append(band_pass * window) filters = K.stack(output_list) #(80, 251) filters = K.transpose(filters) #(251, 80) filters = K.reshape( filters, (self.Filt_dim, 1, self.N_filt) ) #(251,1,80) in TF: (filter_width, in_channels, out_channels) in PyTorch (out_channels, in_channels, filter_width) return filters
def call(self, inputs): theta_b_output, theta_f_output = super(SeasonalBlock, self).call(inputs) t = K.cast(K.arange(-self.fdw, self.fw, 1) / self.fdw, tf.float32) cos_num = self.theta_units // 2 sin_num = (self.theta_units // 2 if self.theta_units % 2 == 0 else self.theta_units // 2 + 1) cos = K.stack([K.cos(2 * np.pi * i * t) for i in range(cos_num)], axis=0) sin = K.stack([K.sin(2 * np.pi * i * t) for i in range(sin_num)], axis=0) s = K.concatenate([cos, sin], axis=0) s_b = s[:, :self.fdw] s_f = s[:, self.fdw:] backcast = K.dot(theta_b_output, s_b) forecast = K.dot(theta_f_output, s_f) return backcast, forecast
def accuracy_angle(y_true, y_pred): from tensorflow.keras import backend as K import tensorflow as tf pred_x = -1 * K.cos(y_pred[0]) * K.sin(y_pred[1]) pred_y = -1 * K.sin(y_pred[0]) pred_z = -1 * K.cos(y_pred[0]) * K.cos(y_pred[1]) pred_norm = K.sqrt(pred_x * pred_x + pred_y * pred_y + pred_z * pred_z) true_x = -1 * K.cos(y_true[0]) * K.sin(y_true[1]) true_y = -1 * K.sin(y_true[0]) true_z = -1 * K.cos(y_true[0]) * K.cos(y_true[1]) true_norm = K.sqrt(true_x * true_x + true_y * true_y + true_z * true_z) angle_value = (pred_x * true_x + pred_y * true_y + pred_z * true_z) / (true_norm * pred_norm) K.clip(angle_value, -0.9999999999, 0.999999999) return (tf.acos(angle_value) * 180.0) / math.pi
def call(self, inputs, mask=None): inputs, pos_input = inputs batch_size, seq_len, output_dim = self._get_shape(inputs) if self.mode == self.MODE_EXPAND: pos_input = inputs if K.dtype(pos_input) != K.floatx(): pos_input = K.cast(pos_input, K.floatx()) evens = K.arange(0, output_dim // 2) * 2 odds = K.arange(0, output_dim // 2) * 2 + 1 even_embd = K.sin( K.dot( K.expand_dims(pos_input, -1), K.expand_dims(1.0 / K.pow( 10000.0, K.cast(evens, K.floatx()) / K.cast(output_dim, K.floatx()) ), 0) ) ) odd_embd = K.cos( K.dot( K.expand_dims(pos_input, -1), K.expand_dims(1.0 / K.pow( 10000.0, K.cast((odds - 1), K.floatx()) / K.cast(output_dim, K.floatx()) ), 0) ) ) embd = K.stack([even_embd, odd_embd], axis=-1) output = K.reshape(embd, [-1, seq_len, output_dim]) if self.mode == self.MODE_CONCAT: output = K.concatenate([inputs, output], axis=-1) if self.mode == self.MODE_ADD: output += inputs return output
def _rotation_matrix_axis(self, dim, theta): # following are left handed system (clockwise rotation) # IMPORTANT: different to MATLAB version, this dim starts from 0, instead of 1 if dim == 0: # x-axis rm = tf.stack([[1.0, 0.0, 0.0], [0.0, K.cos(theta), -K.sin(theta)], [0.0, K.sin(theta), K.cos(theta)]]) elif dim == 1: # y-axis rm = tf.stack([[K.cos(theta), 0.0, K.sin(theta)], [0.0, 1.0, 0.0], [-K.sin(theta), 0.0, K.cos(theta)]]) elif dim == 2: # z-axis rm = tf.stack([[K.cos(theta), -K.sin(theta), 0.0], [K.sin(theta), K.cos(theta), 0.0], [0.0, 0.0, 1.0]]) else: raise return rm
def noised(): Ni = tf.shape(x)[0] #This is the number in the batch #get an angle to shift each image in the batch anglesx = K.clip( self.amount*K.random_normal((Ni,)), self.lower, self.upper) anglesy = K.clip( self.amount*K.random_normal((Ni,)), self.lower, self.upper) anglesz = K.clip( self.amount*K.random_normal((Ni,)), self.lower, self.upper) #We are going to post multiply the vector (x'=xR) with the matrix #rather than the normal way (x'=Rx) #so we use the transpose of what is shown in literature for R zeros = tf.zeros((Ni,)) ones = tf.ones((Ni,)) Rx = K.stack( (K.stack((ones, zeros, zeros), axis=1), K.stack((zeros, K.cos(anglesx),K.sin(anglesx)),axis=1) , K.stack((zeros, -K.sin(anglesx),K.cos(anglesx)),axis=1)) , axis=1) Ry = K.stack( (K.stack((K.cos(anglesy), zeros, -K.sin(anglesy)),axis=1), K.stack((zeros, ones, zeros),axis=1) , K.stack((K.sin(anglesy), zeros, K.cos(anglesy)),axis=1)) , axis=1) Rz = K.stack( (K.stack((K.cos(anglesz), K.sin(anglesz), zeros),axis=1), K.stack((-K.sin(anglesz), K.cos(anglesz),zeros),axis=1) , K.stack((zeros,zeros,ones),axis=1)) , axis=1) return tf.matmul(x,tf.matmul(Rx,tf.matmul(Ry,Rz)))
def cutoff_function(self, distance_matrix): cos_component = 0.5 * (1 + K.cos(np.pi * distance_matrix / self.cutoff)) return K.switch(distance_matrix <= self.cutoff, cos_component, K.zeros_like(distance_matrix))