NotImplementedError:无法将符号张量 (2nd_target:0) 转换为 numpy 数组

T D*_*yen 43 python keras tensorflow loss-function

我尝试将 2 个损失函数传递给模型,因为Keras 允许这样做。

损失:字符串(目标函数名称)或目标函数或损失实例。见损失。如果模型有多个输出,您可以通过传递字典或损失列表对每个输出使用不同的损失。模型将最小化的损失值将是所有单个损失的总和。

两个损失函数:

def l_2nd(beta):
    def loss_2nd(y_true, y_pred):
        ...
        return K.mean(t)

    return loss_2nd
Run Code Online (Sandbox Code Playgroud)

def l_1st(alpha):
    def loss_1st(y_true, y_pred):
        ...
        return alpha * 2 * tf.linalg.trace(tf.matmul(tf.matmul(Y, L, transpose_a=True), Y)) / batch_size

    return loss_1st
Run Code Online (Sandbox Code Playgroud)

然后我建立模型:

l2 = K.eval(l_2nd(self.beta))
l1 = K.eval(l_1st(self.alpha))
self.model.compile(opt, [l2, l1])
Run Code Online (Sandbox Code Playgroud)

当我训练时,它会产生错误:

1.15.0-rc3 警告:tensorflow:来自 /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630:调用 BaseResourceVariable。不推荐使用带有约束的init(来自 tensorflow.python.ops.resource_variable_ops)并将在未来版本中删除。说明

更新:如果使用 Keras 将 *_constraint 参数传递给层。

NotImplementedError Traceback (最近一次调用最后一次) in () 47 create_using=nx.DiGraph(), nodetype=None, data=[('weight', int)]) 48 ---> 49 model = SDNE(G, hidden_​​size= [256, 128],) 50 model.train(batch_size=100, epochs=40,verbose=2) 51 embeddings = model.get_embeddings()

init 10 帧(self, graph, hidden_​​size, alpha, beta, nu1, nu2) 72 self.A, self.L = self._create_A_L( 73 self.graph, self.node2idx) # Adj Matrix,L Matrix --- > 74 self.reset_model() 75 self.inputs = [self.A, self.L] 76 self._embeddings = {}

在 reset_model(self, opt)

---> 84 self.model.compile(opt, loss=[l2, l1]) 85 self.get_embeddings() 86

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected -access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

NotImplementedError:无法将符号张量 (2nd_target:0) 转换为 numpy 数组。

请帮忙,谢谢!

CGF*_*FoX 81

对我来说,问题发生在从numpy 1.19to升级1.20ray使用tensorflow 2.2内部使用的 RLlib 时。简单地降级

pip install numpy==1.19.5
Run Code Online (Sandbox Code Playgroud)

解决了问题;错误不再发生。

  • 这也是我的问题,解决方案有效,谢谢 (7认同)
  • 我想知道是否有解决方法。我不想回滚我的 numpy。 (3认同)
  • 这是一个红鲱鱼..正确的答案应该解决以下解决方案完美执行的根本原因..我们都从中学到了一些东西 (2认同)

T D*_*yen 24

我找到了这个问题的解决方案:

这是因为我将符号张量与非符号类型(例如 numpy. 例如。不建议有这样的事情:

def my_mse_loss_b(b):
     def mseb(y_true, y_pred):
         ...
         a = np.ones_like(y_true) #numpy array here is not recommended
         return K.mean(K.square(y_pred - y_true)) + a
     return mseb
Run Code Online (Sandbox Code Playgroud)

相反,您应该将所有内容转换为符号张量,如下所示:

def my_mse_loss_b(b):
     def mseb(y_true, y_pred):
         ...
         a = K.ones_like(y_true) #use Keras instead so they are all symbolic
         return K.mean(K.square(y_pred - y_true)) + a
     return mseb
Run Code Online (Sandbox Code Playgroud)

希望这有帮助!

  • 但我在计算中没有使用 numpy。https://github.com/siddhantkushwaha/east_1x/blob/east_tf2.0/losses.py (6认同)

Cod*_*ker 22

这可能是 numpy 版本的问题。尝试使用小于 1.20 的 numpy

pip install numpy==1.19
Run Code Online (Sandbox Code Playgroud)

  • 你的答案与上面的答案重复 (7认同)
  • 我有一个 conda 环境,所以对我来说 `conda install numpy=1.19` 确实有效。Tensorflow 很糟糕,pytorch 从来没有遇到过这种问题。 (4认同)
  • 不,这不是因为这只是“1.19”(这对我来说很有效) (2认同)