2017-04-02 13 views
-1

を持っていないすべてのない場合は、次のように損失関数を定義するプログラムがあります:プログラムを実行とValueErrorに関する: `inputs`は同じ形状とDTYPEや形状

def loss(hypes, decoded_logits, labels): 
"""Calculate the loss from the logits and the labels. 

Args: 
    logits: Logits tensor, float - [batch_size, NUM_CLASSES]. 
    labels: Labels tensor, int32 - [batch_size]. 

Returns: 
    loss: Loss tensor of type float. 
""" 
logits = decoded_logits['logits'] 
with tf.name_scope('loss'): 
    logits = tf.reshape(logits, (-1, 2)) 
    shape = [logits.get_shape()[0], 2] 
    epsilon = tf.constant(value=hypes['solver']['epsilon']) 
    # logits = logits + epsilon 
    labels = tf.to_float(tf.reshape(labels, (-1, 2))) 

    softmax = tf.nn.softmax(logits) + epsilon 

    if hypes['loss'] == 'xentropy': 
     cross_entropy_mean = _compute_cross_entropy_mean(hypes, labels, 
                 softmax) 
    elif hypes['loss'] == 'softF1': 
     cross_entropy_mean = _compute_f1(hypes, labels, softmax, epsilon) 

    elif hypes['loss'] == 'softIU': 
     cross_entropy_mean = _compute_soft_ui(hypes, labels, softmax, 
               epsilon) 



    reg_loss_col = tf.GraphKeys.REGULARIZATION_LOSSES 

    print('******'*10) 
    print('loss type ',hypes['loss']) 
    print('type ', type(tf.get_collection(reg_loss_col))) 
    print("Regression loss collection: {}".format(tf.get_collection(reg_loss_col))) 
    print('******'*10) 


    weight_loss = tf.add_n(tf.get_collection(reg_loss_col)) 

    total_loss = cross_entropy_mean + weight_loss 

    losses = {} 
    losses['total_loss'] = total_loss 
    losses['xentropy'] = cross_entropy_mean 
    losses['weight_loss'] = weight_loss 

return losses 

は、次のエラーメッセージを発生させます

File "/home/ decoder/kitti_multiloss.py", line 86, in loss 
    name='reg_loss') 
    File "/devl /tensorflow/tf_0.12/lib/python3.4/site-packages/tensorflow/python/ops/math_ops.py", line 1827, in add_n 
    raise ValueError("inputs must be a list of at least one Tensor with the " 
ValueError: inputs must be a list of at least one Tensor with the same dtype and shape 

tf.add_nの機能を確認しました。その実装は次のとおりです。私の質問は、最初のパラメータtf.get_collection(reg_loss_col)tf.add_nにチェックインし、その情報を表示してエラーメッセージが生成された理由を理解する方法です。

def add_n(inputs, name=None): 
    """Adds all input tensors element-wise. 
    Args: 
    inputs: A list of `Tensor` objects, each with same shape and type. 
    name: A name for the operation (optional). 
    Returns: 
    A `Tensor` of same shape and type as the elements of `inputs`. 
    Raises: 
    ValueError: If `inputs` don't all have same shape and dtype or the shape 
    cannot be inferred. 
    """ 
    if not inputs or not isinstance(inputs, (list, tuple)): 
    raise ValueError("inputs must be a list of at least one Tensor with the " 
        "same dtype and shape") 
    inputs = ops.convert_n_to_tensor_or_indexed_slices(inputs) 
    if not all(isinstance(x, ops.Tensor) for x in inputs): 
    raise ValueError("inputs must be a list of at least one Tensor with the " 
        "same dtype and shape") 

答えて

0

なぜあなたもtf.get_collection(reg_loss_col)が何であるかを見るためにadd_nに取得する必要がありますか? tmp = tf.get_collection(reg_loss_col)を入力して、タイプを確認できます。ところで、それはあなたのグラフに正規化された損失がないように見えます。その場合、tf.get_collection(reg_loss_col)は空のリストを返します。

Pythonでのオブジェクトの種類を確認するには、組み込み関数typeを使用できます。たとえば、タイプtmpを表示するには:print type(tmp)

+0

こんにちはアリ、返信いただきありがとうございます。どの機能を使ってtmp = tf.get_collection(reg_loss_col)のタイプを見ることができますか?さらに、元のプログラムでは、reg_loss_col = tf.GraphKeys.REGULARIZATION_LOSSESが正規化された損失を表していますか? – user785099

+0

オブジェクトのshow hoe check typeに対する回答を更新しました。 'tf.GraphKeys.REGULARIZATION_LOSSES'は文字列であり、名前であり、' tf.get_collection() 'を呼び出すことで、その名前のグラフノードを要求します。あなたはグラフの損失を定義する必要があります。 – Ali

+0

http://stackoverflow.com/questions/37107223/how-to-add-regularizations-in-tensorflowは、 'tf.GraphKeys.REGULARIZATION_LOSSES'が何であるかを理解するのに役立ちます。 – Ali

関連する問題