You could perhaps use np.multiply.outer instead of np.outer to get the required outer product:

>>> a = np.arange(4)
>>> b = np.ones(5)
>>> mo = np.multiply.outer

Then we have:

>>> mo(mo(a, a), b).shape
(4, 4, 5)

A better way could be to use np.einsum (this avoids creating intermediate arrays):

>>> c = np.einsum('i,j,k->ijk', a, a, b)
>>> c.shape
(4, 4, 5)
Answer from Alex Riley on Stack Overflow
Discussions

Create a 3d tensor from a pandas dataframe (pytorch) - Stack Overflow
What is the easiest way (I am looking for the minimum number of code lines) to convert a pandas dataframe of 4 columns into a 3d tensor padding the missing values along the way. import pandas as pd... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to create a 3D tensor from a .csv file in Python3?

Of course both are possible. The question is what are the performance requirements for that sparse tensor once you have it.

It can be as simple as:

import csv

with open('my_data.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    # skip header
    next(reader)
    my_tensor = {(A, B, C): D for A, B, C, D in reader}

# example usage
print(my_tensor.get((dim1, dim2, dim3), 0.0))

and a csvreader can be used to turn it back into a csv.

More on reddit.com
🌐 r/learnprogramming
3
1
July 8, 2022
How to create 3-D Tensors in Python using Numpy - Stack Overflow
14 Numpy: Multiplying a matrix with a 3d tensor -- Suggestion · 2 Creating 3rd order tensors with python and numpy More on stackoverflow.com
🌐 stackoverflow.com
Creating 3d Tensor Array from 2d Array (Python) - Stack Overflow
I have two numpy arrays (4x4 each). I would like to concatenate them to a tensor of (4x4x2) in which the first 'sheet' is the first array, second 'sheet' is the second array, etc. However, when I t... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Can I create a tensor with more than three dimensions in Python?
Yes, you can create a tensor with four or more dimensions.
🌐
how.okpedia.org
how.okpedia.org › en › python › how-to-create-a-tensor-in-python
[Python] How to Create a Tensor in PyTorch or NumPy - Okpedia
How to create a tensor in NumPy?
To define a tensor in NumPy, you can use the function: numpy.array()
🌐
how.okpedia.org
how.okpedia.org › en › python › how-to-create-a-tensor-in-python
[Python] How to Create a Tensor in PyTorch or NumPy - Okpedia
How to create a 4-dimensional tensor with PyTorch?
To define a 4-dimensional matrix with all zeros, you can use the function: torch.zeros (4,4,4,4)
🌐
how.okpedia.org
how.okpedia.org › en › python › how-to-create-a-tensor-in-python
[Python] How to Create a Tensor in PyTorch or NumPy - Okpedia
🌐
Packt Publishing
hub.packtpub.com › how-to-create-tensors-in-pytorch
How to Create Tensors in PyTorch
April 20, 2021 - The rest can be found in the PyTorch documentation. There are three ways to create a tensor in PyTorch: By calling a constructor of the required type. By converting a NumPy array or a Python list into a tensor.
🌐
TensorFlow
tensorflow.org › tensorflow core › introduction to tensors
Introduction to Tensors | TensorFlow Core
When creating a tf.Tensor from a Python object you may optionally specify the datatype.
🌐
Open3D
open3d.org › docs › latest › python_api › open3d.core.Tensor.html
open3d.core.Tensor - Open3D primary (unknown) documentation
Generates a tensor containing points from a quasirandom sequence. This function creates a tensor of shape (n, dims) where each row is a point in a low-discrepancy quasirandom sequence (specifically, the generalized golden ratio based R_n sequence). The 2D variant is the plastic sequence.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-correctly-access-elements-in-a-3d-pytorch-tensor
How to Correctly Access Elements in a 3D Pytorch Tensor? - GeeksforGeeks
August 23, 2021 - In this article, we will discuss how to access elements in a 3D Tensor in Pytorch. PyTorch is an optimized tensor library majorly used for Deep Learning applications using GPUs and CPUs. It is one of the widely used Machine learning libraries, others being TensorFlow and Keras. The python supports the torch module, so to work with this first we import the module to the workspace. ... # import torch module import torch # create an 3 D tensor with 8 elements each a = torch.tensor([[[1, 2, 3, 4, 5, 6, 7, 8], [10, 11, 12, 13, 14, 15, 16, 17]], [[71, 72, 73, 74, 75, 76, 77, 78], [81, 82, 83, 84, 85, 86, 87, 88]]]) # display actual tensor print(a)
🌐
GeeksforGeeks
geeksforgeeks.org › python › creating-a-tensor-in-pytorch
Creating a Tensor in Pytorch - GeeksforGeeks
July 4, 2021 - Python3 · import torch M_data = [[1., 2., 3.], [4, 5, 6]] M = torch.tensor(M_data) print(M) Output: tensor([[1., 2., 3.], [4., 5., 6.]]) To create a 3D tensor you can use the following code template: Python3 ·
🌐
PyTorch
docs.pytorch.org › introduction to pytorch
Introduction to PyTorch — PyTorch Tutorials 2.13.0+cu130 documentation
July 20, 2022 - Tensors can be created from Python lists with the torch.tensor() function. # torch.tensor(data) creates a torch.Tensor object with the given data. V_data = [1., 2., 3.] V = torch.tensor(V_data) print(V) # Creates a matrix M_data = [[1., 2., ...
🌐
Stack Overflow
stackoverflow.com › questions › 70062836 › create-a-3d-tensor-from-a-pandas-dataframe-pytorch
Create a 3d tensor from a pandas dataframe (pytorch) - Stack Overflow
import pandas as pd import numpy as pd import torch data = ... df = pd.DataFrame(data) CAT = df.columns.tolist() CAT.remove("Age") # encode categories as integers and extract the shape shape = [] for c in CAT: shape.append(len(df[c].unique())) ...
🌐
Medium
medium.com › @rp48166 › tensor-operations-and-applications-in-python-6bb107761209
Tensor Operations and Applications in Python | by Rahul Pradhan | Medium
April 27, 2024 - # Creating a 3D tensor tensor_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [12, 13, 14], [12, 13, 14]]]) print("The 3D tensor is:") print(tensor_3d) print("The dimension of the tensor is:", tensor_3d.ndim) # Printing the ...
🌐
Stack Overflow
stackoverflow.com › questions › 33907127 › how-to-create-3-d-tensors-in-python-using-numpy
How to create 3-D Tensors in Python using Numpy - Stack Overflow
If you know about the Christoffel symbols, that's what I'm trying to build. I need to create this tensor. I'd like to be able to just fill in a blank tensor with specific components. https://en.
🌐
Open3D
open3d.org › docs › 0.11.0 › tutorial › core › tensor.html
Tensor — Open3D 0.11.0 documentation
Tensor can be created from list, numpy array, another tensor. A tensor of specific data type and device can be constructed by passing a o3c.Dtype and/or o3c.Device to a constructor. If not passed, the default data type is inferred from the data, and the default device is CPU.
🌐
KDnuggets
kdnuggets.com › 2018 › 05 › wtf-tensor.html
WTF is a Tensor?!? - KDnuggets
March 24, 2022 - While, technically, all of the above constructs are valid tensors, colloquially when we speak of tensors we are generally speaking of the generalization of the concept of a matrix to N ≥ 3 dimensions. We would, then, normally refer only to tensors of 3 dimensions or more as tensors, in order to avoid confusion (referring to the scalar '42' as a tensor would not be beneficial or lend to clarity, generally speaking). The code below creates a 3D tensor.