【发布时间】:2025-12-10 17:10:01
【问题描述】:
我正在使用有限的 RAM(AWS 免费层 EC2 服务器 - 1GB)。
我有一个相对较大的 txt 文件“vectors.txt”(800mb),我正在尝试读入 R。尝试了各种方法后,我未能将该向量读入内存。
所以,我正在研究分块阅读它的方法。我知道生成的数据帧的暗淡应该是 300K * 300。如果我能够读取文件,例如一次 10K 行,然后将每个块保存为 RDS 文件,我将能够循环遍历结果并获得我需要的内容,尽管与将整个内容放在内存中相比,它的速度稍慢且不方便。
复制:
# Get data
url <- 'https://github.com/eyaler/word2vec-slim/blob/master/GoogleNews-vectors-negative300-SLIM.bin.gz?raw=true'
file <- "GoogleNews-vectors-negative300-SLIM.bin.gz"
download.file(url, file) # takes a few minutes
R.utils::gunzip(file)
# word2vec r library
library(rword2vec)
w2v_gnews <- "GoogleNews-vectors-negative300-SLIM.bin"
bin_to_txt(w2v_gnews,"vector.txt")
到目前为止一切顺利。这是我挣扎的地方:
word_vectors = as.data.frame(read.table("vector.txt",skip = 1, nrows = 10))
返回“无法分配大小为 [size] 的向量”错误消息。
尝试过的替代方案:
word_vectors <- ff::read.table.ffdf(file = "vector.txt", header = TRUE)
相同,内存不足
word_vectors <- readr::read_tsv_chunked("vector.txt",
callback = function(x, i) saveRDS(x, i),
chunk_size = 10000)
导致:
Parsed with column specification:
cols(
`299567 300` = col_character()
)
|=========================================================================================| 100% 817 MB
Error in read_tokens_chunked_(data, callback, chunk_size, tokenizer, col_specs, :
Evaluation error: bad 'file' argument.
还有其他方法可以将vectors.txt 转换为数据框吗?也许通过将它分成几块并读取每一块,保存为数据框然后保存到rds?还是有其他选择?
编辑: 从下面乔纳森的回答中,尝试了:
library(rword2vec)
library(RSQLite)
# Download pre trained Google News word2vec model (Slimmed down version)
# https://github.com/eyaler/word2vec-slim
url <- 'https://github.com/eyaler/word2vec-slim/blob/master/GoogleNews-vectors-negative300-SLIM.bin.gz?raw=true'
file <- "GoogleNews-vectors-negative300-SLIM.bin.gz"
download.file(url, file) # takes a few minutes
R.utils::gunzip(file)
w2v_gnews <- "GoogleNews-vectors-negative300-SLIM.bin"
bin_to_txt(w2v_gnews,"vector.txt")
# from https://privefl.github.io/bigreadr/articles/csv2sqlite.html
csv2sqlite <- function(tsv,
every_nlines,
table_name,
dbname = sub("\\.txt$", ".sqlite", tsv),
...) {
# Prepare reading
con <- RSQLite::dbConnect(RSQLite::SQLite(), dbname)
init <- TRUE
fill_sqlite <- function(df) {
if (init) {
RSQLite::dbCreateTable(con, table_name, df)
init <<- FALSE
}
RSQLite::dbAppendTable(con, table_name, df)
NULL
}
# Read and fill by parts
bigreadr::big_fread1(tsv, every_nlines,
.transform = fill_sqlite,
.combine = unlist,
... = ...)
# Returns
con
}
vectors_data <- csv2sqlite("vector.txt", every_nlines = 1e6, table_name = "vectors")
导致:
Splitting: 12.4 seconds.
Error: nThread >= 1L is not TRUE
【问题讨论】:
标签: r