### Install Xcode Command Line Tools on macOS Source: https://github.com/deng-group/mle4217_5219_book/blob/main/orientation/setup.md This command installs the necessary command-line tools for Xcode on macOS, which are often required for development tasks and package installations. Open the Terminal application and execute the command. ```shell xcode-select --install ``` -------------------------------- ### Setup Phonon Workflow for VASP Calculations with Atomate2 Source: https://github.com/deng-group/mle4217_5219_book/blob/main/high_throughput/high_throughput.ipynb Creates a phonon calculation workflow using atomate2's PhononMaker for VASP-based DFT calculations. Generates a computational graph showing workflow tasks and dependencies. Requires VASP to be installed on HPC systems; this example demonstrates workflow construction without execution. ```python from atomate2.vasp.flows.phonons import PhononMaker phonon_flow = PhononMaker(born_maker=None).make(structure=silicon_structure) phonon_flow.draw_graph().show() ``` -------------------------------- ### Install Atomate2 and Dependencies Source: https://github.com/deng-group/mle4217_5219_book/blob/main/high_throughput/high_throughput.ipynb Installs the necessary libraries for atomate2, MACE, and related calculation tools using pip. This command is essential for setting up the environment to run the subsequent code examples. ```python !pip install atomate2 mace-torch phonopy seekpath ``` -------------------------------- ### Install PyTorch and Related Libraries Source: https://github.com/deng-group/mle4217_5219_book/blob/main/machine_learning_I/pytorch.ipynb Installs PyTorch, torchvision, torchaudio, and torch_geometric using pip. This is a prerequisite for using PyTorch functionalities. ```python # Installation !pip3 install torch torchvision torchaudio torch_geometric ``` -------------------------------- ### Basic Python Syntax Examples Source: https://github.com/deng-group/mle4217_5219_book/blob/main/orientation/programming.ipynb Demonstrates fundamental Python operations including printing, arithmetic, list manipulation, dictionary usage, loops, and function definition. Assumes a standard Python environment. ```python # Example 1: Print a message print("Hello, World!") # Example 2: Basic arithmetic operations a = 10 b = 5 print("Addition:", a + b) print("Subtraction:", a - b) print("Multiplication:", a * b) print("Division:", a / b) # Example 3: List operations my_list = [1, 2, 3, 4, 5] print("Original list:", my_list) my_list.append(6) print("List after appending 6:", my_list) print("List slice [1:4]:", my_list[1:4]) # Example 4: Dictionary operations my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'} print("Original dictionary:", my_dict) my_dict['age'] = 26 print("Dictionary after updating age:", my_dict) print("Keys in dictionary:", my_dict.keys()) print("Values in dictionary:", my_dict.values()) # Example 5: Looping through a list for item in my_list: print("Item:", item) # Example 6: Defining and calling a function def greet(name): return f"Hello, {name}!" print(greet("Alice")) ``` -------------------------------- ### Install Numba Source: https://github.com/deng-group/mle4217_5219_book/blob/main/models_and_theories_II/md_mc.ipynb Installs the Numba library using pip. Numba is a just-in-time compiler for Python that accelerates numerical functions. ```python !pip install numba ``` -------------------------------- ### Basic PyTorch Training Loop Structure in Python Source: https://github.com/deng-group/mle4217_5219_book/blob/main/machine_learning_I/pytorch.ipynb This example outlines a fundamental PyTorch training loop. It includes essential steps such as moving data to the device, zeroing gradients, performing a forward pass, calculating loss, performing a backward pass, and updating model parameters with an optimizer. It assumes model, criterion, optimizer, and DataLoader are pre-defined and handles basic setup and epoch iteration. ```python # --- Setup (Assume model, criterion, optimizer, train_loader are defined) --- # model = YourModel(...) # criterion = YourLossFunction() # optimizer = YourOptimizer(model.parameters(), lr=...) # train_loader = DataLoader(your_train_dataset, ...) # device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # model.to(device) # Move model to the appropriate device # Let's create dummy versions for illustration: model = LinearRegression(5, 1).to(device) # Simple model: 5 features -> 1 output criterion = nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=0.01) dummy_features = torch.randn(100, 5) dummy_labels = torch.randn(100, 1) * 3 + 2 # y = 3x + 2 + noise approx dummy_dataset = TensorDataset(dummy_features, dummy_labels) train_loader = DataLoader(dummy_dataset, batch_size=10, shuffle=True) # --- Training Loop --- num_epochs = 10 print("\nStarting Dummy Training Loop:") model.train() # Set the model to training mode (important for layers like dropout, batchnorm) for epoch in range(num_epochs): running_loss = 0.0 for i, (inputs, targets) in enumerate(train_loader): # Move data to the same device as the model inputs = inputs.to(device) targets = targets.to(device) # 1. Zero the parameter gradients optimizer.zero_grad() # 2. Forward pass: compute predicted outputs outputs = model(inputs) # 3. Calculate the loss loss = criterion(outputs, targets) # 4. Backward pass: compute gradient of the loss w.r.t. parameters loss.backward() # 5. Perform a single optimization step (parameter update) optimizer.step() # Accumulate loss for reporting running_loss += loss.item() * inputs.size(0) # loss.item() gets scalar loss epoch_loss = running_loss / len(train_loader.dataset) print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss:.4f}') print('Finished Training') # After training, you typically evaluate on a separate test set # model.eval() # Set model to evaluation mode # with torch.no_grad(): # Disable gradient calculation for evaluation # # ... loop through test_loader and calculate metrics ... ``` -------------------------------- ### Install MACE-Torch Package (Shell) Source: https://github.com/deng-group/mle4217_5219_book/blob/main/optimization/optimization.ipynb Installs or upgrades the mace-torch package, which provides machine learning-based force fields for materials science simulations. ```shell !pip install mace-torch --upgrade ``` -------------------------------- ### Install mp_api Package Source: https://github.com/deng-group/mle4217_5219_book/blob/main/database/materials_project.ipynb Installs the 'mp_api' Python package, which provides an interface to the Materials Project API. This is a prerequisite for querying the database. The '!' prefix indicates that this command should be run in a Jupyter Notebook or similar environment. ```python # you can skip this if you've already installed the mp_api package. !pip install mp_api ``` -------------------------------- ### Install Line Profiler Package Source: https://github.com/deng-group/mle4217_5219_book/blob/main/computer/scaling.ipynb This command installs the `line_profiler` package, a tool used for line-by-line profiling of Python code to identify performance bottlenecks. It is executed using pip. ```bash !pip install line_profiler ``` -------------------------------- ### Jupyter Book - Build HTML and PDF Versions Source: https://context7.com/deng-group/mle4217_5219_book/llms.txt Instructions for building an interactive textbook using Jupyter Book. Requires installation of required packages via 'requirements.txt' and a LaTeX distribution for PDF builds. Commands generate HTML and PDF output in the '_build' directory. ```bash # Install required packages pip install -r requirements.txt # Build HTML version make # View the book (open in web browser) open ./_build/html/index.html # Build PDF version (requires LaTeX installation) make book # The book uses MyST markdown and Jupyter Book framework # Configuration is in myst.yml and pyproject.toml ``` -------------------------------- ### Install mp_api Python Package Source: https://github.com/deng-group/mle4217_5219_book/blob/main/figures/original_figs/make_figures.ipynb This command installs the 'mp_api' Python package using pip. The output shows that the package and its dependencies (like setuptools, msgpack, maggma, pymatgen, etc.) are already satisfied, indicating a successful prior installation or an environment where they are available. ```python !pip install mp_api ``` -------------------------------- ### Install ASAP3 Package for EMT Potential Source: https://github.com/deng-group/mle4217_5219_book/blob/main/models_and_theories_II/md_mc.ipynb Installs the ASAP3 package which provides faster EMT potential implementation compared to ASE's built-in version. This is a prerequisite for running efficient molecular dynamics simulations of copper systems. ```python !pip install asap3 ``` -------------------------------- ### Initialize Monte Carlo π Calculation Setup Source: https://github.com/deng-group/mle4217_5219_book/blob/main/figures/original_figs/make_figures.ipynb Initializes variables and imports for Monte Carlo simulation to estimate π by randomly sampling points in a unit circle. Sets up tracking arrays for points inside and outside the circle for visualization. Requires numpy, matplotlib, and random modules. ```python import random import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import AutoMinorLocator # Number of points to use in the simulation n_points = 100000 inside_circle = 0 x_inside, y_inside = [], [] x_outside, y_outside = [], [] ``` -------------------------------- ### Illustrative Conflict Scenario Setup (Markdown) Source: https://github.com/deng-group/mle4217_5219_book/blob/main/computer/git_examples.md This markdown snippet demonstrates how to create a merge conflict by adding new content to a README file on a feature branch. This is typically followed by switching to another branch and modifying the same file to simulate a conflict scenario. ```markdown # MLE5219 Project This is the README file for the MLE5219 project. This is a new feature added to the README file. ``` -------------------------------- ### Install PyTorch Geometric and Dependencies Source: https://github.com/deng-group/mle4217_5219_book/blob/main/machine_learning_II/adv_ml.ipynb Installs the necessary libraries for PyTorch Geometric, including PyTorch, torchvision, torchaudio, and torch_geometric. It provides commands for both CPU and GPU setups. Ensure you check the PyTorch website for specific CUDA compatibility if using NVIDIA GPUs. ```python # CPU: For macOS (Apple Silicon) or Windows !pip3 install torch torchvision torchaudio torch_geometric # For macOS (Apple Silicon) #!pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu # For NVIDIA GPUs: check PyTorch website for more details https://pytorch.org/ ``` -------------------------------- ### Set Up and Use PyTorch Optimizers in Python Source: https://github.com/deng-group/mle4217_5219_book/blob/main/machine_learning_I/pytorch.ipynb Shows how to initialize popular PyTorch optimizers like Adam and SGD for training a model. It outlines the typical steps within a training loop: zeroing gradients, calculating loss, backpropagation, and updating parameters using `optimizer.step()`. ```python import torch.optim as optim # Use the MLP model we defined earlier model_to_train = SimpleMLP(10, 1) # --- Choose an Optimizer --- # Adam is a popular and often effective choice optimizer_adam = optim.Adam(model_to_train.parameters(), lr=0.001) # lr is the learning rate # Stochastic Gradient Descent (SGD) is another common one # optimizer_sgd = optim.SGD(model_to_train.parameters(), lr=0.01, momentum=0.9) # --- Optimizer Steps (within a training loop) --- # 1. Zero the gradients before calculating loss for a new batch # optimizer_adam.zero_grad() # # 2. Calculate the loss # loss = criterion(outputs, targets) # # 3. Compute gradients w.r.t. parameters # loss.backward() # # 4. Update the model parameters based on gradients # optimizer_adam.step() ``` -------------------------------- ### Initialize Git Repository (VS Code & CLI) Source: https://github.com/deng-group/mle4217_5219_book/blob/main/computer/git_examples.md Initializes a new Git repository in the current directory. This is the first step to start version control for a project. VS Code provides a button in the Source Control view, while the command line uses the 'git init' command. ```VS Code - Go to the Source Control view by clicking on the Source Control icon in the Activity Bar on the side of the window. - Click on `Initialize Repository`. ``` ```Command Line git init ``` -------------------------------- ### Uninstall and Recreate Python Environment (if needed) Source: https://github.com/deng-group/mle4217_5219_book/blob/main/database/materials_project.ipynb Provides bash commands to manage Python environments using Anaconda. This is a troubleshooting step for users experiencing installation issues with Python 3.13, guiding them to uninstall the current environment, create a new one with Python 3.12, and install necessary packages. ```bash conda remove --name mi --all ``` ```bash conda create -n mi python=3.12 ``` ```bash conda activate mi ``` ```bash pip install jupyter ase pymatgen ``` -------------------------------- ### Install mp_api Package Source: https://github.com/deng-group/mle4217_5219_book/blob/main/database/bulk_modulus.ipynb Installs the mp_api Python package, which is necessary for interacting with the Materials Project API. This is a one-time installation. ```python # you can skip this if you've already installed the mp_api package. !pip install mp_api ``` -------------------------------- ### Launch ASE GUI for Visualization Source: https://github.com/deng-group/mle4217_5219_book/blob/main/models_and_theories_I/ase.ipynb Launches the ASE graphical user interface (GUI) to visualize the structures saved in the 'structures.xyz' file. The `ase gui` command is a command-line tool that opens the visualizer. ```bash !ase gui structures.xyz ``` -------------------------------- ### Set Up Initial Guess, Bounds, and Constraints for Optimization (Python) Source: https://github.com/deng-group/mle4217_5219_book/blob/main/optimization/optimization.ipynb This code segment prepares the necessary inputs for an optimization function. It defines an initial guess `x0`, sets boundary conditions `bnds` for the optimization variables, and structures the constraints `cons` into a format recognized by optimization libraries. The initial objective function value is also printed. ```python #Initial guess x0 = [1,0,5,2] print(objective(x0)) #setting boundries and constraints b = (1.0,5.0) bnds = (b,b,b,b) con1 = {'type': 'ineq', 'fun': constraint1} con2 = {'type': 'eq', 'fun': constraint2} cons = ([con1,con2]) ``` -------------------------------- ### Initiating MACE Model Training from Configuration Source: https://github.com/deng-group/mle4217_5219_book/blob/main/machine_learning_potentials/mlp.ipynb Provides a Python function to initiate the training of a MACE model using a specified configuration file. It clears existing logging handlers and sets the command-line arguments to point to the configuration file before calling the MACE training main function. ```python import warnings warnings.filterwarnings("ignore") from mace.cli.run_train import main as mace_run_train_main import sys import logging def train_mace(config_file_path): logging.getLogger().handlers.clear() sys.argv = ["program", "--config", config_file_path] mace_run_train_main() ``` -------------------------------- ### Install wulffpack (Shell) Source: https://github.com/deng-group/mle4217_5219_book/blob/main/atomistic_structure_II/advanced_structure.ipynb Installs the 'wulffpack' Python package using pip. This package is necessary for constructing Wulff shapes using the SingleCrystal class. ```shell !pip install wulffpack ``` -------------------------------- ### Build Graphene and Visualize Source: https://github.com/deng-group/mle4217_5219_book/blob/main/models_and_theories_I/ase.ipynb Creates a graphene sheet using `ase.build.graphene` and visualizes it using the 'x3d' viewer. The `graphene` function allows specifying the size of the unit cell. This demonstrates building 2D materials. ```python from ase.build import graphene graphene_atoms = graphene(size=(4, 4, 1)) print("Graphene:", graphene_atoms) view(graphene_atoms, viewer='x3d') ``` -------------------------------- ### Install Seaborn for Advanced Visualization Source: https://github.com/deng-group/mle4217_5219_book/blob/main/database/bulk_modulus.ipynb Installs the seaborn library, which provides enhanced data visualization capabilities, including advanced statistical plots. This command is typically run in a terminal or notebook environment. ```bash !pip install seaborn ``` -------------------------------- ### Install Matplotlib and NumPy Source: https://github.com/deng-group/mle4217_5219_book/blob/main/orientation/programming.ipynb Installs the Matplotlib and NumPy libraries, which are essential for plotting and numerical operations in Python. This command is typically run in a Jupyter Notebook or a similar environment. ```shell !pip install matplotlib numpy ``` -------------------------------- ### Setup and Execution of Optimization Algorithms in Python Source: https://github.com/deng-group/mle4217_5219_book/blob/main/figures/original_figs/make_figures.ipynb Sets up parameters for a quadratic function, including an ill-conditioned matrix A and initial guess x0. It then executes both the Gradient Descent and Conjugate Gradient algorithms, storing their results. The paths and function values obtained from both methods are converted to NumPy arrays for subsequent plotting and analysis. ```python # Define the quadratic function parameters - make it ill-conditioned A = np.array([[50, 3], [3, 1]]) # Highly ill-conditioned matrix b = np.array([2, 4]) x0 = np.array([-2, 0]) # Initial guess # Run both optimization methods with adjusted learning rate gd_min_x, gd_path, gd_values, gd_iterations = gradient_descent(A, b, x0, learning_rate=0.03, max_iterations=1000) cg_min_x, cg_path, cg_values, cg_iterations = conjugate_gradient(A, b, x0) # Convert paths to numpy arrays for easier plotting gd_path = np.array(gd_path) cg_path = np.array(cg_path) ``` -------------------------------- ### Clone a Remote Git Repository using Git Source: https://github.com/deng-group/mle4217_5219_book/blob/main/computer/git_examples.md Downloads an existing remote repository to the local machine. Requires the remote repository URL. This is the first step to working on an existing project. ```shell git clone ``` -------------------------------- ### MLP Regression and Classification with Varying Hidden Layers in Python Source: https://github.com/deng-group/mle4217_5219_book/blob/main/figures/original_figs/make_figures.ipynb Implements Multi-Layer Perceptron (MLP) for both regression and classification tasks using scikit-learn. The regression example demonstrates the effect of different hidden layer configurations on predictions for a sine wave dataset. The classification example uses the iris dataset. ```python from sklearn.neural_network import MLPRegressor from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split import torch from sklearn.datasets import make_blobs from sklearn.cluster import KMeans import matplotlib.pyplot as plt import numpy as np # Neural Network for Regression Example # Generate synthetic regression data: y = sin(x) with noise X_reg = np.linspace(0, 10, 200).reshape(-1, 1) y_reg = np.sin(X_reg).ravel() + np.random.normal(0, 0.1, X_reg.shape[0]) # Split into training and testing sets X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X_reg, y_reg, test_size=0.2, random_state=42) # Try a range of hidden layer configurations and collect their predictions hidden_configs = [(40), (20,20),(40, 40), (20,20,20), (40,40,40)] predictions = {} for config in hidden_configs: regressor = MLPRegressor(hidden_layer_sizes=config, activation='relu', max_iter=1000, random_state=42) regressor.fit(X_train_reg, y_train_reg) predictions[config] = regressor.predict(X_test_reg) # Plot the true test values and the predictions for each configuration plt.figure(figsize=(6, 6)) indices = np.argsort(X_test_reg.ravel()) plt.plot(X_test_reg[indices], y_test_reg[indices],'o', color='blue', label='True', alpha=0.6) for config, y_pred in predictions.items(): plt.plot(X_test_reg[indices], y_pred[indices], marker='',ls='-', label=f'{config}') plt.xlabel('X') plt.ylabel('y') plt.title('MLP Regression: Effect of Increasing Hidden Layer Sizes') plt.legend() plt.tight_layout() plt.savefig('nn_regression_hyperparams.pdf') plt.show() # Neural Network for Classification Example # Load the iris dataset iris = load_iris() X_clf = iris.data y_clf = iris.target # Split into training and testing sets X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(X_clf, y_clf, test_size=0.2, random_state=42) # Example of training an MLP Classifier (can be expanded with different configurations) classifier = MLPClassifier(hidden_layer_sizes=(50, 50), max_iter=1000, random_state=42) classifier.fit(X_train_clf, y_train_clf) # Evaluate the classifier (optional) accuracy = classifier.score(X_test_clf, y_test_clf) print(f"MLP Classifier Accuracy on Iris Dataset: {accuracy:.2f}") # To visualize classification boundaries, a similar approach to the first snippet would be needed, # but for a 2D projection of the iris dataset or using only two features. ```