কম্পিউটার

পাইথনে অ্যারের মতো অক্ষ সহ বিভিন্ন মাত্রা সহ অ্যারের জন্য টেনসর ডট পণ্য গণনা করুন


দুটি টেনসর, a এবং b, এবং একটি array_like অবজেক্ট যেখানে দুটি array_like অবজেক্ট রয়েছে, (a_axes,b_axes), a_axes এবং b_axes দ্বারা নির্দিষ্ট করা অক্ষগুলির উপর a's এবং 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']

  1. পাইথনে দুটি এক-মাত্রিক অ্যারের অভ্যন্তরীণ পণ্য পান

  2. পাইথনে ভিন্ন মাত্রা সহ দুটি (অ্যারের) ভেক্টরের ক্রস পণ্য ফেরত দিন

  3. পাইথনে দুটি বহুমাত্রিক অ্যারের অভ্যন্তরীণ পণ্য পান

  4. পাইথনে 4D এবং 3D মাত্রা সহ অ্যারের ক্রোনকার পণ্য পান