Comments (6)
Hey @xiajohnnyxia I just replyed to your email.
However, I'm gonna copy my reply here in order to help you and anyone else that'll face your same problem.
Now, you can easily find out that your problem is in the invocation of op.RandomUniform.
Let's look at the godoc: https://godoc.org/github.com/tensorflow/tensorflow/tensorflow/go/op#RandomUniform
Here's the signature:
func RandomUniform(scope *Scope, shape tf.Output, dtype tf.DataType, optional ...RandomUniformAttr) (output tf.Output)
Here's your invocation:
output := op.RandomUniform(root,T,tf.Float,seed,seed2)
The first parameter is scope *Scope
: you're passing root
that's a *Scope
-> perfect.
The second parameter is shape tf.Output
: the tf.Output is a terrible type. It means "anything". The compiler can't help you raising an error and it will accept any parameter of tf.Output type.
The check if the right parameter is executed during runtime into the tensorflow environment.
We have to read the documentation: shape: The shape of the output tensor.
That's not really helpful if you are not familiar with tensorflow. But instead, if you are familiar with it, you know that the shape (even when using tensorflow in python) is always a list or tuple.
This means, in the context of tensorflow + go, that the shape parameter must be a vector of int32 or int64. (remember that a scalar value is a tensor with shape = () [ in python] that means a shape of []int32{} in go).
You're passing T
as a shape
. Your T
parameter is a placeholder. A scalar placeholder, NOT a vector.
Thus you first have to change the definition of T, making it a vector of integers (with shape 0 -> that means empty tuple in python () -> that means the shape of a scalr value).
T := op.Placeholder(root.SubScope("input"), tf.Int32, op.PlaceholderShape(tf.MakeShape(0)))
Now you should be fine. First error fixed.
Let's go on comparing the signature and your function call:
The third parameter is: dtype tf.DataType
: you're passing tf.Float
that's a tf.DataType
-> perect.
The 4th, 5h, ... parameters are optional ...RandomUniforAttr
: you're passing seed
and seed2
that are RandomUniforAttr
: -> perfect.
There's another problem in your code, in the Go side: when you execute the graph sess.run(...)
you are fetching the value of output
that's a tf.float32
value.
This means that your cast result.Value().([]int32)
will fail.
Instead, you have to cast the resulting interface to []float32
.
Complete code
package testTensorflow
import (
"fmt"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
"testing"
)
func TestRandomUniform(t *testing.T) {
root := op.NewScope()
T := op.Placeholder(root.SubScope("input"), tf.Int32, op.PlaceholderShape(tf.MakeShape(0)))
seed := op.RandomUniformSeed(-1.0)
seed2 := op.RandomUniformSeed2(1.0)
output := op.RandomUniform(root, T, tf.Float, seed, seed2)
graph, err := root.Finalize()
if err != nil {
panic(err.Error())
}
var sess *tf.Session
sess, err = tf.NewSession(graph, &tf.SessionOptions{})
if err != nil {
panic(err.Error())
}
var A *tf.Tensor
if A, err = tf.NewTensor([]int32{1}); err != nil {
panic(err.Error())
}
var results []*tf.Tensor
if results, err = sess.Run(
map[tf.Output]*tf.Tensor{
T: A,
},
[]tf.Output{output}, nil); err != nil {
panic(err.Error())
}
for _, result := range results {
fmt.Println(result.Value().([]float32))
}
}
Any question?
from tfgo.
However, seriously, try to use tfgo
as much as possible instead of using only the op
package.
I know that's not the case where tfgo
could help, but whatever. Also, when you'll become an expert, I'd love to have someone that contribute actively to tfgo
from tfgo.
Thank you for your reply.It is work for me.Is tf.MakeShape
's param's value doesn't matter?
from tfgo.
Yes, tf.MakeShape
is useless when you define a placeholder because the placeholder is overwritten at runtime, thus its shape will be overwritten as well.
In fact you could change your placeholder definiition with:
T := op.Placeholder(root.SubScope("input"), tf.Int32)
and the code will work without any problem.
In the context of placeholder what it matters is the shape of the tensor you feed.
from tfgo.
Thanks for your help.And it work for tfgo too.
from tfgo.
You're welcome!
from tfgo.
Related Issues (20)
- tf.Variable support HOT 1
- windows installation error HOT 5
- loading model HOT 2
- cc1.exe: sorry, unimplemented: 64-bit mode not compiled in HOT 1
- Error: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at lookup_table_op.cc:929 : FAILED_PRECONDITION: Table not initialized. HOT 1
- SparseTensor support HOT 2
- Failed to go get tfgo HOT 3
- tensor example support HOT 6
- Potential memory leak on reloading model HOT 8
- libtensorflow.so: .dynsym local symbol at index 3 (>= sh_info of 3) HOT 2
- Matching between output tensor name and prediction HOT 7
- Thread safety HOT 2
- Panic vs Error HOT 2
- Reading jpg image from disk and passing as input to a pretrained model loaded from disk HOT 4
- Error building "Getting Started" example: rebuild TensorFlow with the appropriate compiler flags HOT 5
- Can I use tfgo for tensorflow 2.11.x? HOT 2
- Convert opencv's `mat` to tfgo's `tensor` HOT 7
- Dark Souls level frustration with compilation project with this library HOT 1
- Discrepancy between python and go outputs HOT 2
- tfgo Object detection
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tfgo.