为了账号安全,请及时绑定邮箱和手机立即绑定

python中的模型训练和Golang中的模型运行,模型导入过程中的问题

python中的模型训练和Golang中的模型运行,模型导入过程中的问题

Go
蝴蝶不菲 2022-06-27 17:32:05
我安装了 TensorFlow (2.3) 的最新版本,在 Python 下运行良好,但在 Golang 下却出现异常:...但不包含包 github.com/tensorflow/tensorflow/tensorflow/go/core/protobuf/for_core_protos_go_proto通过将版本更改为 1.15.0,我让 TensorFlow 与 Golang 一起使用现在,我面临以下问题:使用 TensorFlow 2.3 的 Python 代码import tensorflow as tf    df = pd.read_csv(data_path, sep=';')X = df[df.columns[:8]]y = df[df.columns[8:-1]]X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)    model = tf.keras.Sequential()model.add(tf.keras.layers.Dense(8, activation='relu', name="inputNode"))model.add(tf.keras.layers.Dense(150, activation='relu'))model.add(tf.keras.layers.Dense(3, name="inferNode"))model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])model.fit(X_train, y_train, epochs=500)    tf.keras.models.save_model(model=model, filepath='./', save_format='tf')使用 TensorFlow 1.15.0 的 Golang 代码model, err := tf.LoadSavedModel("./", []string{"serve"}, nil)if err != nil {    fmt.Printf("Error loading saved model: %s\n", err.Error())    return}defer model.Session.Close()data := [][]float32{make([]float32, 8)}data[0][0] = 1.0data[0][1] = 1.0data[0][2] = 1.0data[0][3] = 1.0data[0][4] = 1.0data[0][5] = 1.0data[0][6] = 1.0data[0][7] = 1.0tensor, _ := tf.NewTensor(data)   result, err := model.Session.Run(    map[tf.Output]*tf.Tensor{        model.Graph.Operation("inputNode_input").Output(0): tensor, // Replace this with your input layer name    },    []tf.Output{        model.Graph.Operation("inferNode").Output(0), // Replace this with your output layer name    },    nil,)    if err != nil {    fmt.Printf("Error running the session with input, err: %s\n", err.Error())    return}    fmt.Printf("Result value: %v \n", result[0].Value())golang 抛出这个异常:-- FAIL: TestMlPredict (6.93s)panic: nil-Operation. If the Output was created with a Scope object, see Scope.Err() for details. [recovered]    panic: nil-Operation. If the Output was created with a Scope object, see Scope.Err() for details.
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

实际上,我在我维护的 TensorFlow 的一个分支中修复了第一个问题,其中包含所有已编译的 protobuf。fork 是包的依赖项galeone/tfgo,它使用go mod.

你试试

go get github.com/galeone/tfgo

您将能够使用 tensorflow/go 和 tensorflow/op 包 + tfgo 的所有附加功能。


查看完整回答
反对 回复 2022-06-27
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

操作名称错误。我做了什么来找出这些名字?我执行了cli命令: saved_model_cli show --dir ./ --all

那么 golang 和 tensorflow 2.3 呢?请检查问题:github问题 和自述文件:自述文件


查看完整回答
反对 回复 2022-06-27
  • 2 回答
  • 0 关注
  • 424 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号