Returns the gradient function for cell_fn. Args: cell_fn: The recurrent neural net's cell function. cell_grad: If not None, cell_fn's gradient function. Returns: Returns cell_grad if not None. Otherwise, assume cell_fn is a python function representing the recurrent neural net'
(cell_fn, cell_grad)
| 607 | |
| 608 | |
| 609 | def _GetCellGrad(cell_fn, cell_grad): |
| 610 | """Returns the gradient function for cell_fn. |
| 611 | |
| 612 | Args: |
| 613 | cell_fn: The recurrent neural net's cell function. |
| 614 | cell_grad: If not None, cell_fn's gradient function. |
| 615 | |
| 616 | Returns: |
| 617 | Returns cell_grad if not None. Otherwise, assume cell_fn is a python |
| 618 | function representing the recurrent neural net's cell function, i.e., |
| 619 | cell_fn: (theta, state0, inputs) -> (state1, extra) |
| 620 | returns its default gradient python function, i.e., |
| 621 | cell_grad: (theta, state0, inputs, extras, dstate1) -> ( |
| 622 | dtheta, dstate0, dinputs) |
| 623 | """ |
| 624 | |
| 625 | if cell_grad: |
| 626 | return cell_grad |
| 627 | |
| 628 | def CellGrad(theta, state0, inputs, extras, dstate1): |
| 629 | """Default gradient function for cell_fn.""" |
| 630 | # NOTE: The default grad function recomputes the forward |
| 631 | # function and does not take advantage of 'extras' returned by |
| 632 | # the forward function. |
| 633 | del extras |
| 634 | state1, extras = cell_fn(theta, state0, inputs) |
| 635 | ys = _Flatten([state1]) |
| 636 | xs = _Flatten([theta, state0, inputs]) |
| 637 | grad_ys = _Flatten([dstate1]) |
| 638 | grads = gradients_impl.gradients(ys=ys, xs=xs, grad_ys=grad_ys) |
| 639 | return _ConvertNoneGradientToZeros([theta, state0, inputs], |
| 640 | _Pack(grads, [theta, state0, inputs])) |
| 641 | |
| 642 | return CellGrad |
| 643 | |
| 644 | |
| 645 | def _IsSingleTimeStep(inputs, max_input_length): |