Hut Knowledge Base
website·Indexed 30 days ago
https://aisuandebowen.github.io/Hut/A personal technical knowledge base covering front-end development (JavaScript, Vue 2/3, TypeScript, HTML/CSS, Webpack, Babel), Python ecosystem (NumPy, Pandas, SciPy, Matplotlib, Scikit-learn), and deep learning (PyTorch, CNN, RNN, GCN, RSNN). Includes practical guides on Git, conda, and general software development reflections.
What's inside Hut
- Graph Convolutional Networks (GCN) are designed to process data stored in graph formats (non-Euclidean structures) such as knowledge graphs, social networks, communication networks, and protein molecular structures. Unlike CNNs or LSTMs, which require grid-like data, GCNs operate on generalized topological structures using adjacency matrices to extract features and patterns.
Overview of Recurrent Spiking Neural Networks (RSNN)
Recurrent Spiking Neural Networks (RSNN) combine the biological pulse characteristics of Spiking Neural Networks (SNN) with the recurrent structure of Recurrent Neural Networks (RNN). They are primarily used for processing time-series data, speech recognition, and brain-computer interface (BCI) tasks.Overview of Convolutional Neural Network (CNN) Core Concepts
Convolutional Neural Networks (CNNs) are designed to overcome the limitations of fully connected networks when processing images, specifically addressing high parameter counts, difficulty in capturing local invariant features (scale, translation, rotation), and loss of spatial information during vectorization.
CNNs rely on three primary layers:
- Convolutional Layers: Extract local features from the image.
- Pooling Layers: Reduce dimensionality and parameter count to improve training efficiency.
- Fully Connected Layers: Perform linear transformations to produce the final output.
Key structural advantages include local connectivity, weight sharing (via kernels), and pooling.
Understand Abstract Syntax Trees (AST) for Babel Plugins
An Abstract Syntax Tree (AST) is an abstract representation of the syntactic structure of source code. It represents the programming language's grammar as a tree where each node corresponds to a specific structure in the source code. To visualize and test ASTs, use AST Explorer.Compare Pandas and NumPy for data processing
NumPy and Pandas are both fundamental Python libraries for numerical data, but they serve different primary purposes:
- NumPy: A numerical computing package designed for efficient processing of N-dimensional arrays (matrices). It requires all elements in an array to be of the same type. It supports parallel computing and is written in C for high performance, making it compatible with frameworks like TensorFlow and PyTorch.
- Pandas: A data analysis library built on top of NumPy, primarily used for processing 2D tabular data (DataFrames). Unlike NumPy, Pandas can handle columns of different data types (e.g., one column as integers, another as strings).
Conversion between the two:
- Convert a Pandas DataFrame to a NumPy ndarray: Use
df.values. - Convert a NumPy ndarray to a Pandas DataFrame: Use
pd.DataFrame(array).
# DataFrame to ndarray array = df.values # ndarray to DataFrame import pandas as pd df = pd.DataFrame(array)Vue 2 Lifecycle Hooks Overview
The basic sequence of the Vue 2 lifecycle:
new Vue(): Initializes events and lifecycle.beforeCreate(): Instance created; can access$options, but cannot accessdataor$el.created(): Data observation and computed properties are set up; can accessdata, but cannot access$el.beforeMount(): Occurs aftercreatedand before the component is mounted to the DOM.
Compare MVC, MVP, and MVVM architectural patterns
These patterns manage the separation of concerns between data (Model) and UI (View):
- MVC (Model-View-Controller): The Controller handles application logic and coordinates between View and Model. When the Model changes, it notifies the View via the Observer pattern; the View then requests updated data from the Model.
- MVP (Model-View-Presenter): Similar to MVC, but the Presenter acts as a middleman. When the Model changes, it notifies the Presenter, which then explicitly tells the View to update via a provided interface. This makes the View more componentized and the Presenter easier to unit test.
- MVVM (Model-View-ViewModel): Introduces a 'Binder' for two-way data binding. Changes in the Model are automatically reflected in the View via the Binder, and user interactions in the View automatically update the Model, reducing manual synchronization code.
Implement a basic CNN architecture in PyTorch
TheConv2dModuledefines a CNN that processes single-channel images (like MNIST). It consists of two convolutional layers with ReLU activation and MaxPool2d for downsampling, followed by three fully connected (Linear) layers for classification.import torch.nn as nn class Conv2dModule(nn.Module): def __init__(self, num_classes=10): super(Conv2dModule, self).__init__() # Layer 1: 1 channel -> 32 channels, 3x3 kernel, padding 1 self.Conv2d_1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1) self.MaxPool2d = nn.MaxPool2d(kernel_size=2, stride=2) self.ReLU = nn.ReLU(inplace=True) # Layer 2: 32 channels -> 64 channels, 3x3 kernel, padding 1 self.Conv2d_2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1) # Fully Connected Layers # Input size calculation: 64 channels * 14 * 14 (after pooling) self.fc1 = nn.Linear(64 * 14 * 14, 1024) self.fc2 = nn.Linear(1024, 512) self.fc3 = nn.Linear(512, num_classes) def forward(self, x): x = self.Conv2d_1(x) x = self.ReLU(x) x = self.Conv2d_2(x) x = self.MaxPool2d(x) x = self.ReLU(x) x = x.view(-1, 64 * 14 * 14) # Flatten x = self.fc1(x) x = self.ReLU(x) x = self.fc2(x) x = self.ReLU(x) x = self.fc3(x) return xSave and load PyTorch model parameters using state_dict
To save only the model parameters (weights and biases) without saving the entire model architecture, use thestate_dictmethod. This is the recommended approach as it is more flexible. When loading, you must first instantiate the model architecture before loading the state dictionary into it.import torch # Save parameters torch.save(model.state_dict(), 'model_params.pth') # Load parameters model = init_model() # Initialize the model architecture first model.load_state_dict(torch.load('model_params.pth'))Understand the basic architecture of Recurrent Neural Networks (RNN)
Recurrent Neural Networks (RNN) are designed to handle time-series data by establishing connections between inputs at different time steps. Unlike standard multi-layer perceptrons, the hidden state of an RNN depends on both the current input and the hidden state from the previous time step.
Key components include:
- X: Input layer values.
- U: Weight matrix from input to hidden layer.
- V: Weight matrix from hidden layer to output layer.
- W: Weight matrix connecting the previous hidden state to the current hidden state.
- S: The hidden state vector, which captures temporal dependencies.
Save and load the entire PyTorch model structure and parameters
You can save the entire model object, including its structure and parameters, usingtorch.save(). This allows you to load the model without manually initializing the architecture first.# Save model structure and parameters torch.save(model, 'model_params.pth') # Load model structure and parameters model = torch.load('model_params.pth')Implement shallow and deep copying of objects
Copying objects depends on whether nested structures need to be duplicated:
- Shallow Copy: Copies top-level properties. Use
Object.assign({}, obj1, obj2), the spread operator{...obj1, ...obj2}, orObject.create()to maintain the prototype chain. - Deep Copy: Recursively copies all levels of an object. This is necessary when the object contains other objects or arrays.
// Modern Deep Copy Implementation function cloneDeep(obj) { if (obj instanceof Object) { const res = Array.isArray(obj) ? [] : {}; for (const [key, value] of Object.entries(obj)) { res[key] = cloneDeep(value); } return res; } else { return obj; } }- Shallow Copy: Copies top-level properties. Use