MCPcopy Create free account
hub / github.com/apache/singa / forward

Method forward

python/singa/layer.py:1618–1655  ·  view source on GitHub ↗
(self, x, hx=None, cx=None, seq_lengths=None)

Source from the content-addressed store, hash-verified

1616 self.W.uniform(-math.sqrt(k), math.sqrt(k))
1617
1618 def forward(self, x, hx=None, cx=None, seq_lengths=None):
1619
1620 self.device_check(x, self.W)
1621 if self.batch_first: # (bs,seq,data) -> (seq,bs,data)
1622 x = autograd.transpose(x, (1, 0, 2))
1623
1624 batch_size = x.shape[1]
1625 directions = 2 if self.bidirectional else 1
1626 if hx == None:
1627 hx = Tensor(shape=(self.num_layers * directions, batch_size,
1628 self.hidden_size),
1629 requires_grad=False,
1630 stores_grad=False,
1631 device=x.device).set_value(0.0)
1632 if cx == None:
1633 cx = Tensor(shape=(self.num_layers * directions, batch_size,
1634 self.hidden_size),
1635 requires_grad=False,
1636 stores_grad=False,
1637 device=x.device).set_value(0.0)
1638
1639 # outputs returned is list
1640 # inputs has shape of {sequence length, batch size, feature size}
1641 if self.use_mask:
1642 assert type(seq_lengths) == Tensor, "wrong type for seq_lengths"
1643 y = autograd._RNN(self.handle,
1644 return_sequences=self.return_sequences,
1645 use_mask=self.use_mask,
1646 seq_lengths=seq_lengths)(x, hx, cx, self.W)[0]
1647 else:
1648 y = autograd._RNN(
1649 self.handle,
1650 return_sequences=self.return_sequences,
1651 )(x, hx, cx, self.W)[0]
1652 if self.return_sequences and self.batch_first:
1653 # (seq, bs, hid) -> (bs, seq, hid)
1654 y = autograd.transpose(y, (1, 0, 2))
1655 return y
1656
1657 def get_params(self):
1658 return {self.W.name: self.W}

Callers

nothing calls this directly

Calls 5

typeFunction · 0.85
TensorClass · 0.70
device_checkMethod · 0.45
transposeMethod · 0.45
set_valueMethod · 0.45

Tested by

no test coverage detected