Python中Tensorflow的Angles Comparasion Loss函数

问题描述 投票:1回答:1

我有一个CNN,接收一个图像,出一个单一的值 - 一个角度。数据集由(x =图像,y =角度)对组成。

我想要每个图像的网络,以预测角度。

我发现了这个建议:https://stats.stackexchange.com/a/218547但我似乎无法理解如何将其转换为Python代码中的Tensorflow。

x_CNN = tf.placeholder(tf.float32, (None, 14, 14, 3))
y_CNN = tf.placeholder(tf.int32, (None))
keep_prob_CNN = tf.placeholder(tf.float32)
one_hot_y_CNN = tf.one_hot(y_CNN, 1)
def MyCNN(x):
    # Network's architecture: In: Image, Out: Angle.
logits_CNN = MyCNN(x)

# Loss Function attempt <------------------------------
outs = tf.tanh(logits_CNN)
outc = tf.tanh(logits_CNN)
loss_operation_CNN = tf.reduce_mean(0.5 * (tf.square(tf.sin(one_hot_y_CNN) - outs) + tf.square(tf.cos(one_hot_y_CNN) - outc)))

learning_rate_placeholder_CNN = tf.placeholder(tf.float32, shape=[])
optimizer_CNN = tf.train.AdamOptimizer(learning_rate = learning_rate_placeholder_CNN)
training_operation_CNN = optimizer_CNN.minimize(loss_operation_CNN)
correct_prediction_CNN = tf.equal(tf.argmax(logits_CNN, 1), tf.argmax(one_hot_y_CNN, 1))
accuracy_operation_CNN = tf.reduce_mean(tf.cast(correct_prediction_CNN, tf.float32))

# And a working Training and testing code...
python tensorflow conv-neural-network angle loss-function
1个回答
1
投票

这是正确的方向,但是这个想法是,不是让MyCNN为每个例子产生单个角度值,而是产生两个值。因此,如果MyCNN的返回值目前具有像(None,)(None, 1)这样的形状,则应将其更改为(None, 2) - 也就是说,最后一层应该还有一个输出。如果您对如何做到这一点有疑问,请提供有关MyCNN身体的更多详细信息。

然后你会有:

outs = tf.tanh(logits_CNN[:, 0])
outc = tf.tanh(logits_CNN[:, 1])
out_radians = tf.atan2(outs, outc)  # This is the final angle output in radians

关于损失,我不确定我理解你的Y输入。如果你试图预测一个角度,它不应该是一个浮点值,而不是一个整数?在这种情况下,您将拥有:

# Example angle in radians
y_CNN = tf.placeholder(tf.float32, (None,))

# ... 

loss_operation_CNN = tf.reduce_mean(0.5 * (tf.square(tf.sin(y_CNN) - outs) +
                                           tf.square(tf.cos(y_CNN) - outc)))
© www.soinside.com 2019 - 2024. All rights reserved.