main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. var labelName = []string{
  7. "Case_Based",
  8. "Genetic_Algorithms",
  9. "Neural_Networks",
  10. "Probabilistic_Methods",
  11. "Reinforcement_Learning",
  12. "Rule_Learning",
  13. "Theory",
  14. }
  15. var nodeLabel = make(map[int]int)
  16. func main() {
  17. file, err := os.Open("cora/cora.content")
  18. if err != nil {
  19. panic(err)
  20. }
  21. G := MakeGraph()
  22. for i := 0; i < 2708; i++ {
  23. var u int
  24. fmt.Fscan(file, &u)
  25. V := MakeVector(1433)
  26. for j := 0; j < 1433; j++ {
  27. fmt.Fscan(file, &V[j])
  28. }
  29. G.AddNode(u, V)
  30. var label string
  31. fmt.Fscan(file, &label)
  32. for j := 0; j < 7; j++ {
  33. if labelName[j] == label {
  34. nodeLabel[u] = j
  35. break
  36. }
  37. }
  38. if i%100 == 0 {
  39. fmt.Println("reading node", i)
  40. }
  41. }
  42. file.Close()
  43. file, err = os.Open("cora/cora.cites")
  44. if err != nil {
  45. panic(err)
  46. }
  47. for i := 0; i < 5429; i++ {
  48. var u, v int
  49. fmt.Fscan(file, &u, &v)
  50. G.AddEdge(u, v)
  51. }
  52. file.Close()
  53. l := Train(G)
  54. cnt, tot := 0, 0
  55. for u := range G.X {
  56. GetEmbedding(G, u, 2, l)
  57. id, max := 0, 0.
  58. for i := 0; i < 7; i++ {
  59. if l[2].E[0][i] > max {
  60. id, max = i, l[2].E[0][i]
  61. }
  62. }
  63. if nodeLabel[u] == id {
  64. cnt++
  65. }
  66. tot++
  67. }
  68. fmt.Println(cnt, "/", tot)
  69. }