def _apply_dense(self, grad, var): lr_t = math_ops.cast(self._lr_t, var.dtype.base_dtype) alpha_t = math_ops.cast(self._alpha_t, var.dtype.base_dtype) beta_t = math_ops.cast(self._beta_t, var.dtype.base_dtype) epsilon_t = math_ops.cast(self._epsilon_t, var.dtype.base_dtype) mu_t = math_ops.cast(self._mu_t, var.dtype.base_dtype) decay_t = math_ops.cast(self._decay_t, var.dtype.base_dtype) decaypower_t = math_ops.cast(self._decaypower_t, var.dtype.base_dtype) speed_ini_t = math_ops.cast(self._speed_ini_t, var.dtype.base_dtype) v = self.get_slot(var, "v1") #(1.-self.alpha*self.beta)*p ) #Initialise v such that the initial speed is in the direction of -grad v_temp = cond( equal(num_iter(),0) , lambda : (1.-alpha_t*beta_t) * var - beta_t**2 * grad + beta_t * speed_ini_t * grad, lambda : v ) g = self.get_slot(var, "g1") g_temp = cond( equal(num_iter(),0) , lambda : grad, lambda : g ) v_t = v.assign( v_temp - ( lr_t * decay_t / math_ops.pow(math_ops.cast(num_iter()+1, var.dtype.base_dtype),decaypower_t) ) * ( (alpha_t-1./beta_t) * var + 1./beta_t * v_temp ) ) pre_g = g.assign( g_temp ) var_update = state_ops.assign_sub( var, ( lr_t * decay_t / math_ops.pow(math_ops.cast(num_iter()+1, var.dtype.base_dtype),decaypower_t) ) * ( (alpha_t-1./beta_t) * var + 1./beta_t * v_temp + beta_t * ((1. + mu_t) * grad - mu_t * pre_g)) ) #Update 'ref' by subtracting 'value #print("grad=",tf.eval(tf.print(grad))) #print("pre_grad=",tf.eval(tf.print(pre_g))) return control_flow_ops.group(*[var_update, v_t])
def dice_tumor_core(y_true, y_pred): mask_true, mask_pred = get_one_hot_from_output(y_true, y_pred) mask_true = tfmth.logical_or(tfmth.equal(mask_true, 1), tfmth.equal(mask_true, 3)) mask_pred = tfmth.logical_or(tfmth.equal(mask_pred, 1), tfmth.equal(mask_pred, 3)) return dice_coefficient(mask_true, mask_pred)
def update(self, features, labels, pred_mean): test_for_ybar0_s1 = tfm.logical_and( tfm.equal(features['ybar'], 0), tfm.equal(features['sensitive'], 1)) accepted = tf.gather_nd(tf.cast(pred_mean < 0.5, tf.float32), tf.where(test_for_ybar0_s1)) self.mean(accepted)
def update(self, features, labels, pred_mean): test_for_ybar1_s1 = tfm.logical_and( tfm.equal(features['ybar'], 1), tfm.equal(features['sensitive'], 1)) accepted = tft.gather_nd(tf.cast(pred_mean > 0.5, tf.float32), tf.where(test_for_ybar1_s1)) return self._return_and_store(self.mean(accepted))
def map_label_to_angle_32(label): """ return a list of tensors, each is the rotation angle corresponding to label [l] """ batch_size = label.shape[0] label = tf.dtypes.cast(label, dtype=tf.float32) label = tf.split(label, batch_size, 0) label = [tf.squeeze(l) for l in label] angles = [] for l in label: cond0 = tm.equal(tflt(0), l) cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(5))) cond2 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(10))) cond3 = tm.logical_and(tm.less_equal(tflt(11), l), tm.less_equal(l, tflt(20))) cond4 = tm.logical_and(tm.less_equal(tflt(21), l), tm.less_equal(l, tflt(25))) cond5 = tm.logical_and(tm.less_equal(tflt(26), l), tm.less_equal(l, tflt(30))) cond6 = tm.equal(tflt(31), l) call0= re(0., 0., 0.) call1= re(0.646, (l-1)*np.pi/2.5, 0) call2 = re(1.108, (l*2-11)*np.pi/5, 0) call3 = re(np.pi/2, (l-11)*np.pi/5, 0) call4 = re(2.033, (l*2-11)*np.pi/5, 0) call5 = re(2.496, (l-26)*np.pi/2.5, 0) call6 = re(np.pi, 0, 0) conditions = [cond0, cond1, cond2, cond3, cond4, cond5, cond6] callables = [call0, call1, call2, call3, call4, call5, call6] angle = nested_tf_cond(conditions, callables) angles.append(angle) return angles
def mask_for(features, **kwargs): """Create a 'mask' that filters for certain values Args: features: a dictionary of tensors **kwargs: entries of the dictionary with the values, only the first two are used Returns: a mask """ entries = list(kwargs.items()) return tf.where( tfm.logical_and(tfm.equal(features[entries[0][0]], entries[0][1]), tfm.equal(features[entries[1][0]], entries[1][1])))
def map_label_to_angle_18(label): """ return a list of tensors, each is the rotation angle corresponding to label [l] """ batch_size = label.shape[0] label = tf.dtypes.cast(label, dtype=tf.float32) label = tf.split(label, batch_size, 0) label = [tf.squeeze(l) for l in label] angles = [] for l in label: cond0 = tm.equal(tflt(0), l) cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(3))) cond2 = tm.logical_and(tm.less_equal(tflt(4), l), tm.less_equal(l, tflt(5))) cond3 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(9))) cond4 = tm.logical_and(tm.less_equal(tflt(10), l), tm.less_equal(l, tflt(13))) cond5 = tm.logical_and(tm.less_equal(tflt(14), l), tm.less_equal(l, tflt(17))) call0= re(0., 0., 0.) call1= re(l*np.pi/2, 0, 0) call2 = re(0, 0, (l*2-7)*np.pi/2) call3 = re((l*2-11)*np.pi/4, 0, 0) call4 = re(0, 0, (l*2-19)*np.pi/4) call5 = re(np.pi/2, 0, (l*2-27)*np.pi/4) conditions = [cond0, cond1, cond2, cond3, cond4, cond5] callables = [call0, call1, call2, call3, call4, call5] angle = nested_tf_cond(conditions, callables) angles.append(angle) return angles
def rotate_tensor_by_label_32(batch_data, label, graph): """ Rotate a batch of points by the label 32 possible directions: vertices of a regular icosahedron vertices of a regular dodecahedron Input: BxNx3 array Return: BxNx3 array """ with graph.as_default(): batch_size = batch_data.get_shape().as_list()[0] rotated_data = [None] * batch_size splits = tf.split(batch_data, batch_size, 0) label = tf.dtypes.cast(label, dtype=tf.float32) label = tf.split(label, batch_size, 0) label = [tf.squeeze(l) for l in label] for k in range(batch_size): shape_pc = splits[k] l = label[k] cond0 = tm.equal(tflt(0), l) cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(5))) cond2 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(10))) cond3 = tm.logical_and(tm.less_equal(tflt(11), l), tm.less_equal(l, tflt(20))) cond4 = tm.logical_and(tm.less_equal(tflt(21), l), tm.less_equal(l, tflt(25))) cond5 = tm.logical_and(tm.less_equal(tflt(26), l), tm.less_equal(l, tflt(30))) cond6 = tm.equal(tflt(31), l) call0= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=0) call1= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=0.646, angle_y=(l-1)*np.pi/2.5) call2 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=1.108, angle_y=(l*2-11)*np.pi/5) call3 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/2, angle_y=(l-11)*np.pi/5) call4 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=2.033, angle_y=(l*2-11)*np.pi/5) call5 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=2.496, angle_y=(l-26)*np.pi/2.5) call6 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi) conditions = [cond0, cond1, cond2, cond3, cond4, cond5, cond6] callables = [call0, call1, call2, call3, call4, call5, call6] shape_pc = nested_tf_cond(conditions, callables) rotated_data[k] = shape_pc rotated_data = tf.squeeze(tf.stack(rotated_data, axis=0)) return rotated_data
def rotate_tensor_by_label_54(batch_data, label, graph): """ Rotate a batch of points by the label 54 possible directions: vertices of a regular icosahedron vertices of a regular dodecahedron Input: BxNx3 array Return: BxNx3 array """ with graph.as_default(): batch_size = batch_data.get_shape().as_list()[0] rotated_data = [None] * batch_size splits = tf.split(batch_data, batch_size, 0) label = tf.dtypes.cast(label, dtype=tf.float32) label = tf.split(label, batch_size, 0) label = [tf.squeeze(l) for l in label] for k in range(batch_size): shape_pc = splits[k] l = label[k] cond0 = tm.logical_or(tm.equal(tflt(0), l), tm.equal(tflt(1), l)) cond1 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(2), l), tm.less_equal(l, tflt(15))), tm.equal(tflt(0), tm.mod(l, tflt(2)))) cond2 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(2), l), tm.less_equal(l, tflt(15))), tm.equal(tflt(1), tm.mod(l, tflt(2)))) cond3 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(16), l), tm.less_equal(l, tflt(39))), tm.equal(tflt(0), tm.mod(l, tflt(2)))) cond4 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(16), l), tm.less_equal(l, tflt(39))), tm.equal(tflt(1), tm.mod(l, tflt(2)))) cond5 = tm.logical_and(tm.less_equal(tflt(40), l), tm.less_equal(l, tflt(53))) call0= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi*l) call1= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/6, angle_z=2*(l-2)*np.pi/7) call2 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=5*np.pi/6, angle_z=2*(l-2)*np.pi/7) call3 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/3, angle_z=2*(l-16)*np.pi/12) call4 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=2*np.pi/3, angle_z=2*(l-16)*np.pi/12) call5 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/2, angle_z=2*(l-40)*np.pi/14) conditions = [cond0, cond1, cond2, cond3, cond4, cond5] callables = [call0, call1, call2, call3, call4, call5] shape_pc = nested_tf_cond(shape_pc, conditions, callables) rotated_data[k] = shape_pc rotated_data = tf.squeeze(tf.stack(rotated_data, axis=0)) return rotated_data
def _apply_dense(self, grad, var): # 1st step: we convert our 'Tensor' objects to have the type of the training variables lr_t = math_ops.cast(self._lr_t, var.dtype.base_dtype) alpha_t = math_ops.cast(self._alpha_t, var.dtype.base_dtype) beta_t = math_ops.cast(self._beta_t, var.dtype.base_dtype) gamma_0_t = math_ops.cast(self._gamma_0_t, var.dtype.base_dtype) gamma_power_t = math_ops.cast(self._gamma_power_t, var.dtype.base_dtype) init_velocity_t = math_ops.cast(self._init_velocity_t, var.dtype.base_dtype) # 2nd step: we define the gradient accumulations, using the identifier 'psi' from '_create_slots()' # We also memorize the old accumulator, since we will update the 'psi' variable. Here, 'psi_k' is defined in Castera et. al. psi_k = self.get_slot(var, "psi") # 3rd step: define the current iteration needed for the momentum inertial sequence # It must be converted to the same type as the trainable variables n = math_ops.cast(curr_it(), var.dtype.base_dtype) # Here, we consider the adaptive learning rate, denoted by 'gamma_k' in the original article # The formula is given in the upper part of page 10 adaptive_lr = lr_t * gamma_0_t / math_ops.pow(n + 1, gamma_power_t) # 4th step: initialize the old value of 'psi_k', depending on the current iteration (if it is equal to 0 or not) # If the number of iterations > 0, then 'psi_k' = psi (the additional 'Slot' variable) # the formula for iteration no. 0 is given in the upper part of page 20 # A correction: we must have '(beta^2 - beta * initial_velocity) * grad' in order to match the original implementation psi_cond = cond( equal(n, 0), lambda: (1.0 - alpha_t * beta_t) * var - beta_t**2 * grad + beta_t * init_velocity_t * grad, lambda: psi_k) # 5th step: we have the INDIAN formula 'psi_k <- psi_k - adaptive_lr * ((alpha - 1/beta) * theta_k + 1/beta * psi_k)' # We update 'accum' by assigning the value 'momentum_t * accum + grad' to it. Furthermore, the new value is return in the 'Tensor' object 'accum_t' with ops.control_dependencies([psi_cond]): psi_k_plus_one = psi_k.assign(psi_cond - adaptive_lr * ( (alpha_t - 1.0 / beta_t) * var + 1.0 / beta_t * psi_cond)) # 6th step: variables updates by using 'theta_k <- theta_k - adaptive_lr * ( (alpha-1/beta) * theta_k + 1/beta * psi_k + beta * grad)' # Here we use 'state_ops.assign_sub', so the sign of the coefficients is opposite to the one appearing in the 3rd line of relation (7) # Here, 'grad' is in fact 'v_k' from the algorithm (7) with ops.control_dependencies([psi_cond]): var_update = state_ops.assign_sub( var, adaptive_lr * ((alpha_t - 1.0 / beta_t) * var + 1.0 / beta_t * psi_cond + beta_t * grad)) # 7th step: return the updates, i.e. we return the Graph 'Operation' that will group multiple 'Tensor' ops. # For more complex algorithms, the 'control_flow_ops.group' is used in the '_finish()' function, after '_apply_dense()' return control_flow_ops.group(*[var_update, psi_k_plus_one])
def body(i, extras, n_set, current_val, seg): def case1(i, extras, n_set, current_val: tf.Tensor, seg): # current_val += 0 extras -= 1 n_set += 1 seg = tf.sparse.add( seg, tf.SparseTensor(indices=[[0, i]], values=[current_val], dense_shape=[1, n])) return extras, n_set, current_val, seg def case2(i, extras, n_set, current_val, seg): current_val += 1 n_set -= n_set n_set += 1 seg = tf.sparse.add( seg, tf.SparseTensor(indices=[[0, i]], values=[current_val], dense_shape=[1, n])) return extras, n_set, current_val, seg def case3(i, extras, n_set, current_val, seg): # current_val += 0 n_set += 1 seg = tf.sparse.add( seg, tf.SparseTensor(indices=[[0, i]], values=[current_val], dense_shape=[1, n])) return extras, n_set, current_val, seg extras, n_set, current_val, seg = tf.cond( logical_and(greater(extras, 0), equal(n_set, y)), lambda: case1(i, extras, n_set, current_val, seg), lambda: tf.cond( greater_equal(n_set, y), lambda: case2( i, extras, n_set, current_val, seg), lambda: case3( i, extras, n_set, current_val, seg))) return b(i), extras, n_set, current_val, seg
def rotate_tensor_by_label(batch_data, label, graph): """ Rotate a batch of points by [label] to 6 or 18 possible angles [label] is a tensor Input: BxNx3 array Return: BxNx3 array """ with graph.as_default(): batch_size = batch_data.get_shape().as_list()[0] rotated_data = [None] * batch_size splits = tf.split(batch_data, batch_size, 0) label = tf.dtypes.cast(label, dtype=tf.float32) label = tf.split(label, batch_size, 0) label = [tf.squeeze(l) for l in label] for k in range(batch_size): shape_pc = splits[k] l = label[k] cond0 = tm.equal(tflt(0), l) cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(3))) cond2 = tm.logical_and(tm.less_equal(tflt(4), l), tm.less_equal(l, tflt(5))) cond3 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(9))) cond4 = tm.logical_and(tm.less_equal(tflt(10), l), tm.less_equal(l, tflt(13))) cond5 = tm.logical_and(tm.less_equal(tflt(14), l), tm.less_equal(l, tflt(17))) call0= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=0) call1= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=(l)*np.pi/4) call2 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_z=(l*2-7)*np.pi/2) call3 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=(l*2-11)*np.pi/4) call4 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_z=(l*2-19)*np.pi/4) call5 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/2, angle_z=(l*2-27)*np.pi/4) conditions = [cond0, cond1, cond2, cond3, cond4, cond5] callables = [call0, call1, call2, call3, call4, call5] shape_pc = nested_tf_cond(conditions, callables) rotated_data[k] = shape_pc rotated_data = tf.squeeze(tf.stack(rotated_data, axis=0)) return rotated_data
def _apply_dense(self, grad, var): lr_t = math_ops.cast(self._lr_t, var.dtype.base_dtype) alpha_t = math_ops.cast(self._alpha_t, var.dtype.base_dtype) beta_t = math_ops.cast(self._beta_t, var.dtype.base_dtype) epsilon_t = math_ops.cast(self._epsilon_t, var.dtype.base_dtype) decay_t = math_ops.cast(self._decay_t, var.dtype.base_dtype) decaypower_t = math_ops.cast(self._decaypower_t, var.dtype.base_dtype) speed_ini_t = math_ops.cast(self._speed_ini_t, var.dtype.base_dtype) v = self.get_slot(var, "v1") #(1.-self.alpha*self.beta)*p ) #Initialise v such that the initial speed is in the direction of -grad v_temp = cond( equal(num_iter(),0) , lambda : (1.-alpha_t*beta_t) * var - beta_t**2 * grad + beta_t * speed_ini_t * grad, lambda : v ) ''' if k == 0: v_temp = (1.-alpha_t*beta_t) * var - beta_t**2 * grad + beta_t * speed_ini_t * grad else: v_temp = v ''' v_t = v.assign( v_temp - ( lr_t * decay_t / math_ops.pow(math_ops.cast(num_iter()+1, var.dtype.base_dtype),decaypower_t) ) * ( (alpha_t-1./beta_t) * var + 1./beta_t * v_temp ) )
def update(self, features, labels, pred_mean): accepted = tft.gather_nd(tf.cast(pred_mean > 0.5, tf.float32), tf.where(tfm.equal(features['sensitive'], 0))) return self._return_and_store(self.mean(accepted))
def update(self, features, labels, pred_mean): test_for_ybar0_s0 = tfm.logical_and( tfm.equal(features['ybar'], 0), tfm.equal(features['sensitive'], 0)) accepted = tf.gather_nd(1 - labels, tf.where(test_for_ybar0_s0)) self.mean(accepted)
def update(self, features, labels, pred_mean): accepted = tf.gather_nd(labels, tf.where(tfm.equal(features['sensitive'], 1))) self.mean(accepted)
def update(self, features, labels, pred_mean): accepted = tf.gather_nd(tf.cast(pred_mean > 0.5, tf.float32), tf.where(tfm.equal(features['sensitive'], 1))) self.mean(accepted)
def dice_enhancing_tumor(y_true, y_pred): mask_true, mask_pred = get_one_hot_from_output(y_true, y_pred) mask_true = tfmth.equal(mask_true, 3) mask_pred = tfmth.equal(mask_pred, 3) return dice_coefficient(mask_true, mask_pred)
def update(self, features, labels, pred_mean): accepted = tft.gather_nd(labels, tf.where(tfm.equal(features['sensitive'], 1))) return self._return_and_store(self.mean(accepted))