在 skflow 中使用 batch_size in model_fn
Use batch_size in model_fn in skflow
我需要在 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
我用 sess.as_default()or pass an explicit session to eval(session=sess)
得到 ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use
(可以理解,因为我没有提供 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.
我需要在 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
我用 sess.as_default()or pass an explicit session to eval(session=sess)
得到 ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use
(可以理解,因为我没有提供 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.