Technology Sharing

CNN -1 Neural Network - Overview 2

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Neural Network (operator)

Operator: usually refers to the basic mathematical operations used in a neural network layer;

1> Linear layer (Fully Connected Layer)

Also known as a fully connected layer, it is the most common type of layer. It multiplies the input vector by the weight matrix and then adds the bias vector to get the output vector. The linear layer is the basic layer in a neural network, and its main function is to map high-dimensional input to low-dimensional output;

Linear layer example code

import torch
import torch.nn as nn
 
# 定义输入向量的维度
input_size = 10
 
# 定义线性层的输出维度
output_size = 5
 
# 创建线性层
fc_layer = nn.Linear(input_size, output_size)
 
# 创建输入向量
input_vector = torch.randn(1, input_size)  # 假设输入向量的形状为(1, input_size)
 
# 进行前向传播计算
output_vector = fc_layer(input_vector)
 
# 打印输出向量
print(output_vector)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

First, we imported the torch and torch.nn modules. Then, we defined the input vector dimension input_size and the output dimension output_size of the linear layer.

Next, we created a linear layer object named fc_layer using the nn.Linear class. The first argument of the class is the input dimension and the second argument is the output dimension.

Then, we create a random input vector input_vector and pass it to the linear layer for forward propagation. Finally, we print the output vector