Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 3bddd5a

Browse files
Merge branch 'main' into Adding-skewX-entry
2 parents a3fd285 + ce6964f commit 3bddd5a

4 files changed

Lines changed: 224 additions & 1 deletion

File tree

bin/concept-of-the-week.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
content/kotlin/concepts/interfaces/interfaces.md
1+
content/swift/concepts/ranges/ranges.md
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
Title: '.cast()'
3+
Description: 'Returns a new map with the same entries but with keys and values cast to the specified types.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Dart'
9+
- 'Map'
10+
- 'Methods'
11+
CatalogContent:
12+
- 'learn-dart'
13+
- 'paths/computer-science'
14+
---
15+
16+
In Dart, the **`.cast()`** method returns a new `Map` containing the same key–value pairs as the original map, but with the specified key and value types. The returned map performs runtime type checks when reading or writing entries. If a key or value does not match the specified types, a `TypeError` is thrown.
17+
18+
This method is commonly used when working with dynamically typed maps that need to be treated as a map with specific types.
19+
20+
## Syntax
21+
22+
```pseudo
23+
mapVariable.cast<NewKeyType, NewValueType>()
24+
```
25+
26+
`mapVariable` is the `Map` object whose entries are cast to new types.
27+
28+
**Parameters:**
29+
30+
- `NewKeyType`: The target key type to cast the map’s keys to.
31+
- `NewValueType`: The target value type to cast the map’s values to.
32+
33+
**Return value:**
34+
35+
Returns a new `Map<K, V>` view of the original map, where keys and values are cast to the specified types.
36+
37+
## Example
38+
39+
In this example, a dynamically typed map is cast to a `Map<String, int>` so its entries can be accessed with strong typing:
40+
41+
```dart
42+
void main() {
43+
Map<dynamic, dynamic> rawData = {
44+
'Alice': 90,
45+
'Bob': 85,
46+
};
47+
48+
Map<String, int> scores = rawData.cast<String, int>();
49+
50+
print(scores);
51+
}
52+
```
53+
54+
The output of this code is:
55+
56+
```shell
57+
{Alice: 90, Bob: 85}
58+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
Title: '.argmin()'
3+
Description: 'Returns the index of the minimum value in a PyTorch tensor, or along a specified dimension.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Deep Learning'
9+
- 'Methods'
10+
- 'PyTorch'
11+
- 'Tensor'
12+
CatalogContent:
13+
- 'intro-to-py-torch-and-neural-networks'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.argmin()`** method in PyTorch returns the index of the minimum value in a flattened [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) tensor by default, or along a specified dimension. This method is commonly used in tasks such as finding the closest data point, selecting the best prediction, or identifying the least likely class in machine learning workflows.
18+
19+
## Syntax
20+
21+
```pseudo
22+
torch.argmin(input, dim=None, keepdim=False)
23+
```
24+
25+
**Parameters:**
26+
27+
- `input` (Tensor): The input tensor to search for the minimum value.
28+
- `dim` (int, optional): The dimension to reduce. If not specified, the index of the minimum value in the flattened tensor is returned.
29+
- `keepdim` (bool, optional): Whether the output tensor retains the reduced dimension. Defaults to `False`.
30+
31+
**Return value:**
32+
33+
The `.argmin()` method returns a `LongTensor` containing the index or indices of the minimum value(s). If `dim` is not specified, a scalar tensor is returned.
34+
35+
## Example
36+
37+
This example shows how to use the `.argmin()` method to find the index of the minimum value in a 2D tensor:
38+
39+
```py
40+
import torch
41+
42+
# Define a 2D tensor
43+
tensor = torch.tensor([[8, 3, 5],
44+
[2, 7, 4]])
45+
46+
# Index of minimum in flattened tensor
47+
print(torch.argmin(tensor))
48+
49+
# Index of minimum along each column (dim=0)
50+
print(torch.argmin(tensor, dim=0))
51+
52+
# Index of minimum along each row (dim=1)
53+
print(torch.argmin(tensor, dim=1))
54+
```
55+
56+
This example results in the following output:
57+
58+
```shell
59+
tensor(3)
60+
tensor([1, 0, 1])
61+
tensor([1, 0])
62+
```
63+
64+
In this example:
65+
66+
- **Flattened tensor**: The tensor is treated as `[8, 3, 5, 2, 7, 4]`, and the minimum value `2` is at index `3`.
67+
- **Along columns (`dim=0`)**: The minimum values in each column are `2`, `3`, and `4`, found in rows `1`, `0`, and `1`.
68+
- **Along rows (`dim=1`)**: The minimum values in each row are `3` (at index `1`) and `2` (at index `0`).
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
Title: '.softmax()'
3+
Description: 'Applies the Softmax function to an n-dimensional input Tensor, rescaling elements so they lie in the range [0, 1] and sum to 1.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Machine Learning'
7+
Tags:
8+
- 'Neural Networks'
9+
- 'PyTorch'
10+
- 'Tensor'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/machine-learning'
14+
---
15+
16+
The **`.softmax()`** function applies the Softmax mathematical transformation to an input tensor. It is a critical operation in deep learning, particularly for multi-class classification tasks. Softmax converts a vector of raw scores (often called logits) into a probability distribution where each value represents the likelihood of a specific class.
17+
18+
The Softmax function for an element $x_i$ in a vector $x$ is defined as:
19+
20+
$$\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j} \exp(x_j)}$$
21+
22+
By exponentiating the inputs, the function ensures all outputs are non-negative. Dividing by the sum of these exponentials ensures that the resulting values sum to exactly 1.
23+
24+
## Syntax
25+
26+
```pseudo
27+
torch.softmax(input, dim, dtype=None)
28+
```
29+
30+
**Parameters:**
31+
32+
- `input`: The input tensor containing the raw scores (logits).
33+
- `dim`: A dimension along which Softmax will be computed. Every slice along the dim will sum to `1`.
34+
- `dtype` (Optional): The desired data type of the returned tensor.
35+
36+
**Return value:**
37+
38+
Returns a tensor of the same shape as `input`, with values scaled between 0 and 1.
39+
40+
## Example 1: Basic Softmax on a 1D Tensor
41+
42+
This example shows how to convert a simple 1D tensor of logits into probabilities:
43+
44+
```py
45+
import torch
46+
47+
# A 1D tensor of raw scores
48+
logits = torch.tensor([1.0, 2.0, 3.0])
49+
50+
# Apply softmax along the only dimension (0)
51+
probabilities = torch.softmax(logits, dim=0)
52+
print("Logits:", logits)
53+
print("Probabilities:", probabilities)
54+
print("Sum of probabilities:", probabilities.sum().item())
55+
```
56+
57+
Here is the output:
58+
59+
```shell
60+
Logits: tensor([1., 2., 3.])
61+
Probabilities: tensor([0.0900, 0.2447, 0.6652])
62+
Sum of probabilities: 1.0
63+
```
64+
65+
The function converts raw logits into probabilities where the highest input value (3.0) yields the highest probability (~0.66), and the sum of all probabilities equals `1.0`.
66+
67+
## Example 2: Softmax on a 2D Tensor
68+
69+
For batched inputs where rows represent samples and columns represent classes, Softmax is typically applied along the class dimension:
70+
71+
```py
72+
import torch
73+
74+
# A 2D tensor (2 samples, 3 classes)
75+
logits = torch.tensor([
76+
[2.0, 1.0, 0.1],
77+
[1.0, 3.0, 0.2]
78+
])
79+
80+
# Apply softmax along the class dimension (dim=1)
81+
probs = torch.softmax(logits, dim=1)
82+
83+
print("Probabilities:\n", probs)
84+
print("\nSum of each row:", probs.sum(dim=1))
85+
```
86+
87+
Here is the output:
88+
89+
```shell
90+
Probabilities:
91+
tensor([[0.6590, 0.2424, 0.0986],
92+
[0.1131, 0.8360, 0.0508]])
93+
94+
Sum of each row: tensor([1.0000, 1.0000])
95+
```
96+
97+
By specifying `dim=1`, the operation is applied independently to each row (sample), ensuring that the class probabilities for each individual sample sum to `1.0`.

0 commit comments

Comments
 (0)