2017-09-09 3 views
1

Ich habe einfaches Modell wie folgt aus:Wie wird der Verlust im Tensorflow berechnet?

n_input = 14 
n_out = 1 

weights = { 
    'out': tf.Variable(tf.random_normal([n_input, n_out])) 
} 
biases = { 
    'out': tf.Variable(tf.random_normal([n_out])) 
} 
def perceptron(input_tensor, weights, biases): 
    out_layer_multiplication = tf.matmul(input_tensor, weights['out']) 
    out_layer_addition = out_layer_multiplication + biases['out'] 
    return out_layer_addition 

input_tensor = rows 
model = perceptron 

"Reihen" Dimension ist (N, 14) und "out" Dimension ist (N), wobei "out" ist Ergebnis Modell des Laufens mit "Zeilen" als "input_tensor".

Und ich möchte Verlust im Tensorflow berechnen. Algorythm der Berechnung ist:

ls = 0 
for i in range(len(out)-1): 
    if out[i] < out[i+1]: 
     ls += 1 

Wo "ls" ist Modellverlust. Wie kann ich es in Tensorflow-Notation berechnen?

Antwort

1

Sie können etwas tun:

l = out.get_shape()[0] 
a = out[0:l-1] 
b = out[1:l] 
c = tf.where(a<b, tf.ones_like(a), tf.zeros_like(a)) 
return tf.reduce_sum(c) 

In der Praxis a enthält out[i] und b enthält out[i+1]. c hat 1s jedes Mal out[i]<out[i+1]. Also summiert man sie jedes Mal um +1.

Verwandte Themen