gpt4 book ai didi

python - 在 skflow 的 model_fn 中使用 batch_size

转载 作者:行者123 更新时间:2023-11-28 17:28:11 25 4
gpt4 key购买 nike

我需要在我的 model_fn() 中创建一个随机变量,其形状为 [batch_size, 20]

我不想将 batch_size 作为参数传递,因为这样我就无法使用不同的批量大小进行预测。

去掉与这个问题无关的部分,我的model_fn()是:

def model(inp, out):
eps = tf.random_normal([batch_size, 20], 0, 1, name="eps"))) # batch_size is the
# value I do not want to hardcode

# dummy example
predictions = tf.add(inp, eps)
return predictions, 1

如果我用 inp.get_shape() 替换 [batch_size, 20],我得到

ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 20)

运行 myclf.setup_training() 时。

如果我尝试

def model(inp, out):
batch_size = tf.placeholder("float", [])
eps = tf.random_normal([batch_size.eval(), 20], 0, 1, name="eps")))

# dummy example
predictions = tf.add(inp, eps)
return predictions, 1

我得到 ValueError: Cannot evaluate tensor using eval(): No default session is registered.使用与 sess.as_default()或将显式 session 传递给 eval(session=sess)(可以理解,因为我没有提供 feed_dict)

如何在 model_fn() 中访问 batch_size 的值,同时仍然能够在预测期间更改它?

最佳答案

我不知道 Tensor.get_shape()tf.shape(Tensor) 之间的区别。后者有效:

eps = tf.random_normal(tf.shape(inp), 0, 1, name="eps")))

如 Tensorflow 0.8 常见问题解答中所述:

How do I build a graph that works with variable batch sizes?

It is often useful to build a graph that works with variable batch sizes, for example so that the same code can be used for (mini-)batch training, and single-instance inference. The resulting graph can be saved as a protocol buffer and imported into another program.

When building a variable-size graph, the most important thing to remember is not to encode the batch size as a Python constant, but instead to use a symbolic Tensor to represent it. The following tips may be useful:

Use batch_size = tf.shape(input)[0] to extract the batch dimension from a Tensor called input, and store it in a Tensor called batch_size.

Use tf.reduce_mean() instead of tf.reduce_sum(...) / batch_size.

If you use placeholders for feeding input, you can specify a variable batch dimension by creating the placeholder with tf.placeholder(..., shape=[None, ...]). The None element of the shape corresponds to a variable-sized dimension.

关于python - 在 skflow 的 model_fn 中使用 batch_size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com