Skip to content
Open
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
278 changes: 278 additions & 0 deletions tensors-ex (1).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import torch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Create two random tensors with shapes (6, 5) and (1, 5), and perform a matrix multiplication on these tensors."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Set random seed for reproducibility\n",
"torch.manual_seed(7)\n",
"\n",
"# Create random tensors\n",
"tensor1 = np.random.randn(6, 5)\n",
"tensor2 = np.random.randn(1, 5)\n",
"\n",
"# Perform matrix multiplication\n",
"result = np.matmul(tensor1, tensor2.T) # Transpose tensor2 to match dimensions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Given the provided code for generating data, create a simple Python function or class that multiplies the generated features tensor by the corresponding weights tensor and adds the bias term. Assume that the function/class takes the features, weights, and bias tensors as inputs and returns the result of the linear operation. Provide an example of using this function/class with the generated data."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"### Generate some data\n",
"torch.manual_seed(7) # Set the random seed so things are predictable\n",
"\n",
"# Features are 3 random normal variables\n",
"features = torch.randn((1, 5))\n",
"# True weights for our data, random normal variables again\n",
"weights = torch.randn_like(features)\n",
"# and a true bias term\n",
"bias = torch.randn((1, 1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Find the maximum and minimum values as well as the corresponding index values in the output of task 1."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Find max\n",
"max_value = np.max(result)\n",
"\n",
"# Find min\n",
"min_value = np.min(result)\n",
"\n",
"# Find argmax\n",
"argmax_value = np.argmax(result)\n",
"\n",
"# Find argmin\n",
"argmin_value = np.argmin(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Generate a unique tensor with dimensions (1, 1, 1, 25), and subsequently transform it into a new tensor by removing all singleton dimensions, resulting in a tensor with shape (25)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial Tensor:\n",
"[[[[0.69646919 0.28613933 0.22685145 0.55131477 0.71946897 0.42310646\n",
" 0.9807642 0.68482974 0.4809319 0.39211752 0.34317802 0.72904971\n",
" 0.43857224 0.0596779 0.39804426 0.73799541 0.18249173 0.17545176\n",
" 0.53155137 0.53182759 0.63440096 0.84943179 0.72445532 0.61102351\n",
" 0.72244338]]]]\n",
"Shape of Initial Tensor: (1, 1, 1, 25)\n",
"\n",
"Transformed Tensor:\n",
"[0.69646919 0.28613933 0.22685145 0.55131477 0.71946897 0.42310646\n",
" 0.9807642 0.68482974 0.4809319 0.39211752 0.34317802 0.72904971\n",
" 0.43857224 0.0596779 0.39804426 0.73799541 0.18249173 0.17545176\n",
" 0.53155137 0.53182759 0.63440096 0.84943179 0.72445532 0.61102351\n",
" 0.72244338]\n",
"Shape of Transformed Tensor: (25,)\n"
]
}
],
"source": [
"# Set random seed for reproducibility\n",
"np.random.seed(123)\n",
"\n",
"# Generate a tensor with random values\n",
"initial_tensor = np.random.rand(1, 1, 1, 25)\n",
"\n",
"# Eliminate singleton dimensions\n",
"transformed_tensor = np.squeeze(initial_tensor)\n",
"\n",
"# Display the tensors along with their shapes\n",
"print(\"Initial Tensor:\")\n",
"print(initial_tensor)\n",
"print(\"Shape of Initial Tensor:\", initial_tensor.shape)\n",
"\n",
"print(\"\\nTransformed Tensor:\")\n",
"print(transformed_tensor)\n",
"print(\"Shape of Transformed Tensor:\", transformed_tensor.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Create a 1D tensor of size 5 with value ranging from 1 to 5. Reshape the 1D tensor into a 2D tensor of shape (1, 5)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"original_tensor = np.arange(1, 6)\n",
"\n",
"reshaped_tensor = original_tensor.reshape(1, 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6. Create two 2D tensors of shape (2, 3) and perform element-wise addition."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"tensor1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
"tensor2 = np.array([[7, 8, 9], [10, 11, 12]])\n",
"\n",
"result_tensor = tensor1 + tensor2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7. Create a 2D tensor of shape (4, 4) filled with random values. Extract the first row and the last column as seperate tensors."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"random_tensor = np.random.rand(4, 4)\n",
"\n",
"first_row = random_tensor[0, :]\n",
"\n",
"last_column = random_tensor[:, -1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"8. Create a 2D tensor of shape (3, 3) and a 1D tensor of shape (3,). Add the 1D tensor to each row of the 2D tensor using broadcasting."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"tensor_2d = np.array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])\n",
"\n",
"tensor_1d = np.array([10, 20, 30])\n",
"\n",
"result_tensor = tensor_2d + tensor_1d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"9. Create a 2D tensor of shape (3, 4) filled with random values and compute the sum of all elements."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"random_tensor = np.random.rand(3, 4)\n",
"\n",
"sum_of_elements = np.sum(random_tensor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"10. Create a 2D tensor of shape (3, 4) filled with random values and compute the mean along each row."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"random_tensor = np.random.rand(3, 4)\n",
"\n",
"mean_along_rows = np.mean(random_tensor, axis=1)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}