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.

Tokens
37.5K
Snippets
199
Records
246
Agent score
75%

What's inside Hut

  1. Overview of Graph Convolutional Networks (GCN)

    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.
  2. 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.

  3. 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)
  4. Vue 2 Lifecycle Hooks Overview

    The basic sequence of the Vue 2 lifecycle:

    1. new Vue(): Initializes events and lifecycle.
    2. beforeCreate(): Instance created; can access $options, but cannot access data or $el.
    3. created(): Data observation and computed properties are set up; can access data, but cannot access $el.
    4. beforeMount(): Occurs after created and before the component is mounted to the DOM.
  5. 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.
  6. Implement a basic CNN architecture in PyTorch

    The Conv2dModule defines 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 x
  7. Save and load PyTorch model parameters using state_dict

    To save only the model parameters (weights and biases) without saving the entire model architecture, use the state_dict method. 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'))
  8. 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.
  9. Save and load the entire PyTorch model structure and parameters

    You can save the entire model object, including its structure and parameters, using torch.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')
  10. 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}, or Object.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;
      }
    }