def __init__(self, features, class_num=1000): super(VGG, self).__init__() self.features = features self.flatten = layers.Flatten() self.classifier = layers.SequentialLayer([ layers.Dense(512 * 7 * 7, 4096), layers.ReLU(), layers.Dropout(), layers.Dense(4096, 4096), layers.ReLU(), layers.Dropout(), layers.Dense(4096, class_num), ])
def __init__(self, in_channels, out_channels, initializer_range=0.02, dropout_prob=0.1, compute_type=ts.float32): super(BertOutput, self).__init__() self.dense = layers.Dense(in_channels, out_channels, weight_init=TruncatedNormal(initializer_range)).to_float(compute_type) self.dropout = layers.Dropout(1 - dropout_prob) self.dropout_prob = dropout_prob self.add = P.Add() self.layernorm = layers.LayerNorm((out_channels,)).to_float(compute_type) self.cast = P.Cast()
def __init__(self, config, is_training, num_labels=2, dropout_prob=0.0, use_one_hot_embeddings=False, assessment_method=""): super(BertCLSModel, self).__init__() if not is_training: config.hidden_dropout_prob = 0.0 config.hidden_probs_dropout_prob = 0.0 self.bert = Bert(config, is_training, use_one_hot_embeddings) self.cast = P.Cast() self.weight_init = TruncatedNormal(config.initializer_range) self.log_softmax = P.LogSoftmax(axis=-1) self.dtype = config.dtype self.num_labels = num_labels self.dense_1 = layers.Dense(config.hidden_size, self.num_labels, weight_init=self.weight_init, has_bias=True).to_float(config.compute_type) self.dropout = layers.Dropout(1 - dropout_prob) self.assessment_method = assessment_method
def __init__(self, outer_nc, inner_nc, in_planes=None, dropout=False, submodule=None, outermost=False, innermost=False, alpha=0.2, norm_mode='batch'): super(UnetSkipConnectionBlock, self).__init__() downnorm = layers.BatchNorm2d(inner_nc) upnorm = layers.BatchNorm2d(outer_nc) use_bias = False if norm_mode == 'instance': downnorm = layers.BatchNorm2d(inner_nc, affine=False) upnorm = layers.BatchNorm2d(outer_nc, affine=False) use_bias = True if in_planes is None: in_planes = outer_nc downconv = layers.Conv2d(in_planes, inner_nc, kernel_size=4, stride=2, padding=1, has_bias=use_bias, pad_mode='pad') downrelu = layers.LeakyReLU(alpha) uprelu = layers.ReLU() if outermost: upconv = layers.Conv2dTranspose(inner_nc * 2, outer_nc, kernel_size=4, stride=2, padding=1, pad_mode='pad') down = [downconv] up = [uprelu, upconv, layers.Tanh()] model = down + [submodule] + up elif innermost: upconv = layers.Conv2dTranspose(inner_nc, outer_nc, kernel_size=4, stride=2, padding=1, has_bias=use_bias, pad_mode='pad') down = [downrelu, downconv] up = [uprelu, upconv, upnorm] model = down + up else: upconv = layers.Conv2dTranspose(inner_nc * 2, outer_nc, kernel_size=4, stride=2, padding=1, has_bias=use_bias, pad_mode='pad') down = [downrelu, downconv, downnorm] up = [uprelu, upconv, upnorm] model = down + [submodule] + up if dropout: model.append(layers.Dropout(0.5)) self.model = layers.SequentialLayer(model) self.skip_connections = not outermost self.concat = Concat(axis=1)
def __init__(self, dim, norm_mode='batch', dropout=True, pad_mode="CONSTANT"): super(ResidualBlock, self).__init__() self.conv1 = ConvNormReLU(dim, dim, 3, 1, 0, norm_mode, pad_mode) self.conv2 = ConvNormReLU(dim, dim, 3, 1, 0, norm_mode, pad_mode, use_relu=False) self.dropout = dropout if dropout: self.dropout = layers.Dropout(0.5)
def __init__(self, config, is_training, num_labels=11, use_crf=False, dropout_prob=0.0, use_one_hot_embeddings=False): super(BertNERModel, self).__init__() if not is_training: config.hidden_dropout_prob = 0.0 config.hidden_probs_dropout_prob = 0.0 self.bert = Bert(config, is_training, use_one_hot_embeddings) self.cast = P.Cast() self.weight_init = TruncatedNormal(config.initializer_range) self.log_softmax = P.LogSoftmax(axis=-1) self.dtype = config.dtype self.num_labels = num_labels self.dense_1 = layers.Dense(config.hidden_size, self.num_labels, weight_init=self.weight_init, has_bias=True).to_float(config.compute_type) self.dropout = layers.Dropout(1 - dropout_prob) self.reshape = P.Reshape() self.shape = (-1, config.hidden_size) self.use_crf = use_crf self.origin_shape = (-1, config.seq_length, self.num_labels)
def __init__(self, embedding_size, embedding_shape, use_relative_positions=False, use_token_type=False, token_type_vocab_size=16, use_one_hot_embeddings=False, initializer_range=0.02, max_position_embeddings=512, dropout_prob=0.1): super(EmbeddingPostprocessor, self).__init__() self.use_token_type = use_token_type self.token_type_vocab_size = token_type_vocab_size self.use_one_hot_embeddings = use_one_hot_embeddings self.max_position_embeddings = max_position_embeddings self.embedding_table = Parameter(initializer (TruncatedNormal(initializer_range), [token_type_vocab_size, embedding_size]), name='embedding_table') self.shape_flat = (-1,) self.one_hot = layers.OneHot() self.on_value = Tensor(1.0, ts.float32) self.off_value = Tensor(0.1, ts.float32) self.array_mul = P.MatMul() self.reshape = P.Reshape() self.shape = tuple(embedding_shape) self.layernorm = layers.LayerNorm((embedding_size,)) self.dropout = layers.Dropout(1 - dropout_prob) self.gather = P.Gather() self.use_relative_positions = use_relative_positions self.slice = P.StridedSlice() self.full_position_embeddings = Parameter(initializer (TruncatedNormal(initializer_range), [max_position_embeddings, embedding_size]), name='full_position_embeddings')
def __init__(self, from_tensor_width, to_tensor_width, from_seq_length, to_seq_length, num_attention_heads=1, size_per_head=512, query_act=None, key_act=None, value_act=None, has_attention_mask=False, attention_probs_dropout_prob=0.0, use_one_hot_embeddings=False, initializer_range=0.02, do_return_2d_tensor=False, use_relative_positions=False, compute_type=ts.float32): super(BertAttention, self).__init__() self.from_seq_length = from_seq_length self.to_seq_length = to_seq_length self.num_attention_heads = num_attention_heads self.size_per_head = size_per_head self.has_attention_mask = has_attention_mask self.use_relative_positions = use_relative_positions self.scores_mul = 1.0 / math.sqrt(float(self.size_per_head)) self.reshape = P.Reshape() self.shape_from_2d = (-1, from_tensor_width) self.shape_to_2d = (-1, to_tensor_width) weight = TruncatedNormal(initializer_range) units = num_attention_heads * size_per_head self.query_layer = layers.Dense(from_tensor_width, units, activation=query_act, weight_init=weight).to_float(compute_type) self.key_layer = layers.Dense(to_tensor_width, units, activation=key_act, weight_init=weight).to_float(compute_type) self.value_layer = layers.Dense(to_tensor_width, units, activation=value_act, weight_init=weight).to_float(compute_type) self.shape_from = (-1, from_seq_length, num_attention_heads, size_per_head) self.shape_to = (-1, to_seq_length, num_attention_heads, size_per_head) self.matmul_trans_b = P.BatchMatMul(transpose_b=True) self.multiply = P.Mul() self.transpose = P.Transpose() self.trans_shape = (0, 2, 1, 3) self.trans_shape_relative = (2, 0, 1, 3) self.trans_shape_position = (1, 2, 0, 3) self.multiply_data = -10000.0 self.matmul = P.BatchMatMul() self.softmax = layers.Softmax() self.dropout = layers.Dropout(1 - attention_probs_dropout_prob) if self.has_attention_mask: self.expand_dims = P.ExpandDims() self.sub = P.Sub() self.add = P.Add() self.cast = P.Cast() self.get_dtype = P.DType() if do_return_2d_tensor: self.shape_return = (-1, num_attention_heads * size_per_head) else: self.shape_return = (-1, from_seq_length, num_attention_heads * size_per_head) self.cast_compute_type = SaturateCast(dst_type=compute_type) if self.use_relative_positions: self._generate_relative_positions_embeddings = \ RelaPosEmbeddingsGenerator(length=to_seq_length, depth=size_per_head, max_relative_position=16, initializer_range=initializer_range, use_one_hot_embeddings=use_one_hot_embeddings)