View source: R/r-all-the-things.R
embed_entityrelationspace | R Documentation |
Build a Starspace model for entity relationship completion (graphspace).
embed_entityrelationspace(
x,
model = "graphspace.bin",
early_stopping = 0.75,
useBytes = FALSE,
...
)
x |
a data.frame with columns entity_head, entity_tail and relation indicating the relation between the head and tail entity |
model |
name of the model which will be saved, passed on to |
early_stopping |
the percentage of the data that will be used as training data. If set to a value smaller than 1, 1- |
useBytes |
set to TRUE to avoid re-encoding when writing out train and/or test files. See |
... |
further arguments passed on to |
an object of class textspace
as returned by starspace
.
## Example on Freebase - download the data
filename <- paste(
"https://raw.githubusercontent.com/bnosac-dev/GraphEmbeddings/master/",
"diffbot_data/FB15k/freebase_mtr100_mte100-train.txt",
sep = "")
tmpfile <- tempfile(pattern = "freebase_mtr100_mte100_", fileext = "txt")
ok <- suppressWarnings(try(
download.file(url = filename, destfile = tmpfile),
silent = TRUE))
if(!inherits(ok, "try-error") && ok == 0){
## Build the model on the downloaded data
x <- read.delim(tmpfile, header = FALSE, nrows = 1000,
col.names = c("entity_head", "relation", "entity_tail"),
stringsAsFactors = FALSE)
head(x)
set.seed(123456789)
model <- embed_entityrelationspace(x, dim = 50)
plot(model)
predict(model, "/m/027rn /location/country/form_of_government")
## Also add reverse relation
x_reverse <- x
colnames(x_reverse) <- c("entity_tail", "relation", "entity_head")
x_reverse$relation <- sprintf("REVERSE_%s", x_reverse$relation)
relations <- rbind(x, x_reverse)
set.seed(123456789)
model <- embed_entityrelationspace(relations, dim = 50)
predict(model, "/m/027rn /location/country/form_of_government")
predict(model, "/m/06cx9 REVERSE_/location/country/form_of_government")
}
## cleanup for cran
if(file.exists(tmpfile)) file.remove(tmpfile)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.