R语言绘制PCA双标图、碎石图、变量载荷图和变量贡献图

news2024/12/23 14:45:30

1、原论文数据双标图

代码:

setwd("D:/Desktop/0000/R") #更改路径

#导入数据
df <- read.table("Input data.csv", header = T, sep = ",")

# -----------------------------------
#所需的包:
packages <- c("ggplot2", "tidyr", "dplyr", "readr", "ggrepel", "cowplot", "factoextra")
#安装你尚未安装的R包
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}
invisible(lapply(packages, library, character.only = TRUE))

# -----------------------------------
# 设置一些颜色、文字的基础设置
# Colors:
CatCol <- c(
  CSH = "#586158", DBF = "#C46B39", EBF = "#4DD8C0", ENF = "#3885AB", GRA = "#9C4DC4",
  MF = "#C4AA4D", OSH = "#443396", SAV = "#CC99CC", WET = "#88C44D", WSA = "#AB3232"
)
Three_colorblind <- c("#A8AD6F", "#AD6FA8", "#6FA8AD") #c("#809844", "#4f85b0", "#b07495")
graph_elements_dark <- "black"
plot_elements_light <- "gray75"
plot_elements_dark <- "gray25"

# Transparency:
boot_alpha_main <- 0.9
boot_alpha_small <- 0.05

# Text:
# if (n_pcs > 3) {x_angle <- 270; x_adjust <- 0.25} else {x_angle <- 0; x_adjust <- 0} # option to change orientation of x axis text
x_angle <- 0; x_adjust <- 0
title_text <- 9 # Nature Communications: max 7 pt; cowplot multiplier: 1/1.618; 7 pt : 1/1.618 = x pt : 1; x = 7 / 1/1.618; x = 11.326 (round up to integer)
subtitle_text <- 9
normal_text <- 9 # Nature Communications: min 5 pt; cowplot multiplier: 1/1.618; 5 pt : 1/1.618 = x pt : 1; x = 5 / 1/1.618; x = 8.09 (round up to integer)

# Element dimensions:
plot_linewidth <- 0.33
point_shape <- 18
point_size <- 1.5

# Initialize figure lists:
p_biplot <- list(); p_r2 <- list(); p_load <- list(); p_contr <- list(); col_ii <- list()

# Labels:
veg_sub_labels <- c("All Sites", "All Forests", "Evergreen Needle-Forests") 

# -----------------------------------
#选择PCA所需的数据
codes_4_PCA <- c("SITE_ID", "IGBP", "GPPsat", "wLL", "wNmass", "wLMA", "RECOmax") # 选择需要的列数据
#执行筛选
df_subset <- df %>%
  dplyr::select(all_of(codes_4_PCA))
#运行PCA。dplyr::select(-species):将不需要的列数据去除
pca_result <- FactoMineR::PCA(df_subset %>% dplyr::select(-SITE_ID, -IGBP), scale.unit = T, ncp = 10, graph = F)

# -----------------------------------
#绘图
p1<- fviz_pca_biplot(pca_result,
                     axes = c(1, 2),
                     col.ind = df_subset$IGBP, #"grey50",
                     # col.ind = NA, #plot_elements_light, #"white",
                     geom.ind = "point",
                     palette = CatCol,#'futurama',
                     label = "var",
                     col.var = plot_elements_dark,
                     labelsize = 3,
                     repel = TRUE,
                     pointshape = 16,
                     pointsize = 2,
                     alpha.ind = 0.67,
                     arrowsize = 0.5)

# -----------------------------------
# 它是ggplot2对象,我们在此基础上进一步修改一下标注。
p1<-p1+
  labs(title = "",
       x = "PC1",
       y = "PC2",
       fill = "IGBP") +
  guides(fill = guide_legend(title = "")) +
  theme(title = element_blank(),
        text = element_text(size = normal_text),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_text(size = title_text, face = "bold"),
        axis.text = element_text(size = normal_text),
        #plot.margin = unit(c(0, 0, 0, 0), "cm"),
        # legend.position = "none"
        legend.text = element_text(size = subtitle_text),
        legend.key.height = unit(5, "mm"),
        legend.key.width = unit(2, "mm")
  )
p1

参考:Leaf-level coordination principles propagate to the ecosystem scale (https://doi.org/10.1038/s41467-023-39572-5)、主成分分析图。

2、我选用iris数据进行重新绘制测试双标图

代码:

setwd("D:/Desktop/0000/R") #更改路径

#导入数据
df <- read.table("iris1.csv", header = T, sep = ",")

# -----------------------------------
#所需的包:
packages <- c("ggplot2", "tidyr", "dplyr", "readr", "ggrepel", "cowplot", "factoextra")
#安装你尚未安装的R包
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}
invisible(lapply(packages, library, character.only = TRUE))

# -----------------------------------
# 设置一些颜色、文字的基础设置
# Colors:
CatCol <- c(
  setosa = "#586158", versicolor = "#C46B39", virginica = "#4DD8C0") # 设置类别颜色
Three_colorblind <- c("#A8AD6F", "#AD6FA8", "#6FA8AD") #c("#809844", "#4f85b0", "#b07495")
graph_elements_dark <- "black"
plot_elements_light <- "gray75"
plot_elements_dark <- "gray25"

# Transparency:
boot_alpha_main <- 0.9
boot_alpha_small <- 0.05

# Text:
# if (n_pcs > 3) {x_angle <- 270; x_adjust <- 0.25} else {x_angle <- 0; x_adjust <- 0} # option to change orientation of x axis text
x_angle <- 0; x_adjust <- 0
title_text <- 9 # Nature Communications: max 7 pt; cowplot multiplier: 1/1.618; 7 pt : 1/1.618 = x pt : 1; x = 7 / 1/1.618; x = 11.326 (round up to integer)
subtitle_text <- 9
normal_text <- 9 # Nature Communications: min 5 pt; cowplot multiplier: 1/1.618; 5 pt : 1/1.618 = x pt : 1; x = 5 / 1/1.618; x = 8.09 (round up to integer)

# Element dimensions:
plot_linewidth <- 0.33
point_shape <- 18
point_size <- 1.5

# Initialize figure lists:
p_biplot <- list(); p_r2 <- list(); p_load <- list(); p_contr <- list(); col_ii <- list()

# Labels:
veg_sub_labels <- c("All Sites", "All Forests", "Evergreen Needle-Forests") 

# -----------------------------------
#选择PCA所需的数据
codes_4_PCA <- c("sepal_length", "sepal_width", "petal_length", "petal_width", "species") # 选择需要的列数据
#执行筛选
df_subset <- df %>%
  dplyr::select(all_of(codes_4_PCA))
#运行PCA。dplyr::select(-species):将不需要的列数据去除
pca_result <- FactoMineR::PCA(df_subset %>% dplyr::select(-species), scale.unit = T, ncp = 10, graph = F)

# -----------------------------------
#绘图
p1<- fviz_pca_biplot(pca_result,
                     axes = c(1, 2),
                     col.ind = df_subset$species, #"grey50",
                     # col.ind = NA, #plot_elements_light, #"white",
                     geom.ind = "point",
                     palette = CatCol,#'futurama',
                     label = "var",
                     col.var = plot_elements_dark,
                     labelsize = 3,
                     repel = TRUE,
                     pointshape = 16,
                     pointsize = 2,
                     alpha.ind = 0.67,
                     arrowsize = 0.5)

# -----------------------------------
# 它是ggplot2对象,我们在此基础上修改一下标注。
p1<-p1+
  labs(title = "",
       x = "PC1",
       y = "PC2",
       fill = "IGBP") +
  guides(fill = guide_legend(title = "")) +
  theme(title = element_blank(),
        text = element_text(size = normal_text),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_text(size = title_text, face = "bold"),
        axis.text = element_text(size = normal_text),
        #plot.margin = unit(c(0, 0, 0, 0), "cm"),
        # legend.position = "none"
        legend.text = element_text(size = subtitle_text),
        legend.key.height = unit(5, "mm"),
        legend.key.width = unit(2, "mm")
  )
p1

3、iris数据进行绘制碎石图、变量载荷图、变量贡献图

代码:

#加载包
library(dplyr) #用于数据预处理
library(tidyr) #用于数据预处理
library(stringr) #用于字符串处理
library(modelr) #用于自助法重抽样
library(FactoMineR) #用于PCA
library(ade4) #用于PCA
library(factoextra) #用于PCA结果提取及绘图
#所需的包:
packages <- c("ggplot2", "tidyr", "dplyr", "readr", "ggrepel", "cowplot", "factoextra")
#安装你尚未安装的R包
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}
invisible(lapply(packages, library, character.only = TRUE))

setwd("D:/Desktop/0000/R") #更改路径
# 加载数据
df <- read.csv("iris.csv",header = T, row.names = 1) # row.names = 1: 第一列为标签,这时赋值给df时就没有这列了

#重抽样
set.seed(123) #设置随机种子
tt=99 #设置重抽样的次数。iris[,-5]:表示去除第5列,因为这是类别
df_boot <- iris[,-5] %>% modelr::bootstrap(n = tt) #重抽样,结果是一个列表,包含499个数据框

#使用循环对每一个数据集进行PCA
#初始化3个空变量
N_PCS <- tibble() #使用维数检验保留的PC数量
pca_stats <- tibble() #变量的贡献和载荷
R2 <- c() #解释方差占比

#使用循环对每一个数据集进行PCA
#初始化3个空变量
N_PCS <- tibble() #使用维数检验保留的PC数量
pca_stats <- tibble() #变量的贡献和载荷
R2 <- c() #解释方差占比

#循环
for (j in 1:tt) {
  ##提取第j次bootstrap的数据
  dat <- df_boot %>% 
    slice(j) %>% # 选择第j行
    pull(strap) %>% # 提取列表
    as.data.frame() # 提取数据集
  
  #使用FactoMineR包执行PCA
  pca_result <- FactoMineR::PCA(dat, scale.unit = T, ncp = 4, graph = F) # ncp = 4:降维几个主成分,设置最大即为全部
  #使用ade4包执行PCA
  # center:指定是否对数据进行中心化,默认为 TRUE。中心化意味着将数据减去各自的均值,使得数据在每个维度上的平均值为零。
  # scale:指定是否对数据进行缩放,默认为 TRUE。缩放意味着将数据除以各自的标准差,使得数据在每个维度上的标准差为一。
  # scannf:指定是否计算特征值和特征向量,默认为 FALSE。如果设置为 TRUE,则会计算特征值和特征向量。
  pca1 <- ade4::dudi.pca(dat, center = TRUE, scale = TRUE, scannf = FALSE, nf = 4) # nf= 4:降维几个主成分,设置最大即为全部
  #检测不确定性和显著性
  #执行维数检验
  pc_tested <-testdim(pca1, nrepet = 999)
  
  ###提取bootstrap数据集的PCA结果
  N_PCS <- N_PCS %>% 
    bind_rows(tibble(strap = j, n_pcs = pc_tested$nb.cor)) #第j次运行的PCA
  
  pca_stats <- bind_rows(pca_stats,
                         pca_result$var$contrib %>% # add contributions
                           as_tibble(rownames = "var") %>%
                           pivot_longer(cols = !var, names_to = "PC", values_to = "contrib") %>% 
                           left_join(pca_result$var$coord %>% # add loadings
                                       as_tibble(rownames = "var") %>%
                                       pivot_longer(cols = !var, names_to = "PC", values_to = "loading"),
                                     by = c("var", "PC")
                           ) %>% 
                           mutate(PC = str_sub(PC, start = 5), #提取PC名称中的数字
                                  strap = j) # bootstrap run number
  ) #得到变量贡献和载荷
  
  R2 <- bind_rows(R2,
                  tibble(PC = pca_result[["eig"]]%>% rownames(),
                         exp_var = pca_result[["eig"]][,2],
                         strap = j) %>% 
                    mutate(PC = str_sub(PC, start = 6)) #提取PC名称中的数字
  )
}

#保留的PC数量
N_PCS <- N_PCS %>%
  group_by(n_pcs) %>% 
  summarise(n_rep = n()) %>% #对重复值进行计数
  mutate(retained = n_rep / tt * 100) #计算运行次数百分比
pc_ret <- N_PCS %>% filter(retained == max(retained))
#输出结果的摘要
print(paste0("Number of statistical significant components according to Dray method (Dray et al., 2008) was ",pc_ret[1,1], " in ", round(pc_ret[1,3], digits = 1), "% of runs."))


n_pcs <- NA #保留PC数的初始设置
# n_pcs <- 2 #可以手动设置保留PC数
if (is.na(n_pcs)) {
  n_pcs <- N_PCS %>% 
    filter(retained == max(retained)) %>% 
    select(n_pcs) %>% 
    unlist() %>% unname()
} #按照Dray等人的方法设置保留PC数

##变量贡献和载荷
pca_stats <- pca_stats %>% 
  group_by(PC, var) %>% 
  mutate(
    contrib_mean = mean(contrib),
    contrib_median = median(contrib),
    contrib_std = sd(contrib),
    # contrib_q25 = quantile(contrib, 0.25), contrib_q75 = quantile(contrib, 0.75),
    loading_mean = mean(loading),
    loading_median = median(loading),
    loading_std = sd(loading),
    # loading_q25 = quantile(loading, 0.25), loading_q75 = quantile(loading, 0.75)
  ) %>% 
  ungroup() %>% 
  dplyr::rename(contrib_boot = contrib, loading_boot = loading) #重命名以免后续的匹配过程出现混乱

##修改PC名称
pca_stats <- pca_stats %>%
  mutate(PC_name = paste0("PC", PC))

##解释方差占比
R2 <- R2 %>% 
  group_by(PC) %>% 
  mutate(
    R2_mean = mean(exp_var),
    R2_median = median(exp_var),
    R2_std = sd(exp_var),
    # R2_q25 = quantile(exp_var, 0.25), R2_q75 = quantile(exp_var, 0.75)
  ) %>% 
  ungroup() %>% 
  dplyr::rename(R2_boot = exp_var) #重命名以免后续的匹配过程出现混乱

##添加到pca_stats的表格中
pca_stats <- pca_stats %>% left_join(R2, by = c("PC", "strap"))

#对原始数据的PCA
pca_result <- FactoMineR::PCA(iris[,-5], scale.unit = T, ncp = 4, graph = F)

#添加原始数据计算得到的实际值
pca_stats <- pca_stats %>% 
  dplyr::left_join( #添加原始数据的R2(不是bootstrapping的均值)
    tibble(PC = pca_result[["eig"]] %>% rownames(),
           R2 = pca_result[["eig"]][,2]
    ) %>% 
      mutate(PC = str_sub(PC, start = 6)), #提取PC数
    by = "PC"
  ) %>% 
  dplyr::left_join( #添加原始数据的变量贡献(不是bootstrapping的均值)
    pca_result$var$contrib %>% #添加贡献
      as_tibble(rownames = "var") %>%
      pivot_longer(cols = !var, names_to = "PC", values_to = "contrib") %>% 
      mutate(PC = str_sub(PC, start = 5)), #提取PC数
    by = c("PC", "var")
  ) %>%
  dplyr::left_join( #添加原始数据的变量载荷(不是bootstrapping的均值)
    pca_result$var$coord %>% #添加载荷
      as_tibble(rownames = "var") %>%
      pivot_longer(cols = !var, names_to = "PC", values_to = "loading") %>% 
      mutate(PC = str_sub(PC, start = 5)), # extract PC numbers
    by = c("PC", "var")
  )

## 添加PC数的保留百分比(在自助法中PC被保留得有多频繁)
pca_stats <- pca_stats %>% 
  dplyr::left_join(N_PCS %>% dplyr::mutate(PC = n_pcs %>% as.character) %>% dplyr::select(PC, retained),
                   by = "PC"
  ) 

# -----------------------------------
# 绘制图碎石图
dat_boot <- pca_stats %>%
  dplyr::select(PC_name, PC, R2_boot) %>% unique()%>% #去除重复
  dplyr::mutate(PC = as.character(PC))

dat_true <- pca_stats %>%
  dplyr::select(PC_name, PC, R2, R2_median, R2_std) %>% unique() %>% #去除重复
  dplyr::mutate(PC = as.character(PC))

p2 <- ggplot(data = dat_true, aes(x = PC_name, y = R2, group = 1)) + # x = PC -> only numbers on axis, x = PC_name -> can give problems with PC10 being ordered before PC2;
  # group 1 是用来避免某些warning/error的
  geom_errorbar(aes(ymin = R2 - R2_std, ymax = R2 + R2_std),
                color = Three_colorblind[1], linewidth = plot_linewidth, width = 0.4) + # bootstrapping的标准差
  # geom_bar(stat = "identity", position = position_dodge(), fill = Three_colorblind[1], width = 0.61) + #b07a4f, #9c6a5e, #643c3c
  geom_line(color = Three_colorblind[1]) +
  geom_point(color = Three_colorblind[1], size = point_size) + #实际值
  geom_jitter(data = dat_boot, aes(x = PC_name, y = R2_boot, group = 1), alpha = 0.1,
              color = "black", shape = point_shape, size = 0.5, width = 0.1) + #每次自助样本的值
  geom_point(aes(x = PC_name, y = R2_median), color = plot_elements_dark,
             alpha = boot_alpha_main, shape = point_shape, size = point_size) + #添加自助法得到的中位数值
  geom_text(aes(x = PC_name, y = R2 + R2_std + 2, label = paste0(R2 %>% round(digits = 1), "%")),
            nudge_x = 0.33, size = 2) + #添加数值标注
  labs(title = "", x = "", y = "Explained variance") +
  theme_classic() +
  theme(title = element_blank(),
        text = element_text(size = normal_text),
        axis.line = element_line(color = graph_elements_dark),
        axis.ticks.x = element_line(color = graph_elements_dark),
        axis.ticks.y = element_blank(),
        axis.title = element_text(size = title_text, face = "bold"),
        # axis.title.x = element_blank(), #已经在'labs'中指定
        axis.text = element_text(size = normal_text),
        axis.text.y = element_blank(),
        plot.margin = unit(c(0, 1, 0, 1), "cm"),
        legend.position = "none"
  ) +
  NULL
p2

# -----------------------------------
# 绘制变量载荷图
dat_boot <- pca_stats %>%
  dplyr::filter(PC <= n_pcs[1]) %>% #去除额外的PC
  dplyr::select(PC_name, var, loading_boot) %>% unique() #去除重复

dat_true <- pca_stats %>%
  dplyr::filter(PC <= n_pcs[1]) %>% #去除额外的PC
  dplyr::select(PC_name, var, loading, loading_median, loading_std) %>% unique() #去除重复

p3 <- ggplot(data = dat_true, aes(x = var, y = loading)) +
  facet_grid(. ~ PC_name, scales = "free_y") +
  geom_errorbar(aes(ymin = loading - loading_std, ymax = loading + loading_std), # loading_q25, ymax = loading_q75
                color = Three_colorblind[2], linewidth = plot_linewidth, width = 0.9) + # standard error = std from bootstrapping
  geom_bar(stat = "identity", position = position_dodge(), fill = Three_colorblind[2]) + #b07a4f, #9c6a5e, #643c3c
  geom_hline(yintercept = 0, color = graph_elements_dark) +
  geom_jitter(data = dat_boot, aes(x = var, y = loading_boot), alpha = boot_alpha_small, color = plot_elements_dark,
              shape = point_shape, size = 0.2, width = 0.1) + #每次自助抽样的值
  geom_point(aes(x = var, y = loading_median), alpha = boot_alpha_main, shape = point_shape,
             size = point_size, color = plot_elements_dark) + #添加自助法得到的中位数值
  coord_flip() +  #对调坐标轴以更好地展示图形
  scale_y_continuous(breaks = waiver(), n.breaks = 4) + #修改x轴(对调后,这就是y轴)
  labs(y = "Loadings", x = "", title = "") +
  theme_classic() +
  theme(title = element_text(size = normal_text, face = "bold"),
        text = element_text(size = normal_text),
        axis.line.x = element_line(color = graph_elements_dark),
        axis.line.y = element_blank(),
        axis.ticks.x = element_line(color = graph_elements_dark),
        axis.ticks.y = element_blank(),
        axis.title = element_text(size = title_text),
        axis.text = element_text(size = normal_text),
        axis.text.x = element_text(angle = x_angle, vjust = x_adjust),
        legend.position = "none",
        legend.title = element_text(size = title_text),
        legend.text = element_text(size = subtitle_text),
        legend.key.height = unit(1.0, "mm"),
        legend.key.width = unit(1.0, "mm"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        strip.text = element_text(face = "bold", size = title_text),
        strip.background = element_blank()
  ) +
  NULL
p3

# -----------------------------------
# 绘制变量贡献图
dat_boot <- pca_stats %>%
  dplyr::filter(PC <= n_pcs[1]) %>% #去除额外的PC
  dplyr::select(PC_name, var, contrib_boot) %>% unique() #去除重复

dat_true <- pca_stats %>%
  dplyr::filter(PC <= n_pcs[1]) %>% # remove additional PCs
  dplyr::select(PC_name, var, contrib, contrib_median, contrib_std) %>% unique() #去除重复

p4<- ggplot(data = dat_true, aes(x = var, y = contrib)) +
  facet_grid(. ~ PC_name, scales = "free_y") +
  geom_errorbar(aes(ymin = contrib_median - contrib_std, ymax = contrib_median + contrib_std), # ymin = contrib_q25, ymax = contrib_q75
                color = Three_colorblind[3], linewidth = plot_linewidth, width = 0.9) + # standard error = standard deviation from bootstrapping
  geom_bar(stat = "identity", position = position_dodge(), fill = Three_colorblind[3]) + #4f85b0, #59918e, #3c6464
  geom_hline(yintercept = 0, color = graph_elements_dark) +
  geom_jitter(data = dat_boot, aes(x = var, y = contrib_boot), alpha = boot_alpha_small, color = plot_elements_dark,
              shape = point_shape, size = 0.2, width = 0.1) + #每次自助抽样的值
  geom_point(aes(x = var, y = contrib_median), alpha = boot_alpha_main, shape = point_shape,
             size = point_size, color = plot_elements_dark) + #添加自助法得到的中位数值
  coord_flip() + #对调坐标轴以更好地展示图形
  scale_y_continuous(breaks = waiver(), n.breaks = 4) + #添加自助法得到的中位数值
  labs(y = "Contribution [%]", x = "", title = "") +
  theme_classic() +
  theme(title = element_text(size = normal_text, face = "bold"),
        text = element_text(size = normal_text),
        axis.line.x = element_line(color = graph_elements_dark),
        axis.line.y = element_blank(),
        axis.ticks.x = element_line(color = graph_elements_dark),
        axis.ticks.y = element_blank(),
        axis.title = element_text(size = title_text),
        axis.text = element_text(size = normal_text),
        axis.text.x = element_text(angle = x_angle, vjust = x_adjust),
        legend.position = "none",
        legend.title = element_text(size = title_text),
        legend.text = element_text(size = subtitle_text),
        legend.key.height = unit(1.0, "mm"),
        legend.key.width = unit(1.0, "mm"),
        # plot.margin = unit(c(0, 0, 0, 0), "cm"),
        strip.text = element_text(face = "bold", size = title_text),
        strip.background = element_blank()
  ) +
  NULL
p4

# -----------------------------------
# 拼图
library(patchwork)
p2+p3/p4

文中用到的数据代码:R语言绘制 PCA 双标图、碎石图、变量载荷图和变量贡献图(self).zip - 蓝奏云

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1018617.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SkyWalking快速上手(一)——安装单机版SkyWalking、使用SkyWalking

文章目录 什么是SkyWalking为什么选择SkyWalking安装步骤前置条件环境要求下载 SkyWalking 配置 SkyWalkingSkywalking 使用Agent 配置Collector 配置 启动 SkyWalking配置SkyWalking代理 SkyWalking的监控功能分布式调用链追踪性能指标监控告警和报警 总结 什么是SkyWalking …

自然语言处理实战项目18-NLP模型训练中的Logits与损失函数的计算应用项目

大家好,我是微学AI,今天给大家介绍一下,自然语言处理实战项目18-NLP模型训练中的Logits与损失函数的计算应用项目,在NLP模型训练中,Logits常用于计算损失函数并进行优化。损失函数的计算是用来衡量模型预测结果与真实标签之间的差异,从而指导模型参数的更新。 Logits是模…

Jenkins自动化:简化部署流程

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

[JAVEee]SpringBoot项目的创建

SpringBoot可以更好的开发Spring项目,本文章将使用idea社区版来演示创建项目的过程与注意事项. SpringBoot的优点 SpringBoot中内置快速添加依赖的功能,能够便捷的集成各种框架,帮助开发.内置运行容器,无需配置Tomcat容器等其他web容器,可直接进行项目的部署与运行.更好的使用…

解决秋叶整合包(绘世2.2.19)isnet Pro无法抠图的问题

查看错误输出 ImportError: Imageio Pillow requires Pillow, not PIL!解决方法&#xff1a; 更新新的pilow版本 进入秋叶整合包下的python&#xff0c;用shift右键 运行指令&#xff1a;&#xff08;注意./是必须的&#xff09; ./python -m pip list找到pillow发现版本低…

微服务保护-流量控制1

个人名片&#xff1a; 博主&#xff1a;酒徒ᝰ. 个人简介&#xff1a;沉醉在酒中&#xff0c;借着一股酒劲&#xff0c;去拼搏一个未来。 本篇励志&#xff1a;三人行&#xff0c;必有我师焉。 本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》&#xff0c;SpringCloud…

Learn Prompt-角色扮演

模拟面试​ 当你在新闻中读到更多关于ChatGPT的内容时&#xff0c;你会听说ChatGPT可以代替医生、面试官、教师、律师等。但如果你想在实践中使用它&#xff0c;除了使用简单的提示或例子&#xff0c;你还可以根据不同的场景为ChatGPT设置不同的角色&#xff0c;这样我们就可以…

基于matlab寻找并显示一维数组t中的素数

一、方法介绍 首先&#xff0c;要编写一个寻找素数的子函数&#xff1a; function primeNumbers findPrimeNumbers(t)primeNumbers [];for i 1:length(t)num t(i);isPrime true;if num < 1isPrime false;elsefor j 2:sqrt(num)if mod(num, j) 0isPrime false;brea…

Learn Prompt-“标准“提示

在前面的教程中&#xff0c;我们介绍了指令输入的简单提示&#xff0c;提供实例的提示和角色扮演类的提示&#xff0c;那么是否有一个公式来列出提示的各个部分&#xff0c;并将其组合成一个标准化的提示&#xff1f;答案是肯定的。 角色扮演&#xff08;Role&#xff09; 指令…

Ae 效果:CC Hair

模拟/CC Hair Simulation/CC Hair CC Hair&#xff08;CC 毛发&#xff09;可以在源图像上模拟生成毛发、绒线等&#xff0c;并可调整它们的长度、方向、重量等属性&#xff0c;从而创建出非常独特的效果。 CC Hair 本质上是基于 Alpha 通道来生成毛发&#xff0c;无毛发处将变…

多款大模型向公众开放,百模大战再升级?

作为一种使用大量文本数据训练的深度学习模型&#xff0c;大模型可以生成自然语言文本或理解语言文本的含义&#xff0c;是通向人工智能的一条重要途径。大模型可以应用于各种机器学习任务&#xff0c;包括自然语言处理、计算机视觉、语音识别、机器翻译、推荐系统、强化学习等…

【电子元件】常用电子元器件的识别之二极管

目录 前言1. 二极管的识别1.1 普通整流二极管/开关二极管的识别1.2 快速恢复二极管的识别1.3 肖特基二极管的识别1.4 稳压(齐纳)二极管的识别1.5 发光二极管的识别1.6 整流桥的识别1.7 检波二极管的识别1.8 变容二极管的识别1.9 双向触发二极管的识别1.10 二极管在电路图中的…

【记录】深度学习环境配置(pytorch版)

1080面对Transformer连勉强也算不上了&#xff0c;还是要去用小组公用的卡 完整记一个环境配置&#xff0c;方便后面自用✍️ nvidia-smi查看GPU信息 ** CUDA版本12.2 conda -V查询conda版本 22.9.0 新建conda环境 准备装python3.8 conda create --name caiman python3.8.2激…

五层网络模型

分层的意义 当遇到一个复杂问题的时候&#xff0c;可以使用分层的思想把问题简单化 比如&#xff0c;你有半杯82年的可乐&#xff0c;想分享给你的朋友王富贵&#xff0c;但你们已经10年没有联系了。要完成这件事&#xff0c;你可能要考虑&#xff1a; 我用什么装可乐&#x…

shell脚本学习教程(一)

shell脚本学习 一、什么是 Shell&#xff1f;1. shell概述2. Shell 的分类3. 第一个shell脚本4. 多命令执行 二、Shell 变量3.1 变量的命名规则3.2 变量的特殊符号3.3 用户自定义变量3.4 环境变量3.5 位置参数变量3.6 预定义变量3.7 接受键盘输入 三、Shell 运算符3.1 算术运算…

NSSCTF web 刷题记录2

文章目录 前言题目[广东强网杯 2021 团队组]love_Pokemon[NCTF 2018]Easy_Audit[安洵杯 2019]easy_web[NCTF 2018]全球最大交友网站prize_p2[羊城杯 2020]easyser[FBCTF 2019]rceservice方法一方法二 前言 今天是2023年9月13号&#xff0c;刷题记录2正式开始。时间来到九月十七…

Doris数据库FE——SQL 接收

SQL 接收 首先看定义在fe/fe-core/src/main/java/org/apache/doris/qe/QeService.java文件中的public class QeService类&#xff0c;该类is the encapsulation of the entire front-end service, including the creation of services that support the MySQL protocol是整个前…

89 # express 构建 layer 和 route 的关系

上一节实现了实现应用和路由的分离&#xff0c;这一节来构建 layer 和 route 的关系 先看个例子如下&#xff1a;路由中间件&#xff0c;将处理的逻辑拆分成一个个的模块 const express require("express"); const app express();app.get("/",(req, re…

大语言模型之十-Byte Pair Encoding

Tokenizer 诸如GPT-3/4以及LlaMA/LlaMA2大语言模型都采用了token的作为模型的输入输出&#xff0c;其输入是文本&#xff0c;然后将文本转为token&#xff08;正整数&#xff09;&#xff0c;然后从一串token&#xff08;对应于文本&#xff09;预测下一个token。 进入OpenAI官…

六、数学建模之插值与拟合

1.概念 2.例题和matlab代码求解 一、概念 1.插值 &#xff08;1&#xff09;定义&#xff1a;插值是数学和统计学中的一种技术&#xff0c;用于估算在已知数据点之间的未知数据点的值。插值的目标是通过已知数据点之间的某种函数或方法来估计中间位置的数值。插值通常用于数…