英文:
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='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)))
Until here it is ok, I receive :
data
<MapDataset shapes: (<unknown>, <unknown>, <unknown>), types: (tf.float32, tf.float32, tf.float32)>
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='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]))
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论