Python numpy.range() Examples

The following are 30 code examples for showing how to use numpy.range(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: neural-structured-learning   Author: tensorflow   File: dataset.py    License: Apache License 2.0 6 votes vote down vote up
def get_graph_nbrhd_paths(train_graph, ent, exclude_tuple):
  """Helper to get neighbor (rels, ents) excluding a particular tuple."""
  es, er, et = exclude_tuple
  neighborhood = []
  for i in range(train_graph.max_path_length):
    if ent == es:
      paths = _proc_paths(train_graph.paths[i][ent], er, et,
                          train_graph.max_path_length,
                          (train_graph.rel_pad, train_graph.ent_pad))
    else:
      paths = _proc_paths(train_graph.paths[i][ent],
                          max_length=train_graph.max_path_length,
                          pad=(train_graph.rel_pad, train_graph.ent_pad))
    neighborhood += paths
  if not neighborhood:
    neighborhood = [[]]
  neighborhood = np.array(neighborhood, dtype=np.int)
  return neighborhood 
Example 2
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 6 votes vote down vote up
def ot2bio_ote(ote_tag_sequence):
    """
    ot2bio function for ote tag sequence
    :param ote_tag_sequence:
    :return:
    """
    new_ote_sequence = []
    n_tag = len(ote_tag_sequence)
    prev_ote_tag = '$$$'
    for i in range(n_tag):
        cur_ote_tag = ote_tag_sequence[i]
        assert cur_ote_tag == 'O' or cur_ote_tag == 'T'
        if cur_ote_tag == 'O':
            new_ote_sequence.append(cur_ote_tag)
        else:
            # cur_ote_tag is T
            if prev_ote_tag == 'T':
                new_ote_sequence.append('I')
            else:
                # cur tag is at the beginning of the opinion target
                new_ote_sequence.append('B')
        prev_ote_tag = cur_ote_tag
    return new_ote_sequence 
Example 3
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 6 votes vote down vote up
def ot2bieos_batch(ote_tags, ts_tags):
    """
    batch version of function ot2bieos
    :param ote_tags: a batch of ote tag sequence
    :param ts_tags: a batch of ts tag sequence
    :return:
    :param ote_tags:
    :param ts_tags:
    :return:
    """
    new_ote_tags, new_ts_tags = [], []
    assert len(ote_tags) == len(ts_tags)
    n_seqs = len(ote_tags)
    for i in range(n_seqs):
        ote, ts = ot2bieos(ote_tag_sequence=ote_tags[i], ts_tag_sequence=ts_tags[i])
        new_ote_tags.append(ote)
        new_ts_tags.append(ts)
    return new_ote_tags, new_ts_tags 
Example 4
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 6 votes vote down vote up
def bio2ot_batch(ote_tags, ts_tags):
    """
    batch version of function bio2ot
    :param ote_tags: a batch of ote tag sequence
    :param ts_tags: a batch of ts tag sequence
    :return:
    """
    new_ote_tags, new_ts_tags = [], []
    assert len(ote_tags) == len(ts_tags)
    n_seqs = len(ote_tags)
    for i in range(n_seqs):
        ote, ts = bio2ot(ote_tag_sequence=ote_tags[i], ts_tag_sequence=ts_tags[i])
        new_ote_tags.append(ote)
        new_ts_tags.append(ts)
    return new_ote_tags, new_ts_tags


# TODO 
Example 5
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 6 votes vote down vote up
def set_cid(dataset, char_vocab):
    """
    set cid field for the records in the dataset
    :param dataset: dataset
    :param char_vocab: vocabulary of character
    :return:
    """
    n_records = len(dataset)
    cids = []
    for i in range(n_records):
        words = dataset[i]['words']
        cids = []
        for w in words:
            cids.append([char_vocab[ch] for ch in list(w)])
        dataset[i]['cids'] = list(cids)
    return dataset 
Example 6
Project: basenji   Author: calico   File: blocks.py    License: Apache License 2.0 5 votes vote down vote up
def conv_tower(inputs, filters_init, filters_mult=1, repeat=1, **kwargs):
  """Construct a reducing convolution block.

  Args:
    inputs:        [batch_size, seq_length, features] input sequence
    filters_init:  Initial Conv1D filters
    filters_mult:  Multiplier for Conv1D filters
    repeat:        Conv block repetitions

  Returns:
    [batch_size, seq_length, features] output sequence
  """

  # flow through variable current
  current = inputs

  # initialize filters
  rep_filters = filters_init

  for ri in range(repeat):
    # convolution
    current = conv_block(current,
      filters=int(np.round(rep_filters)),
      **kwargs)

    # update filters
    rep_filters *= filters_mult

  return current 
Example 7
Project: basenji   Author: calico   File: blocks.py    License: Apache License 2.0 5 votes vote down vote up
def xception_tower(inputs, filters_init, filters_mult=1, repeat=1, **kwargs):
  """Construct a reducing convolution block.

  Args:
    inputs:        [batch_size, seq_length, features] input sequence
    filters_init:  Initial Conv1D filters
    filters_mult:  Multiplier for Conv1D filters
    repeat:        Conv block repetitions

  Returns:
    [batch_size, seq_length, features] output sequence
  """

  # flow through variable current
  current = inputs

  # initialize filters
  rep_filters = filters_init

  for ri in range(repeat):
    # convolution
    current = xception_block(current,
      filters=int(np.round(rep_filters)),
      **kwargs)

    # update filters
    rep_filters *= filters_mult

  return current


############################################################
# Attention
############################################################ 
Example 8
Project: basenji   Author: calico   File: blocks.py    License: Apache License 2.0 5 votes vote down vote up
def position_encoding(current, min_rate=.0001):
  """Add original Transformer positional encodings,

  Args:
    current:  [batch_size, seq_length, features] sequence
    min_rate:

  Returns:
    sequence w/ positional encodings concatenated.
  """
  seq_length = current.shape[1].value
  features = current.shape[2].value

  assert(features % 2 == 0)

  # compute angle rates
  angle_rate_exponents = np.linspace(0, 1, features//2)
  angle_rates = min_rate**angle_rate_exponents

  # compute angle radians
  positions = np.range(seq_length)
  angle_rads = positions[:, np.newaxis] * angle_rates[np.newaxis, :]

  # sines and cosines
  sines = np.sin(angle_rads)
  cosines = np.cos(angle_rads)
  pos_encode = np.concatenate([sines, cosines], axis=-1)

  return current 
Example 9
Project: SpecAugment   Author: DemisEom   File: sparse_image_warp_np.py    License: Apache License 2.0 5 votes vote down vote up
def dense_image_warp(image, flow):
    # batch_size, height, width, channels = (array_ops.shape(image)[0],
    #                                        array_ops.shape(image)[1],
    #                                        array_ops.shape(image)[2],
    #                                        array_ops.shape(image)[3])
    batch_size, height, width, channels = (np.shape(image)[0],
                                           np.shape(image)[1],
                                           np.shape(image)[2],
                                           np.shape(image)[3])

    # The flow is defined on the image grid. Turn the flow into a list of query
    # points in the grid space.
    # grid_x, grid_y = array_ops.meshgrid(
    #     math_ops.range(width), math_ops.range(height))
    # stacked_grid = math_ops.cast(
    #     array_ops.stack([grid_y, grid_x], axis=2), flow.dtype)
    # batched_grid = array_ops.expand_dims(stacked_grid, axis=0)
    # query_points_on_grid = batched_grid - flow
    # query_points_flattened = array_ops.reshape(query_points_on_grid,
    #                                            [batch_size, height * width, 2])
    grid_x, grid_y = np.meshgrid(
        np.range(width), np.range(height))
    stacked_grid = np.cast(
        np.stack([grid_y, grid_x], axis=2), flow.dtype)
    batched_grid = np.expand_dims(stacked_grid, axis=0)
    query_points_on_grid = batched_grid - flow
    query_points_flattened = np.reshape(query_points_on_grid,
                                        [batch_size, height * width, 2])
    # Compute values at the query points, then reshape the result back to the
    # image grid.
    interpolated = interp2d(image, query_points_flattened)
    interpolated = np.reshape(interpolated,
                              [batch_size, height, width, channels])
    return interpolated 
Example 10
Project: neural-structured-learning   Author: tensorflow   File: dataset.py    License: Apache License 2.0 5 votes vote down vote up
def _sample_next_edges(edges, to_sample):
  if len(edges) < to_sample:
    return edges
  sample_ids = np.random.choice(range(len(edges)), size=to_sample,
                                replace=False)
  return [edges[i] for i in sample_ids] 
Example 11
Project: neural-structured-learning   Author: tensorflow   File: dataset.py    License: Apache License 2.0 5 votes vote down vote up
def sample_or_pad(arr, max_size, pad_value=-1):
  """Helper to pad arr along axis 0 to max_size or subsample to max_size."""
  arr_shape = arr.shape
  if arr.size == 0:
    if isinstance(pad_value, list):
      result = np.ones((max_size, len(pad_value)), dtype=arr.dtype) * pad_value
    else:
      result = np.ones((max_size,), dtype=arr.dtype) * pad_value
  elif arr.shape[0] > max_size:
    if arr.ndim == 1:
      result = np.random.choice(arr, size=max_size, replace=False)
    else:
      idx = np.arange(arr.shape[0])
      np.random.shuffle(idx)
      result = arr[idx[:max_size], :]
  else:
    padding = np.ones((max_size-arr.shape[0],) + arr_shape[1:],
                      dtype=arr.dtype)
    if isinstance(pad_value, list):
      for i in range(len(pad_value)):
        padding[..., i] *= pad_value[i]
    else:
      padding *= pad_value
    result = np.concatenate((arr, padding), axis=0)
  # result = np.pad(arr,
  #                 [[0, max_size-arr.shape[0]]] + ([[0, 0]] * (arr.ndim-1)),
  #                 "constant", constant_values=pad_value)
  return result 
Example 12
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bio_ts(ts_tag_sequence):
    """
    ot2bio function for ts tag sequence
    :param ts_tag_sequence:
    :return:
    """
    new_ts_sequence = []
    n_tag = len(ts_tag_sequence)
    prev_pos = '$$$'
    for i in range(n_tag):
        cur_ts_tag = ts_tag_sequence[i]
        if cur_ts_tag == 'O':
            new_ts_sequence.append('O')
            cur_pos = 'O'
        else:
            # current tag is subjective tag, i.e., cur_pos is T
            # print(cur_ts_tag)
            cur_pos, cur_sentiment = cur_ts_tag.split('-')
            if cur_pos == prev_pos:
                # prev_pos is T
                new_ts_sequence.append('I-%s' % cur_sentiment)
            else:
                # prev_pos is O
                new_ts_sequence.append('B-%s' % cur_sentiment)
        prev_pos = cur_pos
    return new_ts_sequence 
Example 13
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bio_ote_batch(ote_tag_seqs):
    """
    batch version of function ot2bio_ote
    :param ote_tags:
    :return:
    """
    new_ote_tag_seqs = []
    n_seqs = len(ote_tag_seqs)
    for i in range(n_seqs):
        new_ote_seq = ot2bio_ote(ote_tag_sequence=ote_tag_seqs[i])
        new_ote_tag_seqs.append(new_ote_seq)
    return new_ote_tag_seqs 
Example 14
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bio_ts_batch(ts_tag_seqs):
    """
    batch version of function ot2bio_ts
    :param ts_tag_seqs:
    :return:
    """
    new_ts_tag_seqs = []
    n_seqs = len(ts_tag_seqs)
    for i in range(n_seqs):
        new_ts_seq = ot2bio_ts(ts_tag_sequence=ts_tag_seqs[i])
        new_ts_tag_seqs.append(new_ts_seq)
    return new_ts_tag_seqs 
Example 15
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bio_batch(ote_tags, ts_tags):
    """
    batch version of function ot2bio
    :param ote_tags: a batch of ote tag sequence
    :param ts_tags: a batch of ts tag sequence
    :return:
    """
    new_ote_tags, new_ts_tags = [], []
    assert len(ote_tags) == len(ts_tags)
    n_seqs = len(ote_tags)
    for i in range(n_seqs):
        ote, ts = ot2bio(ote_tag_sequence=ote_tags[i], ts_tag_sequence=ts_tags[i])
        new_ote_tags.append(ote)
        new_ts_tags.append(ts)
    return new_ote_tags, new_ts_tags 
Example 16
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bieos_ts(ts_tag_sequence):
    """
    ot2bieos function for ts task
    :param ts_tag_sequence: tag sequence for targeted sentiment
    :return:
    """
    n_tags = len(ts_tag_sequence)
    new_ts_sequence = []
    prev_pos = '$$$'
    for i in range(n_tags):
        cur_ts_tag = ts_tag_sequence[i]
        if cur_ts_tag == 'O':
            new_ts_sequence.append('O')
            cur_pos = 'O'
        else:
            cur_pos, cur_sentiment = cur_ts_tag.split('-')
            # cur_pos is T
            if cur_pos != prev_pos:
                # prev_pos is O and new_cur_pos can only be B or S
                if i == n_tags - 1:
                    new_ts_sequence.append('S-%s' % cur_sentiment)
                else:
                    next_ts_tag = ts_tag_sequence[i + 1]
                    if next_ts_tag == 'O':
                        new_ts_sequence.append('S-%s' % cur_sentiment)
                    else:
                        new_ts_sequence.append('B-%s' % cur_sentiment)
            else:
                # prev_pos is T and new_cur_pos can only be I or E
                if i == n_tags - 1:
                    new_ts_sequence.append('E-%s' % cur_sentiment)
                else:
                    next_ts_tag = ts_tag_sequence[i + 1]
                    if next_ts_tag == 'O':
                        new_ts_sequence.append('E-%s' % cur_sentiment)
                    else:
                        new_ts_sequence.append('I-%s' % cur_sentiment)
        prev_pos = cur_pos
    return new_ts_sequence 
Example 17
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bieos_ote_batch(ote_tag_seqs):
    """
    batch version of function ot2bieos_ote
    :param ote_tags:
    :return:
    """
    new_ote_tag_seqs = []
    n_seqs = len(ote_tag_seqs)
    for i in range(n_seqs):
        new_ote_seq = ot2bieos_ote(ote_tag_sequence=ote_tag_seqs[i])
        new_ote_tag_seqs.append(new_ote_seq)
    return new_ote_tag_seqs 
Example 18
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def ot2bieos_ts_batch(ts_tag_seqs):
    """
    batch version of function ot2bieos_ts
    :param ts_tag_seqs:
    :return:
    """
    new_ts_tag_seqs = []
    n_seqs = len(ts_tag_seqs)
    for i in range(n_seqs):
        new_ts_seq = ot2bieos_ts(ts_tag_sequence=ts_tag_seqs[i])
        new_ts_tag_seqs.append(new_ts_seq)
    return new_ts_tag_seqs 
Example 19
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def bio2ot_ote(ote_tag_sequence):
    """
    perform bio-->ot for ote tag sequence
    :param ote_tag_sequence:
    :return:
    """
    new_ote_sequence = []
    n_tags = len(ote_tag_sequence)
    for i in range(n_tags):
        ote_tag = ote_tag_sequence[i]
        if ote_tag == 'B' or ote_tag == 'I':
            new_ote_sequence.append('T')
        else:
            new_ote_sequence.append('I')
    return new_ote_sequence 
Example 20
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def bio2ot_ote_batch(ote_tag_seqs):
    """
    batch version of function bio2ot_ote
    :param ote_tag_seqs: ote tag sequences
    :return:
    """
    new_ote_tag_seqs = []
    n_seqs = len(ote_tag_seqs)
    for i in range(n_seqs):
        new_ote_seq = bio2ot_ote(ote_tag_sequence=ote_tag_seqs[i])
        new_ote_tag_seqs.append(new_ote_seq)
    return new_ote_tag_seqs 
Example 21
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def bio2ot_ts_batch(ts_tag_seqs):
    """
    batch version of function bio2ot_ts
    :param ts_tag_seqs:
    :return:
    """
    new_ts_tag_seqs = []
    n_seqs = len(ts_tag_seqs)
    for i in range(n_seqs):
        new_ts_seq = bio2ot_ts(ts_tag_sequence=ts_tag_seqs[i])
        new_ts_tag_seqs.append(new_ts_seq)
    return new_ts_tag_seqs 
Example 22
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def set_wid(dataset, vocab, win=1):
    """
    set wid field for the dataset
    :param dataset: dataset
    :param vocab: vocabulary
    :param win: context window size, for window-based input, should be an odd number
    :return: dataset with field wid
    """
    n_records = len(dataset)
    for i in range(n_records):
        words = dataset[i]['words']
        lm_labels = []
        # set labels for the auxiliary language modeling task
        for w in words:
            lm_labels.append(vocab[w])
        dataset[i]['lm_labels'] = list(lm_labels)
        n_padded_words = win // 2
        pad_left = ['PADDING' for _ in range(n_padded_words)]
        pad_right = ['PADDING' for _ in range(n_padded_words)]
        padded_words = pad_left + words + pad_right
        # the window-based input
        win_input = list(ngrams(padded_words, win))
        assert len(win_input) == len(words)
        n_grams = []
        for t in win_input:
            n_grams.append(t)
        wids = [[vocab[w] for w in ngram] for ngram in n_grams]
        dataset[i]['wids'] = list(wids)
    return dataset 
Example 23
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def set_padding(dataset, max_len):

    n_records = len(dataset)
    for i in range(n_records):
        sent_len = len(dataset[i]['words'])
        words = dataset[i]['words'] + ['PADDING'] * (max_len-sent_len)
        ote_tags = dataset[i]['ote_raw_tags'] + ['O'] * (max_len-sent_len)
        ts_tags = dataset[i]['ts_raw_tags'] + ['O'] * (max_len-sent_len)

        dataset[i]['words'] = list(words)
        dataset[i]['ote_raw_tags'] = list(ote_tags)
        dataset[i]['ts_raw_tags'] = list(ts_tags)
        dataset[i]['length'] = sent_len

    return dataset 
Example 24
Project: Transferable-E2E-ABSA   Author: hsqmlzno1   File: utils.py    License: MIT License 5 votes vote down vote up
def to_conll(train, val, test, embeddings, vocab, ds_name):
    """

    :param train: training dataset
    :param val: validation / development dataset
    :param test: testing dataset
    :param embeddings: pre-trained word embeddings
    :param vocab: vocabulary
    :return:
    """
    inv_vocab = {}
    for w in vocab:
        wid = vocab[w]
        inv_vocab[wid] = w
    train_lines = semeval2conll(dataset=train)
    dev_lines = semeval2conll(dataset=val)
    test_lines = semeval2conll(dataset=test)
    base_folder = '/projdata9/info_fil/lixin/Research/NCRFpp/sample_data'
    with open('%s/%s_train.txt' % (base_folder, ds_name), 'w+') as fp:
        fp.writelines(train_lines)
    with open('%s/%s_dev.txt' % (base_folder, ds_name), 'w+') as fp:
        fp.writelines(dev_lines)
    with open('%s/%s_test.txt' % (base_folder, ds_name), 'w+') as fp:
        fp.writelines(test_lines)

    emb_lines = []
    for i in range(len(embeddings)):
        word = inv_vocab[i]
        emb_vec = embeddings[i]
        emb_lines.append('%s %s\n' % (word, ' '.join([str(ele) for ele in emb_vec])))
    # write the embeddings back to the NCRFpp foler
    with open('%s/%s_emb.txt' % (base_folder, ds_name), 'w+') as fp:
        fp.writelines(emb_lines) 
Example 25
Project: Speech-Transformer   Author: foamliu   File: sparse_image_warp_np.py    License: MIT License 5 votes vote down vote up
def dense_image_warp(image, flow):
    # batch_size, height, width, channels = (array_ops.shape(image)[0],
    #                                        array_ops.shape(image)[1],
    #                                        array_ops.shape(image)[2],
    #                                        array_ops.shape(image)[3])
    batch_size, height, width, channels = (np.shape(image)[0],
                                           np.shape(image)[1],
                                           np.shape(image)[2],
                                           np.shape(image)[3])

    # The flow is defined on the image grid. Turn the flow into a list of query
    # points in the grid space.
    # grid_x, grid_y = array_ops.meshgrid(
    #     math_ops.range(width), math_ops.range(height))
    # stacked_grid = math_ops.cast(
    #     array_ops.stack([grid_y, grid_x], axis=2), flow.dtype)
    # batched_grid = array_ops.expand_dims(stacked_grid, axis=0)
    # query_points_on_grid = batched_grid - flow
    # query_points_flattened = array_ops.reshape(query_points_on_grid,
    #                                            [batch_size, height * width, 2])
    grid_x, grid_y = np.meshgrid(
        np.range(width), np.range(height))
    stacked_grid = np.cast(
        np.stack([grid_y, grid_x], axis=2), flow.dtype)
    batched_grid = np.expand_dims(stacked_grid, axis=0)
    query_points_on_grid = batched_grid - flow
    query_points_flattened = np.reshape(query_points_on_grid,
                                        [batch_size, height * width, 2])
    # Compute values at the query points, then reshape the result back to the
    # image grid.
    interpolated = interp2d(image, query_points_flattened)
    interpolated = np.reshape(interpolated,
                              [batch_size, height, width, channels])
    return interpolated 
Example 26
Project: decoding-brain-challenge-2016   Author: alexandrebarachant   File: covariance.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def covariances(X, estimator='cov'):
    est = _check_est(estimator)
    Nt, Ne, Ns = X.shape
    covmats = numpy.zeros((Nt, Ne, Ne))
    for i in range(Nt):
        covmats[i, :, :] = est(X[i, :, :])
    return covmats 
Example 27
Project: decoding-brain-challenge-2016   Author: alexandrebarachant   File: covariance.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def covariances_EP(X, P, estimator='cov'):
    est = _check_est(estimator)
    Nt, Ne, Ns = X.shape
    Np, Ns = P.shape
    covmats = numpy.zeros((Nt, Ne + Np, Ne + Np))
    for i in range(Nt):
        covmats[i, :, :] = est(numpy.concatenate((P, X[i, :, :]), axis=0))
    return covmats 
Example 28
Project: decoding-brain-challenge-2016   Author: alexandrebarachant   File: covariance.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def coherence(X, nfft=256, fs=2, noverlap=0):
    """Compute coherence."""
    n_chan = X.shape[0]
    ij = []
    for i in range(n_chan):
        for j in range(i+1, n_chan):
            ij.append((i, j))
    Cxy, Phase, freqs = mlab.cohere_pairs(X, ij, NFFT=nfft, Fs=fs,
                                          noverlap=noverlap)
    coh = numpy.zeros((n_chan, n_chan, len(freqs)))
    for i in range(n_chan):
        coh[i, i] = 1
        for j in range(i+1, n_chan):
            coh[i, j] = coh[j, i] = Cxy[(i, j)]
    return coh 
Example 29
Project: basenji   Author: calico   File: blocks.py    License: Apache License 2.0 4 votes vote down vote up
def xception_block(inputs, filters=None, kernel_size=1,
  dropout=0, pool_size=2, **kwargs):
  """Construct a single convolution block.

  Args:
    inputs:        [batch_size, seq_length, features] input sequence
    filters:       Conv1D filters
    kernel_size:   Conv1D kernel_size
    dropout:       Dropout rate probability
    pool_size:     Pool/stride width

  Returns:
    [batch_size, seq_length, features] output sequence
  """

  # flow through variable current
  current = inputs

  if filters is None:
    filters = inputs.shape[-1]

  # strided convolution
  current_stride = conv_block(current,
    filters=filters,
    kernel_size=pool_size,
    strides=pool_size,
    dropout=0,
    kernel_initializer='ones',
    **kwargs)

  # pooled convolution
  current_pool = current
  for ci in range(2):
    current_pool = conv_block(current_pool,
      filters=filters,
      kernel_size=kernel_size,
      conv_type='separable',
      dropout=dropout,
      **kwargs)

  # should the last conv_block be set to bn_gamma='zeros'?
  # I don't think so since we really need that new information

  # max pool
  current_pool = tf.keras.layers.MaxPool1D(
    pool_size=int(1.5*pool_size),
    strides=pool_size,
    padding='same')(current_pool)

  # residual add
  current = tf.keras.layers.Add()([current_stride,current_pool])

  return current


############################################################
# Towers
############################################################ 
Example 30
Project: basenji   Author: calico   File: blocks.py    License: Apache License 2.0 4 votes vote down vote up
def res_tower(inputs, filters_init, filters_mult=1, dropout=0,
              pool_size=2, repeat=1, num_convs=2, **kwargs):
  """Construct a reducing convolution block.

  Args:
    inputs:        [batch_size, seq_length, features] input sequence
    filters_init:  Initial Conv1D filters
    filters_mult:  Multiplier for Conv1D filters
    dropout:       Dropout on subsequent convolution blocks.
    repeat:        Residual block repetitions
    num_convs:     Conv blocks per residual layer

  Returns:
    [batch_size, seq_length, features] output sequence
  """

  # flow through variable current
  current = inputs

  # initialize filters
  rep_filters = filters_init

  for ri in range(repeat):
    rep_filters_int = int(np.round(rep_filters))

    # initial
    current = conv_block(current,
      filters=rep_filters_int,
      dropout=0,
      bn_gamma='ones',
      **kwargs)
    current0 = current

    # subsequent
    for ci in range(1,num_convs):
      bg = 'ones' if ci < num_convs-1 else 'zeros'
      current = conv_block(current,
                           filters=rep_filters_int,
                           dropout=dropout,
                           bn_gamma=bg,
                           **kwargs)

    # residual add
    current = tf.keras.layers.Add()([current0,current])

    # pool
    if pool_size > 1:
      current = tf.keras.layers.MaxPool1D(
        pool_size=pool_size,
        padding='same')(current)

    # update filters
    rep_filters *= filters_mult

  return current