def sigmas_learned_coef(ctx, log_var0, log_var1): v0 = F.exp(log_var0) v1 = F.exp(log_var1) c0 = F.minimum_scalar(v0, 1.) c1 = F.minimum_scalar(v1, 1.) c = c1 / c0 return c
def build_train_graph(self, batch): self.solver = S.Adam(self.learning_rate) obs, action, reward, terminal, newobs = batch # Create input variables s = nn.Variable(obs.shape) a = nn.Variable(action.shape) r = nn.Variable(reward.shape) t = nn.Variable(terminal.shape) snext = nn.Variable(newobs.shape) with nn.parameter_scope(self.name_q): q = self.q_builder(s, self.num_actions, test=False) self.solver.set_parameters(nn.get_parameters()) with nn.parameter_scope(self.name_qnext): qnext = self.q_builder(snext, self.num_actions, test=True) qnext.need_grad = False clipped_r = F.minimum_scalar(F.maximum_scalar( r, -self.clip_reward), self.clip_reward) q_a = F.sum( q * F.one_hot(F.reshape(a, (-1, 1), inplace=False), (q.shape[1],)), axis=1) target = clipped_r + self.gamma * (1 - t) * F.max(qnext, axis=1) loss = F.mean(F.huber_loss(q_a, target)) Variables = namedtuple( 'Variables', ['s', 'a', 'r', 't', 'snext', 'q', 'loss']) self.v = Variables(s, a, r, t, snext, q, loss) self.sync_models() self.built = True
def net(n_class, xs, xq, init_type='nnabla', embedding='conv4', net_type='prototypical', distance='euclid', test=False): ''' Similarity net function This function implements the network with settings as specified. Args: n_class (int): number of classes. Typical setting is 5 or 20. xs (~nnabla.Variable): support images. xq (~nnabla.Variable): query images. init_type (str, optional): initialization type for weights and bias parameters. See conv_initializer function. embedding(str, optional): embedding network. distance (str, optional): similarity metric to use. See similarity function. test (bool, optional): switch flag for training dataset and test dataset Returns: h (~nnabla.Variable): output variable indicating similarity between support and query. ''' # feature embedding for supports and queries n_shot = xs.shape[0] / n_class n_query = xq.shape[0] / n_class if embedding == 'conv4': fs = conv4(xs, test, init_type) # tensor of (n_support, fdim) fq = conv4(xq, test, init_type) # tensor of (n_query, fdim) if net_type == 'matching': # This example does not include the full-context-embedding of matching networks. fs = F.reshape(fs, (1, ) + fs.shape) # (1, n_way, fdim) # (n_way*n_query, 1, fdim) fq = F.reshape(fq, (fq.shape[0], 1) + fq.shape[1:]) h = similarity(fq, fs, distance) h = h - F.mean(h, axis=1, keepdims=True) if 1 < n_shot: h = F.minimum_scalar(F.maximum_scalar(h, -35), 35) h = F.softmax(h) h = F.reshape(h, (h.shape[0], n_class, n_shot)) h = F.mean(h, axis=2) # Reverse to logit to use same softmax cross entropy h = F.log(h) elif net_type == 'prototypical': if 1 < n_shot: fs = F.reshape(fs, (n_class, n_shot) + fs.shape[1:]) fs = F.mean(fs, axis=1) fs = F.reshape(fs, (1, ) + fs.shape) # (1, n_way, fdim) # (n_way*n_query, 1, fdim) fq = F.reshape(fq, (fq.shape[0], 1) + fq.shape[1:]) h = similarity(fq, fs, distance) h = h - F.mean(h, axis=1, keepdims=True) return h
def sample_pdf(bins, weights, N_samples, det=False): """Sample additional points for training fine network Args: bins: int. Height in pixels. weights: int. Width in pixels. N_samples: float. Focal length of pinhole camera. det Returns: samples: array of shape [batch_size, 3]. Depth samples for fine network """ weights += 1e-5 pdf = weights / F.sum(weights, axis=-1, keepdims=True) cdf = F.cumsum(pdf, axis=-1) # if isinstance(pdf, nn.Variable): # cdf = nn.Variable.from_numpy_array(tf.math.cumsum(pdf.d, axis=-1)) # else: # cdf = nn.Variable.from_numpy_array(tf.math.cumsum(pdf.data, axis=-1)).data cdf = F.concatenate(F.constant(0, cdf[..., :1].shape), cdf, axis=-1) if det: u = F.arange(0., 1., 1 / N_samples) u = F.broadcast(u[None, :], cdf.shape[:-1] + (N_samples, )) u = u.data if isinstance(cdf, nn.NdArray) else u else: u = F.rand(shape=cdf.shape[:-1] + (N_samples, )) indices = F.searchsorted(cdf, u, right=True) # if isinstance(cdf, nn.Variable): # indices = nn.Variable.from_numpy_array( # tf.searchsorted(cdf.d, u.d, side='right').numpy()) # else: # indices = nn.Variable.from_numpy_array( # tf.searchsorted(cdf.data, u.data, side='right').numpy()) below = F.maximum_scalar(indices - 1, 0) above = F.minimum_scalar(indices, cdf.shape[-1] - 1) indices_g = F.stack(below, above, axis=below.ndim) cdf_g = F.gather(cdf, indices_g, axis=-1, batch_dims=len(indices_g.shape) - 2) bins_g = F.gather(bins, indices_g, axis=-1, batch_dims=len(indices_g.shape) - 2) denom = (cdf_g[..., 1] - cdf_g[..., 0]) denom = F.where(F.less_scalar(denom, 1e-5), F.constant(1, denom.shape), denom) t = (u - cdf_g[..., 0]) / denom samples = bins_g[..., 0] + t * (bins_g[..., 1] - bins_g[..., 0]) return samples
def sample_network(x_curr, sdf_cur, raydir, grad_curr): """ x_curr: Points (B, R, 3) either on surface or not sdf_cur: SDF on x_curr (B, R, 1) raydir: Ray direction (B, R, 3) grad_curr: Gradients on x_curr (B, R, 3) """ # Denominator de = F.batch_matmul(grad_curr[..., np.newaxis, :], raydir[..., np.newaxis, :], transpose_b=True) de = de.reshape(sdf_cur.shape) de_inv = (1.0 / de).apply(need_grad=False) de_inv = F.minimum_scalar(de_inv, 1e30).apply( need_grad=False) # (numerical issue de = cos(x, y) = 0) # Differentiable intersection point (discrete update of implicit differentiation) sdf_cur0 = sdf_cur.get_unlinked_variable(need_grad=False) x_hat = x_curr - (sdf_cur - sdf_cur0) * de_inv * raydir return x_hat
def clip_scalar(v, min_value, max_value): return F.minimum_scalar(F.maximum_scalar(v, min_value), max_value)
def srwu_learned_coef(ctx, log_var): v = F.exp(log_var) c = F.minimum_scalar(v, 1.) return c
def clip_by_value(x, minimum, maximum): return F.minimum_scalar(F.maximum_scalar(x, minimum), maximum)