Skip to content

Doesn´t work in Tensorflow #1

@sagagk

Description

@sagagk

I call your function from:


from RevIN import RevIN

%load_ext autoreload
%autoreload 2

revin_layer = RevIN(2)

x=Input(shape=(12,2))
model=revin_layer(x,mode="norm")

model2=LSTM(32,return_sequences=False)(model)
output_layer=Dense(2)(model2)
output_layer1=revin_layer(output_layer,mode="denorm")
model1 = Model(inputs=x, outputs=output_layer1)

model1.summary()

But I obtain error.. I appears to be in the backpropagation phase. The output:

Epoch 1/1000
Tensor("model/rev_in/StopGradient_1:0", shape=(None, 1, 2), dtype=float32)
Tensor("model/rev_in/StopGradient_1:0", shape=(None, 1, 2), dtype=float32)
Tensor("model/rev_in/StopGradient_1:0", shape=(None, 1, 2), dtype=float32)
Tensor("model/rev_in/StopGradient_1:0", shape=(None, 1, 2), dtype=float32)
Tensor("StopGradient_1:0", shape=(None, 1, 2), dtype=float32)

---------------------------------------------------------------------------

InaccessibleTensorError                   Traceback (most recent call last)

[<ipython-input-64-d0481f2f6b0e>](https://localhost:8080/#) in <cell line: 1>()
----> 1 history = model1.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=1000, callbacks=[cp,es], verbose=2)

1 frames

[/usr/lib/python3.10/contextlib.py](https://localhost:8080/#) in __exit__(self, typ, value, traceback)
    140         if typ is None:
    141             try:
--> 142                 next(self.gen)
    143             except StopIteration:
    144                 return False

InaccessibleTensorError: <tf.Tensor 'StopGradient_1:0' shape=(None, 1, 2) dtype=float32> is out of scope and cannot be used here. Use return values, explicit Python locals or TensorFlow collections to access it.
Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.

<tf.Tensor 'StopGradient_1:0' shape=(None, 1, 2) dtype=float32> was defined here:
    File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
(.........)
      return wrapped_call(*new_args, **new_kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/saving/legacy/saved_model/save_impl.py", line 698, in call_and_return_conditional_losses
      call_output = layer_call(*args, **kwargs)
    File "/content/RevIN.py", line 34, in call
      self._get_statistics(inputs)
    File "/content/RevIN.py", line 45, in _get_statistics
      self.stdev = K.stop_gradient(K.sqrt(K.var(x, axis=dim2reduce, keepdims=True) + self.eps))
    File "/usr/local/lib/python3.10/dist-packages/keras/backend.py", line 4716, in stop_gradient
      return tf.stop_gradient(variables)

The tensor <tf.Tensor 'StopGradient_1:0' shape=(None, 1, 2) dtype=float32> cannot be accessed from FuncGraph(name=model_layer_call_and_return_conditional_losses, id=133472755512848), because it was defined in FuncGraph(name=rev_in_layer_call_and_return_conditional_losses, id=133472753537696), which is out of scope.

I see that your simple demo works, but when I train a neural network, appears this "out of scope" error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions