0

I am calculating some features from a set of data. I made as function for each calculation. I want to put all of those values in an array in another function and I want to call the function. how to do it ?

The 3 functions that I have are

def peak_value(x):
    return tf.reduce_max(tf.abs(x))
def rootms(x):
    return tf.sqrt(tf.reduce_mean(tf.square(x)))
def meanofabs(x):
    return tf.reduce_mean(tf.abs(x))

I want these values to be assigned to a array inside function

def pooldata(x,size):
    pool = tf.zeros([1,size],tf.float32)
    # i want to 
    # assign pool[0] with peak_value(x)
    # assign pool[1] with rootms(x)
    # assign pool[2] with meanofabs(x)
    return pool

then I want to call the function

# define x, size
model = tf.intialize_all_variables()
sess = tf.Session()
sess.run(model)
print(sess.run(pooldata)) # print all the three values

How can i do it ?

I tried

tf.assign(pool[0],peak_value(x)) 

but it gives me error

TypeError: Input 'ref' of 'Assign' Op requires l-value input

1 Answer 1

4

tf.assign only lets you assign a value to the entire variable, and not to a slice of a variable. You would have to use tf.scatter_update to assign to a slice of a variable. However, in your case, you can simply create the pool tensor by concatenating the three values. Here is the complete working program :

import tensorflow as tf

def peak_value(x):
    return tf.reduce_max(tf.abs(x))
def rootms(x):
    return tf.sqrt(tf.reduce_mean(tf.square(x)))
def meanofabs(x):
    return tf.reduce_mean(tf.abs(x))


def pooldata(x,size):
    pool = tf.concat_v2([tf.expand_dims(peak_value(x), 0),
                         tf.expand_dims(rootms(x), 0),
                         tf.expand_dims(meanofabs(x), 0)], axis=0)
    # i want to
    # assign pool[0] with peak_value(x)
    # assign pool[1] with rootms(x)
    # assign pool[2] with meanofabs(x)
    return pool

sess = tf.Session()
size = 1
x = [1.0]

print(sess.run(pooldata(x, size)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.