Rubyではbundleコマンドを使ってプロジェクトを作る。Pythonではvenvモジュールを使えばプロジェクトを作ることが出来る
1. Rubyプロジェクト作り方
$bundle init
$echo "gem 'mysql2'" > Gemfile
$bundle config set path vendor
$bundle install
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Fetching bigdecimal 4.0.1
Installing bigdecimal 4.0.1 with native extensions
Fetching mysql2 0.5.7
Installing mysql2 0.5.7 with native extensions
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
Bundled gems are installed into `./vendor`
$echo "require 'mysql2'" > a.rb
$bundle exec ruby a.rb # no error
errorの例
$ruby a.rb
<internal:/opt/homebrew/Cellar/ruby/3.4.2/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:136:in 'Kernel#require': cannot load such file -- mysql2 (LoadError)
...
2. Pythonプロジェクトの作り方
$mkdir workingTree ; cd workingTree; python -m venv .
$source bin/activate
$python -m pip install numpy
$echo "import numpy as np
x = np.array([1, 2, 3])
print(str(x))
" > a.py
$python a.py # [1 2 3]
$deactivate
3. Python依存パッケージの確認
$pip freeze > requirements.txt
依存ファイルリストを使ってインストール
$python -m pip install -r requirements.txt
PytorchをInstallすればMetal対応のが自動的にInstallされる。約500MBのDiskを消費する。
(test02) [13675@5:13]$python
Python 3.13.2 (main, Feb 4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
... # create a 3D tensor from NumPy array
... ary3d = np.array([[[1, 2], [3, 4]],
... [[5, 6], [7, 8]]])
... tensor3d_2 = torch.tensor(ary3d) # Copies NumPy array
>>> print(tensor3d_2)
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
>>> print(torch.backends.mps.is_available()) # Metal Performance Shader
... print(torch.__version__)
True
2.9.1
>>> import os
>>> os.system('du -sh')
543M .
LLMs-from-scratch/appendix-A/01_main-chapter-code/code-part1.ipynb at main · rasbt/LLMs-from-scratch
Scientific Computing in Python: Introduction to NumPy and Matplotlib: Sebastian Raschka, PhD
