库中持续出现 Tensorflow 错误,我对同一函数的两个实例使用相同的输入

问题描述 投票:0回答:0

我正在尝试使用 TensorFlow 实现 https://github.com/ts-kim/RevIN 的 ReVin 功能。但是,我得到了相同的“超出范围”错误。我打印了一些标志,但是,我想在反向传播中出现了这个错误:

XXXXXXXXXX
[[[0.328842312 0.5]]

 [[0.32185629 0.5]]

 [[0.316616774 0.50000006]]

 ...

 [[0.291666657 0.50000006]]

 [[0.277694613 0.50000006]]

 [[0.274700612 0.5]]]
YYYYYYYYY
[[[0.328842312 0.5]]

 [[0.32185629 0.5]]

 [[0.316616774 0.50000006]]

 ...

 [[0.291666657 0.50000006]]

 [[0.277694613 0.50000006]]

 [[0.274700612 0.5]]]
AAAAAAA
[[[0.328842312 0.5]]

 [[0.32185629 0.5]]

 [[0.316616774 0.50000006]]

 ...

 [[0.291666657 0.50000006]]

 [[0.277694613 0.50000006]]

 [[0.274700612 0.5]]]
BBBBBBB
[[[0.0613426 0.341575056]]

 [[0.0573474802 0.341575056]]

 [[0.0628619641 0.341575056]]

 ...

 [[0.0749944896 0.341575056]]

 [[0.0566082373 0.341575056]]

 [[0.0575896166 0.341575056]]]
CCCCCCC
DDDDDDD
[[[0.328842312 0.5]]

 [[0.32185629 0.5]]

 [[0.316616774 0.50000006]]

 ...

 [[0.291666657 0.50000006]]

 [[0.277694613 0.50000006]]

 [[0.274700612 0.5]]]


InaccessibleTensorError                   Traceback (most recent call last)

<ipython-input-112-d0481f2f6b0e> 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 in __exit__(self, typ, value, traceback)
    140         if typ is None:
    141             try:
--> 142                 next(self.gen)
    143             except StopIteration:
    144                 return False

InaccessibleTensorError: Exception encountered when calling layer 'rev_in_13' (type RevIN).

<tf.Tensor 'Mean: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.

(.....)
      call_output = layer_call(*args, **kwargs)
    File "/content/RevIN.py", line 27, in call
      mean, stdev = self._get_statistics(x)
    File "/content/RevIN.py", line 58, in _get_statistics
      mean = tf.reduce_mean(x, axis=[i for i in range(1, len(x.shape) - 1)], keepdims=True)

The tensor <tf.Tensor 'Mean:0' shape=(None, 1, 2) dtype=float32> cannot be accessed from FuncGraph(name=model_9_layer_call_and_return_conditional_losses, id=134545682327840), because it was defined in FuncGraph(name=rev_in_13_layer_call_and_return_conditional_losses, id=134545682318048), which is out of scope.

Call arguments received by layer 'rev_in_13' (type RevIN):
  • args=('tf.Tensor(shape=(None, 2), dtype=float32)',)
  • kwargs={'mode': "'denorm'"}


代码:


from RevIN import RevIN

%load_ext autoreload
%autoreload 2

#tf.enable_eager_execution()

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)

我称之为典型的编译和拟合:

history = model1.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=1000, callbacks=[cp,es], verbose=2)

Revin函数中的代码为:

import tensorflow as tf
from tensorflow.keras import layers
import tensorflow.keras.backend as K

class RevIN(tf.keras.layers.Layer):
      def __init__(self, num_features, eps=1e-5, affine=True, **kwargs):
          """
          :param num_features: the number of features or channels
          :param eps: a value added for numerical stability
          :param affine: if True, RevIN has learnable affine parameters
          """
          super(RevIN, self).__init__(**kwargs)
          self.num_features = num_features
          self.eps = eps
          self.affine = affine
          if self.affine:
              print("XXXXXXXXXXX")
              self._init_params()
          else:
              print("YYYYYYYYYYY")
              self.affine_weight = None
              self.affine_bias = None

      def call(self, x, mode):
          x = tf.convert_to_tensor(x, dtype=self.dtype)  # Ensure the input tensor is converted to the correct dtype
          if mode == 'norm':
              mean, stdev = self._get_statistics(x)
              self.mean=mean
              self.stdev=stdev
              x = self._normalize(x, self.mean, self.stdev)
          elif mode == 'denorm':
              x = self._denormalize(x,self.mean,self.stdev)
          else:
              raise NotImplementedError
          return x

      def _init_params(self):
          # initialize RevIN params: (C,)
          self.affine_weight = self.add_weight(name='affine_weight',
                                              shape=(self.num_features,),
                                              initializer='ones',
                                              trainable=True)
          self.affine_bias = self.add_weight(name='affine_bias',
                                            shape=(self.num_features,),
                                            initializer='zeros',
                                            trainable=True)
          #self.mean=tf.Variable([],trainable=False)
          #self.stdev=tf.Variable(None,trainable=False)

      # def _get_statistics(self, x):
      #     mean = tf.stop_gradient(tf.reduce_mean(x, axis=[i for i in range(1, len(x.shape) - 1)], keepdims=True))
      #     stdev = tf.stop_gradient(tf.math.reduce_std(x, axis=[i for i in range(1, len(x.shape) - 1)], keepdims=True) + self.eps)
      #     self.mean=mean
      #     self.stdev=stdev
      #     return mean, stdev
      
      def _get_statistics(self, x):
          mean = tf.reduce_mean(x, axis=[i for i in range(1, len(x.shape) - 1)], keepdims=True)
          stdev = tf.math.reduce_std(x, axis=[i for i in range(1, len(x.shape) - 1)], keepdims=True) + self.eps
          #self.mean = K.stop_gradient(mean)
          #self.stdev = K.stop_gradient(stdev)
          return mean, stdev

      def _normalize(self, x, mean, stdev):
          tf.print("XXXXXXXXXX")
          tf.print(self.mean)
          tf.print("YYYYYYYYY")
          tf.print(mean)
          x = x - mean
          x = x / stdev
          if self.affine:
              x = x * self.affine_weight
              x = x + self.affine_bias
          return x

      def _denormalize(self, x,mean,stdev ):
          tf.print("AAAAAAA")
          tf.print(self.mean)
          tf.print("BBBBBBB")
          tf.print(self.stdev)
          
          if self.affine:
              x = x - self.affine_bias
              x = x / (self.affine_weight + self.eps*self.eps)
          #mean, stdev = self._get_statistics(x)
          tf.print("CCCCCCC")
          #tf.print(mean)
          tf.print("DDDDDDD")
          tf.print(self.mean)
          x = x * stdev
          x = x + mean
          return x


我不知道如何使用 self.mean...总是出现超出范围的错误...:///

tensorflow keras deep-learning
© www.soinside.com 2019 - 2024. All rights reserved.