|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pytensor.configdefaults import config |
| 4 | +from pytensor.graph.fg import FunctionGraph |
| 5 | +from pytensor.tensor.type import matrix, scalar, vector |
| 6 | +from tests.link.pytorch.test_basic import compare_pytorch_and_py |
| 7 | + |
| 8 | + |
| 9 | +def test_pytorch_dot(): |
| 10 | + y = vector("y") |
| 11 | + y_test = np.r_[1.0, 2.0].astype(config.floatX) |
| 12 | + x = vector("x") |
| 13 | + x_test = np.r_[3.0, 4.0].astype(config.floatX) |
| 14 | + A = matrix("A") |
| 15 | + A_test = np.array([[6, 3], [3, 0]], dtype=config.floatX) |
| 16 | + alpha = scalar("alpha") |
| 17 | + alpha_test = np.array(3.0, dtype=config.floatX) |
| 18 | + beta = scalar("beta") |
| 19 | + beta_test = np.array(5.0, dtype=config.floatX) |
| 20 | + |
| 21 | + # 2D * 2D |
| 22 | + out = A.dot(A * alpha) + beta * A |
| 23 | + fgraph = FunctionGraph([A, alpha, beta], [out]) |
| 24 | + compare_pytorch_and_py(fgraph, [A_test, alpha_test, beta_test]) |
| 25 | + |
| 26 | + # 1D * 2D and 1D * 1D |
| 27 | + out = y.dot(alpha * A).dot(x) + beta * y |
| 28 | + fgraph = FunctionGraph([y, x, A, alpha, beta], [out]) |
| 29 | + compare_pytorch_and_py(fgraph, [y_test, x_test, A_test, alpha_test, beta_test]) |
0 commit comments