2016-04-27 5 views
3

model_fn()の中にランダム変数を作成する必要があります。形状は[batch_size, 20]です。skflowのmodel_fnでbatch_sizeを使用する

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]を交換する場合myclf.setup_training()を実行しているとき、私は

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

を取得します。

私は

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とValueError: Cannot evaluate tensor using eval(): No default session is registered. Use()or pass an explicit session to eval(session=sess)を取得した場合は(当然のことながら、私はfeed_dictを提供していないので)

にはどうすればmodel_fn()batch_sizeの値にアクセスすることができ、予測の間にそれを変更することができますか?

+0

ではなくbatch_size.evalのどれを(使用していない試してみてください) – Aaron

+0

@Aaron私が試したが、それは 'TypeError例外私を与える:予想されるバイナリまたはユニコード文字列、取得済みなし –

答えて

2

私はTensor.get_shape()tf.shape(Tensor)の違いに気づいていませんでした。後者の作品:

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

Tensorflow 0.8 FAQにmentionned通り:

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.

関連する問題