"""Various high level TF models."""
#  Copyright 2015-present The Scikit Flow Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.contrib.learn.python.learn.ops import dnn_ops
from tensorflow.contrib.learn.python.learn.ops import losses_ops
from tensorflow.contrib.learn.python.learn.ops import autoencoder_ops
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops as array_ops_
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import logging_ops
from tensorflow.python.ops import nn
from tensorflow.python.ops import variable_scope as vs


def linear_regression_zero_init(X, y):
  """Creates a linear regression TensorFlow subgraph, in which weights and
    bias terms are initialized to exactly zero.

  Args:
    X: tensor or placeholder for input features.
    y: tensor or placeholder for target.

  Returns:
    Predictions and loss tensors.
  """
  return linear_regression(X, y, init_mean=0.0, init_stddev=0.0)


def logistic_regression_zero_init(X, y):
  """Creates a logistic regression TensorFlow subgraph, in which weights and
       bias terms are initialized to exactly zero.

  Args:
    X: tensor or placeholder for input features.
    y: tensor or placeholder for target.

  Returns:
    Predictions and loss tensors.
  """
  return logistic_regression(X, y, init_mean=0.0, init_stddev=0.0)


def linear_regression(X, y, init_mean=None, init_stddev=1.0):
  """Creates linear regression TensorFlow subgraph.

  Args:
    X: tensor or placeholder for input features.
    y: tensor or placeholder for target.
    init_mean: the mean value to use for initialization.
    init_stddev: the standard devation to use for initialization.

  Returns:
    Predictions and loss tensors.

  Side effects:
    The variables linear_regression.weights and linear_regression.bias are
    initialized as follows.  If init_mean is not None, then initialization
    will be done using a random normal initializer with the given init_mean
    and init_stddv.  (These may be set to 0.0 each if a zero initialization
    is desirable for convex use cases.)  If init_mean is None, then the
    uniform_unit_scaling_initialzer will be used.
  """
  with vs.variable_scope('linear_regression'):
    logging_ops.histogram_summary('linear_regression.X', X)
    logging_ops.histogram_summary('linear_regression.y', y)
    y_shape = y.get_shape()
    if len(y_shape) == 1:
      output_shape = 1
    else:
      output_shape = y_shape[1]
    # Set up the requested initialization.
    if (init_mean is None):
      weights = vs.get_variable('weights', [X.get_shape()[1], output_shape])
      bias = vs.get_variable('bias', [output_shape])
    else:
      weights = vs.get_variable('weights', [X.get_shape()[1], output_shape],
                                initializer=init_ops.random_normal_initializer(
                                    init_mean, init_stddev))