I have a CNN, takes in an image, outs a single value - an angle. The data set is made of (x = image, y = angle) couples.
I want the network for each image, to predict an angle.
I have found this suggestion: https://stats.stackexchange.com/a/218547 But I can't seem to understand how to translate it into a working Tensorflow in Python code.
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...
That is going in the right direction, but the idea is that, instead of having MyCNN
produce a single angle value for each example, produce two values. So if the return value of MyCNN
is currently something with shape like (None,)
or (None, 1)
, you should change it to (None, 2)
- that is, the last layer should have one more output. If you have doubts about how to do this please provide more details about the body of MyCNN
.
Then you would just have:
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
About the loss, I am not sure I understand your Y input. If you are trying to predict an angle, shouldn't it be a float value, and not an integer? In that case you would have:
# 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)))