<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/glm/family.py#L55-L240) |

Specifies a mean-value parameterized exponential family.

    tfp.glm.ExponentialFamily(
        name=None
    )

Subclasses implement [exponential-family distribution](https://en.wikipedia.org/wiki/Exponential_family) properties (e.g.,
`log_prob`, `variance`) as a function of a real-value which is transformed via
some [link function](https://en.wikipedia.org/wiki/Generalized_linear_model#Link_function)
to be interpreted as the distribution's mean. The distribution is
parameterized by this mean, i.e., "mean-value parameterized."

Subclasses are typically used to specify a Generalized Linear Model (GLM). A
[GLM](https://en.wikipedia.org/wiki/Generalized_linear_model) is a
generalization of linear regression which enables efficient fitting of
log-likelihood losses beyond just assuming `Normal` noise. See [`tfp.glm.fit`](https://www.tensorflow.org/probability/api_docs/python/tfp/glm/fit)
for more details.

Subclasses must implement `_as_distribution` which does not need to be either
"tape-safe" or "variable-safe." ([`tfp.glm`](https://www.tensorflow.org/probability/api_docs/python/tfp/glm) families are however guaranteed to
be both tape and variable safe.)

Subclasses may optionally implement `_call` and `_log_prob` which otherwise
default to:

    def _call(self, predicted_linear_response):
      with tf.GradientTape(watch_accessed_variables=False) as tape:
        tape.watch(predicted_linear_response)
        likelihood = self.as_distribution(predicted_linear_response)
        mean = likelihood.mean()
      variance = likelihood.variance()
      grad_mean = tape.gradient(mean, predicted_linear_response)
      return mean, variance, grad_mean

    def _log_prob(self, response, predicted_linear_response):
      likelihood = self.as_distribution(predicted_linear_response)
      return likelihood.log_prob(response)

In context of [`tfp.glm.fit`](https://www.tensorflow.org/probability/api_docs/python/tfp/glm/fit) and [`tfp.glm.fit_sparse`](https://www.tensorflow.org/probability/api_docs/python/tfp/glm/fit_sparse), these functions are used
to find the best fitting weights for given model matrix ("X") and responses
("Y").

<br />

<br />

<br />

| ## Args ||
|---|---|
| `name` | Python `str` used as TF namescope for ops created by member functions. Default value: `None` (i.e., the subclass name). |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `name` | Returns the name of this module as passed or determined in the ctor. <br /> > [!NOTE] > **Note:** This is not the same as the `self.name_scope.name` which includes parent module names. |
| `name_scope` | Returns a [`tf.name_scope`](https://www.tensorflow.org/api_docs/python/tf/name_scope) instance for this class. |
| `non_trainable_variables` | Sequence of non-trainable variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `submodules` | Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on). a = tf.Module() b = tf.Module() c = tf.Module() a.b = b b.c = c list(a.submodules) == [b, c] True list(b.submodules) == [c] True list(c.submodules) == [] True <br /> |
| `trainable_variables` | Sequence of trainable variables owned by this module and its submodules. <br /> > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `variables` | Sequence of variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |

## Methods

### `as_distribution`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/glm/family.py#L196-L223)

    as_distribution(
        predicted_linear_response, name=None
    )

Builds a mean parameterized TFP Distribution from linear response.

#### Example:

    model = tfp.glm.Bernoulli()
    r = tfp.glm.compute_predicted_linear_response(x, w)
    yhat = model.as_distribution(r)

<br />

<br />

<br />

| Args ||
|---|---|
| `predicted_linear_response` | `response`-shaped `Tensor` representing linear predictions based on new `model_coefficients`, i.e., `tfp.glm.compute_predicted_linear_response( model_matrix, model_coefficients, offset)`. |
| `name` | Python `str` used as TF namescope for ops created by member functions. Default value: `None` (i.e., 'log_prob'). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `model` | [`tfp.distributions.Distribution`](https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Distribution)-like object with mean parameterized by `predicted_linear_response`. |

<br />

### `log_prob`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/glm/family.py#L166-L190)

    log_prob(
        response, predicted_linear_response, name=None
    )

Computes `D(param=mean(r)).log_prob(response)` for linear response, `r`.

<br />

<br />

<br />

| Args ||
|---|---|
| `response` | `float`-like `Tensor` representing observed ("actual") responses. |
| `predicted_linear_response` | `float`-like `Tensor` corresponding to [`tf.linalg.matmul(model_matrix, weights)`](https://www.tensorflow.org/api_docs/python/tf/linalg/matmul). |
| `name` | Python `str` used as TF namescope for ops created by member functions. Default value: `None` (i.e., 'log_prob'). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `log_prob` | `Tensor` with shape and dtype of `predicted_linear_response` representing the distribution prescribed log-probability of the observed `response`s. |

<br />

### `with_name_scope`

    @classmethod
    with_name_scope(
        method
    )

Decorator to automatically enter the module name scope.

    class MyModule(tf.Module):
      @tf.Module.with_name_scope
      def __call__(self, x):
        if not hasattr(self, 'w'):
          self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
        return tf.matmul(x, self.w)

Using the above module would produce [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable)s and [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s whose
names included the module name:

    mod = MyModule()
    mod(tf.ones([1, 2]))
    <tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
    mod.w
    <tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
    numpy=..., dtype=float32)>

<br />

<br />

<br />

| Args ||
|---|---|
| `method` | The method to wrap. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The original method wrapped such that it enters the module's name scope. ||

<br />

### `__call__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/glm/family.py#L121-L159)

    __call__(
        predicted_linear_response, name=None
    )

Computes `mean(r), var(mean), d/dr mean(r)` for linear response, `r`.

Here `mean` and `var` are the mean and variance of the sufficient statistic,
which may not be the same as the mean and variance of the random variable
itself. If the distribution's density has the form

    p_Y(y) = h(y) Exp[dot(theta, T(y)) - A]

where `theta` and `A` are constants and `h` and `T` are known functions,
then `mean` and `var` are the mean and variance of `T(Y)`. In practice,
often `T(Y) := Y` and in that case the distinction doesn't matter.

<br />

<br />

<br />

| Args ||
|---|---|
| `predicted_linear_response` | `float`-like `Tensor` corresponding to [`tf.linalg.matmul(model_matrix, weights)`](https://www.tensorflow.org/api_docs/python/tf/linalg/matmul). |
| `name` | Python `str` used as TF namescope for ops created by member functions. Default value: `None` (i.e., 'call'). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `mean` | `Tensor` with shape and dtype of `predicted_linear_response` representing the distribution prescribed mean, given the prescribed linear-response to mean mapping. |
| `variance` | `Tensor` with shape and dtype of `predicted_linear_response` representing the distribution prescribed variance, given the prescribed linear-response to mean mapping. |
| `grad_mean` | `Tensor` with shape and dtype of `predicted_linear_response` representing the gradient of the mean with respect to the linear-response and given the prescribed linear-response to mean mapping. |

<br />