दो टेंसर, a और b, और एक array_like ऑब्जेक्ट जिसमें दो array_like ऑब्जेक्ट हैं, (a_axes,b_axes), a_axes और b_axes द्वारा निर्दिष्ट कुल्हाड़ियों पर a और b के तत्वों (घटकों) के उत्पादों को योग करें। तीसरा तर्क एक एकल गैर-ऋणात्मक पूर्णांक_जैसे अदिश, N हो सकता है; यदि ऐसा है, तो a के अंतिम N आयामों और b के पहले N आयामों को जोड़ दिया जाता है।
कदम
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import numpy as np
सरणी () विधि का उपयोग करके विभिन्न आयामों के साथ दो सुस्पष्ट सरणियाँ बनाना -
arr1 = np.array(range(1, 9))
arr1.shape = (2, 2, 2)
arr2 = np.array(('p', 'q', 'r', 's'), dtype=object)
arr2.shape = (2, 2) सरणियों को प्रदर्शित करें -
print("Array1...\n",arr1)
print("\nArray2...\n",arr2) दोनों सरणियों के आयामों की जाँच करें -
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim) दोनों सरणियों के आकार की जाँच करें -
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape) विभिन्न आयामों वाले सरणियों के लिए टेंसर डॉट उत्पाद की गणना करने के लिए, numpy.tensordot()विधि का उपयोग करें -
print("\nTensor dot product...\n", np.tensordot(arr1, arr2, ((0, 1), (0, 1))))
उदाहरण
import numpy as np
# Creating two numpy arrays with different dimensions using the array() method
arr1 = np.array(range(1, 9))
arr1.shape = (2, 2, 2)
arr2 = np.array(('p', 'q', 'r', 's'), dtype=object)
arr2.shape = (2, 2)
# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)
# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)
# To compute the tensor dot product for arrays with different dimensions, use the numpy.tensordot() method in Python
print("\nTensor dot product...\n", np.tensordot(arr1, arr2, ((0, 1), (0, 1)))) आउटपुट
Array1... [[[1 2] [3 4]] [[5 6] [7 8]]] Array2... [['p' 'q'] ['r' 's']] Dimensions of Array1... 3 Dimensions of Array2... 2 Shape of Array1... (2, 2, 2) Shape of Array2... (2, 2) Tensor dot product... ['pqqqrrrrrsssssss' 'ppqqqqrrrrrrssssssss']