Python 3.9.16
tensorflow 2.17.0
Я столкнулся с проблема при построении модели VAE (вариационный автоэнкодер) с использованием следующей конфигурации: я выполняю эту работу в рамках вменения данных о мошенничестве
Код: Выделить всё
Input dimension: 250
Latent dimension: 32
Эта ошибка произошла при вызове build_vae_model в модуле impute_data_vae, что привело к сбою со следующим описанием ошибки:
Ошибка:
Код: Выделить всё
A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.operations`). You are likely doing something like:
```
x = Input(...)
...
tf_fn(x) # Invalid.
```
What you should do instead is wrap `tf_fn` in a layer:
```
class MyLayer(Layer):
def call(self, x):
return tf_fn(x)
x = MyLayer()(x)
```
Дальнейшее расследование
Вызвана ошибка. путем прямого применения операций TensorFlow (tf.shape и tf.random.normal) к тензорам Keras (z_mean, z_log_var), что Keras не позволяет. Решение, как указано в сообщении об ошибке, состоит в инкапсуляции этих операций внутри слоя Keras. Я реализовал собственный слой выборки следующим образом:
Код: Выделить всё
from tensorflow.keras.layers import Layer
class Sampling(Layer):
def call(self, inputs):
z_mean, z_log_var = inputs
batch = tf.shape(z_mean)[0]
dim = tf.shape(z_mean)[1]
epsilon = tf.random.normal(shape=(batch, dim))
return z_mean + tf.exp(0.5 * z_log_var) * epsilon
Подробнее здесь: https://stackoverflow.com/questions/791 ... -functions
Мобильная версия