alpha-davidson / TensorBNN

Full Bayesian inference for neural networks using TensorFlow
https://alpha-davidson.github.io/TensorBNN/
MIT License
15 stars 7 forks source link

TypeError: An op outside of the function building code is being passed a "Graph" tensor #2

Closed akgopan closed 4 years ago

akgopan commented 4 years ago

When running the regression example, I get the following error:

TypeError: An op outside of the function building code is being passed a "Graph" tensor. It is possible to have Graph tensors leak out of the function building context by including a tf.init_scope in your function building code. For example, the following function will fail: @tf.function def has_init_scope(): my_constant = tf.constant(1.) with tf.init_scope(): added = my_constant * 2 The graph tensor has name: hypers_1:0

On searching about this error, it seems that this is due to how the states are being assigned ([https://github.com/tensorflow/tensorflow/issues/32889]).

Please advise if this is something that can be easily fixed?

I am running it in Google Colabs, with GPU. I am using tensorflow 2.3.0 Ipython code attached. TensorBNN_example.zip

brkronheim commented 4 years ago

Hello, thank you for using this package and bringing this issue to my attention. The error message was the result of a few errant @tf.function decorators in the layer object, which have since been removed. If you try to run the Google Colab again with the most recent version of TensorBNN it should work. If you have any other issues please let us know.

Also, just so you are aware, when you actually get the code to run the percent error on the training data will be Inf on account of some of the y-values be 0. This is to be expected, but you can avoid the issue by using 31 training points instead, using the following code to generate your training data.

    trainIn=np.linspace(-2,2,num=31)
    valIn=np.linspace(-2+2/30,2.0-2/30,num=30)
    trainOut = np.sin(trainIn*math.pi*2)*trainIn-np.cos(trainIn*math.pi)
    valOut = np.sin(valIn*math.pi*2)*valIn-np.cos(valIn*math.pi)