Files
Obsidian-Main/21.01. Programming/Python/PyTorch.md

45 lines
1.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
tags:
aliases:
date: 2024-11-10
time: 17:00:12
description:
---
**可以用來代替[scikit-learn](https://scikit-learn.org/stable/)**
I know, `Scikit-Learn` isnt supposed to be a deep learning library, but people use it as if it were. It is incredibly handy at quick prototyping and traditional machine learning models, but when it comes to neural networks, its just not in the same league as a library designed with tensors in mind.
## Why [scikit-learn](https://scikit-learn.org/stable/) is Overrated:
**No GPU Support:** Deep learning can be life-changing when training on GPUs. However, this is something that is not supported in `Scikit-Learn`.
**Not Optimized for Neural Networks:** `Scikit-learn` wasnt designed for doing deep learning; using it this way is reactively assured poor results.
## What You Should Use Instead: PyTorch
`PyTorch` is more general and supports GPU. Hence, its perfect for deep learning projects. Its Pythonic-this means for one coming from `Scikit-Learn`, it will feel natural, but with much more power.
import torch
import torch.nn as nn
import torch.optim as optim
# Define a simple model
```python
model = nn.Sequential(
nn.Linear(10, 5),
nn.ReLU(),
nn.Linear(5, 2)
)
```
# Define optimizer and loss
```python
optimizer = optim.SGD(model.parameters(), lr=0.01)
loss_fn = nn.CrossEntropyLoss()
```
If youre serious about deep learning, youll want to use a library worked out for the task at hand-which will save you from such limitations and inefficiencies. You will fine tune models with `PyTorch` and leverage the GPUs to your hearts content.
# 參考來源
- [5 Overrated Python Libraries (And What You Should Use Instead) | by Abdur Rahman | Nov, 2024 | Python in Plain English](https://python.plainenglish.io/5-overrated-python-libraries-and-what-you-should-use-instead-106bd9ded180)