How to set up a basic Go project with neural networks?

huangapple go评论80阅读模式
英文:

How to set up a basic Go project with neural networks?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是一个刚开始使用Go语言的新手,需要一些设置方面的帮助。首先是$GOPATH。似乎我所有的Go项目都应该放在机器上的同一个位置?所以我已经设置好了:

$ echo $GOPATH
/sites/gopath

在其中,我设置了推荐的文件夹:

$ ls -a /sites/gopath
.   ..  bin pkg src

src文件夹下,我有一个名为github.com的文件夹,其中有一个名为shamoons的文件夹,里面又有一个名为go-ann-test的文件夹。

在其中,我有一个名为autompg的文件夹,里面有一个名为autompg.go的文件。

哇!这是一个为一个项目做的很多设置!但我要假设我要么做错了,要么这样做是值得的。

在我的autompg.go文件中,我有以下代码:

package main

import (
  "fmt"
  "github.com/fxsjy/gonn"
)

func main() {
  nn := gonn.DefaultNetwork(2,3,1,true)
  inputs := [][]float64{
    []float64{0,0},
    []float64{0,1},
    []float64{1,0},
    []float64{1,1},
  }

  targets := [][]float64{
    []float64{0},//0+0=0
    []float64{1},//0+1=1
    []float64{1},//1+0=1
    []float64{2},//1+1=2
  }

  nn.Train(inputs,targets,1000)

  for _,p := range inputs{
    fmt.Println(nn.Forward(p))
  }
}

当我从go-ann-test文件夹中输入go install autompg时,我得到以下错误信息:

$ go install autompg 
can't load package: package autompg: cannot find package "autompg" in any of:
    /usr/local/Cellar/go/1.4.1/libexec/src/autompg (from $GOROOT)
    /sites/gopath/src/autompg (from $GOPATH)

那么我做错了什么,如何正确设置项目以接受外部包?

英文:

I am super new go Go and need some help setting things up. First, the $GOPATH. It seems that all of my go projects should be in the same place on my machine? So I have that set:

$ echo $GOPATH
/sites/gopath

Inside, I set up the folders recommended:

$ ls -a /sites/gopath
.   ..  bin pkg src

Inside src, I have a folder called github.com, which has a folder in it called shamoons which has a folder in it called go-ann-test.

Inside that, I have a folder called autompg and inside that, I have a file called autompg.go.

Phew! That's a lot of set up for a project! But I'm going assume that I'm either doing it wrong or that it's somehow worth it.

In my autompg.go, I have:

package main

import (
  "fmt"
  "github.com/fxsjy/gonn"
)

func main() {
  nn := gonn.DefaultNetwork(2,3,1,true)
  inputs := [][]float64{
    []float64{0,0},
    []float64{0,1},
    []float64{1,0},
    []float64{1,1},
  }

  targets := [][]float64{
    []float64{0},//0+0=0
    []float64{1},//0+1=1
    []float64{1},//1+0=1
    []float64{2},//1+1=2
  }

  nn.Train(inputs,targets,1000)

  for _,p := range inputs{
    fmt.Println(nn.Forward(p))
  }
}

And when I type go install autompg from my go-ann-test folder, I get:

$ go install autompg 
can't load package: package autompg: cannot find package "autompg" in any of:
	/usr/local/Cellar/go/1.4.1/libexec/src/autompg (from $GOROOT)
	/sites/gopath/src/autompg (from $GOPATH)

So what am I doing wrong and how can I properly set up the project to accept the external package?

答案1

得分: 1

从我的go-ann-test文件夹中执行go install autompg

不,你需要进入go-ann-test/autompg文件夹,然后输入go install

这将会将autompg.go编译成一个可执行文件autompg,并将其放在你的$GOPATH/bin目录下。

最后,你可以将其添加、提交并推送到https://github.com/shamoons/go-ann-test。

OP Shamoon评论中添加了以下内容:

> 我得到了

 autompg.go:35:3: no buildable Go source files in /sites/gopath/src/github.com/fxsjy/gonn

这意味着需要执行go get github.com/fxsjy/gonn来首先导入和编译github.com/fxsjy/gonn

考虑到导入的仓库结构https://github.com/fxsjy/gonn,实际上需要执行以下操作:

cd /sites/gopath/src/github.com/fxsjy/gonn/gonn
go install 

这意味着实际的导入应该是:

import github.com/fxsjy/gonn/gonn
英文:

> go install autompg from my go-ann-test folder:

No, you need to go to the go-ann-test/autompg folder, and there type go install

That will compile autompg.go into an executable autompg and put in in your $GOPATH/bin.

Finally, you can add, commit and push to https://github.com/shamoons/go-ann-test.

The OP Shamoon adds in the comments:

> I get

 autompg.go:35:3: no buildable Go source files in /sites/gopath/src/github.com/fxsjy/gonn

That means a go get github.com/fxsjy/gonn is needed to import and compile github.com/fxsjy/gonn first.

Considering that imported repo structure https://github.com/fxsjy/gonn, it actually needed:

cd /sites/gopath/src/github.com/fxsjy/gonn/gonn
go install 

That means the actual import was:

import github.com/fxsjy/gonn/gonn

huangapple
  • 本文由 发表于 2015年2月7日 04:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/28374371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定