Code Monkey home page Code Monkey logo

Comments (6)

galeone avatar galeone commented on August 20, 2024

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 Tparameter 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.

galeone avatar galeone commented on August 20, 2024

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.

xiajohnnyxia avatar xiajohnnyxia commented on August 20, 2024

Thank you for your reply.It is work for me.Is tf.MakeShape 's param's value doesn't matter?

from tfgo.

galeone avatar galeone commented on August 20, 2024

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.

xiajohnnyxia avatar xiajohnnyxia commented on August 20, 2024

Thanks for your help.And it work for tfgo too.

from tfgo.

galeone avatar galeone commented on August 20, 2024

You're welcome!

from tfgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.