GithubHelp home page GithubHelp logo

tf_utils's Introduction

tf_utils

Some additional utils which can be used to easily manipulate tensorflow's tensors.

Introduction

tfs.ops.get_top_k() is used to get the top k elements and corresponding positions in a tensor with any shape;
tfs.ops.assign() is used to set some values to a tensor at designated positions;

tfs.ops.get_top_k(input, k, sorted=True)

"""This function would compare the values of input tensor(any shape) in the lowest
dimension(axis=-1),and choose top k values and corresponding positions
Args:
    input:a tensor with any shape
    k:the number of top_k values
    sorted: if true the resulting k elements will be sorted by the values in descending order.
Return:
    the top k value and corresponding position.

Example:
    value = tf.random_normal(shape=[30,30,30,3], dtype=tf.float32) ##random produce some data
    v,p = get_top_k(value,2)  ##return the top 2 values and postions

    with tf.Session() as sess:
        v,p = sess.run([v,p]) ##get the top 2 values and postion
"""

tfs.ops.assign(input, position, value)

"""This function would assign a value to the input in the specific position

Args:
    input: A tensor with any shape.
    position: Specify the postions of input where you wanna assign the value.
    value: the value you wanna assign in. Must be 1-D array(or tensor,list)
    ---The length of value must be 1 or the same with the position's.
    ---Dtype of value must be the same with input's.

Return:
    the tensor after modify.

Example:
    ## bulid the tf graph ##
    # random produce some data #
    raw_tensor = tf.constant(np.random.normal(size=[10,2, 10, 3]).astype(np.float32))
    # get the top 500 values and corresponding positions #
    top_k_tensor, position = get_top_k(raw_tensor, k=500)
    #replace the raw_tensor with the new value 1. at the designated position.
    new_tensor = assign(input=raw_tensor, position=position, value=[1.])

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        new_value = sess.run(new_tensor)
"""

tf_utils's People

Contributors

youngyoung619 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.