def _transformation_matrix(self, batch_size, rect_parms): #make matrices for rect parms a = K.cos(rect_parms[:, 0:1]) a /= rect_parms[:, 1:2] b = -K.sin(rect_parms[:, 0:1]) b /= rect_parms[:, 1:2] c = K.cos(rect_parms[:, 0:1]) * rect_parms[:, 3:4] - K.sin( rect_parms[:, 0:1]) * rect_parms[:, 4:5] c /= rect_parms[:, 1:2] d = K.sin(rect_parms[:, 0:1]) d /= rect_parms[:, 2:3] e = K.cos(rect_parms[:, 0:1]) e /= rect_parms[:, 2:3] f = K.sin(rect_parms[:, 0:1]) * rect_parms[:, 3:4] + K.cos( rect_parms[:, 0:1]) * rect_parms[:, 4:5] f /= rect_parms[:, 2:3] new_shape = (batch_size, 1) K.reshape(a, new_shape) K.reshape(b, new_shape) K.reshape(c, new_shape) K.reshape(d, new_shape) K.reshape(e, new_shape) K.reshape(f, new_shape) mat = K.concatenate([a, b, c, d, e, f], 1) mat = K.reshape(mat, shape=(batch_size, 2, 3)) return mat
def rot3d_from_euler(euler): # Convert Euler-Rodrigues angles to a 3D rotation matrix (with order of rotation X-Y-Z) euler_x = Lambda(lambda x: x[:, :, 0])(euler) euler_y = Lambda(lambda x: x[:, :, 1])(euler) euler_z = Lambda(lambda x: x[:, :, 2])(euler) cx = Lambda(lambda x: K.cos(x))(euler_x) sx = Lambda(lambda x: K.sin(x))(euler_x) cy = Lambda(lambda x: K.cos(x))(euler_y) sy = Lambda(lambda x: K.sin(x))(euler_y) cz = Lambda(lambda x: K.cos(x))(euler_z) sz = Lambda(lambda x: K.sin(x))(euler_z) R11 = Lambda(lambda x: x[0]*x[1])([cy, cz]) R12 = Lambda(lambda x: x[0]*x[1]*x[2] - x[3]*x[4])([sx, sy, cz, cx, sz]) R13 = Lambda(lambda x: x[0]*x[1]*x[2] + x[3]*x[4])([cx, sy, cz, sx, sz]) R21 = Lambda(lambda x: x[0]*x[1])([cy, sz]) R22 = Lambda(lambda x: x[0]*x[1]*x[2] + x[3]*x[4])([sx, sy, sz, cx, cz]) R23 = Lambda(lambda x: x[0]*x[1]*x[2] - x[3]*x[4])([cx, sy, sz, sx, cz]) R31 = Lambda(lambda x: -x)(sy) R32 = Lambda(lambda x: x[0]*x[1])([sx, cy]) R33 = Lambda(lambda x: x[0]*x[1])([cx, cy]) print("R11 shape: " + str(R11.shape)) R = Lambda(lambda x: K.stack(x, axis=-1))([R11, R12, R13, R21, R22, R23, R31, R32, R33]) print("R shape: " + str(R.shape)) R = Reshape((-1, 3, 3))(R) print("R shape: " + str(R.shape)) #exit(1) return R
def cal_lola(X): x_head, x_eye = X head_lo = x_head[:, 0] head_la = x_head[:, 1] eye_lo = x_eye[:, 0] eye_la = x_eye[:, 1] cA = K.cos(head_lo / 180 * np.pi) sA = K.sin(head_lo / 180 * np.pi) cB = K.cos(head_la / 180 * np.pi) sB = K.sin(head_la / 180 * np.pi) cC = K.cos(eye_lo / 180 * np.pi) sC = K.sin(eye_lo / 180 * np.pi) cD = K.cos(eye_la / 180 * np.pi) sD = K.sin(eye_la / 180 * np.pi) g_x = -cA * sC * cD + sA * sB * sD - sA * cB * cC * cD g_y = cB * sD + sB * cC * cD g_z = sA * sC * cD + cA * sB * sD - cA * cB * cC * cD gaze_lo = tf.atan2(-g_x, -g_z) * 180.0 / np.pi gaze_la = -tf.asin(g_y) * 180.0 / np.pi gaze_la = tf.expand_dims(gaze_la, dim=1) gaze_lo = tf.expand_dims(gaze_lo, dim=1) #gaze_lo = gaze_lo.unsqueeze(1) #gaze_la = gaze_la.unsqueeze(1) gaze_lola = tf.concat((gaze_lo, gaze_la), 1) return gaze_lola
def call(self, inputs): if not isinstance(inputs, list): raise ValueError('This layer should be called ' 'on a list of 2 inputs.') if len(inputs) != 2: raise ValueError('This layer should be called ' 'on a list of 2 inputs.' 'Got ' + str(len(inputs)) + ' inputs.') phase = inputs[0]###相位 amplitude = inputs[1]###振幅 embedding_dim = amplitude.shape[-1] if len(amplitude.shape) == len(phase.shape)+1: cos = K.repeat_elements(K.expand_dims(K.cos(phase)), embedding_dim, axis = len(phase.shape)) sin = K.repeat_elements(K.expand_dims(K.sin(phase)), embedding_dim, axis = len(phase.shape)) elif len(amplitude.shape) == len(phase.shape): #Each dimension has different phases cos = K.cos(phase) sin = K.sin(phase) else: raise ValueError('input dimensions of phase and amplitude do not agree to each other.') real_part = cos*amplitude imag_part = sin*amplitude return [real_part,imag_part]
def get_3d_optical_flow(width, height, optical_flow): tf_batch_size = tf.shape(optical_flow)[0] tf_height = tf.shape(optical_flow)[1] tf_width = tf.shape(optical_flow)[2] # u,v tensor u_tensor = K.tile(K.arange(0, width, step=1, dtype='float32'), [height]) u_tensor = K.reshape(u_tensor, [height, width]) v_tensor = K.tile(K.arange(0, height, step=1, dtype='float32'), [width]) v_tensor = K.transpose(K.reshape(v_tensor, [width, height])) # 3D location original X = K.sin(math.pi * u_tensor / height) * K.sin(math.pi * v_tensor / height) Y = K.cos(math.pi * u_tensor / height) * K.sin(math.pi * v_tensor / height) Z = K.cos(math.pi * v_tensor / height) XYZ = K.tile( K.expand_dims(K.reshape(K.stack([X, Y, Z]), [3, tf_height * tf_width]), 0), [tf_batch_size, 1, 1]) # Optical flow, FX/FY/FZ original FX = K.sin(math.pi * (u_tensor + optical_flow[:, :, :, 0]) / height) * \ K.sin(math.pi * (v_tensor + optical_flow[:, :, :, 1]) / height) FY = K.cos(math.pi * (u_tensor + optical_flow[:, :, :, 0]) / height) * \ K.sin(math.pi * (v_tensor + optical_flow[:, :, :, 1]) / height) FZ = K.cos(math.pi * (v_tensor + optical_flow[:, :, :, 1]) / height) return FX, FY, FZ, XYZ, Z
def call(self, x): y = x[1] x_normalize = tf.math.l2_normalize(x[0]) #|x = x'/ ||x'||2 k_normalize = tf.math.l2_normalize(self.kernel) # Wj = Wj' / ||Wj'||2 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) # W.Txの内積 sine = K.sqrt(1.0 - K.square(cosine)) phi = cosine * cos_m - sine * sin_m if self.easy_magin: phi = tf.where(cosine > 0, phi, cosine) else: phi = tf.where(cosine > th, phi, cosine - mm) output = (y * phi) + ( (1.0 - y) * cosine) # true cos(θ+m), False cos(θ) output *= self.s return output
def mse(yTrue, yPred): yt = K.reshape(yTrue, (-1, self.timesteps, self.output_dim)) yp = K.reshape(yPred, (-1, self.timesteps, self.output_dim)) loss = K.square(K.sin(yt[:,:,self.euler_start:]) - K.sin(yp[:,:,self.euler_start:])) loss = loss + K.square(K.cos(yt[:,:,self.euler_start:]) - K.cos(yp[:,:,self.euler_start:])) loss = loss + K.square(yt[:,:,:self.euler_start] - yp[:,:,:self.euler_start]) loss = K.mean(K.sqrt(loss)) return loss
def FKLoss(y_true, y_pred): y_true = K.concatenate([ K.sin(y_true[0]) + K.sin(y_true[0] + y_true[1]), K.cos(y_true[0]) + K.cos(y_true[0] + y_true[1]) ]) y_pred = K.concatenate([ K.sin(y_pred[0]) + K.sin(y_pred[0] + y_pred[1]), K.cos(y_pred[0]) + K.cos(y_pred[0] + y_pred[1]) ]) return losses.mean_squared_error(y_true, y_pred)
def call(self, inputs): if not isinstance(inputs, list): raise ValueError('This layer should be called ' 'on a list of 2 inputs.') if len(inputs) != 2: raise ValueError('This layer should be called ' 'on a list of 2 inputs.' 'Got ' + str(len(inputs)) + ' inputs.') phase = inputs[0] amplitude = inputs[1] embedding_dim = amplitude.shape[-1] if len(amplitude.shape) == len( phase.shape) + 1: #Assigning each dimension with same phase cos = K.repeat_elements(K.expand_dims(K.cos(phase)), embedding_dim, axis=len(phase.shape)) sin = K.repeat_elements(K.expand_dims(K.sin(phase)), embedding_dim, axis=len(phase.shape)) elif len(amplitude.shape) == len( phase.shape): #Each dimension has different phases cos = K.cos(phase) sin = K.sin(phase) else: raise ValueError( 'input dimensions of phase and amplitude do not agree to each other.' ) # if(len(phase.shape) == 2): # cos = K.repeat_elements(K.cos(phase), embedding_dim, axis = 2) # sin = K.repeat_elements(K.sin(phase), embedding_dim, axis = 2) # elif(len(phase.shape) == 3): # cos = K.cos(phase) # sin = K.sin(phase) # else: # raise ValueError('Each input should be of dimension 2 or 3.' # 'Got ' + str(len(phase.shape)) + ' dimension.') # print(cos.shape) # print(sin.shape) real_part = cos * amplitude imag_part = sin * amplitude # print(real_part.shape) # print(imag_part.shape) return [real_part, imag_part]
def _loss_tensor(y_true, y_pred): split0, split1 = tf.split(y_pred, num_or_size_splits=2, axis=-1) split3, split4 = tf.split(y_true, num_or_size_splits=2, axis=-1) out = 2 * 6371. * tf.asin( tf.sqrt( K.sin((split1 - split4) / (180 * math.pi))**2 + K.cos(split1 / (180 * math.pi)) * K.cos(split4 / (180 * math.pi)) * K.sin((split3 - split0) * 0.5 / (180 * math.pi))**2)) out2 = -1e4 * bimix_gauss.prob(y_pred) return K.mean(out, axis=-1) + K.mean(out2, axis=-1)
def angles2vector(angles): """ Convert 2D angle (yaw and pitch) to 3D unit vector :param angles: list of 2D angles :return: computed 3D vectors """ x = (-1.0) * K.sin(angles[:, 0]) * K.cos(angles[:, 1]) y = (-1.0) * K.sin(angles[:, 1]) z = (-1.0) * K.cos(angles[:, 0]) * K.cos(angles[:, 1]) vec = K.transpose(K.concatenate([[x], [y], [z]], axis=0)) return vec
def angle_loss(y_true, y_pred): if K._BACKEND == 'theano': import theano arctan2 = theano.tensor.arctan2 elif K._BACKEND == 'tensorflow': # https://www.tensorflow.org/api_docs/python/tf/atan2 import tensorflow as tf arctan2 = tf.atan2 vector_diff_square = K.square(K.cos(y_true) - K.cos(y_pred)) + K.square( K.sin(y_true) - K.sin(y_pred)) return K.mean(vector_diff_square, axis=-1)
def box_ArIoU(b1, b2): '''Return iou tensor Rotated. Parameters ---------- b1: tensor, shape=(i1,...,iN, 5), xywht b2: tensor, shape=(j, 5), xywht t for theta Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) b1_theta = b1[..., 4] # Second theta is true box 4th element is confidence 5th is theta # TODO: fix it so that 4th will be theta eveywhere. b2_theta = b2[..., 5] return iou * K.abs(K.cos(b2_theta) - K.cos(b1_theta))
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 positional_signal(d_model: 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 pos: seq中的位置 i: 某个单词的向量的第i个维度 PE(pos;2i) = sin(pos/10000^(2i/dmodel)) = sin(pos * 10000^(-2i/dmodel)) = sin(pos * exp^(-i * 4 / (dmodel/2)) ) PE(pos;2i+1) = cos(pos/10000^(2i/dmodel)) = cos(pos * 10000^(-2i/dmodel)) """ if d_model % 2 != 0: raise ValueError( "The hidden dimension (d_model) of the model must be divisible by 2." +"Currently it is { %d }",d_model) position = K.arange(0, length, dtype=K.floatx()) # [seq_len , ] num_timescales = d_model // 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)) # exp^(-i * 4 / (dmodel/2)) scaled_time = K.expand_dims(position, 1) * K.expand_dims(inv_timescales, 0) # [seq_len, hidden//2] # 能直接在dmdel维度上拼接吗??不应该隔行插入?? signal = K.concatenate([K.sin(scaled_time), K.cos(scaled_time)], axis=1) # [seq_len, hidden] return K.expand_dims(signal, axis=0) # [1, seq_len, hidden]
def __init__(self, loss_function, lats, data_format='channels_last', weighting='cosine'): """ Initialize a weighted loss. :param loss_function: method: Keras loss function to apply after the weighting :param lats: ndarray: 1-dimensional array of latitude coordinates :param data_format: Keras data_format ('channels_first' or 'channels_last') :param weighting: str: type of weighting to apply. Options are: cosine: weight by the cosine of the latitude (default) midlatitude: weight by the cosine of the latitude but also apply a 25% reduction to the equator and boost to the mid-latitudes """ self.loss_function = loss_function self.lats = lats self.data_format = K.normalize_data_format(data_format) if weighting not in ['cosine', 'midlatitude']: raise ValueError( "'weighting' must be one of 'cosine' or 'midlatitude'") self.weighting = weighting lat_tensor = K.zeros(lats.shape) print(lats) lat_tensor.assign(K.cast_to_floatx(lats[:])) self.weights = K.cos(lat_tensor * np.pi / 180.) if self.weighting == 'midlatitude': self.weights = self.weights - 0.25 * K.sin( lat_tensor * 2 * np.pi / 180.) self.is_init = False self.__name__ = 'latitude_weighted_loss'
def custom_loss(y_true, y_pred): r_hat = y_pred[:, 1] r_true = y_true[:, 1] th_hat= y_pred[:, 0] th_true= y_true[:, 0] coseno= K.cos(th_hat-th_true) return K.abs(r_true**2 + r_hat**2 - 2*r_true*r_hat*coseno)
def matrix_equation(x, dtPm, dtln, w, N): fi = dtPm gi = dtPm * K.cos(w * dtln) hi = dtPm * K.sin(w * dtln) fi_pow_2 = K.sum(fi * fi) gi_pow_2 = K.sum(gi * gi) hi_pow_2 = K.sum(hi * hi) figi = K.sum(fi * gi) fihi = K.sum(fi * hi) gihi = K.sum(gi * hi) # note that our price is already a log price so we should not log it one more time yi = x # K.log(x) yifi = K.sum(yi * fi) yigi = K.sum(yi * gi) yihi = K.sum(yi * hi) fi = K.sum(fi) gi = K.sum(gi) hi = K.sum(hi) yi = K.sum(yi) A = K.stack([ K.stack([N, fi, gi, hi]), K.stack([fi, fi_pow_2, figi, fihi]), K.stack([gi, figi, gi_pow_2, gihi]), K.stack([hi, fihi, gihi, hi_pow_2]) ], axis=0) b = K.stack([yi, yifi, yigi, yihi]) # do a classic x = (A'A)⁻¹A' b return tf.linalg.solve(A, K.reshape(b, (4, 1)))
def build(self, input_shape): self.thetas = self.add_weight(name='theta', initializer='uniform', shape=(self.output_dim // 2, )) self.c = K.cos(self.thetas) self.s = K.sin(self.thetas) super(Rotation, self).build(input_shape)
def get_position_encoding(timesteps: int, hidden_size: int, min_timescale: float = 1.0, max_timescale: float = 1.0e4): """Returns positional encoding Calculates the positional encoding as a mixture of sin and cosine with increasing wavelengths. Args: timesteps: Sequence length hidden_size: hidden size min_timescale: Minimum scale to apply to each position max_timescale: Maximum scale to apply to each position Returns: Tensor with shape [timesteps, hidden_size] """ position = K.arange(0, timesteps, dtype=K.floatx()) num_timescales = K.cast(hidden_size // 2, K.floatx()) log_timescale_increment = K.log(max_timescale / min_timescale / (num_timescales - 1)) inv_timescales = min_timescale * K.exp( K.arange(0, 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, 0)
def call(self, x, mask=None): if self.mode == 'sum': self.dim = int(x.shape[-1]) d = K.arange(self.dim / 2, dtype=tf.float32) d = 1. / K.pow(10000., 2 * d / self.dim) d = K.expand_dims(d, 0) t = K.ones_like(x[:, :, 0]) t = K.cumsum(t, axis=-1) - 1 t = K.expand_dims(t, -1) pe = K.dot(t, d) sin = K.sin(pe) cos = K.cos(pe) ### sin = K.repeat_elements(sin, 2, axis=-1) cos = K.repeat_elements(cos, 2, axis=-1) sin_mask = np.zeros(self.dim) sin_mask[::2] += 1 sin_mask = K.constant(sin_mask) cos_mask = np.zeros(self.dim) cos_mask[1::2] += 1 cos_mask = K.constant(cos_mask) sin = sin * sin_mask cos = cos * cos_mask pe = sin + cos if self.mode == 'sum': x = x + pe else: x = K.concatenate([x, pe], -1) return x
def call(self, x): if (self.size == None) or (self.mode == 'sum'): self.size = int(x.shape[-1]) # embedding层的第三个参数 # 取出embedding层的batch_size和seq_len的Tensor,backend.shape()对元tensor做了切片 batch_size, seq_len = K.shape(x)[0], K.shape(x)[1] '''计算sin和cos函数中的分母''' # keras.backend.pow(x,a) 元素级的指数运算操作,x为tensor,a为指数 # keras.backend.arange(start, stop, step, dtype) 创建一个包含整数序列的1D tensor position_j = 1. / K.pow(10000., \ 2 * K.arange(self.size / 2, dtype='float32') / self.size) # 在axis索引处添加一个维度 position_j = K.expand_dims(position_j, 0) '''计算sin和cos函数中的分子''' # keras.backend.ones_like(x) 实例化与另一个张量相同形状的全1变量 # keras.backend.cumsum(x, axis=1) 在某一指定轴,计算张量中的值的累加值 position_i = K.cumsum(K.ones_like(x[:, :, 0]), 1) - 1 # K.arange不支持变长,只好用这种方法生成 position_i = K.expand_dims(position_i, 2) '''分子分母合起来,得到sin和cos函数中的值''' position_ij = K.dot(position_i, position_j) '''将两个向量合并,获得位置编码向量''' # keras.backend.concatenate(tensors, axis=-1) 基于指定的轴,连接张量的列表 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 build(self, input_shape): assert len(input_shape) == 2 assert input_shape[-1] % 2 == 0 input_dim = input_shape[-1] // 2 data_format = K.image_data_format() kernel_shape = (input_dim, self.units) fan_in, fan_out = initializers._compute_fans(kernel_shape, data_format=data_format) if self.init_criterion == 'he': s = K.sqrt(1. / fan_in) elif self.init_criterion == 'glorot': s = K.sqrt(1. / (fan_in + fan_out)) rng = RandomStreams(seed=self.seed) def init_theta(shape, dtype=None): return rng.uniform(size=kernel_shape, low=0, high=6) if self.kernel_initializer in {'complex'}: theta_init = init_theta else: raise ValueError('Not recognized choice of initialization!') # Defining layer parameters (Codebook): self.theta = self.add_weight(shape=kernel_shape, initializer=theta_init, name='theta_kernel', regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) self.real_kernel = (1 / self.scale) * K.cos(self.theta) # self.imag_kernel = (1 / self.scale) * K.sin(self.theta) # self.input_spec = InputSpec(ndim=2, axes={-1: 2 * input_dim}) self.built = True
def _get_positional_encoding(max_seq_length, encoding_length): seq_positions = K.expand_dims( K.cast_to_floatx(K.arange(0, max_seq_length)), 1) sin_locations = K.cast_to_floatx(K.arange(0, encoding_length, 2)) cos_locations = K.cast_to_floatx(K.arange(1, encoding_length, 2)) div_term_const = K.log(K.constant(10000)) * K.constant( -2 / encoding_length) sin_encodings = K.sin( seq_positions * K.expand_dims(K.exp(sin_locations * div_term_const), 0)) cos_encodings = K.cos( seq_positions * K.expand_dims(K.exp(cos_locations * div_term_const), 0)) expanded_sin_encodings = K.expand_dims(sin_encodings, 2) expanded_cos_encodings = K.expand_dims(cos_encodings, 2) # Trick to alternate sines and cosines~ concatenated_encodings = K.concatenate( [expanded_sin_encodings, expanded_cos_encodings]) new_shape = concatenated_encodings.shape[:-1].as_list() new_shape[-1] *= 2 final_encoding = K.reshape(concatenated_encodings, new_shape) return final_encoding
def quad(x): lsin = Lambda(lambda x: K.sin(x)) lsin2 = Lambda(lambda x: K.sin(x + 2 * pi / 3)) lsin3 = Lambda(lambda x: K.sin(x + 4 * pi / 3)) lcos = Lambda(lambda x: K.cos(x)) return merge([lsin(x), lcos(x)], mode='concat')
def estimated(self, state, batch_size): # print(state.shape) # batch_size = state.shape[0] # generator_mag = K.ones((batch_size, 3)) # ang_ref = K.zeros((batch_size, 1)) # ref_ang = tf.Variable(tf.zeros((batch_size, 1))) # state = K.concatenate([generator_mag, state[:, :6], ang_ref, state[:, 6:]], axis=-1) # print(state.shape) state_restore = (state + 1) * (max_state - min_state) / 2 + min_state V = state_restore[:, :self.num_bus] * 10 # [k, 9] A = state_restore[:, self.num_bus:] # [k, 9] # print(V.shape, A.shape) # P_bus = K.zeros((A.shape[0], 9)) # [k, 9] # Q_bus = K.zeros((A.shape[0], 9)) # [k, 9] # A_ # print(K.permute_dimensions(K.repeat(A, 9), [0, 2, 1]).shape) # print(K.repeat(A, 9).shape) A_ = K.permute_dimensions(K.repeat(A, self.num_bus), [0, 2, 1]) - K.repeat(A, self.num_bus) G = K.constant(self.G, dtype=tf.float32) B = K.constant(self.B, dtype=tf.float32) cos_ = K.cos(A_ * pi / 180) sin_ = K.sin(A_ * pi / 180) term_1_P = G * cos_ + B * sin_ term_1_Q = G * sin_ - B * cos_ P_bus = (V * K.batch_dot(V, term_1_P, axes=[1, 2])) Q_bus = (V * K.batch_dot(V, term_1_Q, axes=[1, 2])) cols = [4, 5, 8, 10, 12, 21, 24, 26, 27, 34, 35, 38, 40, 54, 57] P_idx = [ 0, 1, 2, 3, 6, 7, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 25, 28, 29 ] Q_idx = [ 0, 1, 2, 3, 6, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 28, 29 ] batch_estimated_measurement = K.concatenate( [self.gather_cols(P_bus, P_idx), self.gather_cols(Q_bus, Q_idx)], axis=1) # print(batch_estimated_measurement.shape) # batch_estimated_measurement = K.concatenate([P_bus, Q_bus], axis=1) ans = (batch_estimated_measurement - min_meas) / (max_meas - min_meas) * 2 - 1 # print(K.eval(ans)) return ans
def _get_positional_encoding2(max_seq_length, encoding_length, min_timescale, max_timescale): # Similar to https://github.com/tensorflow/tensor2tensor/blob/5f9dd2db6d7797162e53adf152310ed13e9fc711/tensor2tensor/layers/common_attention.py seq_positions = K.expand_dims( K.cast_to_floatx(K.arange(0, max_seq_length)), 1) num_of_scales = encoding_length / 2 scales_locations = K.cast_to_floatx(K.arange(0, num_of_scales)) div_term_const = K.constant(-1) * ( K.log(K.constant(max_timescale / min_timescale)) / K.maximum(K.constant(num_of_scales), 1)) sin_encodings = K.sin(seq_positions * K.expand_dims( K.constant(min_timescale) * K.exp(scales_locations * div_term_const), 0)) cos_encodings = K.cos(seq_positions * K.expand_dims( K.constant(min_timescale) * K.exp(scales_locations * div_term_const), 0)) expanded_sin_encodings = K.expand_dims(sin_encodings, 2) expanded_cos_encodings = K.expand_dims(cos_encodings, 2) # Trick to alternate sines and cosines~ concatenated_encodings = K.concatenate( [expanded_sin_encodings, expanded_cos_encodings]) new_shape = concatenated_encodings.shape[:-1].as_list() new_shape[-1] *= 2 final_encoding = K.reshape(concatenated_encodings, new_shape) return final_encoding
def evaluation_branin_keras_tf(session, worker_id, params): """ Args: session (tf.Session | None): worker_id (int): params (data.OrderedDict): Returns: """ """ Code ported from https://www.sfu.ca/~ssurjano/Code/braninm.html Global Minimum = -0.397887 """ from keras import backend as K xx = list(params.values()) x1, x2 = xx[0], xx[1] a = 1.0 b = 5.1 / (4 * (np.pi ** 2)) c = 5.0 / np.pi r = 6.0 s = 10.0 t = 1.0 / (8.0 * np.pi) term1 = a * ((x2 - b * (x1 ** 2) + c * x1 - r) ** 2) term2 = s * (1.0 - t) * K.cos(x1) out = term1 + term2 + s out = K.eval(out) return out
def call(self, x, mask=None): s = 0 for i in range(0, self._N): s = s + (self._p[i] * K.cos(2 * math.pi * i * x / self._N)) + ( self._q[i] * K.sin(2 * math.pi * i * x / self._N)) return s
def call(self, x): # x: (batch, max_length=20, embedding_size=16) if (self.size == None) or (self.mode == 'sum'): self.size = int(x.shape[-1]) # embedding_size=16 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) # (embedding_size/2=8,) # [10^(-4*2*0/8), 10^(-4*2*1/8), 10^(-4*2*2/8)... 10^(-4*2*8/8)] # [10^0, 10^-1, 10^-2... 10^-8] position_j = K.expand_dims(position_j, 0) # coefficient # (1, embedding_size/2=8) # [[10^0, 10^-1, 10^-2... 10^-8]] position_i = K.cumsum(K.ones_like( x[:, :, 0]), 1) - 1 # K.arange generate vec dim value from 1 to dim/2 # (batch, max_length=20) # [[0,1,2...19],[0,1,2...19]...] position_i = K.expand_dims(position_i, 2) #pos # (batch, max_length=20, 1) # [[[0],[1],[2]...[19]],[[0],[1],[2]...[19]]...] position_ij = K.dot(position_i, position_j) # (batch, max_length=20, 1) * (1, embedding_size/2=8) = (batch, max_length=20, embedding_size/2=8) position_ij = K.concatenate( [K.cos(position_ij), K.sin(position_ij)], 2) # (batch, max_length=20, embedding_size=16) if self.mode == 'sum': return position_ij + x elif self.mode == 'concat': return K.concatenate([position_ij, x], 2)
def call(self, x, mask=None): 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)