Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ object ContractionOps:
primeConcat: PrimeConcat[T, OtherShape],
labels: Labels[primeConcat.Out]
): Tensor[primeConcat.Out, V] = Tensor(
// Jax outer product flattens, reshape required
Jax.jnp.reshape(
Jax.jnp.outer(tensor.jaxValue, other.jaxValue),
(tensor.shape.dimensions ++ other.shape.dimensions).toPythonProxy
)
Jax.jnp.tensordot(tensor.jaxValue, other.jaxValue, axes = 0) // generalized outer product
)

/** Computes the dot product of this tensor with another tensor along the specified axis.
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/scala/dimwit/tensor/TensorOpsContractionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,25 @@ class TensorOpsContractionSuite extends DimwitTest:
Array(10.0f, 20.0f, 20.0f, 40.0f)
)
)

it("Tensor2[A, B] and Tensor2[C, D] to Tensor4[A, B, C, D]"):
val mAB = m1
val mCD = m2.relabelAll((Axis[C], Axis[D]))

val res = mAB.outerProduct(mCD)

res.shape.labels shouldBe List("A", "B", "C", "D")
res should approxEqual(
Tensor.like(res).fromArray(
Array(
10.0f, 20.0f,
30.0f, 40.0f,
20.0f, 40.0f,
60.0f, 80.0f,
30.0f, 60.0f,
90.0f, 120.0f,
40.0f, 80.0f,
120.0f, 160.0f
)
)
)
Loading