Code
stringlengths
103
85.9k
Summary
sequencelengths
0
94
Please provide a description of the function:def file_generator(self, filepaths, max_chars_per_file=None, max_chars_total=None): chars_total = 0 for fname in filepaths: chars_this_file = 0 tf.logging.info("reading file %s" % fname) for text in self.filepath_to_unicode_strings(fname): if (max_chars_per_file and chars_this_file + len(text) > max_chars_per_file): text = text[:max_chars_per_file - chars_this_file] if max_chars_total and chars_total + len(text) > max_chars_total: text = text[:max_chars_total - chars_total] chars_total += len(text) chars_this_file += len(text) if text: yield text if max_chars_total and chars_total >= max_chars_total: return if max_chars_per_file and chars_this_file >= max_chars_per_file: break
[ "Read complete text of input files and yield unicode strings.\n\n By default, one unicode string is produced per file, but this is\n not guaranteed, since subclasses can override\n filepath_to_unicode_strings().\n\n max_chars_per_file and max_chars_total can also be specified, in which\n case some strings may be truncated or dropped to limit the total\n amount of output.\n\n Args:\n filepaths: a list of strings\n max_chars_per_file: an optional integer\n max_chars_total: an optional integer\n Yields:\n unicode strings\n " ]
Please provide a description of the function:def example_generator(self, encoder, tmp_dir, task_id): filepaths = self.text_filepaths_for_task(tmp_dir, task_id) if task_id >= self.num_train_shards: # this is dev data - limit the total length. max_chars_per_file = self.max_dev_chars // ( self.num_dev_shards * len(filepaths)) else: max_chars_per_file = None tokens = [] for ftext in self.file_generator( filepaths, max_chars_per_file=max_chars_per_file): tokens.extend(encoder.encode(ftext)) pos = 0 while pos + self.sequence_length <= len(tokens): yield {"targets": tokens[pos:pos + self.sequence_length]} pos += self.sequence_length if pos > 0: tokens = tokens[pos:] if self.remainder_policy == "pad": if tokens: targets = tokens + [0] * (self.sequence_length - len(tokens)) yield {"targets": targets} else: assert self.remainder_policy == "drop"
[ "Generator for examples.\n\n Args:\n encoder: a TextEncoder\n tmp_dir: a string\n task_id: an integer\n Yields:\n feature dictionaries\n " ]
Please provide a description of the function:def prepare_to_generate(self, data_dir, tmp_dir): self.get_or_create_vocab(data_dir, tmp_dir) self.train_text_filepaths(tmp_dir) self.dev_text_filepaths(tmp_dir)
[ "Make sure that the data is prepared and the vocab is generated." ]
Please provide a description of the function:def generate_data(self, data_dir, tmp_dir, task_id=-1): tf.logging.info("generate_data task_id=%s" % task_id) encoder = self.get_or_create_vocab(data_dir, tmp_dir) assert task_id >= 0 and task_id < self.num_generate_tasks if task_id < self.num_train_shards: out_file = self.training_filepaths( data_dir, self.num_train_shards, shuffled=False)[task_id] else: out_file = self.dev_filepaths( data_dir, self.num_dev_shards, shuffled=False)[task_id - self.num_train_shards] generator_utils.generate_files( self.example_generator(encoder, tmp_dir, task_id), [out_file]) generator_utils.shuffle_dataset([out_file])
[ "Generates training/dev data.\n\n Args:\n data_dir: a string\n tmp_dir: a string\n task_id: an optional integer\n Returns:\n shard or shards for which data was generated.\n " ]
Please provide a description of the function:def ConvBlock(kernel_size, filters, strides): ks = kernel_size filters1, filters2, filters3 = filters main = layers.Serial( layers.Conv(filters1, (1, 1), strides), layers.BatchNorm(), layers.Relu(), layers.Conv(filters2, (ks, ks), padding='SAME'), layers.BatchNorm(), layers.Relu(), layers.Conv(filters3, (1, 1)), layers.BatchNorm() ) shortcut = layers.Serial( layers.Conv(filters3, (1, 1), strides), layers.BatchNorm() ) return layers.Serial( layers.Branch(), layers.Parallel(main, shortcut), layers.SumBranches(), layers.Relu() )
[ "ResNet convolutional striding block." ]
Please provide a description of the function:def IdentityBlock(kernel_size, filters): ks = kernel_size filters1, filters2, filters3 = filters main = layers.Serial( layers.Conv(filters1, (1, 1)), layers.BatchNorm(), layers.Relu(), layers.Conv(filters2, (ks, ks), padding='SAME'), layers.BatchNorm(), layers.Relu(), layers.Conv(filters3, (1, 1)), layers.BatchNorm() ) return layers.Serial( layers.Branch(), layers.Parallel(main, layers.Identity()), layers.SumBranches(), layers.Relu() )
[ "ResNet identical size block." ]
Please provide a description of the function:def Resnet50(hidden_size=64, num_output_classes=1001, mode='train'): del mode return layers.Serial( layers.Conv(hidden_size, (7, 7), (2, 2), 'SAME'), layers.BatchNorm(), layers.Relu(), layers.MaxPool(pool_size=(3, 3), strides=(2, 2)), ConvBlock(3, [hidden_size, hidden_size, 4 * hidden_size], (1, 1)), IdentityBlock(3, [hidden_size, hidden_size, 4 * hidden_size]), IdentityBlock(3, [hidden_size, hidden_size, 4 * hidden_size]), ConvBlock(3, [2 * hidden_size, 2 * hidden_size, 8 * hidden_size], (2, 2)), IdentityBlock(3, [2 * hidden_size, 2 * hidden_size, 8 * hidden_size]), IdentityBlock(3, [2 * hidden_size, 2 * hidden_size, 8 * hidden_size]), IdentityBlock(3, [2 * hidden_size, 2 * hidden_size, 8 * hidden_size]), ConvBlock(3, [4 * hidden_size, 4 * hidden_size, 16*hidden_size], (2, 2)), IdentityBlock(3, [4 * hidden_size, 4 * hidden_size, 16 * hidden_size]), IdentityBlock(3, [4 * hidden_size, 4 * hidden_size, 16 * hidden_size]), IdentityBlock(3, [4 * hidden_size, 4 * hidden_size, 16 * hidden_size]), IdentityBlock(3, [4 * hidden_size, 4 * hidden_size, 16 * hidden_size]), IdentityBlock(3, [4 * hidden_size, 4 * hidden_size, 16 * hidden_size]), ConvBlock(3, [8 * hidden_size, 8 * hidden_size, 32*hidden_size], (2, 2)), IdentityBlock(3, [8 * hidden_size, 8 * hidden_size, 32 * hidden_size]), IdentityBlock(3, [8 * hidden_size, 8 * hidden_size, 32 * hidden_size]), layers.AvgPool(pool_size=(7, 7)), layers.Flatten(), layers.Dense(num_output_classes), layers.LogSoftmax())
[ "ResNet.\n\n Args:\n hidden_size: the size of the first hidden layer (multiplied later).\n num_output_classes: how many classes to distinguish.\n mode: whether we are training or evaluating or doing inference.\n\n Returns:\n The ResNet model with the given layer and output sizes.\n " ]
Please provide a description of the function:def WideResnetBlock(channels, strides=(1, 1), channel_mismatch=False): main = layers.Serial(layers.BatchNorm(), layers.Relu(), layers.Conv(channels, (3, 3), strides, padding='SAME'), layers.BatchNorm(), layers.Relu(), layers.Conv(channels, (3, 3), padding='SAME')) shortcut = layers.Identity() if not channel_mismatch else layers.Conv( channels, (3, 3), strides, padding='SAME') return layers.Serial( layers.Branch(), layers.Parallel(main, shortcut), layers.SumBranches())
[ "WideResnet convolutational block." ]
Please provide a description of the function:def WideResnet(num_blocks=3, hidden_size=64, num_output_classes=10, mode='train'): del mode return layers.Serial( layers.Conv(hidden_size, (3, 3), padding='SAME'), WideResnetGroup(num_blocks, hidden_size), WideResnetGroup(num_blocks, hidden_size * 2, (2, 2)), WideResnetGroup(num_blocks, hidden_size * 4, (2, 2)), layers.BatchNorm(), layers.Relu(), layers.AvgPool(pool_size=(8, 8)), layers.Flatten(), layers.Dense(num_output_classes), layers.LogSoftmax())
[ "WideResnet from https://arxiv.org/pdf/1605.07146.pdf.\n\n Args:\n num_blocks: int, number of blocks in a group.\n hidden_size: the size of the first hidden layer (multiplied later).\n num_output_classes: int, number of classes to distinguish.\n mode: is it training or eval.\n\n Returns:\n The WideResnet model with given layer and output sizes.\n " ]
Please provide a description of the function:def GRUCell(units): return GeneralGRUCell( candidate_transform=lambda: core.Dense(units=units), memory_transform=combinators.Identity, gate_nonlinearity=core.Sigmoid, candidate_nonlinearity=core.Tanh)
[ "Builds a traditional GRU cell with dense internal transformations.\n\n Gated Recurrent Unit paper: https://arxiv.org/abs/1412.3555\n\n\n Args:\n units: Number of hidden units.\n\n Returns:\n A Stax model representing a traditional GRU RNN cell.\n " ]
Please provide a description of the function:def ConvGRUCell(units, kernel_size=(3, 3)): def BuildConv(): return core.Conv(filters=units, kernel_size=kernel_size, padding='SAME') return GeneralGRUCell( candidate_transform=BuildConv, memory_transform=combinators.Identity, gate_nonlinearity=core.Sigmoid, candidate_nonlinearity=core.Tanh)
[ "Builds a convolutional GRU.\n\n Paper: https://arxiv.org/abs/1511.06432.\n\n Args:\n units: Number of hidden units\n kernel_size: Kernel size for convolution\n\n Returns:\n A Stax model representing a GRU cell with convolution transforms.\n " ]
Please provide a description of the function:def GeneralGRUCell(candidate_transform, memory_transform=combinators.Identity, gate_nonlinearity=core.Sigmoid, candidate_nonlinearity=core.Tanh, dropout_rate_c=0.1, sigmoid_bias=0.5): r return combinators.Serial( combinators.Branch(num_branches=3), combinators.Parallel( # s_{t-1} branch - optionally transform # Typically is an identity. memory_transform(), # u_t (Update gate) branch combinators.Serial( candidate_transform(), # Want bias to start out positive before sigmoids. core.AddConstant(constant=sigmoid_bias), gate_nonlinearity()), # c_t (Candidate) branch combinators.Serial( combinators.Branch(num_branches=2), combinators.Parallel( combinators.Identity(), # r_t (Reset) Branch combinators.Serial( candidate_transform(), # Want bias to start out positive before sigmoids. core.AddConstant(constant=sigmoid_bias), gate_nonlinearity())), ## Gate S{t-1} with sigmoid(candidate_transform(S{t-1})) combinators.MultiplyBranches(), # Final projection + tanh to get Ct candidate_transform(), candidate_nonlinearity()), # Candidate gate # Only apply dropout on the C gate. # Paper reports that 0.1 is a good default. core.Dropout(rate=dropout_rate_c)), # Gate memory and candidate combinators.GateBranches())
[ "Parametrized Gated Recurrent Unit (GRU) cell construction.\n\n GRU update equations:\n $$ Update gate: u_t = \\sigmoid(U' * s_{t-1} + B') $$\n $$ Reset gate: r_t = \\sigmoid(U'' * s_{t-1} + B'') $$\n $$ Candidate memory: c_t = \\tanh(U * (r_t \\odot s_{t-1}) + B) $$\n $$ New State: s_t = u_t \\odot s_{t-1} + (1 - u_t) \\odot c_t $$\n\n See combinators.GateBranches for details on the gating function.\n\n\n Args:\n candidate_transform: Transform to apply inside the Candidate branch. Applied\n before nonlinearities.\n memory_transform: Optional transformation on the memory before gating.\n gate_nonlinearity: Function to use as gate activation. Allows trying\n alternatives to Sigmoid, such as HardSigmoid.\n candidate_nonlinearity: Nonlinearity to apply after candidate branch. Allows\n trying alternatives to traditional Tanh, such as HardTanh\n dropout_rate_c: Amount of dropout on the transform (c) gate. Dropout works\n best in a GRU when applied exclusively to this branch.\n sigmoid_bias: Constant to add before sigmoid gates. Generally want to start\n off with a positive bias.\n\n Returns:\n A model representing a GRU cell with specified transforms.\n " ]
Please provide a description of the function:def MakeTargetMask(target, pad=0): target_mask = (target != pad)[ :, np.newaxis, :] target_dtype = target_mask.dtype causal_mask = onp.tril(onp.ones((1, target.shape[-1], target.shape[-1]), dtype=target_dtype), k=0) target_mask = target_mask & causal_mask return np.expand_dims(target_mask, axis=1)
[ "Create an attention mask to hide padding and future words." ]
Please provide a description of the function:def PreparePairedSequenceBatch(source, target_in, pad=0): target = target_in[:, :-1] target_y = target_in[:, 1:] source_mask = np.reshape(source != pad, (source.shape[0], 1, 1, source.shape[-1])) target_mask = MakeTargetMask(target, pad) memory_mask = ( np.reshape(np.arange(target.shape[-1]) < source.shape[-1], [-1, 1])) ntokens = np.sum(target_y != pad) return (source, target, target_y, source_mask, target_mask, memory_mask, ntokens)
[ "Build masks for this batch.\n\n Args:\n source: (batch, source_len) array of integer-coded symbols for inputs\n target_in: (batch, batch_len) array of integer-coded symbols for targets\n pad: int: the padding symbol used to pad the above\n\n Returns:\n Prepared batch of tuple of arrays: source, input-target, shifted-target,\n source mask, target mask, source-target \"memory\" mask, minibatch token count\n " ]
Please provide a description of the function:def _layer_norm_new_params(input_shape, rng, epsilon=1e-6): # pylint: disable=invalid-name del rng, epsilon features = input_shape[-1] scale = np.ones(features) bias = np.zeros(features) return (scale, bias)
[ "Helper: create layer norm parameters." ]
Please provide a description of the function:def _positional_encoding_new_params(input_shape, rng, max_len=2048): # pylint: disable=invalid-name del rng # Check if we are operating on chunked inputs by checking if the first # shape is a list/tuple of shapes (otherwise it's an int or numpy array). is_chunked = isinstance(input_shape[0], (list, tuple)) feature_depth = input_shape[0][-1] if is_chunked else input_shape[-1] pe = onp.zeros((max_len, feature_depth), dtype=onp.float32) position = onp.arange(0, max_len)[:, onp.newaxis] div_term = onp.exp( onp.arange(0, feature_depth, 2) * -(onp.log(10000.0) / feature_depth)) pe[:, 0::2] = onp.sin(position * div_term) pe[:, 1::2] = onp.cos(position * div_term) pe = pe[onp.newaxis, :, :] # [1, max_len, feature_depth] return np.array(pe)
[ "Helper: create positional encoding parameters." ]
Please provide a description of the function:def PositionalEncoding(x, params, **unused_kwargs): if not isinstance(x, (list, tuple)): # non-chunked inputs symbol_size = np.shape(x)[1] return x + params[:, :symbol_size, :] # Chunked case: apply to all chunks selecting as much as needed. offset = 0 results = [] for chunk in x: symbol_size = np.shape(chunk)[1] results.append(chunk + params[:, offset:offset + symbol_size, :]) offset += symbol_size return results
[ "Implements bare positional encoding." ]
Please provide a description of the function:def DotProductAttention(query, key, value, mask, dropout, mode, rng): depth = np.shape(query)[-1] dots = np.matmul(query, np.swapaxes(key, -1, -2)) / np.sqrt(depth) if mask is not None: dots = np.where(mask, dots, -1e9) # Softmax. dots = np.exp(dots - backend.logsumexp(dots, axis=-1, keepdims=True)) if dropout >= 1.0: raise ValueError('Dropout rates must be lower than 1.') if dropout is not None and dropout > 0.0 and mode == 'train': keep = backend.random.bernoulli(rng, 1.0 - dropout, dots.shape) dots = np.where(keep, dots / (1.0 - dropout), 0) out = np.matmul(dots, value) return out
[ "Core dot product self-attention.\n\n Args:\n query: array of representations\n key: array of representations\n value: array of representations\n mask: attention-mask, gates attention\n dropout: float: dropout rate\n mode: 'eval' or 'train': whether to use dropout\n rng: JAX PRNGKey: subkey for disposable use\n\n Returns:\n Self attention for q, k, v arrays.\n " ]
Please provide a description of the function:def PureDotProductAttention(dropout=0.0, mode='train'): def init_fun(_, input_shapes): # pylint: disable=invalid-name q_shape, _, v_shape, _ = input_shapes output_shape = q_shape[:-1] + (v_shape[-1],) return output_shape, () def apply_fun(params, inputs, **kwargs): # pylint: disable=invalid-name del params q, k, v, mask = inputs rng = kwargs.get('rng', None) return DotProductAttention(q, k, v, mask, dropout=dropout, mode=mode, rng=rng) return init_fun, apply_fun
[ "Pure single-headed self-attention.\n\n Args:\n dropout: float: dropout rate\n mode: str: 'train' or 'eval'\n\n Returns:\n Pure single-headed attention layer. (No Dense transforms on input.)\n " ]
Please provide a description of the function:def PureMultiHeadedAttention(x, params, num_heads=8, dropout=0.0, mode='train', **kwargs): del params rng = kwargs.get('rng', None) (q, k, v), mask = x feature_depth = q.shape[-1] assert feature_depth % num_heads == 0 head_depth = feature_depth // num_heads nbatch = np.shape(q)[0] # nbatch, seqlen, feature_depth --> nbatch, num_heads, seqlen, head_depth def SplitHeads(x): return np.transpose( np.reshape(x, (nbatch, -1, num_heads, head_depth)), (0, 2, 1, 3)) # nbatch, num_heads, seqlen, head_depth --> nbatch, seqlen, feature_depth def JoinHeads(x): # pylint: disable=invalid-name return np.reshape( np.transpose(x, (0, 2, 1, 3)), (nbatch, -1, num_heads*head_depth)) # Split heads, dot-product attention, rejoin heads. return JoinHeads( DotProductAttention( SplitHeads(q), SplitHeads(k), SplitHeads(v), mask, dropout=dropout, mode=mode, rng=rng))
[ "Pure transformer-style multi-headed attention.\n\n Args:\n x: inputs ((q, k, v), mask)\n params: parameters (none)\n num_heads: int: number of attention heads\n dropout: float: dropout rate\n mode: str: 'train' or 'eval'\n **kwargs: other arguments including the rng\n\n Returns:\n Pure Multi-headed attention layer (no Dense transforms on input).\n " ]
Please provide a description of the function:def MultiHeadedAttentionQKV( feature_depth, num_heads=8, dropout=0.0, mode='train'): return combinators.Serial( combinators.Parallel( combinators.Parallel( core.Dense(feature_depth), core.Dense(feature_depth), core.Dense(feature_depth), ), combinators.Identity() ), PureMultiHeadedAttention( # pylint: disable=no-value-for-parameter feature_depth=feature_depth, num_heads=num_heads, dropout=dropout, mode=mode), core.Dense(feature_depth), )
[ "Transformer-style multi-headed attention.\n\n Accepts inputs of the form (q, k, v), mask.\n\n Args:\n feature_depth: int: depth of embedding\n num_heads: int: number of attention heads\n dropout: float: dropout rate\n mode: str: 'train' or 'eval'\n\n Returns:\n Multi-headed self-attention layer.\n " ]
Please provide a description of the function:def MultiHeadedAttention( feature_depth, num_heads=8, dropout=0.0, mode='train'): return combinators.Serial( combinators.Parallel( combinators.Branch(num_branches=3), # q = k = v = first input combinators.Identity() # pass the mask ), MultiHeadedAttentionQKV( # pylint: disable=no-value-for-parameter feature_depth, num_heads=num_heads, dropout=dropout, mode=mode), )
[ "Transformer-style multi-headed attention.\n\n Accepts inputs of the form (x, mask) and constructs (q, k, v) from x.\n\n Args:\n feature_depth: int: depth of embedding\n num_heads: int: number of attention heads\n dropout: float: dropout rate\n mode: str: 'train' or 'eval'\n\n Returns:\n Multi-headed self-attention layer.\n " ]
Please provide a description of the function:def _chunked_selector_output_shape( # pylint: disable=invalid-name input_shapes, selector=None, **unused_kwargs): # Read the main function below first, the shape logic just follows the ops. selector = selector or (lambda x: [] if x < 1 else [x-1]) triples, _ = zip(*input_shapes) (query_shapes, key_shapes, value_shapes) = zip(*triples) result = [] for i in range(len(input_shapes)): selected = selector(i) cur_key_shape, cur_value_shape = key_shapes[i], value_shapes[i] # Since keys and values are [batch, length, depth] we concatenate on axis=1. new_key_len = sum([key_shapes[j][1] for j in selected]) + cur_key_shape[1] new_key_shape = (cur_key_shape[0], new_key_len, cur_key_shape[2]) new_value_len = sum( [value_shapes[j][1] for j in selected]) + cur_value_shape[1] new_value_shape = (cur_value_shape[0], new_value_len, cur_value_shape[2]) # Masks are (1, query-len, key-len). new_mask_shape = (1, query_shapes[i][1], new_key_len) new_shape = ((query_shapes[i], new_key_shape, new_value_shape), new_mask_shape) result.append(new_shape) return tuple(result)
[ "Helper: calculate output shape for chunked key selector (see below)." ]
Please provide a description of the function:def ChunkedAttentionSelector(x, params, selector=None, **kwargs): del params, kwargs selector = selector or (lambda x: [] if x < 1 else [x-1]) triples, masks = zip(*x) (queries, keys, values) = zip(*triples) result = [] for i in range(len(x)): selected = selector(i) # Since keys and values are [batch, length, depth] we concatenate on axis=1. # We also always include the current key or value at the end. new_key_list = [keys[j] for j in selected] new_key = np.concatenate(new_key_list + [keys[i]], axis=1) new_value = np.concatenate( [values[j] for j in selected] + [values[i]], axis=1) # Masks are (1, query-len, key-len) so we concatenate on axis=2. new_mask_shapes = [(1, queries[i].shape[1], key.shape[1]) for key in new_key_list] cur_mask = masks[i] # Masks are all-1 for the added chunks (no masking). new_mask_list = [np.ones(s, dtype=cur_mask.dtype) for s in new_mask_shapes] # We still use the current (often causal) mask for the final chunk. new_mask = np.concatenate(new_mask_list + [cur_mask], axis=2) result.append(((queries[i], new_key, new_value), new_mask)) return tuple(result)
[ "Select which chunks to attend to in chunked attention.\n\n Args:\n x: inputs, a list of elements of the form (q, k, v), mask for each chunk.\n params: parameters (unused).\n selector: a function from chunk_number -> list of chunk numbers that says\n which other chunks should be appended to the given one (previous if None).\n **kwargs: unused other arguments.\n\n Returns:\n a list of elements of the form (q, k', v'), mask' where k', v' and mask' are\n concatenations of k, v and identity-extended masks from selected chunks.\n " ]
Please provide a description of the function:def ChunkedCausalMultiHeadedAttention( feature_depth, num_heads=8, dropout=0.0, chunk_selector=None, mode='train'): prepare_attention_input = combinators.Serial( combinators.Branch(), combinators.Parallel( combinators.Branch(num_branches=3), # q = k = v = first input CausalMask(axis=-2), # pylint: disable=no-value-for-parameter ), combinators.Parallel( combinators.Parallel( core.Dense(feature_depth), core.Dense(feature_depth), core.Dense(feature_depth), ), combinators.Identity() ) ) return combinators.Serial( combinators.Map(prepare_attention_input), ChunkedAttentionSelector(selector=chunk_selector), # pylint: disable=no-value-for-parameter combinators.Map(PureMultiHeadedAttention( # pylint: disable=no-value-for-parameter feature_depth=feature_depth, num_heads=num_heads, dropout=dropout, mode=mode), check_shapes=False), combinators.Map(core.Dense(feature_depth)) )
[ "Transformer-style causal multi-headed attention operating on chunks.\n\n Accepts inputs that are a list of chunks and applies causal attention.\n\n Args:\n feature_depth: int: depth of embedding\n num_heads: int: number of attention heads\n dropout: float: dropout rate\n chunk_selector: a function from chunk number to list of chunks to attend.\n mode: str: 'train' or 'eval'\n\n Returns:\n Multi-headed self-attention layer.\n " ]
Please provide a description of the function:def ShiftRight(x, **unused_kwargs): if not isinstance(x, (list, tuple)): # non-chunked inputs pad_widths = [(0, 0), (1, 0)] padded = np.pad(x, pad_widths, mode='constant') return padded[:, :-1] # Handling chunked inputs. Recall that the list of chunks represents a big # sequence (the concatenation of the chunks). We want to shift that sequence, # so we put a 0 in the beginning of the first chunk and the last element of # that chunk is used as the new first element of the next chunk, and so on. padded = [] last_value = np.zeros_like(x[0][:, -1]) for chunk in x: padded_chunk = np.concatenate([last_value[:, np.newaxis], chunk], axis=1) last_value = chunk[:, -1] padded.append(padded_chunk[:, :-1]) return padded
[ "Layer to shift the tensor to the right by padding on axis 1." ]
Please provide a description of the function:def zipf_distribution(nbr_symbols, alpha): tmp = np.power(np.arange(1, nbr_symbols + 1), -alpha) zeta = np.r_[0.0, np.cumsum(tmp)] return [x / zeta[-1] for x in zeta]
[ "Helper function: Create a Zipf distribution.\n\n Args:\n nbr_symbols: number of symbols to use in the distribution.\n alpha: float, Zipf's Law Distribution parameter. Default = 1.5.\n Usually for modelling natural text distribution is in\n the range [1.1-1.6].\n\n Returns:\n distr_map: list of float, Zipf's distribution over nbr_symbols.\n\n " ]
Please provide a description of the function:def zipf_random_sample(distr_map, sample_len): u = np.random.random(sample_len) # Random produces values in range [0.0,1.0); even if it is almost # improbable(but possible) that it can generate a clear 0.000..0. return list(np.searchsorted(distr_map, u))
[ "Helper function: Generate a random Zipf sample of given length.\n\n Args:\n distr_map: list of float, Zipf's distribution over nbr_symbols.\n sample_len: integer, length of sequence to generate.\n\n Returns:\n sample: list of integer, Zipf's random sample over nbr_symbols.\n\n " ]
Please provide a description of the function:def reverse_generator_nlplike(nbr_symbols, max_length, nbr_cases, scale_std_dev=100, alpha=1.5): std_dev = max_length / scale_std_dev distr_map = zipf_distribution(nbr_symbols, alpha) for _ in range(nbr_cases): l = int(abs(np.random.normal(loc=max_length / 2, scale=std_dev)) + 1) inputs = zipf_random_sample(distr_map, l) yield {"inputs": inputs, "targets": list(reversed(inputs))}
[ "Generator for the reversing nlp-like task on sequences of symbols.\n\n The length of the sequence is drawn from a Gaussian(Normal) distribution\n at random from [1, max_length] and with std deviation of 1%,\n then symbols are drawn from Zipf's law at random from [0, nbr_symbols) until\n nbr_cases sequences have been produced.\n\n Args:\n nbr_symbols: integer, number of symbols.\n max_length: integer, maximum length of sequences to generate.\n nbr_cases: the number of cases to generate.\n scale_std_dev: float, Normal distribution's standard deviation scale factor\n used to draw the length of sequence. Default = 1% of the max_length.\n alpha: float, Zipf's Law Distribution parameter. Default = 1.5.\n Usually for modelling natural text distribution is in\n the range [1.1-1.6].\n\n Yields:\n A dictionary {\"inputs\": input-list, \"targets\": target-list} where\n target-list is input-list reversed.\n " ]
Please provide a description of the function:def lower_endian_to_number(l, base): return sum([d * (base**i) for i, d in enumerate(l)])
[ "Helper function: convert a list of digits in the given base to a number." ]
Please provide a description of the function:def number_to_lower_endian(n, base): if n < base: return [n] return [n % base] + number_to_lower_endian(n // base, base)
[ "Helper function: convert a number to a list of digits in the given base." ]
Please provide a description of the function:def random_number_lower_endian(length, base): if length == 1: # Last digit can be 0 only if length is 1. return [np.random.randint(base)] prefix = [np.random.randint(base) for _ in range(length - 1)] return prefix + [np.random.randint(base - 1) + 1]
[ "Helper function: generate a random number as a lower-endian digits list." ]
Please provide a description of the function:def remote_run(cmd, instance_name, detach=False, retries=1): if detach: cmd = SCREEN.format(command=cmd) args = SSH.format(instance_name=instance_name).split() args.append(cmd) for i in range(retries + 1): try: if i > 0: tf.logging.info("Retry %d for %s", i, args) return sp.check_call(args) except sp.CalledProcessError as e: if i == retries: raise e
[ "Run command on GCS instance, optionally detached." ]
Please provide a description of the function:def wait_for_ssh(ip): for _ in range(12): with safe_socket() as s: try: s.connect((ip, 22)) return True except socket.timeout: pass time.sleep(10) return False
[ "Wait for SSH to be available at given IP address." ]
Please provide a description of the function:def launch_instance(instance_name, command, existing_ip=None, cpu=1, mem=4, code_dir=None, setup_command=None): # Create instance ip = existing_ip or create_instance(instance_name, cpu=cpu, mem=mem) tf.logging.info("Waiting for SSH %s", instance_name) ready = wait_for_ssh(ip) if not ready: raise ValueError("Instance %s never ready for SSH" % instance_name) # Copy code if code_dir: shell_run_with_retry(COPY_CODE, retries=2, local_dir=code_dir, instance_name=instance_name) # Run setup if setup_command: tf.logging.info("Running setup on %s", instance_name) remote_run(setup_command, instance_name) # Run command tf.logging.info("Running command on %s", instance_name) remote_run(command, instance_name, detach=True)
[ "Launch a GCE instance." ]
Please provide a description of the function:def evolved_transformer_encoder(encoder_input, encoder_self_attention_bias, hparams, name="encoder", nonpadding=None, save_weights_to=None, make_image_summary=True, losses=None, attn_bias_for_padding=None): del losses hidden_state = encoder_input attention_dropout_broadcast_dims = ( common_layers.comma_separated_string_to_integer_list( getattr(hparams, "attention_dropout_broadcast_dims", ""))) with tf.variable_scope(name): if nonpadding is not None: padding = 1.0 - nonpadding else: attention_bias = encoder_self_attention_bias if attn_bias_for_padding is not None: attention_bias = attn_bias_for_padding # Only bfloat16 and float32 supported. float_type = hparams.get("activation_dtype", "float32") if float_type == "bfloat16": cast_fn = tf.to_bfloat16 else: assert float_type == "float32" cast_fn = tf.to_float padding = common_attention.attention_bias_to_padding( attention_bias, cast_fn) nonpadding = 1.0 - padding for layer in range(hparams.num_encoder_layers or hparams.num_hidden_layers): with tf.variable_scope("layer_%d" % layer): with tf.variable_scope("gated_linear_unit"): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) values = common_layers.layers().Dense( hparams.hidden_size)(hidden_state) gates = common_layers.layers().Dense( hparams.hidden_size, activation=tf.nn.sigmoid)(hidden_state) hidden_state = values * gates hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) with tf.variable_scope("conv_branches"): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) # Mask padding from conv layers. mask = tf.tile( tf.expand_dims(nonpadding, 2), [1, 1, hparams.hidden_size]) hidden_state *= mask left_output_dim = int(hparams.hidden_size * 4) left_state = common_layers.layers().Dense( left_output_dim, activation=tf.nn.relu)(hidden_state) left_state = tf.nn.dropout(left_state, 1 - hparams.layer_prepostprocess_dropout) right_output_dim = int(hparams.hidden_size / 2) right_state = common_layers.layers().Conv1D( right_output_dim, 3, padding="SAME", name="standard_conv_3x1", activation=tf.nn.relu)(hidden_state) right_state = tf.nn.dropout(right_state, 1 - hparams.layer_prepostprocess_dropout) right_state = tf.pad( right_state, [[0, 0], [0, 0], [0, left_output_dim - right_output_dim]], constant_values=0) hidden_state = left_state + right_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) # Mask padding from conv layer. mask = tf.tile(tf.expand_dims(nonpadding, 2), [1, 1, left_output_dim]) hidden_state *= mask separable_conv_9x1 = common_layers.layers().SeparableConv1D( right_output_dim, 9, padding="SAME", name="separable_conv_9x1") hidden_state = separable_conv_9x1(hidden_state) hidden_state = tf.pad( hidden_state, [[0, 0], [0, 0], [0, hparams.hidden_size - right_output_dim]], constant_values=0) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) with tf.variable_scope("self_attention"): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) hidden_state = common_attention.multihead_attention( hidden_state, None, encoder_self_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.self_attention_type, max_relative_position=hparams.max_relative_position, heads_share_relative_embedding=( hparams.heads_share_relative_embedding), add_relative_to_values=hparams.add_relative_to_values, save_weights_to=save_weights_to, make_image_summary=make_image_summary, dropout_broadcast_dims=attention_dropout_broadcast_dims, max_length=hparams.get("max_length"), vars_3d=hparams.get("attention_variables_3d"), activation_dtype=hparams.get("activation_dtype", "float32"), weight_dtype=hparams.get("weight_dtype", "float32")) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) with tf.variable_scope("dense_layers"): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) hidden_state = common_layers.layers().Dense( int(hparams.hidden_size * 4), activation=tf.nn.relu)(hidden_state) hidden_state = tf.nn.dropout(hidden_state, 1 - hparams.layer_prepostprocess_dropout) hidden_state = common_layers.layers().Dense( hparams.hidden_size)(hidden_state) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) # If normalization is done in layer_preprocess, then it should also be done # on the output, since the output can grow very large, being the sum of # a whole stack of unnormalized layer outputs. return common_layers.layer_preprocess(hidden_state, hparams)
[ "Evolved Transformer encoder. See arxiv.org/abs/1901.11117 for more details.\n\n Note: Pad remover is not supported.\n\n Args:\n encoder_input: a Tensor.\n encoder_self_attention_bias: bias Tensor for self-attention (see\n common_attention.attention_bias()).\n hparams: hyperparameters for model.\n name: a string.\n nonpadding: optional Tensor with shape [batch_size, encoder_length]\n indicating what positions are not padding. This must either be passed in,\n which we do for \"packed\" datasets, or inferred from\n encoder_self_attention_bias. The knowledge about padding is used for\n pad_remover(efficiency) and to mask out padding in convolutional layers.\n save_weights_to: an optional dictionary to capture attention weights for\n visualization; the weights tensor will be appended there under a string\n key created from the variable scope (including name).\n make_image_summary: Whether to make an attention image summary.\n losses: Not used.\n attn_bias_for_padding: Padded attention bias in case a unidirectional\n encoder is being used where future attention is masked.\n\n Returns:\n Tensor encoder output.\n " ]
Please provide a description of the function:def evolved_transformer_decoder(decoder_input, encoder_output, decoder_self_attention_bias, encoder_decoder_attention_bias, hparams, cache=None, decode_loop_step=None, name="decoder", nonpadding=None, save_weights_to=None, make_image_summary=True, losses=None): del losses attention_dropout_broadcast_dims = ( common_layers.comma_separated_string_to_integer_list( getattr(hparams, "attention_dropout_broadcast_dims", ""))) with tf.variable_scope(name): hidden_state = decoder_input for layer in range(hparams.num_decoder_layers or hparams.num_hidden_layers): layer_name = "layer_%d" % layer layer_cache = cache[layer_name] if cache is not None else None with tf.variable_scope(layer_name): with tf.variable_scope(_SIXTEEN_HEAD_ATTENTION_NAME): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) attention_cache = layer_cache[ _SIXTEEN_HEAD_ATTENTION_NAME] if layer_cache is not None else None left_state = common_attention.multihead_attention( hidden_state, None, decoder_self_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, _capped_double_heads(hparams.num_heads), hparams.attention_dropout, attention_type=hparams.self_attention_type, max_relative_position=hparams.max_relative_position, heads_share_relative_embedding=( hparams.heads_share_relative_embedding), add_relative_to_values=hparams.add_relative_to_values, save_weights_to=save_weights_to, cache=attention_cache, make_image_summary=make_image_summary, dropout_broadcast_dims=attention_dropout_broadcast_dims, max_length=hparams.get("max_length"), decode_loop_step=decode_loop_step, vars_3d=hparams.get("attention_variables_3d"), activation_dtype=hparams.get("activation_dtype", "float32"), weight_dtype=hparams.get("weight_dtype", "float32")) if encoder_output is not None: with tf.variable_scope(_FIRST_ATTEND_TO_ENCODER_NAME): attention_cache = ( layer_cache[_FIRST_ATTEND_TO_ENCODER_NAME] if layer_cache is not None else None) right_state = common_attention.multihead_attention( hidden_state, encoder_output, encoder_decoder_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, max_relative_position=hparams.max_relative_position, heads_share_relative_embedding=( hparams.heads_share_relative_embedding), add_relative_to_values=hparams.add_relative_to_values, save_weights_to=save_weights_to, cache=attention_cache, make_image_summary=make_image_summary, dropout_broadcast_dims=attention_dropout_broadcast_dims, max_length=hparams.get("max_length"), vars_3d=hparams.get("attention_variables_3d"), activation_dtype=hparams.get("activation_dtype", "float32"), weight_dtype=hparams.get("weight_dtype", "float32")) left_state = tf.nn.dropout(left_state, 1 - hparams.layer_prepostprocess_dropout) right_state = tf.nn.dropout( right_state, 1 - hparams.layer_prepostprocess_dropout) hidden_state = residual_state + left_state + right_state else: hidden_state = common_layers.layer_postprocess( residual_state, left_state, hparams) with tf.variable_scope(_CONV_BRANCHES_NAME): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) if nonpadding is not None: # Mask padding from conv layers. mask = tf.tile( tf.expand_dims(nonpadding, 2), [1, 1, hparams.hidden_size]) hidden_state *= mask if layer_cache: if decode_loop_step is None: hidden_state = layer_cache[ _CONV_BRANCHES_FIRST_LAYER_NAME] = tf.concat( [ layer_cache[_CONV_BRANCHES_FIRST_LAYER_NAME], hidden_state ], axis=1)[:, -1 * _DECODER_LEFT_CONV_PADDING - 1:, :] left_state = hidden_state right_state = hidden_state[:, _DECODER_LEFT_CONV_PADDING - _DECODER_RIGHT_CONV_PADDING:, :] else: # Inplace update is required for inference on TPU. # Inplace_ops only supports inplace_update on the first dimension. tmp = tf.transpose( layer_cache[_CONV_BRANCHES_FIRST_LAYER_NAME], perm=[1, 0, 2]) tmp = tf.expand_dims(tmp, axis=1) tmp = inplace_ops.alias_inplace_update( tmp, decode_loop_step * tf.shape(hidden_state)[1] + _DECODER_LEFT_CONV_PADDING, tf.transpose(hidden_state, perm=[1, 0, 2])) tmp = tf.squeeze(tmp, axis=1) hidden_state = layer_cache[ _CONV_BRANCHES_FIRST_LAYER_NAME] = tf.transpose( tmp, perm=[1, 0, 2]) left_state_indexes = [ decode_loop_step + i for i in range(_DECODER_LEFT_CONV_PADDING + 1) ] left_state = tf.gather(hidden_state, left_state_indexes, axis=1) right_state_indexes = [ decode_loop_step + i + (_DECODER_LEFT_CONV_PADDING - _DECODER_RIGHT_CONV_PADDING) for i in range(_DECODER_RIGHT_CONV_PADDING + 1) ] right_state = tf.gather(hidden_state, right_state_indexes, axis=1) else: # No caching. left_state = tf.pad( hidden_state, paddings=[[0, 0], [_DECODER_LEFT_CONV_PADDING, 0], [0, 0]]) right_state = tf.pad( hidden_state, paddings=[[0, 0], [_DECODER_RIGHT_CONV_PADDING, 0], [0, 0]]) left_output_dim = int(hparams.hidden_size * 2) separable_conv_11x1 = tf.layers.SeparableConv1D( left_output_dim, 11, padding="VALID", name="separable_conv11x1", activation=tf.nn.relu) left_state = separable_conv_11x1.apply(left_state) left_state = tf.nn.dropout(left_state, 1 - hparams.layer_prepostprocess_dropout) right_output_dim = int(hparams.hidden_size / 2) separable_conv_7x1_1 = tf.layers.SeparableConv1D( right_output_dim, 7, padding="VALID", name="separable_conv_7x1_1") right_state = separable_conv_7x1_1.apply(right_state) right_state = tf.nn.dropout(right_state, 1 - hparams.layer_prepostprocess_dropout) right_state = tf.pad( right_state, [[0, 0], [0, 0], [0, left_output_dim - right_output_dim]], constant_values=0) hidden_state = left_state + right_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) if nonpadding is not None: # Mask padding from conv layers. mask = tf.tile( tf.expand_dims(nonpadding, 2), [1, 1, hparams.hidden_size * 2]) hidden_state *= mask if layer_cache: if decode_loop_step is None: hidden_state = layer_cache[ _CONV_BRANCHES_SECOND_LAYER_NAME] = tf.concat( [ layer_cache[_CONV_BRANCHES_SECOND_LAYER_NAME], hidden_state ], axis=1)[:, -1 * _DECODER_FINAL_CONV_PADDING - 1:, :] else: # Inplace update is required for inference on TPU. # Inplace_ops only supports inplace_update on the first dimension. tmp = tf.transpose( layer_cache[_CONV_BRANCHES_SECOND_LAYER_NAME], perm=[1, 0, 2]) tmp = tf.expand_dims(tmp, axis=1) tmp = inplace_ops.alias_inplace_update( tmp, (decode_loop_step + _DECODER_FINAL_CONV_PADDING) * tf.shape(hidden_state)[1], tf.transpose(hidden_state, perm=[1, 0, 2])) tmp = tf.squeeze(tmp, axis=1) hidden_state = layer_cache[ _CONV_BRANCHES_SECOND_LAYER_NAME] = tf.transpose( tmp, perm=[1, 0, 2]) hidden_state_indexes = [ decode_loop_step + i for i in range(_DECODER_FINAL_CONV_PADDING + 1) ] hidden_state = tf.gather( hidden_state, hidden_state_indexes, axis=1) else: hidden_state = tf.pad( hidden_state, paddings=[[0, 0], [_DECODER_FINAL_CONV_PADDING, 0], [0, 0]]) separable_conv_7x1_2 = tf.layers.SeparableConv1D( hparams.hidden_size, 7, padding="VALID", name="separable_conv_7x1_2") hidden_state = separable_conv_7x1_2.apply(hidden_state) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) with tf.variable_scope(_VANILLA_ATTENTION_NAME): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) attention_cache = layer_cache[ _VANILLA_ATTENTION_NAME] if layer_cache is not None else None hidden_state = common_attention.multihead_attention( hidden_state, None, decoder_self_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, attention_type=hparams.self_attention_type, max_relative_position=hparams.max_relative_position, heads_share_relative_embedding=( hparams.heads_share_relative_embedding), add_relative_to_values=hparams.add_relative_to_values, save_weights_to=save_weights_to, cache=attention_cache, make_image_summary=make_image_summary, dropout_broadcast_dims=attention_dropout_broadcast_dims, max_length=hparams.get("max_length"), decode_loop_step=decode_loop_step, vars_3d=hparams.get("attention_variables_3d"), activation_dtype=hparams.get("activation_dtype", "float32"), weight_dtype=hparams.get("weight_dtype", "float32")) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) if encoder_output is not None: with tf.variable_scope(_SECOND_ATTEND_TO_ENCODER_NAME): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) attention_cache = ( layer_cache[_SECOND_ATTEND_TO_ENCODER_NAME] if layer_cache is not None else None) hidden_state = common_attention.multihead_attention( hidden_state, encoder_output, encoder_decoder_attention_bias, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout, max_relative_position=hparams.max_relative_position, heads_share_relative_embedding=( hparams.heads_share_relative_embedding), add_relative_to_values=hparams.add_relative_to_values, save_weights_to=save_weights_to, cache=attention_cache, make_image_summary=make_image_summary, dropout_broadcast_dims=attention_dropout_broadcast_dims, max_length=hparams.get("max_length"), vars_3d=hparams.get("attention_variables_3d"), activation_dtype=hparams.get("activation_dtype", "float32"), weight_dtype=hparams.get("weight_dtype", "float32")) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) with tf.variable_scope("dense_layers"): residual_state = hidden_state hidden_state = common_layers.layer_preprocess(hidden_state, hparams) hidden_state = tf.layers.dense( hidden_state, int(hparams.hidden_size * 4), activation=tf.nn.swish) hidden_state = tf.nn.dropout(hidden_state, 1 - hparams.layer_prepostprocess_dropout) hidden_state = common_layers.layer_preprocess(hidden_state, hparams) hidden_state = tf.layers.dense(hidden_state, hparams.hidden_size) hidden_state = common_layers.layer_postprocess( residual_state, hidden_state, hparams) return common_layers.layer_preprocess(hidden_state, hparams)
[ "Evolved Transformer decoder. See arxiv.org/abs/1901.11117 for more details.\n\n Args:\n decoder_input: a Tensor.\n encoder_output: a Tensor.\n decoder_self_attention_bias: bias Tensor for self-attention (see\n common_attention.attention_bias()).\n encoder_decoder_attention_bias: bias Tensor for encoder-decoder attention\n (see common_attention.attention_bias()).\n hparams: hyperparameters for model.\n cache: dict, containing tensors which are the results of previous\n layers, used for fast decoding.\n decode_loop_step: An integer, step number of the decoding loop. Only used\n for inference on TPU.\n name: a string.\n nonpadding: optional Tensor with shape [batch_size, encoder_length]\n indicating what positions are not padding. This is used to mask out\n padding in convolutional layers. We generally only need this mask for\n \"packed\" datasets, because for ordinary datasets, no padding is ever\n followed by nonpadding.\n save_weights_to: an optional dictionary to capture attention weights for\n visualization; the weights tensor will be appended there under a string\n key created from the variable scope (including name).\n make_image_summary: Whether to make an attention image summary.\n losses: Not supported.\n\n Returns:\n Decoder output tensor.\n " ]
Please provide a description of the function:def _add_attend_to_encoder_cache(cache, attention_name, hparams, num_layers, key_channels, value_channels, vars_3d_num_heads, scope_prefix, encoder_output): for layer in range(num_layers): layer_name = "layer_%d" % layer with tf.variable_scope("%sdecoder/%s/%s/multihead_attention" % (scope_prefix, layer_name, attention_name)): k_encdec = common_attention.compute_attention_component( encoder_output, key_channels, name="k", vars_3d_num_heads=vars_3d_num_heads) k_encdec = common_attention.split_heads(k_encdec, hparams.num_heads) v_encdec = common_attention.compute_attention_component( encoder_output, value_channels, name="v", vars_3d_num_heads=vars_3d_num_heads) v_encdec = common_attention.split_heads(v_encdec, hparams.num_heads) cache[layer_name][attention_name] = { "k_encdec": k_encdec, "v_encdec": v_encdec } return cache
[ "Add attend-to-encoder layers to cache." ]
Please provide a description of the function:def _init_evolved_transformer_cache(cache, hparams, batch_size, attention_init_length, encoder_output, encoder_decoder_attention_bias, scope_prefix): key_channels = hparams.attention_key_channels or hparams.hidden_size value_channels = hparams.attention_value_channels or hparams.hidden_size num_layers = hparams.num_decoder_layers or hparams.num_hidden_layers vars_3d_num_heads = ( hparams.num_heads if hparams.get("attention_variables_3d") else 0) # Add self-attentions. if cache is None: cache = {} cache.update({ "layer_%d" % layer: { # pylint: disable=g-complex-comprehension _SIXTEEN_HEAD_ATTENTION_NAME: { "k": common_attention.split_heads( tf.zeros( [batch_size, attention_init_length, key_channels]), _capped_double_heads(hparams.num_heads)), "v": common_attention.split_heads( tf.zeros( [batch_size, attention_init_length, value_channels]), _capped_double_heads(hparams.num_heads)), }, _VANILLA_ATTENTION_NAME: { "k": common_attention.split_heads( tf.zeros( [batch_size, attention_init_length, key_channels]), hparams.num_heads), "v": common_attention.split_heads( tf.zeros( [batch_size, attention_init_length, value_channels]), hparams.num_heads), } } for layer in range(num_layers) }) # Add branched layers. Pad with additional zeros for causal convolution. for layer in range(num_layers): cache["layer_%d" % layer][_CONV_BRANCHES_FIRST_LAYER_NAME] = tf.zeros([ batch_size, attention_init_length + _DECODER_LEFT_CONV_PADDING, hparams.hidden_size ]) cache["layer_%d" % layer][_CONV_BRANCHES_SECOND_LAYER_NAME] = tf.zeros([ batch_size, attention_init_length + _DECODER_FINAL_CONV_PADDING, hparams.hidden_size * 2 ]) # Add encoder embedding attentions. if encoder_output is not None: cache = _add_attend_to_encoder_cache( cache=cache, attention_name=_FIRST_ATTEND_TO_ENCODER_NAME, hparams=hparams, num_layers=num_layers, key_channels=key_channels, value_channels=value_channels, vars_3d_num_heads=vars_3d_num_heads, scope_prefix=scope_prefix, encoder_output=encoder_output) cache = _add_attend_to_encoder_cache( cache=cache, attention_name=_SECOND_ATTEND_TO_ENCODER_NAME, hparams=hparams, num_layers=num_layers, key_channels=key_channels, value_channels=value_channels, vars_3d_num_heads=vars_3d_num_heads, scope_prefix=scope_prefix, encoder_output=encoder_output) cache["encoder_output"] = encoder_output cache["encoder_decoder_attention_bias"] = encoder_decoder_attention_bias return cache
[ "Create the initial cache for Evolved Transformer fast decoding." ]
Please provide a description of the function:def add_evolved_transformer_hparams(hparams): # Evolved Transformer "layers" are twice as deep as Transformer, so roughly # halve the number that we use. These numbers are taken from # arxiv.org/abs/1901.11117 . hparams.num_encoder_layers = 3 hparams.num_decoder_layers = 4 # Learning rate and decay scheme that mimics the transformer Adam config, # but with cosine decay instead of rsqrt. hparams.learning_rate_constant /= hparams.learning_rate_warmup_steps ** 0.5 hparams.learning_rate_schedule = ( "constant*linear_warmup*single_cycle_cos_decay*rsqrt_hidden_size") # The current infrastructure does not support exposing # `train_steps` to the decay functions, and so we are hard coding the decay # steps here to match the default number of train steps used in `t2t_trainer`. # TODO(davidso): Thread `train_steps` through to decay functions so we do not # have to worry about a `learning_rate_decay_steps` mismatch. hparams.learning_rate_decay_steps = 250000 return hparams
[ "Add Evolved Transformer hparams.\n\n Note: These are for the Adam optimizer, not the Adafactor optimizer used in\n the paper.\n\n Args:\n hparams: Current hparams.\n\n Returns:\n hparams updated with Evolved Transformer values.\n " ]
Please provide a description of the function:def evolved_transformer_base_tpu(): hparams = add_evolved_transformer_hparams(transformer.transformer_tpu()) hparams.learning_rate_constant = 1 / hparams.learning_rate_warmup_steps ** 0.5 hparams.learning_rate_schedule = ( "constant*single_cycle_cos_decay") return hparams
[ "Base parameters for Evolved Transformer model on TPU." ]
Please provide a description of the function:def evolved_transformer_big_tpu(): hparams = add_evolved_transformer_hparams(transformer.transformer_big_tpu()) hparams.learning_rate_constant = 1 / hparams.learning_rate_warmup_steps ** 0.5 hparams.learning_rate_schedule = ( "constant*single_cycle_cos_decay") return hparams
[ "Big parameters for Evolved Transformer model on TPU." ]
Please provide a description of the function:def transformer_moe_layer_v1(inputs, output_dim, hparams, train, master_dtype=tf.bfloat16, slice_dtype=tf.float32): orig_inputs = inputs input_dim = inputs.shape.dims[-1] hidden_dim = mtf.Dimension("expert_hidden", hparams.moe_hidden_size) experts_dim = mtf.Dimension("experts", hparams.moe_num_experts) group_size_dim = mtf.Dimension("group", hparams.moe_group_size) batch_dim = mtf.Dimension( orig_inputs.shape[0].name, orig_inputs.shape.size // (group_size_dim.size * input_dim.size)) inputs = mtf.reshape(inputs, [batch_dim, group_size_dim, input_dim]) # Each sequence sends expert_capacity positions to each expert. capacity_factor = ( hparams.moe_capacity_factor_train if train else hparams.moe_capacity_factor_eval) expert_capacity = min( group_size_dim.size, int((group_size_dim.size * capacity_factor) / experts_dim.size)) expert_capacity_dim = mtf.Dimension("expert_capacity", expert_capacity) experts_dim_unsplit = mtf.Dimension("expert_unsplit", experts_dim.size) batch_dim_unsplit = mtf.Dimension("batch_unsplit", batch_dim.size) if hparams.moe_gating == "top_2": dispatch_tensor, combine_tensor, loss = _top_2_gating( inputs=inputs, outer_expert_dims=None, experts_dim=experts_dim_unsplit, expert_capacity_dim=expert_capacity_dim, hparams=hparams, train=train) else: raise ValueError("unknown hparams.moe_gating=%s" % hparams.moe_gating) # put num_experts dimension first to make split easier in alltoall expert_inputs = mtf.einsum([inputs, dispatch_tensor], mtf.Shape( [experts_dim_unsplit, batch_dim, expert_capacity_dim, input_dim])) expert_inputs = mtf.reshape(expert_inputs, mtf.Shape( [experts_dim, batch_dim_unsplit, expert_capacity_dim, input_dim])) # Now feed the expert inputs through the experts. h = mtf.layers.dense( expert_inputs, hidden_dim, expert_dims=[experts_dim], activation=mtf.relu, use_bias=False, master_dtype=master_dtype, slice_dtype=slice_dtype, name="x0") expert_output = mtf.layers.dense( h, output_dim, expert_dims=[experts_dim], use_bias=False, master_dtype=master_dtype, slice_dtype=slice_dtype, name="x1") expert_output = mtf.reshape(expert_output, mtf.Shape( [experts_dim_unsplit, batch_dim, expert_capacity_dim, input_dim])) output = mtf.einsum([expert_output, combine_tensor], mtf.Shape( [batch_dim, group_size_dim, output_dim])) output = mtf.reshape(output, orig_inputs.shape.dims[:-1] + [output_dim]) return output, loss * hparams.moe_loss_coef
[ "Local mixture of experts that works well on TPU.\n\n Adapted from the paper https://arxiv.org/abs/1701.06538\n\n Note: until the algorithm and inferface solidify, we pass in a hyperparameters\n dictionary in order not to complicate the interface in mtf_transformer.py .\n Once this code moves out of \"research\", we should pass the hyperparameters\n separately.\n\n Hyperparameters used:\n hparams.moe_num_experts: number of experts\n hparams.moe_hidden_size: size of hidden layer in each expert\n hparams.moe_group_size: size of each \"group\" for gating purposes\n hparams.moe_capacity_factor_train: a float\n hparams.moe_capacity_factor_eval: a float\n hparams.moe_gating: a string\n + all hyperparmeters used by _top_2_gating()\n\n The number of parameters in the gating network is:\n (input_dim.size * hparams.num_experts) +\n\n The number of parameters in the experts themselves is:\n (hparams.num_experts\n * (input_dim.size + output_dim.size)\n * hparams.moe_hidden_size)\n\n The input is n-dimensional: [<batch_and_length_dims>, input_dim], consisting\n of the representations of all positions in a batch of sequences.\n\n Each position of each sequence is sent to 0-2 experts. The expert\n choices and the combination weights are determined by a learned gating\n function.\n\n This function returns a small auxiliary loss that should be added to the\n training loss of the model. This loss helps to balance expert usage.\n Without the loss, it is very likely that a few experts will be trained and\n the rest will starve.\n\n Several hacks are necessary to get around current TPU limitations:\n\n - To ensure static shapes, we enforce (by truncation/padding)\n that each sequence send the same number of elements to each expert.\n\n It would make more sense to enforce this equality over the entire batch,\n but due to our hacked-up gather-by-matmul implementation, we need to divide\n the batch into \"groups\". For each group, the same number of elements\n are sent to each expert.\n\n TODO(noam): Factor this code better. We want to be able to substitute\n different code for the experts themselves.\n\n Args:\n inputs: a mtf.Tensor with shape [<batch_dims...>, length_dim, input_dim]\n output_dim: a mtf.Dimension (for Transformer, this is input_dim)\n hparams: model hyperparameters\n train: a boolean\n master_dtype: a tf.dtype\n slice_dtype: a tf.dtype\n\n Returns:\n outputs: a Tensor with shape [<batch_dims...>, length_dim, output_dim]\n loss: a mtf scalar\n\n Raises:\n ValueError: on unrecognized hparams.moe_gating\n " ]
Please provide a description of the function:def transformer_moe_layer_v2(inputs, output_dim, hparams, train, master_dtype=tf.bfloat16, slice_dtype=tf.float32): insert_outer_batch_dim = (len(inputs.shape.dims) == 3) if insert_outer_batch_dim: inputs = mtf.reshape( inputs, [mtf.Dimension("outer_batch", 1)] + inputs.shape.dims) assert len(hparams.moe_num_experts) == 2 a0, b1, l, m = inputs.shape.dims hidden_dim = mtf.Dimension("expert_hidden", hparams.moe_hidden_size) x1 = mtf.Dimension("expert_x", hparams.moe_num_experts[0]) y0 = mtf.Dimension("expert_y", hparams.moe_num_experts[1]) x = mtf.Dimension("expert_x_unsplit", hparams.moe_num_experts[0]) y = mtf.Dimension("expert_y_unsplit", hparams.moe_num_experts[1]) n = output_dim # We "cheat" here and look at the mesh shape and layout. This is to ensure # that the number of groups (g.size) is a multiple of the mesh dimension # over which those groups are split. num_groups, group_size = _split_into_groups( b1.size * l.size, hparams.moe_group_size, mtf.tensor_dim_to_mesh_dim_size(hparams.layout, hparams.mesh_shape, b1)) g1 = mtf.Dimension(b1.name, num_groups) g = mtf.Dimension(b1.name + "_unsplit", g1.size) s = mtf.Dimension("group_size_x", group_size) # Each sequence sends (at most?) expert_capacity positions to each expert. # Static expert_capacity dimension is needed for expert batch sizes capacity_factor = ( hparams.moe_capacity_factor_train if train else hparams.moe_capacity_factor_eval) expert_capacity = min(s.size, int((s.size * capacity_factor) / x.size)) expert_capacity = max(expert_capacity, 4) c = mtf.Dimension("expert_capacity_x", expert_capacity) # We "cheat" here and look at the mesh shape and layout. This is to ensure # that the number of groups (h.size) is a multiple of the mesh dimension # over which those groups are split. num_groups, group_size = _split_into_groups( a0.size * g.size * c.size, hparams.moe_group_size, mtf.tensor_dim_to_mesh_dim_size(hparams.layout, hparams.mesh_shape, a0)) t = mtf.Dimension("group_size_y", group_size) h0 = mtf.Dimension(a0.name, num_groups) h = mtf.Dimension(a0.name + "_unsplit", h0.size) expert_capacity = min( t.size, int((t.size * hparams.moe_capacity_factor_second_level) / y.size)) expert_capacity = max(expert_capacity, 4) d = mtf.Dimension("expert_capacity_y", expert_capacity) # First level of expert routing # Reshape the inner batch size to a multiple of group_dim g1 and # group_size_dim s. inputs = mtf.reshape(inputs, [a0, g1, s, m]) # Get the assignments for the first level. # dispatch_tensor_x has shape [a0, g1, s, x, c] if hparams.moe_gating == "top_2": dispatch_tensor_x, combine_tensor_x, loss_outer = _top_2_gating( inputs=inputs, outer_expert_dims=None, experts_dim=x, expert_capacity_dim=c, hparams=hparams, train=train) else: raise ValueError("unknown hparams.moe_gating=%s" % hparams.moe_gating) # Now create expert_inputs based on the assignments. # put num_experts dimension first to make split easier in alltoall expert_inputs_x = mtf.einsum([inputs, dispatch_tensor_x], [x, a0, g1, c, m]) # we construct an "importance" Tensor for the inputs to the second-level # gating. The importance of an input is 1.0 if it represents the # first-choice expert-group and 0.5 if it represents the second-choice expert # group. This is used by the second-level gating. importance = mtf.reduce_sum(combine_tensor_x, output_shape=[x, a0, g1, c]) importance = 0.5 * ( mtf.to_float(mtf.greater(importance, 0.5)) + mtf.to_float(mtf.greater(importance, 0.0))) # First level, all to all. Here we change the split dimension from g1 to x1. expert_inputs_x = mtf.reshape(expert_inputs_x, mtf.Shape( [x1, a0, g, c, m])) importance = mtf.reshape(importance, [x1, a0, g, c]) # Second level of expert routing # Reshape the expert_inputs outer batch dim to be a multiple of group_dim h0 # and group_size_dim t. inputs_y = mtf.reshape(expert_inputs_x, [x1, h0, t, m]) importance = mtf.reshape(importance, [x1, h0, t]) # Get the assignments for the second level. # dispatch_tensor_y has shape [x1, h0, t, y, d] if hparams.moe_gating == "top_2": dispatch_tensor_y, combine_tensor_y, loss_inner = _top_2_gating( inputs=inputs_y, outer_expert_dims=[x1], experts_dim=y, expert_capacity_dim=d, hparams=hparams, train=train, importance=importance) else: raise ValueError("unknown hparams.moe_gating=%s" % hparams.moe_gating) # Now create expert_inputs based on the assignments. # put num_experts dimension first to make split easier in alltoall expert_inputs_y = mtf.einsum([inputs_y, dispatch_tensor_y], [y, x1, h0, d, m]) # Second level, all to all. Here we change the split dimension from h0 to y0. expert_inputs_y = mtf.reshape(expert_inputs_y, mtf.Shape( [y0, x1, h, d, m])) hidden_output = mtf.layers.dense( expert_inputs_y, hidden_dim, expert_dims=[y0, x1], activation=mtf.relu, use_bias=False, master_dtype=master_dtype, slice_dtype=slice_dtype, name="expert0") expert_output = mtf.layers.dense( hidden_output, output_dim, expert_dims=[y0, x1], use_bias=False, master_dtype=master_dtype, slice_dtype=slice_dtype, name="expert1") # NOW COMBINE EXPERT OUTPUTS (reversing everything we have done) # expert_output has shape [y0, x1, h, d, n] # alltoall expert_output = mtf.reshape(expert_output, mtf.Shape( [y, x1, h0, d, n])) # combine results from inner level output_y = mtf.einsum([expert_output, combine_tensor_y], [x1, h0, t, n]) # Reshape the combined tensor from inner level to now contain outer_batch_dim # a0 and group_dim g output = mtf.reshape(output_y, [x1, a0, g, c, n]) # alltoall from expert_dim x to group_dim g1 expert_output_x = mtf.reshape(output, mtf.Shape([x, a0, g1, c, n])) # combine results from outer level output_x = mtf.einsum([expert_output_x, combine_tensor_x], [a0, g1, s, n]) # Reshape the combined tensor to now contain inner_batch_dim # b1 and the original sequence length output = mtf.reshape(output_x, [a0, b1, l, n]) if insert_outer_batch_dim: output = mtf.reshape(output, [b1, l, n]) return output, (loss_outer + loss_inner) * hparams.moe_loss_coef
[ "2-level mixture of experts.\n\n Adapted from the paper https://arxiv.org/abs/1701.06538\n\n Note: until the algorithm and inferface solidify, we pass in a hyperparameters\n dictionary in order not to complicate the interface in mtf_transformer.py .\n Once this code moves out of \"research\", we should pass the hyperparameters\n separately.\n\n Hyperparameters used:\n hparams.moe_num_experts: number of experts\n hparams.moe_hidden_size: size of hidden layer in each expert\n hparams.moe_group_size: size of each \"group\" for gating purposes\n hparams.moe_capacity_factor_train: a float\n hparams.moe_capacity_factor_eval: a float\n hparams.moe_capacity_factor_second_level: a float\n hparams.moe_gating: a string\n + all hyperparmeters used by _top_2_gating()\n\n One set of params for experts in first level and different of hparams\n per expert in the second level.\n The number of parameters in the gating network is:\n (input_dim.size * (hparams.num_experts) +\n (moe_hidden_size * hparams.num_experts) * hparams.num_experts\n\n\n The number of parameters in the experts themselves is:\n (hparams.num_experts\n * (input_dim.size + output_dim.size)\n * hparams.moe_hidden_size)\n\n The input is n-dimensional: [<batch_and_length_dims>, input_dim], consisting\n of the representations of all positions in a batch of sequences.\n\n Each position of each sequence is sent to 0-3 experts. The expert\n choices and the combination weights are determined by a learned gating\n function.\n\n This function returns a small auxiliary loss that should be added to the\n training loss of the model. This loss helps to balance expert usage.\n Without the loss, it is very likely that a few experts will be trained and\n the rest will starve.\n\n Several hacks are necessary to get around current TPU limitations:\n\n - To ensure static shapes, we enforce (by truncation/padding)\n that each sequence send the same number of elements to each expert.\n\n It would make more sense to enforce this equality over the entire batch,\n but due to our hacked-up gather-by-matmul implementation, we need to divide\n the batch into \"groups\". For each group, the same number of elements\n are sent to each expert.\n\n TODO(noam): Factor this code better. We want to be able to substitute\n different code for the experts themselves.\n\n Dimensions cheat sheet:\n a, b: batch size\n l: original sequence length\n m: input depth\n n: output depth\n g, h: number of groups\n s, t: group size\n x, y: number of experts\n c, d: expert capacity\n\n input: [a0, b1, l, m]\n input: [a0, g1, s, m]\n dispatch_tensor_x: [a0, g1, s, x, c]\n expert_input: [a0, g1, x, c, m]\n alltoall: [a0, g, x1, c, m]\n alltoall: [a0, g, x1, c, m]\n transpose: [x1, a0, g, c, m]\n reshape: [x1, h0, s, m]\n assignment2: [x1, h0, t, y, d]\n expert_input2: [x1, h0, y, d, m]\n alltoall: [x1, h, y0, d, m]\n ...\n reverse of that\n\n gating params 0: [m, x]\n gating params 1: [x1, m, y]\n\n expert params:\n [x1, y0, m, hidden]\n [x1, y0, hidden, n]\n\n Args:\n inputs: a mtf.Tensor with shape [a, b, l, m]\n output_dim: a mtf.Dimension (for Transformer, this is input_dim)\n hparams: model hyperparameters\n train: a boolean\n master_dtype: a tf.dtype\n slice_dtype: a tf.dtype\n\n Returns:\n outputs: a Tensor with shape [a, b, l, n]\n loss: a mtf scalar\n\n Raises:\n ValueError: on unrecognized hparams.moe_gating\n " ]
Please provide a description of the function:def _top_2_gating( inputs, outer_expert_dims, experts_dim, expert_capacity_dim, hparams, train, importance=None): group_size_dim, unused_input_dim = inputs.shape.dims[-2:] raw_gates = mtf.softmax(mtf.layers.dense( inputs, experts_dim, use_bias=False, expert_dims=outer_expert_dims), experts_dim) # The internals of this function run in float32. # bfloat16 seems to reduce quality. raw_gates = mtf.to_float(raw_gates) expert_capacity_f = float(expert_capacity_dim.size) # FIND TOP 2 EXPERTS PER POSITON # Find the top expert for each position. shape=[batch, group] index_1, gate_1 = mtf.top_1(raw_gates, experts_dim) # [batch, group, experts] mask_1 = mtf.one_hot(index_1, experts_dim, dtype=raw_gates.dtype) density_1_proxy = raw_gates if importance is not None: mask_1 *= mtf.to_float(mtf.equal(importance, 1.0)) gate_1 *= mtf.to_float(mtf.equal(importance, 1.0)) density_1_proxy *= mtf.to_float(mtf.equal(importance, 1.0)) gates_without_top_1 = raw_gates * (1.0 - mask_1) # [batch, group] index_2, gate_2 = mtf.top_1(gates_without_top_1, experts_dim) # [batch, group, experts] mask_2 = mtf.one_hot(index_2, experts_dim, dtype=raw_gates.dtype) if importance is not None: mask_2 *= mtf.to_float(mtf.greater(importance, 0.0)) denom = gate_1 + gate_2 + 1e-9 gate_1 /= denom gate_2 /= denom # BALANCING LOSSES # shape = [batch, experts] # We want to equalize the fraction of the batch assigned to each expert density_1 = mtf.reduce_mean(mask_1, reduced_dim=group_size_dim) # Something continuous that is correlated with what we want to equalize. density_1_proxy = mtf.reduce_mean(density_1_proxy, reduced_dim=group_size_dim) density_1 = mtf.Print( density_1, [mtf.reduce_mean(density_1, output_shape=[experts_dim])], "density_1", summarize=1000) loss = (mtf.reduce_mean(density_1_proxy * density_1) * float(experts_dim.size * experts_dim.size)) if hparams.moe_use_second_place_loss: # Also add a loss to encourage all experts to be used equally also as the # second-place expert. Experimentally, this seems to be a wash. # We want to equalize the fraction of the batch assigned to each expert: density_2 = mtf.reduce_mean(mask_2, reduced_dim=group_size_dim) # As a proxy for density_2, we renormalize the raw gates after the top one # has been removed. normalized = gates_without_top_1 / ( mtf.reduce_sum(gates_without_top_1, reduced_dim=experts_dim) + 1e-9) density_2_proxy = mtf.reduce_mean(normalized, reduced_dim=group_size_dim) loss_2 = (mtf.reduce_mean(density_2_proxy * density_2) * float(experts_dim.size * experts_dim.size)) loss += loss_2 * 0.5 # Depending on the policy in the hparams, we may drop out some of the # second-place experts. policy = ( hparams.moe_second_policy_train if train else hparams.moe_second_policy_eval) threshold = ( hparams.moe_second_threshold_train if train else hparams.moe_second_threshold_eval) if policy == "all": # Use second-place experts for all examples. pass elif policy == "none": # Never use second-place experts for all examples. mask_2 = mtf.zeros_like(mask_2) elif policy == "threshold": # Use second-place experts if gate_2 > threshold. mask_2 *= mtf.to_float(mtf.greater(gate_2, threshold)) elif policy == "random": # Use second-place experts with probablity min(1.0, gate_2 / threshold). mask_2 *= mtf.to_float( mtf.less(mtf.random_uniform(gate_2.mesh, gate_2.shape), gate_2 / max(threshold, 1e-9))) else: raise ValueError("Unknown policy %s" % policy) mask_2 = mtf.Print( mask_2, [mtf.reduce_mean(mask_2, output_shape=[experts_dim])], "density_2", summarize=1000) # COMPUTE ASSIGNMENT TO EXPERTS # [batch, group, experts] # This is the position within the expert's mini-batch for this sequence position_in_expert_1 = mtf.cumsum( mask_1, group_size_dim, exclusive=True) * mask_1 # Remove the elements that don't fit. [batch, group, experts] mask_1 *= mtf.to_float(mtf.less(position_in_expert_1, expert_capacity_f)) # [batch, experts] # How many examples in this sequence go to this expert mask_1_count = mtf.reduce_sum(mask_1, reduced_dim=group_size_dim) # [batch, group] - mostly ones, but zeros where something didn't fit mask_1_flat = mtf.reduce_sum(mask_1, reduced_dim=experts_dim) # [batch, group] position_in_expert_1 = mtf.reduce_sum( position_in_expert_1, reduced_dim=experts_dim) # Weight assigned to first expert. [batch, group] gate_1 *= mask_1_flat # [batch, group, experts] position_in_expert_2 = ( mtf.cumsum(mask_2, group_size_dim, exclusive=True) + mask_1_count) position_in_expert_2 *= mask_2 mask_2 *= mtf.to_float(mtf.less(position_in_expert_2, expert_capacity_f)) # mask_2_count = mtf.reduce_sum(mask_2, reduced_dim=experts_dim) mask_2_flat = mtf.reduce_sum(mask_2, reduced_dim=experts_dim) gate_2 *= mask_2_flat position_in_expert_2 = mtf.reduce_sum( position_in_expert_2, reduced_dim=experts_dim) # [batch, group, experts, expert_capacity] combine_tensor = ( gate_1 * mask_1_flat * mtf.one_hot(index_1, experts_dim) * mtf.one_hot(mtf.to_int32(position_in_expert_1), expert_capacity_dim) + gate_2 * mask_2_flat * mtf.one_hot(index_2, experts_dim) * mtf.one_hot(mtf.to_int32(position_in_expert_2), expert_capacity_dim)) combine_tensor = mtf.cast(combine_tensor, inputs.dtype) loss = mtf.cast(loss, inputs.dtype) dispatch_tensor = mtf.cast( mtf.cast(combine_tensor, tf.bool), combine_tensor.dtype) return dispatch_tensor, combine_tensor, loss
[ "Compute gating for mixture-of-experts in TensorFlow.\n\n Note: until the algorithm and inferface solidify, we pass in a hyperparameters\n dictionary in order not to complicate the interface in mtf_transformer.py .\n Once this code moves out of \"research\", we should pass the hyperparameters\n separately.\n\n Hyperparameters used:\n hparams.moe_use_second_place_loss: a boolean\n hparams.moe_second_policy_train: a string\n hparams.moe_second_policy_eval: a string\n hparams.moe_second_threshold: a float\n\n The returned forward assignment is a tensor used to map (via einsum) from the\n inputs to the expert_inputs. Likewise, the returned combine_tensor is\n used to map (via einsum) from the expert outputs to the outputs. Both the\n forward and backward assignments are mostly zeros. The shapes of the tensors\n are as follows.\n\n inputs: [<batch_dims>, group_size_dim, input_dim]\n importance: [<batch_dims>, group_size_dim]\n dispatch_tensor:\n [<batch_dims>, group_size_dim, experts_dim, expert_capacity_dim]\n expert_inputs:\n [<batch_dims>, experts_dim, expert_capacity_dim, input_dim]\n\n expert_outputs: [<batch_dims>, experts_dim, expert_capacity_dim, output_dim]\n combine_tensor:\n [<batch_dims>, group_size_dim, experts_dim, expert_capacity_dim]\n outputs: [<batch_dims>, group_size_dim, output_dim]\n\n \"importance\" is an optional tensor with one floating-point value for each\n input vector. If the importance of an input is 1.0, then we send it to\n up to 2 experts. If 0.0 < importance < 1.0, then we send it to at most\n one expert. If importance == 0.0, then we send it to no experts.\n\n We use \"importance\" at the second-level gating function of a hierarchical\n mixture of experts. Inputs to the first-choice expert-group get importance\n 1.0. Inputs to the second-choice expert group get importance 0.5.\n Inputs that represent padding get importance 0.0.\n\n Args:\n inputs: a mtf.Tensor with shape [<batch_dims>, group_size_dim, input_dim]\n outer_expert_dims: an optional list of dimensions. This is for the case\n where we are at an inner level of a hierarchical MoE.\n experts_dim: a Dimension (the number of experts)\n expert_capacity_dim: a Dimension (number of examples per group per expert)\n hparams: model hyperparameters.\n train: a boolean\n importance: an optional tensor with shape [<batch_dims>, group_size_dim]\n\n Returns:\n dispatch_tensor: a Tensor with shape\n [<batch_dims>, group_size_dim, experts_dim, expert_capacity_dim]\n combine_tensor: a Tensor with shape\n [<batch_dims>, group_size_dim, experts_dim, expert_capacity_dim]\n loss: a mtf scalar\n\n Raises:\n ValueError: on illegal hyperparameters\n " ]
Please provide a description of the function:def set_default_moe_hparams(hparams): hparams.moe_num_experts = 16 hparams.moe_loss_coef = 1e-2 hparams.add_hparam("moe_gating", "top_2") # Experts have fixed capacity per batch. We need some extra capacity # in case gating is not perfectly balanced. # moe_capacity_factor_* should be set to a value >=1. hparams.add_hparam("moe_capacity_factor_train", 1.25) hparams.add_hparam("moe_capacity_factor_eval", 2.0) hparams.add_hparam("moe_capacity_factor_second_level", 1.0) # Each expert has a hidden layer with this size. hparams.add_hparam("moe_hidden_size", 4096) # For gating, divide inputs into groups of this size before gating. # Each group sends the same number of inputs to each expert. # Ideally, the group size would be the whole batch, but this is expensive # due to our use of matrix multiplication for reordering. hparams.add_hparam("moe_group_size", 1024) # For top_2 gating, whether to impose an additional loss in order to make # the experts equally used as the second-place expert. hparams.add_hparam("moe_use_second_place_loss", 0) # In top_2 gating, policy for whether to use a second-place expert. # Legal values are: # "all": always # "none": never # "threshold": if gate value > the given threshold # "random": if gate value > threshold*random_uniform(0,1) hparams.add_hparam("moe_second_policy_train", "random") hparams.add_hparam("moe_second_policy_eval", "random") hparams.add_hparam("moe_second_threshold_train", 0.2) hparams.add_hparam("moe_second_threshold_eval", 0.2)
[ "Add necessary hyperparameters for mixture-of-experts." ]
Please provide a description of the function:def _split_into_groups(n, max_group_size, mesh_dim_size): if n % mesh_dim_size != 0: raise ValueError( "n=%d is not a multiple of mesh_dim_size=%d" % (n, mesh_dim_size)) num_groups = max(1, n // max_group_size) while (num_groups % mesh_dim_size != 0 or n % num_groups != 0): num_groups += 1 group_size = n // num_groups tf.logging.info( "_split_into_groups(n=%d, max_group_size=%d, mesh_dim_size=%d)" " = (num_groups=%d group_size=%d)" % (n, max_group_size, mesh_dim_size, num_groups, group_size)) return num_groups, group_size
[ "Helper function for figuring out how to split a dimensino into groups.\n\n We have a dimension with size n and we want to split it into\n two dimensions: n = num_groups * group_size\n\n group_size should be the largest possible value meeting the constraints:\n group_size <= max_group_size\n (num_groups = n/group_size) is a multiple of mesh_dim_size\n\n Args:\n n: an integer\n max_group_size: an integer\n mesh_dim_size: an integer\n\n Returns:\n num_groups: an integer\n group_size: an integer\n\n Raises:\n ValueError: if n is not a multiple of mesh_dim_size\n " ]
Please provide a description of the function:def reset(self, indices=None): return tf.cond( tf.cast(tf.reduce_sum(indices + 1), tf.bool), lambda: self._reset_non_empty(indices), lambda: tf.cast(0, self.observ_dtype))
[ "Reset the batch of environments.\n\n Args:\n indices: The batch indices of the environments to reset.\n\n Returns:\n Batch tensor of the new observations.\n " ]
Please provide a description of the function:def adafactor_decay_rate_adam(beta2): t = tf.to_float(tf.train.get_or_create_global_step()) + 1.0 decay = beta2 * (1.0 - tf.pow(beta2, t - 1.0)) / (1.0 - tf.pow(beta2, t)) # decay = tf.cond(tf.equal(t, 1.0), lambda: beta2, lambda: decay) return decay
[ "Second-moment decay rate like Adam, subsuming the correction factor.\n\n Args:\n beta2: a float between 0 and 1\n Returns:\n a scalar\n " ]
Please provide a description of the function:def adafactor_optimizer_from_hparams(hparams, lr): if hparams.optimizer_adafactor_decay_type == "adam": decay_rate = adafactor_decay_rate_adam( hparams.optimizer_adafactor_beta2) elif hparams.optimizer_adafactor_decay_type == "pow": decay_rate = adafactor_decay_rate_pow( hparams.optimizer_adafactor_memory_exponent) else: raise ValueError("unknown optimizer_adafactor_decay_type") if hparams.weight_dtype == "bfloat16": parameter_encoding = quantization.EighthPowerEncoding() else: parameter_encoding = None return AdafactorOptimizer( multiply_by_parameter_scale=( hparams.optimizer_adafactor_multiply_by_parameter_scale), learning_rate=lr, decay_rate=decay_rate, beta1=hparams.optimizer_adafactor_beta1, clipping_threshold=hparams.optimizer_adafactor_clipping_threshold, factored=hparams.optimizer_adafactor_factored, simulated_quantize_bits=getattr( hparams, "simulated_parameter_quantize_bits", 0), parameter_encoding=parameter_encoding, use_locking=False, name="Adafactor")
[ "Create an Adafactor optimizer based on model hparams.\n\n Args:\n hparams: model hyperparameters\n lr: learning rate scalar.\n Returns:\n an AdafactorOptimizer\n Raises:\n ValueError: on illegal values\n " ]
Please provide a description of the function:def _nargs_validator(nargs, message): if message is None: message = "Registered function must take exactly %d arguments" % nargs def f(key, value): del key spec = inspect.getfullargspec(value) if (len(spec.args) != nargs or spec.varargs is not None or spec.varkw is not None): raise ValueError(message) return f
[ "Makes validator for function to ensure it takes nargs args." ]
Please provide a description of the function:def parse_problem_name(name): # Recursively strip tags until we reach a base name. if name.endswith("_rev"): base, was_reversed, was_copy = parse_problem_name(name[:-4]) if was_reversed: # duplicate rev raise ValueError( "Invalid problem name %s: multiple '_rev' instances" % name) return ProblemSpec(base, True, was_copy) elif name.endswith("_copy"): base, was_reversed, was_copy = parse_problem_name(name[:-5]) if was_copy: raise ValueError( "Invalid problem_name %s: multiple '_copy' instances" % name) return ProblemSpec(base, was_reversed, True) else: return ProblemSpec(name, False, False)
[ "Determines if problem_name specifies a copy and/or reversal.\n\n Args:\n name: str, problem name, possibly with suffixes.\n\n Returns:\n ProblemSpec: namedtuple with [\"base_name\", \"was_reversed\", \"was_copy\"]\n\n Raises:\n ValueError if name contains multiple suffixes of the same type\n ('_rev' or '_copy'). One of each is ok.\n " ]
Please provide a description of the function:def get_problem_name(base_name, was_reversed=False, was_copy=False): if any(base_name.endswith(suffix) for suffix in ("_rev", "_copy")): raise ValueError("`base_name` cannot end in '_rev' or '_copy'") name = base_name if was_copy: name = "%s_copy" % name if was_reversed: name = "%s_rev" % name return name
[ "Construct a problem name from base and reversed/copy options.\n\n Inverse of `parse_problem_name`.\n\n Args:\n base_name: base problem name. Should not end in \"_rev\" or \"_copy\"\n was_reversed: if the problem is to be reversed\n was_copy: if the problem is to be copied\n\n Returns:\n string name consistent with use with `parse_problem_name`.\n\n Raises:\n ValueError if `base_name` ends with \"_rev\" or \"_copy\"\n " ]
Please provide a description of the function:def optimizer(name): warn_msg = ("Please update `registry.optimizer` callsite " "(likely due to a `HParams.optimizer` value)") if name == "SGD": name = "sgd" tf.logging.warning("'SGD' optimizer now keyed by 'sgd'. %s" % warn_msg) elif name == "RMSProp": name = "rms_prop" tf.logging.warning( "'RMSProp' optimizer now keyed by 'rms_prop'. %s" % warn_msg) else: snake_name = misc_utils.camelcase_to_snakecase(name) if name != snake_name: tf.logging.warning( "optimizer names now keyed by snake_case names. %s" % warn_msg) name = snake_name return Registries.optimizers[name]
[ "Get pre-registered optimizer keyed by name.\n\n `name` should be snake case, though SGD -> sgd, RMSProp -> rms_prop and\n UpperCamelCase -> snake_case conversions included for legacy support.\n\n Args:\n name: name of optimizer used in registration. This should be a snake case\n identifier, though others supported for legacy reasons.\n\n Returns:\n optimizer\n " ]
Please provide a description of the function:def problem(problem_name, **kwargs): spec = parse_problem_name(problem_name) try: return Registries.problems[spec.base_name]( was_copy=spec.was_copy, was_reversed=spec.was_reversed) except KeyError: # If name is not found in base problems then try creating an env problem return env_problem(problem_name, **kwargs)
[ "Get possibly copied/reversed problem in `base_registry` or `env_registry`.\n\n Args:\n problem_name: string problem name. See `parse_problem_name`.\n **kwargs: forwarded to env problem's initialize method.\n\n Returns:\n possibly reversed/copied version of base problem registered in the given\n registry.\n " ]
Please provide a description of the function:def env_problem(env_problem_name, **kwargs): ep_cls = Registries.env_problems[env_problem_name] ep = ep_cls() ep.initialize(**kwargs) return ep
[ "Get and initialize the `EnvProblem` with the given name and batch size.\n\n Args:\n env_problem_name: string name of the registered env problem.\n **kwargs: forwarded to env problem's initialize method.\n\n Returns:\n an initialized EnvProblem with the given batch size.\n " ]
Please provide a description of the function:def display_list_by_prefix(names_list, starting_spaces=0): cur_prefix, result_lines = None, [] space = " " * starting_spaces for name in sorted(names_list): split = name.split("_", 1) prefix = split[0] if cur_prefix != prefix: result_lines.append(space + prefix + ":") cur_prefix = prefix result_lines.append(space + " * " + name) return "\n".join(result_lines)
[ "Creates a help string for names_list grouped by prefix." ]
Please provide a description of the function:def help_string(): help_str = lists = tuple( display_list_by_prefix(entries, starting_spaces=4) for entries in [ # pylint: disable=g-complex-comprehension list_models(), list_hparams(), list_ranged_hparams(), list_base_problems(), list_optimizers(), list_attacks(), list_attack_params(), list_pruning_params(), list_pruning_strategies(), list_env_problems(), ]) return help_str % lists
[ "Generate help string with contents of registry.", "\nRegistry contents:\n------------------\n\n Models:\n%s\n\n HParams:\n%s\n\n RangedHParams:\n%s\n\n Problems:\n%s\n\n Optimizers:\n%s\n\n Attacks:\n%s\n\n Attack HParams:\n%s\n\n Pruning HParams:\n%s\n\n Pruning Strategies:\n%s\n\n Env Problems:\n%s\n" ]
Please provide a description of the function:def validate(self, key, value): if self._validator is not None: self._validator(key, value)
[ "Validation function run before setting. Uses function from __init__." ]
Please provide a description of the function:def on_set(self, key, value): if self._on_set is not None: self._on_set(key, value)
[ "Callback called on successful set. Uses function from __init__." ]
Please provide a description of the function:def register(self, key_or_value=None): def decorator(value, key): self[key] = value return value # Handle if decorator was used without parens if callable(key_or_value): return decorator(value=key_or_value, key=None) else: return lambda value: decorator(value, key=key_or_value)
[ "Decorator to register a function, or registration itself.\n\n This is primarily intended for use as a decorator, either with or without\n a key/parentheses.\n ```python\n @my_registry.register('key1')\n def value_fn(x, y, z):\n pass\n\n @my_registry.register()\n def another_fn(x, y):\n pass\n\n @my_registry.register\n def third_func():\n pass\n ```\n\n Note if key_or_value is provided as a non-callable, registration only\n occurs once the returned callback is called with a callable as its only\n argument.\n ```python\n callback = my_registry.register('different_key')\n 'different_key' in my_registry # False\n callback(lambda (x, y): x + y)\n 'different_key' in my_registry # True\n ```\n\n Args:\n key_or_value (optional): key to access the registered value with, or the\n function itself. If `None` (default), `self.default_key` will be called\n on `value` once the returned callback is called with `value` as the only\n arg. If `key_or_value` is itself callable, it is assumed to be the value\n and the key is given by `self.default_key(key)`.\n\n Returns:\n decorated callback, or callback generated a decorated function.\n " ]
Please provide a description of the function:def check_dependicies(objdump_string): GLIBC_version = re.compile(r'0{16}[ \t]+GLIBC_(\d{1,2})[.](\d{1,3})[.]?\d{,3}[ \t]+') versions = GLIBC_version.findall(objdump_string) assert len(versions) > 1 for major, minor in versions: assert int(major) <= 2 assert int(minor) <= 14 GLIBCXX_version = re.compile(r'0{16}[ \t]+GLIBCXX_(\d{1,2})[.](\d{1,2})[.]?(\d{,3})[ \t]+') versions = GLIBCXX_version.findall(objdump_string) assert len(versions) > 1 for major, minor, patch in versions: assert int(major) == 3 assert int(minor) == 4 assert patch == '' or int(patch) <= 19 GOMP_version = re.compile(r'0{16}[ \t]+G?OMP_(\d{1,2})[.](\d{1,2})[.]?\d{,3}[ \t]+') versions = GOMP_version.findall(objdump_string) assert len(versions) > 1 for major, minor in versions: assert int(major) == 1 assert int(minor) == 0
[ "Check the dynamic symbol versions.\n\n Parameters\n ----------\n objdump_string : string\n The dynamic symbol table entries of the file (result of `objdump -T` command).\n " ]
Please provide a description of the function:def _objective_function_wrapper(func): def inner(preds, dataset): labels = dataset.get_label() argc = argc_(func) if argc == 2: grad, hess = func(labels, preds) elif argc == 3: grad, hess = func(labels, preds, dataset.get_group()) else: raise TypeError("Self-defined objective function should have 2 or 3 arguments, got %d" % argc) weight = dataset.get_weight() if weight is not None: if len(weight) == len(grad): grad = np.multiply(grad, weight) hess = np.multiply(hess, weight) else: num_data = len(weight) num_class = len(grad) // num_data if num_class * num_data != len(grad): raise ValueError("Length of grad and hess should equal to num_class * num_data") for k in range_(num_class): for i in range_(num_data): idx = k * num_data + i grad[idx] *= weight[i] hess[idx] *= weight[i] return grad, hess return inner
[ "Decorate an objective function.\n\n Note\n ----\n For multi-class task, the y_pred is group by class_id first, then group by row_id.\n If you want to get i-th row y_pred in j-th class, the access way is y_pred[j * num_data + i]\n and you should group grad and hess in this way as well.\n\n Parameters\n ----------\n func : callable\n Expects a callable with signature ``func(y_true, y_pred)`` or ``func(y_true, y_pred, group):\n\n y_true : array-like of shape = [n_samples]\n The target values.\n y_pred : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task)\n The predicted values.\n group : array-like\n Group/query data, used for ranking task.\n\n Returns\n -------\n new_func : callable\n The new objective function as expected by ``lightgbm.engine.train``.\n The signature is ``new_func(preds, dataset)``:\n\n preds : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task)\n The predicted values.\n dataset : Dataset\n The training set from which the labels will be extracted using ``dataset.get_label()``.\n ", "Call passed function with appropriate arguments.", "weighted for objective", "only one class" ]
Please provide a description of the function:def _eval_function_wrapper(func): def inner(preds, dataset): labels = dataset.get_label() argc = argc_(func) if argc == 2: return func(labels, preds) elif argc == 3: return func(labels, preds, dataset.get_weight()) elif argc == 4: return func(labels, preds, dataset.get_weight(), dataset.get_group()) else: raise TypeError("Self-defined eval function should have 2, 3 or 4 arguments, got %d" % argc) return inner
[ "Decorate an eval function.\n\n Note\n ----\n For multi-class task, the y_pred is group by class_id first, then group by row_id.\n If you want to get i-th row y_pred in j-th class, the access way is y_pred[j * num_data + i].\n\n Parameters\n ----------\n func : callable\n Expects a callable with following signatures:\n ``func(y_true, y_pred)``,\n ``func(y_true, y_pred, weight)``\n or ``func(y_true, y_pred, weight, group)``\n and returns (eval_name->string, eval_result->float, is_bigger_better->bool):\n\n y_true : array-like of shape = [n_samples]\n The target values.\n y_pred : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task)\n The predicted values.\n weight : array-like of shape = [n_samples]\n The weight of samples.\n group : array-like\n Group/query data, used for ranking task.\n\n Returns\n -------\n new_func : callable\n The new eval function as expected by ``lightgbm.engine.train``.\n The signature is ``new_func(preds, dataset)``:\n\n preds : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task)\n The predicted values.\n dataset : Dataset\n The training set from which the labels will be extracted using ``dataset.get_label()``.\n ", "Call passed function with appropriate arguments." ]
Please provide a description of the function:def get_params(self, deep=True): params = super(LGBMModel, self).get_params(deep=deep) params.update(self._other_params) return params
[ "Get parameters for this estimator.\n\n Parameters\n ----------\n deep : bool, optional (default=True)\n If True, will return the parameters for this estimator and\n contained subobjects that are estimators.\n\n Returns\n -------\n params : dict\n Parameter names mapped to their values.\n " ]
Please provide a description of the function:def fit(self, X, y, sample_weight=None, init_score=None, group=None, eval_set=None, eval_names=None, eval_sample_weight=None, eval_class_weight=None, eval_init_score=None, eval_group=None, eval_metric=None, early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None): if self._objective is None: if isinstance(self, LGBMRegressor): self._objective = "regression" elif isinstance(self, LGBMClassifier): self._objective = "binary" elif isinstance(self, LGBMRanker): self._objective = "lambdarank" else: raise ValueError("Unknown LGBMModel type.") if callable(self._objective): self._fobj = _objective_function_wrapper(self._objective) else: self._fobj = None evals_result = {} params = self.get_params() # user can set verbose with kwargs, it has higher priority if not any(verbose_alias in params for verbose_alias in ('verbose', 'verbosity')) and self.silent: params['verbose'] = -1 params.pop('silent', None) params.pop('importance_type', None) params.pop('n_estimators', None) params.pop('class_weight', None) if self._n_classes is not None and self._n_classes > 2: params['num_class'] = self._n_classes if hasattr(self, '_eval_at'): params['eval_at'] = self._eval_at params['objective'] = self._objective if self._fobj: params['objective'] = 'None' # objective = nullptr for unknown objective if callable(eval_metric): feval = _eval_function_wrapper(eval_metric) else: feval = None # register default metric for consistency with callable eval_metric case original_metric = self._objective if isinstance(self._objective, string_type) else None if original_metric is None: # try to deduce from class instance if isinstance(self, LGBMRegressor): original_metric = "l2" elif isinstance(self, LGBMClassifier): original_metric = "multi_logloss" if self._n_classes > 2 else "binary_logloss" elif isinstance(self, LGBMRanker): original_metric = "ndcg" # overwrite default metric by explicitly set metric for metric_alias in ['metric', 'metrics', 'metric_types']: if metric_alias in params: original_metric = params.pop(metric_alias) # concatenate metric from params (or default if not provided in params) and eval_metric original_metric = [original_metric] if isinstance(original_metric, (string_type, type(None))) else original_metric eval_metric = [eval_metric] if isinstance(eval_metric, (string_type, type(None))) else eval_metric params['metric'] = set(original_metric + eval_metric) if not isinstance(X, (DataFrame, DataTable)): _X, _y = _LGBMCheckXY(X, y, accept_sparse=True, force_all_finite=False, ensure_min_samples=2) _LGBMCheckConsistentLength(_X, _y, sample_weight) else: _X, _y = X, y if self.class_weight is not None: class_sample_weight = _LGBMComputeSampleWeight(self.class_weight, y) if sample_weight is None or len(sample_weight) == 0: sample_weight = class_sample_weight else: sample_weight = np.multiply(sample_weight, class_sample_weight) self._n_features = _X.shape[1] def _construct_dataset(X, y, sample_weight, init_score, group, params): ret = Dataset(X, label=y, weight=sample_weight, group=group, params=params) return ret.set_init_score(init_score) train_set = _construct_dataset(_X, _y, sample_weight, init_score, group, params) valid_sets = [] if eval_set is not None: def _get_meta_data(collection, i): if collection is None: return None elif isinstance(collection, list): return collection[i] if len(collection) > i else None elif isinstance(collection, dict): return collection.get(i, None) else: raise TypeError('eval_sample_weight, eval_class_weight, eval_init_score, and eval_group ' 'should be dict or list') if isinstance(eval_set, tuple): eval_set = [eval_set] for i, valid_data in enumerate(eval_set): # reduce cost for prediction training data if valid_data[0] is X and valid_data[1] is y: valid_set = train_set else: valid_weight = _get_meta_data(eval_sample_weight, i) if _get_meta_data(eval_class_weight, i) is not None: valid_class_sample_weight = _LGBMComputeSampleWeight(_get_meta_data(eval_class_weight, i), valid_data[1]) if valid_weight is None or len(valid_weight) == 0: valid_weight = valid_class_sample_weight else: valid_weight = np.multiply(valid_weight, valid_class_sample_weight) valid_init_score = _get_meta_data(eval_init_score, i) valid_group = _get_meta_data(eval_group, i) valid_set = _construct_dataset(valid_data[0], valid_data[1], valid_weight, valid_init_score, valid_group, params) valid_sets.append(valid_set) self._Booster = train(params, train_set, self.n_estimators, valid_sets=valid_sets, valid_names=eval_names, early_stopping_rounds=early_stopping_rounds, evals_result=evals_result, fobj=self._fobj, feval=feval, verbose_eval=verbose, feature_name=feature_name, categorical_feature=categorical_feature, callbacks=callbacks) if evals_result: self._evals_result = evals_result if early_stopping_rounds is not None: self._best_iteration = self._Booster.best_iteration self._best_score = self._Booster.best_score # free dataset self.booster_.free_dataset() del train_set, valid_sets return self
[ "Build a gradient boosting model from the training set (X, y).\n\n Parameters\n ----------\n X : array-like or sparse matrix of shape = [n_samples, n_features]\n Input feature matrix.\n y : array-like of shape = [n_samples]\n The target values (class labels in classification, real numbers in regression).\n sample_weight : array-like of shape = [n_samples] or None, optional (default=None)\n Weights of training data.\n init_score : array-like of shape = [n_samples] or None, optional (default=None)\n Init score of training data.\n group : array-like or None, optional (default=None)\n Group data of training data.\n eval_set : list or None, optional (default=None)\n A list of (X, y) tuple pairs to use as validation sets.\n eval_names : list of strings or None, optional (default=None)\n Names of eval_set.\n eval_sample_weight : list of arrays or None, optional (default=None)\n Weights of eval data.\n eval_class_weight : list or None, optional (default=None)\n Class weights of eval data.\n eval_init_score : list of arrays or None, optional (default=None)\n Init score of eval data.\n eval_group : list of arrays or None, optional (default=None)\n Group data of eval data.\n eval_metric : string, list of strings, callable or None, optional (default=None)\n If string, it should be a built-in evaluation metric to use.\n If callable, it should be a custom evaluation metric, see note below for more details.\n In either case, the ``metric`` from the model parameters will be evaluated and used as well.\n Default: 'l2' for LGBMRegressor, 'logloss' for LGBMClassifier, 'ndcg' for LGBMRanker.\n early_stopping_rounds : int or None, optional (default=None)\n Activates early stopping. The model will train until the validation score stops improving.\n Validation score needs to improve at least every ``early_stopping_rounds`` round(s)\n to continue training.\n Requires at least one validation data and one metric.\n If there's more than one, will check all of them. But the training data is ignored anyway.\n To check only the first metric you can pass in ``callbacks``\n ``early_stopping`` callback with ``first_metric_only=True``.\n verbose : bool or int, optional (default=True)\n Requires at least one evaluation data.\n If True, the eval metric on the eval set is printed at each boosting stage.\n If int, the eval metric on the eval set is printed at every ``verbose`` boosting stage.\n The last boosting stage or the boosting stage found by using ``early_stopping_rounds`` is also printed.\n\n Example\n -------\n With ``verbose`` = 4 and at least one item in ``eval_set``,\n an evaluation metric is printed every 4 (instead of 1) boosting stages.\n\n feature_name : list of strings or 'auto', optional (default='auto')\n Feature names.\n If 'auto' and data is pandas DataFrame, data columns names are used.\n categorical_feature : list of strings or int, or 'auto', optional (default='auto')\n Categorical features.\n If list of int, interpreted as indices.\n If list of strings, interpreted as feature names (need to specify ``feature_name`` as well).\n If 'auto' and data is pandas DataFrame, pandas unordered categorical columns are used.\n All values in categorical features should be less than int32 max value (2147483647).\n Large values could be memory consuming. Consider using consecutive integers starting from zero.\n All negative values in categorical features will be treated as missing values.\n callbacks : list of callback functions or None, optional (default=None)\n List of callback functions that are applied at each iteration.\n See Callbacks in Python API for more information.\n\n Returns\n -------\n self : object\n Returns self.\n\n Note\n ----\n Custom eval function expects a callable with following signatures:\n ``func(y_true, y_pred)``, ``func(y_true, y_pred, weight)`` or\n ``func(y_true, y_pred, weight, group)``\n and returns (eval_name, eval_result, is_bigger_better) or\n list of (eval_name, eval_result, is_bigger_better):\n\n y_true : array-like of shape = [n_samples]\n The target values.\n y_pred : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task)\n The predicted values.\n weight : array-like of shape = [n_samples]\n The weight of samples.\n group : array-like\n Group/query data, used for ranking task.\n eval_name : string\n The name of evaluation.\n eval_result : float\n The eval result.\n is_bigger_better : bool\n Is eval result bigger better, e.g. AUC is bigger_better.\n\n For multi-class task, the y_pred is group by class_id first, then group by row_id.\n If you want to get i-th row y_pred in j-th class, the access way is y_pred[j * num_data + i].\n " ]
Please provide a description of the function:def predict(self, X, raw_score=False, num_iteration=None, pred_leaf=False, pred_contrib=False, **kwargs): if self._n_features is None: raise LGBMNotFittedError("Estimator not fitted, call `fit` before exploiting the model.") if not isinstance(X, (DataFrame, DataTable)): X = _LGBMCheckArray(X, accept_sparse=True, force_all_finite=False) n_features = X.shape[1] if self._n_features != n_features: raise ValueError("Number of features of the model must " "match the input. Model n_features_ is %s and " "input n_features is %s " % (self._n_features, n_features)) return self.booster_.predict(X, raw_score=raw_score, num_iteration=num_iteration, pred_leaf=pred_leaf, pred_contrib=pred_contrib, **kwargs)
[ "Return the predicted value for each sample.\n\n Parameters\n ----------\n X : array-like or sparse matrix of shape = [n_samples, n_features]\n Input features matrix.\n raw_score : bool, optional (default=False)\n Whether to predict raw scores.\n num_iteration : int or None, optional (default=None)\n Limit number of iterations in the prediction.\n If None, if the best iteration exists, it is used; otherwise, all trees are used.\n If <= 0, all trees are used (no limits).\n pred_leaf : bool, optional (default=False)\n Whether to predict leaf index.\n pred_contrib : bool, optional (default=False)\n Whether to predict feature contributions.\n\n Note\n ----\n If you want to get more explanations for your model's predictions using SHAP values,\n like SHAP interaction values,\n you can install the shap package (https://github.com/slundberg/shap).\n Note that unlike the shap package, with ``pred_contrib`` we return a matrix with an extra\n column, where the last column is the expected value.\n\n **kwargs\n Other parameters for the prediction.\n\n Returns\n -------\n predicted_result : array-like of shape = [n_samples] or shape = [n_samples, n_classes]\n The predicted values.\n X_leaves : array-like of shape = [n_samples, n_trees] or shape = [n_samples, n_trees * n_classes]\n If ``pred_leaf=True``, the predicted leaf of every tree for each sample.\n X_SHAP_values : array-like of shape = [n_samples, n_features + 1] or shape = [n_samples, (n_features + 1) * n_classes]\n If ``pred_contrib=True``, the feature contributions for each sample.\n " ]
Please provide a description of the function:def feature_importances_(self): if self._n_features is None: raise LGBMNotFittedError('No feature_importances found. Need to call fit beforehand.') return self.booster_.feature_importance(importance_type=self.importance_type)
[ "Get feature importances.\n\n Note\n ----\n Feature importance in sklearn interface used to normalize to 1,\n it's deprecated after 2.0.4 and is the same as Booster.feature_importance() now.\n ``importance_type`` attribute is passed to the function\n to configure the type of importance values to be extracted.\n " ]
Please provide a description of the function:def fit(self, X, y, sample_weight=None, init_score=None, eval_set=None, eval_names=None, eval_sample_weight=None, eval_init_score=None, eval_metric=None, early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None): super(LGBMRegressor, self).fit(X, y, sample_weight=sample_weight, init_score=init_score, eval_set=eval_set, eval_names=eval_names, eval_sample_weight=eval_sample_weight, eval_init_score=eval_init_score, eval_metric=eval_metric, early_stopping_rounds=early_stopping_rounds, verbose=verbose, feature_name=feature_name, categorical_feature=categorical_feature, callbacks=callbacks) return self
[ "Docstring is inherited from the LGBMModel." ]
Please provide a description of the function:def fit(self, X, y, sample_weight=None, init_score=None, eval_set=None, eval_names=None, eval_sample_weight=None, eval_class_weight=None, eval_init_score=None, eval_metric=None, early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None): _LGBMAssertAllFinite(y) _LGBMCheckClassificationTargets(y) self._le = _LGBMLabelEncoder().fit(y) _y = self._le.transform(y) self._classes = self._le.classes_ self._n_classes = len(self._classes) if self._n_classes > 2: # Switch to using a multiclass objective in the underlying LGBM instance ova_aliases = ("multiclassova", "multiclass_ova", "ova", "ovr") if self._objective not in ova_aliases and not callable(self._objective): self._objective = "multiclass" if eval_metric in ('logloss', 'binary_logloss'): eval_metric = "multi_logloss" elif eval_metric in ('error', 'binary_error'): eval_metric = "multi_error" else: if eval_metric in ('logloss', 'multi_logloss'): eval_metric = 'binary_logloss' elif eval_metric in ('error', 'multi_error'): eval_metric = 'binary_error' if eval_set is not None: if isinstance(eval_set, tuple): eval_set = [eval_set] for i, (valid_x, valid_y) in enumerate(eval_set): if valid_x is X and valid_y is y: eval_set[i] = (valid_x, _y) else: eval_set[i] = (valid_x, self._le.transform(valid_y)) super(LGBMClassifier, self).fit(X, _y, sample_weight=sample_weight, init_score=init_score, eval_set=eval_set, eval_names=eval_names, eval_sample_weight=eval_sample_weight, eval_class_weight=eval_class_weight, eval_init_score=eval_init_score, eval_metric=eval_metric, early_stopping_rounds=early_stopping_rounds, verbose=verbose, feature_name=feature_name, categorical_feature=categorical_feature, callbacks=callbacks) return self
[ "Docstring is inherited from the LGBMModel." ]
Please provide a description of the function:def predict(self, X, raw_score=False, num_iteration=None, pred_leaf=False, pred_contrib=False, **kwargs): result = self.predict_proba(X, raw_score, num_iteration, pred_leaf, pred_contrib, **kwargs) if raw_score or pred_leaf or pred_contrib: return result else: class_index = np.argmax(result, axis=1) return self._le.inverse_transform(class_index)
[ "Docstring is inherited from the LGBMModel." ]
Please provide a description of the function:def predict_proba(self, X, raw_score=False, num_iteration=None, pred_leaf=False, pred_contrib=False, **kwargs): result = super(LGBMClassifier, self).predict(X, raw_score, num_iteration, pred_leaf, pred_contrib, **kwargs) if self._n_classes > 2 or raw_score or pred_leaf or pred_contrib: return result else: return np.vstack((1. - result, result)).transpose()
[ "Return the predicted probability for each class for each sample.\n\n Parameters\n ----------\n X : array-like or sparse matrix of shape = [n_samples, n_features]\n Input features matrix.\n raw_score : bool, optional (default=False)\n Whether to predict raw scores.\n num_iteration : int or None, optional (default=None)\n Limit number of iterations in the prediction.\n If None, if the best iteration exists, it is used; otherwise, all trees are used.\n If <= 0, all trees are used (no limits).\n pred_leaf : bool, optional (default=False)\n Whether to predict leaf index.\n pred_contrib : bool, optional (default=False)\n Whether to predict feature contributions.\n\n Note\n ----\n If you want to get more explanations for your model's predictions using SHAP values,\n like SHAP interaction values,\n you can install the shap package (https://github.com/slundberg/shap).\n Note that unlike the shap package, with ``pred_contrib`` we return a matrix with an extra\n column, where the last column is the expected value.\n\n **kwargs\n Other parameters for the prediction.\n\n Returns\n -------\n predicted_probability : array-like of shape = [n_samples, n_classes]\n The predicted probability for each class for each sample.\n X_leaves : array-like of shape = [n_samples, n_trees * n_classes]\n If ``pred_leaf=True``, the predicted leaf of every tree for each sample.\n X_SHAP_values : array-like of shape = [n_samples, (n_features + 1) * n_classes]\n If ``pred_contrib=True``, the feature contributions for each sample.\n " ]
Please provide a description of the function:def fit(self, X, y, sample_weight=None, init_score=None, group=None, eval_set=None, eval_names=None, eval_sample_weight=None, eval_init_score=None, eval_group=None, eval_metric=None, eval_at=[1], early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None): # check group data if group is None: raise ValueError("Should set group for ranking task") if eval_set is not None: if eval_group is None: raise ValueError("Eval_group cannot be None when eval_set is not None") elif len(eval_group) != len(eval_set): raise ValueError("Length of eval_group should be equal to eval_set") elif (isinstance(eval_group, dict) and any(i not in eval_group or eval_group[i] is None for i in range_(len(eval_group))) or isinstance(eval_group, list) and any(group is None for group in eval_group)): raise ValueError("Should set group for all eval datasets for ranking task; " "if you use dict, the index should start from 0") self._eval_at = eval_at super(LGBMRanker, self).fit(X, y, sample_weight=sample_weight, init_score=init_score, group=group, eval_set=eval_set, eval_names=eval_names, eval_sample_weight=eval_sample_weight, eval_init_score=eval_init_score, eval_group=eval_group, eval_metric=eval_metric, early_stopping_rounds=early_stopping_rounds, verbose=verbose, feature_name=feature_name, categorical_feature=categorical_feature, callbacks=callbacks) return self
[ "Docstring is inherited from the LGBMModel." ]
Please provide a description of the function:def get_parameter_infos(config_hpp): is_inparameter = False parameter_group = None cur_key = None cur_info = {} keys = [] member_infos = [] with open(config_hpp) as config_hpp_file: for line in config_hpp_file: if "#pragma region Parameters" in line: is_inparameter = True elif "#pragma region" in line and "Parameters" in line: cur_key = line.split("region")[1].strip() keys.append(cur_key) member_infos.append([]) elif '#pragma endregion' in line: if cur_key is not None: cur_key = None elif is_inparameter: is_inparameter = False elif cur_key is not None: line = line.strip() if line.startswith("//"): key, _, val = line[2:].partition("=") key = key.strip() val = val.strip() if key not in cur_info: if key == "descl2" and "desc" not in cur_info: cur_info["desc"] = [] elif key != "descl2": cur_info[key] = [] if key == "desc": cur_info["desc"].append(("l1", val)) elif key == "descl2": cur_info["desc"].append(("l2", val)) else: cur_info[key].append(val) elif line: has_eqsgn = False tokens = line.split("=") if len(tokens) == 2: if "default" not in cur_info: cur_info["default"] = [tokens[1][:-1].strip()] has_eqsgn = True tokens = line.split() cur_info["inner_type"] = [tokens[0].strip()] if "name" not in cur_info: if has_eqsgn: cur_info["name"] = [tokens[1].strip()] else: cur_info["name"] = [tokens[1][:-1].strip()] member_infos[-1].append(cur_info) cur_info = {} return keys, member_infos
[ "Parse config header file.\n\n Parameters\n ----------\n config_hpp : string\n Path to the config header file.\n\n Returns\n -------\n infos : tuple\n Tuple with names and content of sections.\n " ]
Please provide a description of the function:def get_names(infos): names = [] for x in infos: for y in x: names.append(y["name"][0]) return names
[ "Get names of all parameters.\n\n Parameters\n ----------\n infos : list\n Content of the config header file.\n\n Returns\n -------\n names : list\n Names of all parameters.\n " ]
Please provide a description of the function:def get_alias(infos): pairs = [] for x in infos: for y in x: if "alias" in y: name = y["name"][0] alias = y["alias"][0].split(',') for name2 in alias: pairs.append((name2.strip(), name)) return pairs
[ "Get aliases of all parameters.\n\n Parameters\n ----------\n infos : list\n Content of the config header file.\n\n Returns\n -------\n pairs : list\n List of tuples (param alias, param name).\n " ]
Please provide a description of the function:def set_one_var_from_string(name, param_type, checks): ret = "" univar_mapper = {"int": "GetInt", "double": "GetDouble", "bool": "GetBool", "std::string": "GetString"} if "vector" not in param_type: ret += " %s(params, \"%s\", &%s);\n" % (univar_mapper[param_type], name, name) if len(checks) > 0: for check in checks: ret += " CHECK(%s %s);\n" % (name, check) ret += "\n" else: ret += " if (GetString(params, \"%s\", &tmp_str)) {\n" % (name) type2 = param_type.split("<")[1][:-1] if type2 == "std::string": ret += " %s = Common::Split(tmp_str.c_str(), ',');\n" % (name) else: ret += " %s = Common::StringToArray<%s>(tmp_str, ',');\n" % (name, type2) ret += " }\n\n" return ret
[ "Construct code for auto config file for one param value.\n\n Parameters\n ----------\n name : string\n Name of the parameter.\n param_type : string\n Type of the parameter.\n checks : list\n Constraints of the parameter.\n\n Returns\n -------\n ret : string\n Lines of auto config file with getting and checks of one parameter value.\n " ]
Please provide a description of the function:def gen_parameter_description(sections, descriptions, params_rst): def parse_check(check, reverse=False): try: idx = 1 float(check[idx:]) except ValueError: idx = 2 float(check[idx:]) if reverse: reversed_sign = {'<': '>', '>': '<', '<=': '>=', '>=': '<='} return check[idx:], reversed_sign[check[:idx]] else: return check[idx:], check[:idx] params_to_write = [] for section_name, section_params in zip(sections, descriptions): params_to_write.append('{0}\n{1}'.format(section_name, '-' * len(section_name))) for param_desc in section_params: name = param_desc['name'][0] default_raw = param_desc['default'][0] default = default_raw.strip('"') if len(default_raw.strip('"')) > 0 else default_raw param_type = param_desc.get('type', param_desc['inner_type'])[0].split(':')[-1].split('<')[-1].strip('>') options = param_desc.get('options', []) if len(options) > 0: options_str = ', options: ``{0}``'.format('``, ``'.join([x.strip() for x in options[0].split(',')])) else: options_str = '' aliases = param_desc.get('alias', []) if len(aliases) > 0: aliases_str = ', aliases: ``{0}``'.format('``, ``'.join([x.strip() for x in aliases[0].split(',')])) else: aliases_str = '' checks = sorted(param_desc.get('check', [])) checks_len = len(checks) if checks_len > 1: number1, sign1 = parse_check(checks[0]) number2, sign2 = parse_check(checks[1], reverse=True) checks_str = ', constraints: ``{0} {1} {2} {3} {4}``'.format(number2, sign2, name, sign1, number1) elif checks_len == 1: number, sign = parse_check(checks[0]) checks_str = ', constraints: ``{0} {1} {2}``'.format(name, sign, number) else: checks_str = '' main_desc = '- ``{0}`` :raw-html:`<a id="{0}" title="Permalink to this parameter" href="#{0}">&#x1F517;&#xFE0E;</a>`, default = ``{1}``, type = {2}{3}{4}{5}'.format(name, default, param_type, options_str, aliases_str, checks_str) params_to_write.append(main_desc) params_to_write.extend([' ' * 3 * int(desc[0][-1]) + '- ' + desc[1] for desc in param_desc['desc']]) with open(params_rst) as original_params_file: all_lines = original_params_file.read() before, start_sep, _ = all_lines.partition('.. start params list\n\n') _, end_sep, after = all_lines.partition('\n\n.. end params list') with open(params_rst, "w") as new_params_file: new_params_file.write(before) new_params_file.write(start_sep) new_params_file.write('\n\n'.join(params_to_write)) new_params_file.write(end_sep) new_params_file.write(after)
[ "Write descriptions of parameters to the documentation file.\n\n Parameters\n ----------\n sections : list\n Names of parameters sections.\n descriptions : list\n Structured descriptions of parameters.\n params_rst : string\n Path to the file with parameters documentation.\n ", "Parse the constraint.\n\n Parameters\n ----------\n check : string\n String representation of the constraint.\n reverse : bool, optional (default=False)\n Whether to reverse the sign of the constraint.\n\n Returns\n -------\n pair : tuple\n Parsed constraint in the form of tuple (value, sign).\n " ]
Please provide a description of the function:def gen_parameter_code(config_hpp, config_out_cpp): keys, infos = get_parameter_infos(config_hpp) names = get_names(infos) alias = get_alias(infos) str_to_write = r str_to_write += "#include<LightGBM/config.h>\nnamespace LightGBM {\n" # alias table str_to_write += "std::unordered_map<std::string, std::string> Config::alias_table({\n" for pair in alias: str_to_write += " {\"%s\", \"%s\"},\n" % (pair[0], pair[1]) str_to_write += "});\n\n" # names str_to_write += "std::unordered_set<std::string> Config::parameter_set({\n" for name in names: str_to_write += " \"%s\",\n" % (name) str_to_write += "});\n\n" # from strings str_to_write += "void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {\n" str_to_write += " std::string tmp_str = \"\";\n" for x in infos: for y in x: if "[doc-only]" in y: continue param_type = y["inner_type"][0] name = y["name"][0] checks = [] if "check" in y: checks = y["check"] tmp = set_one_var_from_string(name, param_type, checks) str_to_write += tmp # tails str_to_write += "}\n\n" str_to_write += "std::string Config::SaveMembersToString() const {\n" str_to_write += " std::stringstream str_buf;\n" for x in infos: for y in x: if "[doc-only]" in y: continue param_type = y["inner_type"][0] name = y["name"][0] if "vector" in param_type: if "int8" in param_type: str_to_write += " str_buf << \"[%s: \" << Common::Join(Common::ArrayCast<int8_t, int>(%s), \",\") << \"]\\n\";\n" % (name, name) else: str_to_write += " str_buf << \"[%s: \" << Common::Join(%s, \",\") << \"]\\n\";\n" % (name, name) else: str_to_write += " str_buf << \"[%s: \" << %s << \"]\\n\";\n" % (name, name) # tails str_to_write += " return str_buf.str();\n" str_to_write += "}\n\n" str_to_write += "} // namespace LightGBM\n" with open(config_out_cpp, "w") as config_out_cpp_file: config_out_cpp_file.write(str_to_write) return keys, infos
[ "Generate auto config file.\n\n Parameters\n ----------\n config_hpp : string\n Path to the config header file.\n config_out_cpp : string\n Path to the auto config file.\n\n Returns\n -------\n infos : tuple\n Tuple with names and content of sections.\n ", "/*!\n * Copyright (c) 2018 Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License. See LICENSE file in the project root for license information.\n *\n * \\note\n * This file is auto generated by LightGBM\\helpers\\parameter_generator.py from LightGBM\\include\\LightGBM\\config.h file.\n */\n" ]
Please provide a description of the function:def _load_lib(): lib_path = find_lib_path() if len(lib_path) == 0: return None lib = ctypes.cdll.LoadLibrary(lib_path[0]) lib.LGBM_GetLastError.restype = ctypes.c_char_p return lib
[ "Load LightGBM library." ]
Please provide a description of the function:def list_to_1d_numpy(data, dtype=np.float32, name='list'): if is_numpy_1d_array(data): if data.dtype == dtype: return data else: return data.astype(dtype=dtype, copy=False) elif is_1d_list(data): return np.array(data, dtype=dtype, copy=False) elif isinstance(data, Series): return data.values.astype(dtype) else: raise TypeError("Wrong type({0}) for {1}.\n" "It should be list, numpy 1-D array or pandas Series".format(type(data).__name__, name))
[ "Convert data to 1-D numpy array." ]
Please provide a description of the function:def cfloat32_array_to_numpy(cptr, length): if isinstance(cptr, ctypes.POINTER(ctypes.c_float)): return np.fromiter(cptr, dtype=np.float32, count=length) else: raise RuntimeError('Expected float pointer')
[ "Convert a ctypes float pointer array to a numpy array." ]
Please provide a description of the function:def cfloat64_array_to_numpy(cptr, length): if isinstance(cptr, ctypes.POINTER(ctypes.c_double)): return np.fromiter(cptr, dtype=np.float64, count=length) else: raise RuntimeError('Expected double pointer')
[ "Convert a ctypes double pointer array to a numpy array." ]
Please provide a description of the function:def cint32_array_to_numpy(cptr, length): if isinstance(cptr, ctypes.POINTER(ctypes.c_int32)): return np.fromiter(cptr, dtype=np.int32, count=length) else: raise RuntimeError('Expected int pointer')
[ "Convert a ctypes int pointer array to a numpy array." ]
Please provide a description of the function:def cint8_array_to_numpy(cptr, length): if isinstance(cptr, ctypes.POINTER(ctypes.c_int8)): return np.fromiter(cptr, dtype=np.int8, count=length) else: raise RuntimeError('Expected int pointer')
[ "Convert a ctypes int pointer array to a numpy array." ]
Please provide a description of the function:def param_dict_to_str(data): if data is None or not data: return "" pairs = [] for key, val in data.items(): if isinstance(val, (list, tuple, set)) or is_numpy_1d_array(val): pairs.append(str(key) + '=' + ','.join(map(str, val))) elif isinstance(val, string_type) or isinstance(val, numeric_types) or is_numeric(val): pairs.append(str(key) + '=' + str(val)) elif val is not None: raise TypeError('Unknown type of parameter:%s, got:%s' % (key, type(val).__name__)) return ' '.join(pairs)
[ "Convert Python dictionary to string, which is passed to C API." ]
Please provide a description of the function:def convert_from_sliced_object(data): if data.base is not None and isinstance(data, np.ndarray) and isinstance(data.base, np.ndarray): if not data.flags.c_contiguous: warnings.warn("Usage of np.ndarray subset (sliced data) is not recommended " "due to it will double the peak memory cost in LightGBM.") return np.copy(data) return data
[ "Fix the memory of multi-dimensional sliced object." ]
Please provide a description of the function:def c_float_array(data): if is_1d_list(data): data = np.array(data, copy=False) if is_numpy_1d_array(data): data = convert_from_sliced_object(data) assert data.flags.c_contiguous if data.dtype == np.float32: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) type_data = C_API_DTYPE_FLOAT32 elif data.dtype == np.float64: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) type_data = C_API_DTYPE_FLOAT64 else: raise TypeError("Expected np.float32 or np.float64, met type({})" .format(data.dtype)) else: raise TypeError("Unknown type({})".format(type(data).__name__)) return (ptr_data, type_data, data)
[ "Get pointer of float numpy array / list." ]
Please provide a description of the function:def c_int_array(data): if is_1d_list(data): data = np.array(data, copy=False) if is_numpy_1d_array(data): data = convert_from_sliced_object(data) assert data.flags.c_contiguous if data.dtype == np.int32: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)) type_data = C_API_DTYPE_INT32 elif data.dtype == np.int64: ptr_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_int64)) type_data = C_API_DTYPE_INT64 else: raise TypeError("Expected np.int32 or np.int64, met type({})" .format(data.dtype)) else: raise TypeError("Unknown type({})".format(type(data).__name__)) return (ptr_data, type_data, data)
[ "Get pointer of int numpy array / list." ]
Please provide a description of the function:def predict(self, data, num_iteration=-1, raw_score=False, pred_leaf=False, pred_contrib=False, data_has_header=False, is_reshape=True): if isinstance(data, Dataset): raise TypeError("Cannot use Dataset instance for prediction, please use raw data instead") data = _data_from_pandas(data, None, None, self.pandas_categorical)[0] predict_type = C_API_PREDICT_NORMAL if raw_score: predict_type = C_API_PREDICT_RAW_SCORE if pred_leaf: predict_type = C_API_PREDICT_LEAF_INDEX if pred_contrib: predict_type = C_API_PREDICT_CONTRIB int_data_has_header = 1 if data_has_header else 0 if num_iteration > self.num_total_iteration: num_iteration = self.num_total_iteration if isinstance(data, string_type): with _TempFile() as f: _safe_call(_LIB.LGBM_BoosterPredictForFile( self.handle, c_str(data), ctypes.c_int(int_data_has_header), ctypes.c_int(predict_type), ctypes.c_int(num_iteration), c_str(self.pred_parameter), c_str(f.name))) lines = f.readlines() nrow = len(lines) preds = [float(token) for line in lines for token in line.split('\t')] preds = np.array(preds, dtype=np.float64, copy=False) elif isinstance(data, scipy.sparse.csr_matrix): preds, nrow = self.__pred_for_csr(data, num_iteration, predict_type) elif isinstance(data, scipy.sparse.csc_matrix): preds, nrow = self.__pred_for_csc(data, num_iteration, predict_type) elif isinstance(data, np.ndarray): preds, nrow = self.__pred_for_np2d(data, num_iteration, predict_type) elif isinstance(data, list): try: data = np.array(data) except BaseException: raise ValueError('Cannot convert data list to numpy array.') preds, nrow = self.__pred_for_np2d(data, num_iteration, predict_type) elif isinstance(data, DataTable): preds, nrow = self.__pred_for_np2d(data.to_numpy(), num_iteration, predict_type) else: try: warnings.warn('Converting data to scipy sparse matrix.') csr = scipy.sparse.csr_matrix(data) except BaseException: raise TypeError('Cannot predict data for type {}'.format(type(data).__name__)) preds, nrow = self.__pred_for_csr(csr, num_iteration, predict_type) if pred_leaf: preds = preds.astype(np.int32) if is_reshape and preds.size != nrow: if preds.size % nrow == 0: preds = preds.reshape(nrow, -1) else: raise ValueError('Length of predict result (%d) cannot be divide nrow (%d)' % (preds.size, nrow)) return preds
[ "Predict logic.\n\n Parameters\n ----------\n data : string, numpy array, pandas DataFrame, H2O DataTable's Frame or scipy.sparse\n Data source for prediction.\n When data type is string, it represents the path of txt file.\n num_iteration : int, optional (default=-1)\n Iteration used for prediction.\n raw_score : bool, optional (default=False)\n Whether to predict raw scores.\n pred_leaf : bool, optional (default=False)\n Whether to predict leaf index.\n pred_contrib : bool, optional (default=False)\n Whether to predict feature contributions.\n data_has_header : bool, optional (default=False)\n Whether data has header.\n Used only for txt data.\n is_reshape : bool, optional (default=True)\n Whether to reshape to (nrow, ncol).\n\n Returns\n -------\n result : numpy array\n Prediction result.\n " ]
Please provide a description of the function:def __get_num_preds(self, num_iteration, nrow, predict_type): if nrow > MAX_INT32: raise LightGBMError('LightGBM cannot perform prediction for data' 'with number of rows greater than MAX_INT32 (%d).\n' 'You can split your data into chunks' 'and then concatenate predictions for them' % MAX_INT32) n_preds = ctypes.c_int64(0) _safe_call(_LIB.LGBM_BoosterCalcNumPredict( self.handle, ctypes.c_int(nrow), ctypes.c_int(predict_type), ctypes.c_int(num_iteration), ctypes.byref(n_preds))) return n_preds.value
[ "Get size of prediction result." ]
Please provide a description of the function:def __pred_for_np2d(self, mat, num_iteration, predict_type): if len(mat.shape) != 2: raise ValueError('Input numpy.ndarray or list must be 2 dimensional') def inner_predict(mat, num_iteration, predict_type, preds=None): if mat.dtype == np.float32 or mat.dtype == np.float64: data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) else: data = np.array(mat.reshape(mat.size), dtype=np.float32) ptr_data, type_ptr_data, _ = c_float_array(data) n_preds = self.__get_num_preds(num_iteration, mat.shape[0], predict_type) if preds is None: preds = np.zeros(n_preds, dtype=np.float64) elif len(preds.shape) != 1 or len(preds) != n_preds: raise ValueError("Wrong length of pre-allocated predict array") out_num_preds = ctypes.c_int64(0) _safe_call(_LIB.LGBM_BoosterPredictForMat( self.handle, ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int(mat.shape[0]), ctypes.c_int(mat.shape[1]), ctypes.c_int(C_API_IS_ROW_MAJOR), ctypes.c_int(predict_type), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.byref(out_num_preds), preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if n_preds != out_num_preds.value: raise ValueError("Wrong length for predict results") return preds, mat.shape[0] nrow = mat.shape[0] if nrow > MAX_INT32: sections = np.arange(start=MAX_INT32, stop=nrow, step=MAX_INT32) # __get_num_preds() cannot work with nrow > MAX_INT32, so calculate overall number of predictions piecemeal n_preds = [self.__get_num_preds(num_iteration, i, predict_type) for i in np.diff([0] + list(sections) + [nrow])] n_preds_sections = np.array([0] + n_preds, dtype=np.intp).cumsum() preds = np.zeros(sum(n_preds), dtype=np.float64) for chunk, (start_idx_pred, end_idx_pred) in zip_(np.array_split(mat, sections), zip_(n_preds_sections, n_preds_sections[1:])): # avoid memory consumption by arrays concatenation operations inner_predict(chunk, num_iteration, predict_type, preds[start_idx_pred:end_idx_pred]) return preds, nrow else: return inner_predict(mat, num_iteration, predict_type)
[ "Predict for a 2-D numpy matrix.", "change non-float data to float data, need to copy" ]
Please provide a description of the function:def __pred_for_csr(self, csr, num_iteration, predict_type): def inner_predict(csr, num_iteration, predict_type, preds=None): nrow = len(csr.indptr) - 1 n_preds = self.__get_num_preds(num_iteration, nrow, predict_type) if preds is None: preds = np.zeros(n_preds, dtype=np.float64) elif len(preds.shape) != 1 or len(preds) != n_preds: raise ValueError("Wrong length of pre-allocated predict array") out_num_preds = ctypes.c_int64(0) ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr) ptr_data, type_ptr_data, _ = c_float_array(csr.data) assert csr.shape[1] <= MAX_INT32 csr.indices = csr.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_BoosterPredictForCSR( self.handle, ptr_indptr, ctypes.c_int32(type_ptr_indptr), csr.indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csr.indptr)), ctypes.c_int64(len(csr.data)), ctypes.c_int64(csr.shape[1]), ctypes.c_int(predict_type), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.byref(out_num_preds), preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if n_preds != out_num_preds.value: raise ValueError("Wrong length for predict results") return preds, nrow nrow = len(csr.indptr) - 1 if nrow > MAX_INT32: sections = [0] + list(np.arange(start=MAX_INT32, stop=nrow, step=MAX_INT32)) + [nrow] # __get_num_preds() cannot work with nrow > MAX_INT32, so calculate overall number of predictions piecemeal n_preds = [self.__get_num_preds(num_iteration, i, predict_type) for i in np.diff(sections)] n_preds_sections = np.array([0] + n_preds, dtype=np.intp).cumsum() preds = np.zeros(sum(n_preds), dtype=np.float64) for (start_idx, end_idx), (start_idx_pred, end_idx_pred) in zip_(zip_(sections, sections[1:]), zip_(n_preds_sections, n_preds_sections[1:])): # avoid memory consumption by arrays concatenation operations inner_predict(csr[start_idx:end_idx], num_iteration, predict_type, preds[start_idx_pred:end_idx_pred]) return preds, nrow else: return inner_predict(csr, num_iteration, predict_type)
[ "Predict for a CSR data." ]
Please provide a description of the function:def __pred_for_csc(self, csc, num_iteration, predict_type): nrow = csc.shape[0] if nrow > MAX_INT32: return self.__pred_for_csr(csc.tocsr(), num_iteration, predict_type) n_preds = self.__get_num_preds(num_iteration, nrow, predict_type) preds = np.zeros(n_preds, dtype=np.float64) out_num_preds = ctypes.c_int64(0) ptr_indptr, type_ptr_indptr, __ = c_int_array(csc.indptr) ptr_data, type_ptr_data, _ = c_float_array(csc.data) assert csc.shape[0] <= MAX_INT32 csc.indices = csc.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_BoosterPredictForCSC( self.handle, ptr_indptr, ctypes.c_int32(type_ptr_indptr), csc.indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csc.indptr)), ctypes.c_int64(len(csc.data)), ctypes.c_int64(csc.shape[0]), ctypes.c_int(predict_type), ctypes.c_int(num_iteration), c_str(self.pred_parameter), ctypes.byref(out_num_preds), preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) if n_preds != out_num_preds.value: raise ValueError("Wrong length for predict results") return preds, nrow
[ "Predict for a CSC data." ]
Please provide a description of the function:def __init_from_np2d(self, mat, params_str, ref_dataset): if len(mat.shape) != 2: raise ValueError('Input numpy.ndarray must be 2 dimensional') self.handle = ctypes.c_void_p() if mat.dtype == np.float32 or mat.dtype == np.float64: data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) else: # change non-float data to float data, need to copy data = np.array(mat.reshape(mat.size), dtype=np.float32) ptr_data, type_ptr_data, _ = c_float_array(data) _safe_call(_LIB.LGBM_DatasetCreateFromMat( ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int(mat.shape[0]), ctypes.c_int(mat.shape[1]), ctypes.c_int(C_API_IS_ROW_MAJOR), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self
[ "Initialize data from a 2-D numpy matrix." ]
Please provide a description of the function:def __init_from_list_np2d(self, mats, params_str, ref_dataset): ncol = mats[0].shape[1] nrow = np.zeros((len(mats),), np.int32) if mats[0].dtype == np.float64: ptr_data = (ctypes.POINTER(ctypes.c_double) * len(mats))() else: ptr_data = (ctypes.POINTER(ctypes.c_float) * len(mats))() holders = [] type_ptr_data = None for i, mat in enumerate(mats): if len(mat.shape) != 2: raise ValueError('Input numpy.ndarray must be 2 dimensional') if mat.shape[1] != ncol: raise ValueError('Input arrays must have same number of columns') nrow[i] = mat.shape[0] if mat.dtype == np.float32 or mat.dtype == np.float64: mats[i] = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) else: # change non-float data to float data, need to copy mats[i] = np.array(mat.reshape(mat.size), dtype=np.float32) chunk_ptr_data, chunk_type_ptr_data, holder = c_float_array(mats[i]) if type_ptr_data is not None and chunk_type_ptr_data != type_ptr_data: raise ValueError('Input chunks must have same type') ptr_data[i] = chunk_ptr_data type_ptr_data = chunk_type_ptr_data holders.append(holder) self.handle = ctypes.c_void_p() _safe_call(_LIB.LGBM_DatasetCreateFromMats( ctypes.c_int(len(mats)), ctypes.cast(ptr_data, ctypes.POINTER(ctypes.POINTER(ctypes.c_double))), ctypes.c_int(type_ptr_data), nrow.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ctypes.c_int(ncol), ctypes.c_int(C_API_IS_ROW_MAJOR), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self
[ "Initialize data from a list of 2-D numpy matrices." ]
Please provide a description of the function:def __init_from_csr(self, csr, params_str, ref_dataset): if len(csr.indices) != len(csr.data): raise ValueError('Length mismatch: {} vs {}'.format(len(csr.indices), len(csr.data))) self.handle = ctypes.c_void_p() ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr) ptr_data, type_ptr_data, _ = c_float_array(csr.data) assert csr.shape[1] <= MAX_INT32 csr.indices = csr.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_DatasetCreateFromCSR( ptr_indptr, ctypes.c_int(type_ptr_indptr), csr.indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csr.indptr)), ctypes.c_int64(len(csr.data)), ctypes.c_int64(csr.shape[1]), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self
[ "Initialize data from a CSR matrix." ]
Please provide a description of the function:def __init_from_csc(self, csc, params_str, ref_dataset): if len(csc.indices) != len(csc.data): raise ValueError('Length mismatch: {} vs {}'.format(len(csc.indices), len(csc.data))) self.handle = ctypes.c_void_p() ptr_indptr, type_ptr_indptr, __ = c_int_array(csc.indptr) ptr_data, type_ptr_data, _ = c_float_array(csc.data) assert csc.shape[0] <= MAX_INT32 csc.indices = csc.indices.astype(np.int32, copy=False) _safe_call(_LIB.LGBM_DatasetCreateFromCSC( ptr_indptr, ctypes.c_int(type_ptr_indptr), csc.indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ptr_data, ctypes.c_int(type_ptr_data), ctypes.c_int64(len(csc.indptr)), ctypes.c_int64(len(csc.data)), ctypes.c_int64(csc.shape[0]), c_str(params_str), ref_dataset, ctypes.byref(self.handle))) return self
[ "Initialize data from a CSC matrix." ]
Please provide a description of the function:def construct(self): if self.handle is None: if self.reference is not None: if self.used_indices is None: # create valid self._lazy_init(self.data, label=self.label, reference=self.reference, weight=self.weight, group=self.group, init_score=self.init_score, predictor=self._predictor, silent=self.silent, feature_name=self.feature_name, params=self.params) else: # construct subset used_indices = list_to_1d_numpy(self.used_indices, np.int32, name='used_indices') assert used_indices.flags.c_contiguous if self.reference.group is not None: group_info = np.array(self.reference.group).astype(int) _, self.group = np.unique(np.repeat(range_(len(group_info)), repeats=group_info)[self.used_indices], return_counts=True) self.handle = ctypes.c_void_p() params_str = param_dict_to_str(self.params) _safe_call(_LIB.LGBM_DatasetGetSubset( self.reference.construct().handle, used_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)), ctypes.c_int(used_indices.shape[0]), c_str(params_str), ctypes.byref(self.handle))) self.data = self.reference.data self.get_data() if self.group is not None: self.set_group(self.group) if self.get_label() is None: raise ValueError("Label should not be None.") else: # create train self._lazy_init(self.data, label=self.label, weight=self.weight, group=self.group, init_score=self.init_score, predictor=self._predictor, silent=self.silent, feature_name=self.feature_name, categorical_feature=self.categorical_feature, params=self.params) if self.free_raw_data: self.data = None return self
[ "Lazy init.\n\n Returns\n -------\n self : Dataset\n Constructed Dataset object.\n " ]
Please provide a description of the function:def create_valid(self, data, label=None, weight=None, group=None, init_score=None, silent=False, params=None): ret = Dataset(data, label=label, reference=self, weight=weight, group=group, init_score=init_score, silent=silent, params=params, free_raw_data=self.free_raw_data) ret._predictor = self._predictor ret.pandas_categorical = self.pandas_categorical return ret
[ "Create validation data align with current Dataset.\n\n Parameters\n ----------\n data : string, numpy array, pandas DataFrame, H2O DataTable's Frame, scipy.sparse or list of numpy arrays\n Data source of Dataset.\n If string, it represents the path to txt file.\n label : list, numpy 1-D array, pandas Series / one-column DataFrame or None, optional (default=None)\n Label of the data.\n weight : list, numpy 1-D array, pandas Series or None, optional (default=None)\n Weight for each instance.\n group : list, numpy 1-D array, pandas Series or None, optional (default=None)\n Group/query size for Dataset.\n init_score : list, numpy 1-D array, pandas Series or None, optional (default=None)\n Init score for Dataset.\n silent : bool, optional (default=False)\n Whether to print messages during construction.\n params : dict or None, optional (default=None)\n Other parameters for validation Dataset.\n\n Returns\n -------\n valid : Dataset\n Validation Dataset with reference to self.\n " ]