eaplatanios / tensorflow_scala

TensorFlow API for the Scala Programming Language
http://platanios.org/tensorflow_scala/
Apache License 2.0
936 stars 96 forks source link

Error during update weight like a placeholder #70

Closed lucataglia closed 6 years ago

lucataglia commented 6 years ago

@eaplatanios I read that in Python TensorFlow is legal assign value to a weight in the same way that can be done to the placeholders: https://stackoverflow.com/a/43580452/9099269

I'm trying to do the same with your ScalaAPI but I can not find the way, can I ask for your help ? This is the line of scala code that I use to retrive the weight from the graph that I generate with the Python API: val weights = session.graph.getOutputByName("foo_weights/Read/ReadVariableOp:0")

I'm looking something like: session.run(feeds = Map(weights -> <new_value>), fetches = weights) that allow me to update the value of a weight with a value I decide

The error I got is: org.platanios.tensorflow.jni.InvalidArgumentException: Expects arg[0] to be resource but float is provided

eaplatanios commented 6 years ago

@lucaRadicalbit What happens if you use val weights = session.graph.getOutputByName("foo_weights:0") instead?

lucataglia commented 6 years ago

@eaplatanios My apologize, I wrote wrong. By the way I already try both alternatives. To summarize, with the following code:

val we = session.graph.getOutputByName("foo_weights:0")   // < - - - - - -
val valueWe: Tensor = Tensor(0f)

val feeds: Map[Output, Tensor] = Map(we -> valueWe)
val fetches: Seq[Output] = Seq(weights)
session.run(feeds, fetches)

The error is:

Exception in thread "main" org.platanios.tensorflow.jni.InvalidArgumentException: Expects arg[0] to be resource but float is provided
    at org.platanios.tensorflow.jni.Session$.run(Native Method)
    at org.platanios.tensorflow.api.core.client.Session.runHelper(Session.scala:137)
    at org.platanios.tensorflow.api.core.client.Session.run(Session.scala:76)

With this code:

val we = session.graph.getOutputByName("foo_weights/Read/ReadVariableOp:0")   // < - - - - - -
val valueWe: Tensor = Tensor(0f)

val feeds: Map[Output, Tensor] = Map(we -> valueWe)
val fetches: Seq[Output] = Seq(weights)
session.run(feeds, fetches)

the error is:

Exception in thread "main" org.platanios.tensorflow.jni.InvalidArgumentException: foo_weights/Read/ReadVariableOp:0 is both fed and fetched.
    at org.platanios.tensorflow.jni.Session$.run(Native Method)
    at org.platanios.tensorflow.api.core.client.Session.runHelper(Session.scala:137)
    at org.platanios.tensorflow.api.core.client.Session.run(Session.scala:76)
lucataglia commented 6 years ago

Sorry @eaplatanios, any updates ? I don't want to rush you, just this project is quite crucial for my academical work. If any help is appreciated I'm 100% available

eaplatanios commented 6 years ago

@lucaRadicalbit Sorry for the delay but I was away and I was also trying to fix an issue with the precompiled binaries. So, regarding the first error you get: you cannot feed a tensor value to a variable tensor. You can use a placeholder if you want to feed values to it. Otherwise, you can create an assign op for your variable that assigns the value of a placeholder to the variable.

In the second case, you're feeding and fetching the same tensor and that's why you get an error. The weights that you use in Seq(weights) seems to be exactly the same tensor as we. You cannot feed and fetch the same tensor in TensorFlow as that involves no computation. Try doing some computation with weights and then fetching the result of that and it'll work that way. It depends on what you're trying to achieve though overall, with your code.

eaplatanios commented 6 years ago

@lucaRadicalbit Did you manage to finally resolve this issue?

eaplatanios commented 6 years ago

@lucaRadicalbit Given that there's been no response in a while, I'll assume this is resolved and close it. Please reopen if the issue persists. :)

lucataglia commented 6 years ago

@eaplatanios I'm so sorry, I just notice that you answered me. At the moment I'm using the assign OP in order to do the weight update. For now the solutions works, if I'll come back to that I'll let you know.