chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:30:25 +08:00
commit f19b2512d7
562 changed files with 38082 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
import numpy as np
from prml.nn.tensor.constant import Constant
from prml.nn.tensor.tensor import Tensor
from prml.nn.function import Function
class BroadcastTo(Function):
"""
Broadcast a tensor to an new shape
"""
def forward(self, x, shape):
x = self._convert2tensor(x)
self.x = x
output = np.broadcast_to(x.value, shape)
if isinstance(self.x, Constant):
return Constant(output)
return Tensor(output, function=self)
def backward(self, delta):
dx = delta
if delta.ndim != self.x.ndim:
dx = dx.sum(axis=tuple(range(dx.ndim - self.x.ndim)))
if isinstance(dx, np.number):
dx = np.array(dx)
axis = tuple(i for i, len_ in enumerate(self.x.shape) if len_ == 1)
if axis:
dx = dx.sum(axis=axis, keepdims=True)
self.x.backward(dx)
def broadcast_to(x, shape):
"""
Broadcast a tensor to an new shape
"""
return BroadcastTo().forward(x, shape)