1

I have been trying to get this to work for a while but I don't know how. Ive tried a dozen combinations.

What I'm trying to do is fetch the input array, compare each element to see if its positive or negative. Apply a unique transformation depending on positivity or negativity. Take the mean and return the result. This is what I have but it doesn't work.

edit: my latest attempt

@tf.function
def transform(x, y):
    et = tf.math.subtract(x, y)
    et_variable = tf.Variable(et)

    max_loop = tf.shape(x)[0]

    loss = tf.TensorArray(tf.float32, size=et.shape[0], clear_after_read=False)
    loss = loss.unstack(et_variable)

    i = tf.constant(0)
    while tf.math.less(i, max_loop):
        if loss.gather([i])[0] > 0:
            val = 3 * loss.gather([i])[0]
        else:
            val = 2 * loss.gather([i])[0]
        loss = loss.scatter([i], value=[val])
        i = tf.Variable(i)
        i = i + 1
        i = tf.constant(i)
    return(K.mean(loss.stack()))

I get an error:

ValueError: condition of if statement expected to be `tf.bool` scalar, got Tensor("loss_funk/while/Greater:0", shape=(11,), dtype=bool); to check for None, use `is not None`

I've tried using tf.Variable and tf.tensorarray but neither work with tf.function.

1 Answer 1

2

Below is an example to meet your expectations. Tensor does not have gather_ Nd, it is tensorflow's API. My tensorflow version is 2.3

import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K

y_true = np.random.normal(size=(30, 50)).astype('float32')
x = np.random.normal(size=(30, )).astype('float32')
y = np.random.normal(size=(30, )).astype('float32')


@tf.function
def transform(x, y):
    et = tf.math.subtract(x, y)
    max_loop = tf.shape(y_true)[0]
    res = tf.TensorArray(tf.float32, size=et.shape[0], clear_after_read=False)
    i = tf.constant(0)
    while tf.math.less(i, max_loop):
        if tf.gather_nd(et, [i]) > 0:
            val = 0.03 * tf.gather_nd(et, [i])
        else:
            val = 0.05 * tf.gather_nd(et, [i])
        res = res.write(i, val)
        i = i + 1
    return K.mean(res.stack())


transform(x, y)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but it didn't work. I get an error on the line: if tf.gather_nd(et, [i]) > 0: ValueError: condition of if statement expected to be tf.bool scalar, got Tensor("transform/while/Greater:0", shape=(11,), dtype=bool);
what's your tensorflow version? this work in tf 2.3
ok it works on a headless linux server but not my windows machine?
It seems to be independent of platform. I guess your x is 2-d tensor?

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.