InaccessibleTensorError-当使用`tf。keras.层。层`的输出,在循环的条件的另一层

0

的问题

当我使用的输出从一个层(tf.keras.layers.Layer)作为回路迭代在另一层,我得到一个 InaccessibleTensorError,

InaccessibleTensorError: The tensor 'Tensor("looper/while/sub:0", shape=(None, 1), dtype=float32)' 
cannot be accessed here: it is defined in another function or code block. Use return values, 
explicit Python locals or TensorFlow collections to access it. Defined in: 
FuncGraph(name=looper_while_body_483, id=2098967820416); accessed from: 
FuncGraph(name=looper_scratch_graph, id=2098808987904).

简约的代码错误,

import tensorflow as tf
import numpy as np

class Looper(tf.keras.layers.Layer):
    # custom layer
    def __init__(self, units, **kwargs):
        super(Looper, self).__init__(**kwargs)
        self.units = units

    def call(self, input):
        output = []
        while input > 0:
            input = input - 0.01
            output.append(input)
        return tf.stack(output, axis=1)

input_label = tf.keras.Input((1, 3))
lstm1 = tf.keras.layers.LSTM(1)
looper = Looper(10)
output = lstm1(input_label)
output = looper(output)

model = tf.keras.Model(input_label, output)
adam = tf.keras.optimizers.Adam(0.01)
model.compile(adam, 'mse')

我不能找到任何类似的问题或疑问,在Tensorflow问题跟踪器也不是这样。 任何帮助或见解是多认识到:)

keras python tensorflow
2021-11-23 10:39:33
1

最好的答案

1

我认为这个问题可能在于python列表中使用的定义层。 你应该使用 Tensorflow 收集样 tf.TensorArray 对于您的使用情况:

import tensorflow as tf
import numpy as np

class Looper(tf.keras.layers.Layer):
    # custom layer
    def __init__(self, units, **kwargs):
        super(Looper, self).__init__(**kwargs)
        self.units = units

    def call(self, input):
        output = tf.TensorArray(dtype=tf.float32, size=0, dynamic_size=True)
        while input > 0:
            input = input - 0.01
            output = output.write(output.size(), input)
        return output.stack()

input_label = tf.keras.Input((1, 3))
lstm1 = tf.keras.layers.LSTM(1)
looper = Looper(10)
output = lstm1(input_label)
output = looper(output)

model = tf.keras.Model(input_label, output)
adam = tf.keras.optimizers.Adam(0.01)
model.compile(adam, 'mse')

print(model(tf.random.normal((1, 1, 3))))
tf.Tensor(
[[[ 0.01392288]]

 [[ 0.00392288]]

 [[-0.00607712]]], shape=(3, 1, 1), dtype=float32)

根据什么你要这样做,你可能会有重塑的输出 Looper 层。

2021-11-23 12:42:58

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................