Skip to content

Instantly share code, notes, and snippets.

@hackintoshrao
Created May 6, 2018 02:13
Show Gist options
  • Save hackintoshrao/23a7532a5866b01207a4ff7e0f357805 to your computer and use it in GitHub Desktop.
Save hackintoshrao/23a7532a5866b01207a4ff7e0f357805 to your computer and use it in GitHub Desktop.
Linear regression cost function and optimizer in tensorflow
def get_cost_optimizer_tensor(Y, model, size, learning_rate):
"""
Tensor for calculating the cost function and the the optimizer for
minimizing the cost.
"""
# Cost function tensor.
cost_function = tf.reduce_sum(tf.pow(model - Y, 2))/(2 * size)
# gradient descent tensor.
gradient_descent = tf.train.GradientDescentOptimizer(learning_rate)
# optimization tensor.
optimizer = gradient_descent.minimize(cost_function)
return optimizer, cost_function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment