Apply tf.ensure_shape for multiple outputs.

huangapple go评论67阅读模式
英文:

Apply tf.ensure_shape for multiple outputs

问题

以下是您要翻译的代码部分:

我有这段代码

import tensorflow as tf
import numpy as np

def scale(X, a=-1, b=1, dtype='float32'):
    if a > b:
        a, b = b, a
    xmin = tf.cast(tf.math.reduce_min(X), dtype=dtype)
    xmax = tf.cast(tf.math.reduce_max(X), dtype=dtype)
    X = (X - xmin) / (xmax - xmin)
    scaled = X * (b - a) + a
    return scaled, xmin, xmax

a = np.ones((10, 20, 20, 2))

dataset = tf.data.Dataset.from_tensor_slices(a)

data = dataset.map(lambda x: tf.py_function(scale,
                                            [x], 
                                            (tf.float32, tf.float32, tf.float32)))

到此为止一切正常我收到

data

<MapDataset shapes: (<unknown>, <unknown>, <unknown>), types: (tf.float32, tf.float32, tf.float32)>

现在我必须使用tf.ensure_shape来创建形状

例如如果`scale`函数只返回一个值,`scale`,那么我会这样做

data = data.map(lambda x: tf.ensure_shape(x, [10, 20, 20, 2]))

现在当我有3个输出值时该怎么办

因此我希望能够使用`scale`函数的结果这就是我在做所有这些的原因如果有其他方法我不知道

缩放值xmin和xmax
英文:

I have this code:

import tensorflow as tf
import numpy as np

def scale(X, a=-1, b=1, dtype=&#39;float32&#39;):
    if a &gt; b:
        a, b = b, a
    xmin = tf.cast(tf.math.reduce_min(X), dtype=dtype)
    xmax = tf.cast(tf.math.reduce_max(X), dtype=dtype)
    X = (X - xmin) / (xmax - xmin)
    scaled = X * (b - a) + a
    return scaled, xmin, xmax


a = np.ones((10, 20, 20, 2))

dataset = tf.data.Dataset.from_tensor_slices(a)


data = dataset.map(lambda x: tf.py_function(scale,
                                            [x], 
                                            (tf.float32, tf.float32, tf.float32)))

Until here it is ok, I receive :

data

&lt;MapDataset shapes: (&lt;unknown&gt;, &lt;unknown&gt;, &lt;unknown&gt;), types: (tf.float32, tf.float32, tf.float32)&gt;

Now, I have to use tf.ensure_shape, to create the shapes.

If for example the scale function returned only one value, scale, then I would do:

data = data.map(lambda x: tf.ensure_shape(x, [10, 20, 20, 2]))

Now that I have 3 output values?

So, I want to be able to use the result of the scale function that's why I am doing all these. If there is another way, I don't know.

scaled values, xmin and xmax

答案1

得分: 1

If it is just about transforming unknown shape to known shape, I think you can use tf.reshape method.

def scale(X, a=-1, b=1, dtype='float32'):
    if a > b:
        a, b = b, a
    xmin = tf.cast(tf.math.reduce_min(X), dtype=dtype)
    xmax = tf.cast(tf.math.reduce_max(X), dtype=dtype)
    X = (X - xmin) / (xmax - xmin)
    scaled = X * (b - a) + a
    return scaled, xmin, xmax

a = tf.random.uniform(shape=[10, 20, 20, 2], minval=1, maxval=5)
dataset = tf.data.Dataset.from_tensor_slices(a)
dataset = dataset.map(
    lambda x: tf.py_function(
        scale,
        [x], 
        (tf.float32, tf.float32, tf.float32))
)

def set_shape(x, y, z):
    x = tf.reshape(x, [-1, 20, 20, 2])
    y = tf.reshape(y, [1])
    z = tf.reshape(z, [1])
    return x, y, z

dataset = dataset.map(set_shape)
a, b, c = next(iter(data))
a.shape, b.shape, c.shape
(TensorShape([1, 20, 20, 2]), TensorShape([1]), TensorShape([1]))
英文:

If it is just about transforming uknown shape to known shape, I think you can use tf.reshape method.

def scale(X, a=-1, b=1, dtype=&#39;float32&#39;):
    if a &gt; b:
        a, b = b, a
    xmin = tf.cast(tf.math.reduce_min(X), dtype=dtype)
    xmax = tf.cast(tf.math.reduce_max(X), dtype=dtype)
    X = (X - xmin) / (xmax - xmin)
    scaled = X * (b - a) + a
    return scaled, xmin, xmax

a = tf.random.uniform(shape=[10, 20, 20, 2], minval=1, maxval=5)
dataset = tf.data.Dataset.from_tensor_slices(a)
dataset = dataset.map(
    lambda x: tf.py_function(
        scale,
        [x], 
        (tf.float32, tf.float32, tf.float32))
)

def set_shape(x, y, z):
    x = tf.reshape(x, [-1, 20, 20, 2])
    y = tf.reshape(y, [1])
    z = tf.reshape(z, [1])
    return x, y, z

dataset = dataset.map(set_shape)
a, b, c = next(iter(data))
a.shape, b.shape, c.shape
(TensorShape([1, 20, 20, 2]), TensorShape([1]), TensorShape([1]))

</details>



huangapple
  • 本文由 发表于 2023年5月17日 22:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定