Beispiel #1
0
func fillVectors(rels map[string]interface{}) *go2vec.Embeddings {
	keys := sortedKeys(rels)
	embedLen := len(keys)
	vectors := go2vec.NewEmbeddings(embedLen)

	idx := 0
	for _, word := range keys {
		vec := make([]float32, embedLen)
		vec[idx] = 1.0

		vectors.Put(word, vec)

		idx++
	}

	return vectors
}
Beispiel #2
0
func main() {
	flag.Parse()
	if flag.NArg() != 3 {
		flag.Usage()
		os.Exit(1)
	}

	vecs := common.ReadEmbeddingsOrExit(common.Embedding{flag.Arg(0), false, false})
	network := common.ReadModelOrExit(flag.Arg(1), cblas.Implementation{})

	out, err := os.Create(flag.Arg(2))
	common.ExitIfError("Cannot open output vectors for writing: ", err)
	defer out.Close()

	if network.Layers() != 1 {
		fmt.Fprintf(os.Stderr, "Weight file contains %d layers, expected 1", network.Layers())
		os.Exit(1)
	}

	layer := network.Layer(0)
	weights := layer.W()

	if layer.Inputs() != uint(vecs.Size()) {
		fmt.Fprintf(os.Stderr, "Embedding layer and one-hot size mismatch: %d - %d", layer.Inputs(), vecs.Size())
		os.Exit(1)
	}

	mergedVecs := go2vec.NewEmbeddings(int(layer.Outputs()))

	wordIdx := 0
	vec := make([]float32, layer.Outputs())
	vecs.Iterate(func(word string, vector []float32) bool {
		for idx := range vec {
			vec[idx] = weights[uint(idx)*layer.Inputs()+uint(wordIdx)]
		}

		mergedVecs.Put(word, vec)
		wordIdx++
		return true
	})

	writer := bufio.NewWriter(out)
	mergedVecs.Write(writer)
	writer.Flush()
}