复数值错误:尝试将类型不受支持的值转换为张量

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

我正在使用tensorflow 1来做复数值神经网络,还可以。但是我没有在急切模式下这样做,现在tensorflow 2让我感到不安。显然现在所有的东西都是keras,所以我尝试实现这样的一层:

from tensorflow.keras import layers
import tensorflow as tf


class Linear(layers.Layer):

  def __init__(self, units=32, input_dim=32):
    super(Linear, self).__init__()
    w_init = tf.complex(tf.random_normal_initializer(), tf.random_normal_initializer())
    self.w = tf.Variable(initial_value=w_init(shape=(input_dim, units)),
                         trainable=True, dtype=tf.complex64)

  def call(self, inputs):
    return tf.abs(tf.matmul(inputs, self.w))

x = tf.complex(tf.ones((2, 2)), tf.ones((2, 2)))
linear_layer = Linear(4, 2)
y = linear_layer(x)
print(y)

但是我得到了错误:

ValueError: Attempt to convert a value (<tensorflow.python.ops.init_ops_v2.RandomNormal object at 0x7f64daf8ad90>) with an unsupported type (<class 'tensorflow.python.ops.init_ops_v2.RandomNormal'>) to a Tensor.
python tensorflow keras
1个回答
1
投票

您的代码的问题是tf.random_normal_initializer()产生了一个tensorflow.python.ops.init_ops_v2.RandomNormal对象,该对象不是数字,但是tf.complex期望张量作为输入。

© www.soinside.com 2019 - 2024. All rights reserved.