Issue
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.
Solution
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)
Answered By - FancyXun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.