sugarme / gotch

Go binding for Pytorch C++ API (libtorch)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Production use for gotch just for inference

NickDatLe opened this issue · comments

I think the best use-case for gotch IMHO is to run inference on a model after it has been trained with python (with pytorch of course) and then using the saved model (.pt file) to run inference.

Is it possible to directly load the model.pt file into Go and run inference from there (I assume yes)? And if so, are there any direct tutorials or instructions on this?

@NickDatLe,

Yes. Look at 'example' folder and closed issues.

I got this working last night:

package main

import (
	"fmt"

	"github.com/sugarme/gotch"
	"github.com/sugarme/gotch/ts"
)

func basicOps() {

	xs := ts.MustRand([]int64{3, 5, 6}, gotch.Float, gotch.CPU)
	fmt.Printf("%8.3f\n", xs)
	fmt.Printf("%i", xs)

	// Basic tensor operations
	ts1 := ts.MustArange(ts.IntScalar(6), gotch.Int64, gotch.CPU).MustView([]int64{2, 3}, true)
	defer ts1.MustDrop()
	ts2 := ts.MustOnes([]int64{3, 4}, gotch.Int64, gotch.CPU)
	defer ts2.MustDrop()

	mul := ts1.MustMatmul(ts2, false)
	defer mul.MustDrop()

	fmt.Printf("ts1:\n%2d", ts1)
	fmt.Printf("ts2:\n%2d", ts2)
	fmt.Printf("mul tensor (ts1 x ts2):\n%2d", mul)

	// In-place operation
	ts3 := ts.MustOnes([]int64{2, 3}, gotch.Float, gotch.CPU)
	fmt.Printf("Before:\n%v", ts3)
	ts3.MustAddScalar_(ts.FloatScalar(2.0))
	fmt.Printf("After (ts3 + 2.0):\n%v", ts3)
}

I'll continue to play around with it. thanks for pointing me to the examples.

Close for now as not a real issue.