void euclidean_module<T1,T2,Tstate1,Tstate2>:: bprop(Tstate1 &in1, Tstate2 &label, Tstate1 &energy) { idx_checkorder1(energy.x, 0); // energy.x must have an order of 0 idx_sub(in1.x, in2.x, in1.dx); // derivative with respect to in1 idx_dotc(in1.dx, energy.dx.get(), in1.dx); // multiply by energy derivative idx_minus(in1.dx, in2.dx); // derivative with respect to in2 }
void euclidean_module<T1,T2,Tstate1,Tstate2>:: fprop(Tstate1 &in1, Tstate2 &label, Tstate1 &energy) { idx<T1> target = targets.select(0, label.x.get()); idx_copy(target, in2.x); // squared distance between in1 and target idx_sqrdist(in1.x, in2.x, energy.x); idx_dotc(energy.x, 0.5, energy.x); // multiply by .5 }
class_answer<T, Tds1, Tds2>:: class_answer(uint nclasses, double target_factor, bool binary_target_, t_confidence conf, bool apply_tanh_, const char *name_, int force, int single, idxdim *kerd, double sigma_scale) : answer_module<T, Tds1, Tds2>(binary_target_ ? 1 : nclasses, name_), conf_type(conf), binary_target(binary_target_), resize_output(true), apply_tanh(apply_tanh_), tmp(1, 1, 1), force_class(force), single_output(single) { // create 1-of-n targets with target 1.0 for shown class, -1.0 for the rest targets = create_target_matrix<T>(nclasses, (T)1.0); // binary target if (binary_target) { if (nclasses != 2) eblerror("expecting 2 classes only when binary_target is on"); targets = idx<T>(2, 1, 1); // int neg_id = ds.get_class_id("bg"); // negative class // if (neg_id == 0) { // targets.set(-1.0, 0, 0); // negative: -1.0 // targets.set( 1.0, 1, 0); // positive: 1.0 // } else { targets.sset((T)1.0, 0); // positive: 1.0 targets.sset((T)-1.0, 1); // negative: -1.0 // } } // target factor idx_dotc(targets, target_factor, targets); print_targets(targets); // set min/max of target target_min = idx_min(targets); target_max = idx_max(targets); target_range = target_max - target_min; // set confidence parameters T max_dist; switch (conf_type) { case confidence_sqrdist: max_dist = target_max - target_min; conf_ratio = targets.dim(0) * max_dist * max_dist; // shift value to be subtracted before dividing by conf_ratio conf_shift = target_min; eblprint("Using sqrdist confidence formula with normalization ratio " << conf_ratio << " and shift value " << conf_shift << std::endl); break; case confidence_single: conf_ratio = target_max - target_min; // shift value to be subtracted before dividing by conf_ratio conf_shift = target_min; eblprint("Using single output confidence with normalization ratio " << conf_ratio << " and shift value " << conf_shift << std::endl); break; case confidence_max: if (force >= 0) { conf_ratio = target_max - target_min; conf_shift = -conf_ratio; conf_ratio *= 2; } else { conf_ratio = target_max - target_min; conf_shift = 0; // no shift needed, the difference min is 0. } eblprint("Using max confidence formula with normalization ratio " << conf_ratio << std::endl); break; default: eblerror("confidence type " << conf_type << " undefined"); } if (kerd && kerd->order() == 2) smoothing_kernel = create_mexican_hat2<T>(kerd->dim(0), kerd->dim(1), 1, sigma_scale); else smoothing_kernel = create_mexican_hat2<T>(9, 9, 1, sigma_scale); eblprint("smoothing kernel:" << std::endl); smoothing_kernel.print(); }