-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add documentation for specifying data types in Pytorch tensors * Updated tags.md and added new directory for specifying data types * add main file to the branch * Update specifying-data-types.md minor fixes * Update yarn.lock * Update content/pytorch/concepts/tensor-operations/terms/specifiying-data-types/specifying-data-types.md * Update content/pytorch/concepts/tensor-operations/terms/specifiying-data-types/specifying-data-types.md * Update content/pytorch/concepts/tensor-operations/terms/specifiying-data-types/specifying-data-types.md * Update content/pytorch/concepts/tensor-operations/terms/specifiying-data-types/specifying-data-types.md * Fix file path ---------
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...concepts/tensor-operations/terms/specifying-data-types/specifying-data-types.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
Title: 'Specifying Data Types' | ||
Description: 'Determines how tensors are stored and processed, impacting precision, memory usage, and computation speed.' | ||
Subjects: | ||
- 'Data Science' | ||
- 'Machine Learning' | ||
- 'Deep Learning' | ||
Tags: | ||
- 'Pytorch' | ||
- 'Tensor' | ||
- 'Data Types' | ||
CatalogContent: | ||
- 'learn-Intro-to-PyTorch-and-Neural-Networks' | ||
- 'paths/data-science' | ||
--- | ||
|
||
In PyTorch, specifying the data types for [`tensors`](https://www.codecademy.com/resources/docs/pytorch/tensors) is crucial as they are the core data structures used to store and process data. Each tensor's data type (`dtype`) defines the kind of values it holds (e.g., `integer`, `float`, `boolean`), ensuring precision, improving performance, and maintaining compatibility during computations. | ||
|
||
## Syntax | ||
|
||
To specify a data type in a PyTorch tensor, use the `dtype` parameter when creating a tensor or the `.to()` method for converting an existing one. | ||
|
||
### For specifying `dtype` when creating a tensor | ||
|
||
```pseudo | ||
torch.tensor(data, dtype=torch.<data_type>) | ||
``` | ||
|
||
- `data`: The input data used to create the tensor. This can be a list, NumPy array, or another tensor. | ||
- `dtype`: Specifies the data type of the tensor. Common data types include: | ||
- `torch.float32` (default): 32-bit floating-point | ||
- `torch.float64`: 64-bit floating-point | ||
- `torch.int32`: 32-bit integer | ||
- `torch.int64`: 64-bit integer | ||
- `torch.bool`: Boolean | ||
|
||
### For converting an existing tensor to a different data type | ||
|
||
```pseudo | ||
tensor.to(torch.<data_type>) | ||
``` | ||
|
||
## Example | ||
|
||
In the example below a tensor is created with a specified data type, another with a different type, and one tensor is converted to a new data type: | ||
|
||
```py | ||
import torch | ||
|
||
# Creating a float32 tensor | ||
float_tensor = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32) | ||
print(float_tensor) | ||
|
||
# Creating an int64 tensor | ||
int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64) | ||
print(int_tensor) | ||
|
||
# Converting a tensor to a different data type | ||
converted_tensor = float_tensor.to(torch.int64) | ||
print(converted_tensor) | ||
``` | ||
|
||
The code above generates the output as: | ||
|
||
```shell | ||
tensor([1., 2., 3.]) | ||
tensor([1, 2, 3]) | ||
tensor([1, 2, 3]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,6 +336,7 @@ Tags | |
Target | ||
Technical Interviews | ||
Templates | ||
Tensor | ||
TensorFlow | ||
Text-To-Image | ||
Text Processing | ||
|