Notes, summaries, assignments, exams, and problems for Computers

Sort by
Subject
Level

Tensors and Variables in PyTorch and TensorFlow

Classified in Computers

Written on in English with a size of 2.4 KB

Tensors and Variables in PyTorch and TensorFlow

Here's a brief explanation of tensors and variables in the context of deep learning frameworks like PyTorch and TensorFlow:

Tensors

  • Definition: A tensor is a multi-dimensional array used to represent data (such as scalars, vectors, matrices, or higher-dimensional data).
  • Common Operations: Tensors can be manipulated with mathematical operations (addition, multiplication, etc.), reshaped, sliced, etc.

In PyTorch, tensors are the core data structure:

import torch
# Create a tensor
a = torch.tensor([[1, 2], [3, 4]])
# Basic operations
b = a + 2         # Adds 2 to each element
c = a * a         # Element-wise multiplication
d = a @ a         # Matrix multiplication

Output:

Tensor `b`: [[3, 4], [5, 6]]
Tensor
... Continue reading "Tensors and Variables in PyTorch and TensorFlow" »

C++ Concepts: Exception Handling to Friend Functions

Classified in Computers

Written on in English with a size of 3.16 KB

Exception Handling

#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
    try {
        int numerator = 10;
        int denominator = 0;
        int res;
        if (denominator == 0) {
            throw runtime_error("Division by zero not allowed!");
        }
        res = numerator / denominator;
        cout << "Result after division: " << res << endl;
    }
    catch (const exception& e) {
        cout << "Exception " << e.what() << endl;
    }
    return 0;
}

Operator Overloading

#include <iostream>
using namespace std;
class Test {
private:
    int num;
public:
    Test(): num(8){}
    void operator ++() {
        num = num + 2;
    }
    void Print() {
... Continue reading "C++ Concepts: Exception Handling to Friend Functions" »

CUDA Matrix Multiplication: Shared Memory

Classified in Computers

Written on in English with a size of 3.23 KB

CUDA Matrix Multiplication Using Shared Memory

This code demonstrates matrix multiplication in CUDA, leveraging shared memory for optimization. It includes two examples: a kernel using shared memory and a host-side implementation using the Thrust library.

CUDA Kernel with Shared Memory

The following CUDA kernel performs matrix multiplication using shared memory to optimize data access:


__global__ void matMulShared(int *A, int *B, int *C, int rowsA, int colsA, int colsB) {
    __shared__ int tile_A[TILE_SIZE][TILE_SIZE], tile_B[TILE_SIZE][TILE_SIZE];
    int row = blockIdx.y * TILE_SIZE + threadIdx.y, col = blockIdx.x * TILE_SIZE + threadIdx.x, temp = 0;
    for (int i = 0; i < (colsA + TILE_SIZE - 1) / TILE_SIZE; ++i) {
        if (row <
... Continue reading "CUDA Matrix Multiplication: Shared Memory" »

Nmap, Netcat, and Metasploit Commands Cheat Sheet

Classified in Computers

Written on in English with a size of 7.67 KB

Nmap Options

-PE: Quickly check if host is up.

-sn: Disable port scanning (host discovery).

-n: Disables DNS resolution (checks IP online without looking up hostnames).

-O: OS detection.

-A: OS detection, Version detection, Script scanning, traceroute.

-sV: Service detection (banner info, version).

-vV: Provides verbose output.

-sC: Scan with default scripts for additional info gathering.

--min-rate=5000: Ensures scan sends at least 5k packets per second.

nmap --script smb-enum-shares.nse -p 445 (ip): List shares and their properties.


To see scripts starting with X: ls /path/X

To execute script with script tracing: sudo nmap -script=smb-os-discovery -script-trace target_ip

To enumerate the SMB share files: sudo nmap -script=smb-enum-shares target_ip

Vulnerability... Continue reading "Nmap, Netcat, and Metasploit Commands Cheat Sheet" »

Java Code Examples: User Name Generation and Repair Scheduling

Classified in Computers

Written on in English with a size of 6.34 KB

This document presents Java code snippets demonstrating two distinct functionalities: user name generation and management, and a car repair scheduling system. Each section includes method implementations with explanations of their purpose and logic.

User Name Management System

The UserName class is designed to generate and manage potential user names based on a user's first and last names. It also provides functionality to filter out names that are already in use.

UserName Class Constructor

This constructor initializes a UserName object, populating a list of possible user names. It assumes that firstName and lastName are valid strings containing only letters and have a length greater than zero.

import java.util.ArrayList;

public class UserName {

... Continue reading "Java Code Examples: User Name Generation and Repair Scheduling" »

BPSK and QPSK Modulation Techniques with Python

Classified in Computers

Written on in English with a size of 4.97 KB

BPSK Signal Generation

This section demonstrates the generation of a Binary Phase Shift Keying (BPSK) signal using Python.

import numpy as np
import matplotlib.pyplot as plt

def bpsk_detect(modulated_signal, carrier):
    return np.sign(modulated_signal * carrier)

message_frequency = 10
carrier_frequency = 20
sampling_frequency = 30 * carrier_frequency
t = np.arange(0, 4/carrier_frequency, 1/sampling_frequency)
message = np.sign(np.cos(2 * np.pi * message_frequency * t) + np.random.normal(scale = 0.01, size = len(t)))
carrier = np.cos(2 * np.pi * sampling_frequency/carrier_frequency * t)
modulated_signal = carrier * message
detected_message = bpsk_detect(modulated_signal, carrier)

plt.figure(figsize=(12, 8))
plt.subplot(4, 1, 1)
plt.plot(t,
... Continue reading "BPSK and QPSK Modulation Techniques with Python" »

Core Concepts in Compiler Design and Language Runtime

Classified in Computers

Written on in English with a size of 10.45 KB

Core Concepts in Compiler Design

Compiler

Compiler: Translates entire source code to target code before execution. It requires a full parse and upfront error checking, then executes the generated target code.

Interpreter

Interpreter: Executes source code incrementally (line-by-line or statement-by-statement). It translates and executes on the fly, and may partially execute ill-formed programs until an error is encountered.

LVar vs. x86 Architecture

LVar: Features nested expressions, implicit control flow (represented by an Abstract Syntax Tree - AST), and an unlimited number of logical variables.

x86: Characterized by flat instructions, atomic operands (registers/memory), explicit control flow (jumps), and a limited set of registers. Compilation passes... Continue reading "Core Concepts in Compiler Design and Language Runtime" »

Digital Logic Circuits: Flip-Flops, Comparators, Decoders, and Registers

Classified in Computers

Written on in English with a size of 4.04 KB

Digital Logic Circuits

Flip-Flops

Clocked RS Flip-Flop Drawbacks

Clocked RS flip-flops have some drawbacks, such as susceptibility to race conditions, where the output can become unpredictable if the inputs change too close to the clock edge. They also require careful handling of the inputs to avoid metastability issues, which can lead to incorrect output states. Additionally, they can have higher power consumption compared to other flip-flop types due to the need for a clock signal.

JK Flip-Flop Operation

Content about JK Flip-Flop operation, characteristic table, characteristics equation, circuit diagram, and timing diagram would be added here.

Magnitude Comparator

What is a Magnitude Comparator?

A magnitude comparator is a digital circuit that compares... Continue reading "Digital Logic Circuits: Flip-Flops, Comparators, Decoders, and Registers" »

Software Design Principles and Patterns

Classified in Computers

Written on in English with a size of 4.98 MB

Lecture 2: Dynamic Dispatch and Interfaces

  • Dynamic Dispatch: The process of selecting which implementation of a polymorphic operation to call at runtime.
  • Interface: Calling a method that is not in the interface will cause a compilation error.

Lecture 3: N/A

Lecture 4: Method Contracts, Exceptions, and Unit Testing

  • Method Contract: Should define pre/post conditions and exceptional behavior. The client is to blame if the precondition is not met, and the service is to blame if the postcondition is not met. Exceptional behavior specifies what the code will do if a precondition is violated.
  • Exception: Runtime exception (unchecked) and IO exception (checked). The IO exception must be caught; otherwise, the code won't compile.
  • Unit Test: Test boundary
... Continue reading "Software Design Principles and Patterns" »

C++ Priority Queue Implementation: Code & Explanation

Classified in Computers

Written on in English with a size of 3.58 KB

C++ Priority Queue Implementation

This document provides a C++ implementation of a priority queue using a heap data structure. The code includes the class definition, member functions, and supporting utilities.

Priority Queue Class Definition


#ifndef priority_queue_h_
#define priority_queue_h_

#include <iostream>
#include <vector>
#include <cassert>

template <class T>
class priority_queue {
private:
    std::vector<T> m_heap;

public:
    priority_queue() {}

    priority_queue(std::vector<T> const& values)
    {
        m_heap = values;
        for (int i = 0; i < m_heap.size(); i++){
            percolate_down(i);
            for (int j = i; j < m_heap.size(); j++){
                percolate_down(
... Continue reading "C++ Priority Queue Implementation: Code & Explanation" »