def _mutate(self): weight_mask = cp.random.normal( size=(self.weights.shape), dtype='single') * .05 * cp.tile( cp.triu(cp.ones(shape=(self.total, self.total), dtype='bool_'), 1), (self.pop_size, 1, 1)) * (cp.random.uniform( size=(self.weights.shape), dtype='single') < .2) * (self.weights != 0) new_weights_mask = cp.random.normal( size=(self.weights.shape), dtype='single') * .05 * cp.tile( cp.triu(cp.ones(shape=(self.total, self.total), dtype='bool_'), 1), (self.pop_size, 1, 1)) * (cp.random.uniform( size=(self.weights.shape), dtype='single') < .05) * (self.weights == 0) self.weights += weight_mask + new_weights_mask self.weights *= (cp.random.uniform(size=self.weights.shape, dtype='single') < 1) self.weights[:, :self.inputs, :self.inputs] = 0 self.weights[:, -self.outputs:, -self.outputs:] = cp.tile( cp.diag( cp.diag( cp.ones(shape=(self.outputs, self.outputs), dtype='bool_'))), (self.pop_size, 1, 1)) self.biases += cp.random.normal( size=self.biases.shape, dtype='single') * .05 * (cp.random.normal( size=self.biases.shape, dtype='single') < .2)
def test_lu_factor_reconstruction(self, dtype): m, n = self.shape A = testing.shaped_random(self.shape, cupy, dtype=dtype) lu, piv = cupyx.scipy.linalg.lu_factor(A) # extract ``L`` and ``U`` from ``lu`` L = cupy.tril(lu, k=-1) cupy.fill_diagonal(L, 1.) L = L[:, :m] U = cupy.triu(lu) U = U[:n, :] # check output shapes assert lu.shape == (m, n) assert L.shape == (m, min(m, n)) assert U.shape == (min(m, n), n) assert piv.shape == (min(m, n),) # apply pivot (on CPU since slaswp is not available in cupy) piv = cupy.asnumpy(piv) rows = numpy.arange(m) for i, row in enumerate(piv): if i != row: rows[i], rows[row] = rows[row], rows[i] PA = A[rows] # check that reconstruction is close to original LU = L.dot(U) cupy.testing.assert_allclose(LU, PA, atol=1e-5)
def forward(self, input_encodings, output_units, input_masks=None, output_masks=None): if input_masks is not None: input_masks = F.expand_dims(input_masks, -2) mask_shape = list(output_units.shape) mask_shape[-1] = mask_shape[-2] mask = xp.triu(xp.ones(mask_shape, dtype=xp.bool), k=1) if output_masks is not None: output_masks = F.expand_dims(output_masks, -2) mask = xp.logical_or(mask, output_masks.array) x1 = F.dropout( self.mmha(output_units, output_units, output_units, mask=mask), self.p_drop) x2 = self.lnorm1(output_units + x1) x3 = F.dropout( self.mha(x2, input_encodings, input_encodings, mask=input_masks), self.p_drop) x4 = self.lnorm2(x2 + x3) x5 = F.dropout(self.ff(x4), self.p_drop) x6 = self.lnorm3(x4 + x5) return x6
def test_csrilu02(self, dtype): dtype = numpy.dtype(dtype) a_ref = self._make_matrix(dtype) a = sparse.csr_matrix(a_ref) cusparse.csrilu02(a, level_info=self.level_info) a = a.todense() al = cupy.tril(a, k=-1) al = al + cupy.diag(cupy.ones((self.n, ), dtype=dtype.char.lower())) au = cupy.triu(a) a = al @ au tol = self._tol[dtype.char.lower()] cupy.testing.assert_allclose(a, a_ref, atol=tol, rtol=tol)
def __call__(self, f_atoms, f_bonds, super_node_x, action, pair_label, mask_pair_select, batch_size, atom_selected): loss = 0.0 mask_selection = cp.ones((batch_size, self.topK, self.topK)) mask_selection = cp.triu(mask_selection, 1) mask_selection = mask_selection.reshape(batch_size, self.topK * self.topK) for step in range(action.shape[1] - 1): h = self.ggnngwm(f_atoms, f_bonds, super_node_x) f = cp.zeros( (batch_size, self.topK, self.out_dim)).astype('float32') for i in range(batch_size): f[i] = h[i][atom_selected[i]].array f = functions.broadcast_to(functions.expand_dims(f, axis=2), (batch_size, self.topK, self.topK, self.out_dim)) + \ functions.broadcast_to(functions.expand_dims(f, axis=1), (batch_size, self.topK, self.topK, self.out_dim)) f = self.mlp( f.reshape(batch_size, self.topK * self.topK, self.out_dim))[:, :, 0] l = pair_label[:, step, :, :].reshape(batch_size, self.topK * self.topK) l = l * mask_selection m = mask_pair_select.reshape( batch_size, self.topK * self.topK) * mask_selection loss += softmax_cross_entropy_with_mask(f, l, m, batch_size) # execute one action in random order during each epoch action_step = action[:, step, :] mask_selection[cp.arange(batch_size), action_step[:, -1]] = 0 for i in range(batch_size): a = action_step[i] if a[0] < 0: continue else: f_bonds[i, :4, a[0], a[1]] = 0.0 f_bonds[i, :4, a[1], a[0]] = 0.0 f_bonds[i, 4, a[0], a[1]] = 1.0 f_bonds[i, 4, a[1], a[0]] = 1.0 if a[2] + 1 != 0: f_bonds[i, a[2], a[0], a[1]] = 1.0 f_bonds[i, a[2], a[1], a[0]] = 1.0 return loss
def _initialize_nets(self): self.weights = cp.random.normal( size=(self.pop_size, self.total, self.total), dtype='single') * cp.tile( cp.triu(cp.ones(shape=(self.total, self.total), dtype='bool_'), 1), (self.pop_size, 1, 1)) * (cp.random.uniform( size=(self.pop_size, self.total, self.total), dtype='single') < .5) self.weights[:, :, self.inputs:] *= cp.sqrt(4 / cp.minimum( cp.arange(self.inputs, self.total), self.inputs + self.hidden)) self.weights[:, :self.inputs, :self.inputs] = 0 self.weights[:, -self.outputs:, -self.outputs:] = cp.tile( cp.diag( cp.diag( cp.ones(shape=(self.outputs, self.outputs), dtype='bool_'))), (self.pop_size, 1, 1)) self.biases = cp.random.normal( size=(self.pop_size, 1, self.hidden + self.outputs), dtype='single') * .5
def time_triu_l10x10(self): np.triu(self.l10x10)
raise ValueError("x must be at least 2-dimensional for tril") return Array._new(np.tril(x._array, k=k)) def triu(x: Array, /, *, k: int = 0) -> Array: """ Array API compatible wrapper for :py:func:`np.triu <numpy.triu>`. See its docstring for more information. """ from ._array_object import Array if x.ndim < 2: # Note: Unlike np.triu, x must be at least 2-D raise ValueError("x must be at least 2-dimensional for triu") return Array._new(np.triu(x._array, k=k)) def zeros( shape: Union[int, Tuple[int, ...]], *, dtype: Optional[Dtype] = None, device: Optional[Device] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.zeros <numpy.zeros>`. See its docstring for more information. """ from ._array_object import Array
def test_each_channel_with_asymmetric_kernel(): mask = cp.triu(cp.ones(COLOR_IMAGE.shape[:2], dtype=np.bool_)) mask_each(COLOR_IMAGE, mask)