NotImplementedError:无法将符号张量 (2nd_target:0) 转换为 numpy 数组 [英] NotImplementedError: Cannot convert a symbolic Tensor (2nd_target:0) to a numpy array

查看:87
本文介绍了NotImplementedError:无法将符号张量 (2nd_target:0) 转换为 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to pass 2 loss functions to a model as Keras allows that.

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

loss: String (name of objective function) or objective function or Loss instance. See losses. If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.

两个损失函数:

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

    return loss_2nd

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

然后我建立模型:

l2 = K.eval(l_2nd(self.beta))
l1 = K.eval(l_1st(self.alpha))
self.model.compile(opt, [l2, l1])

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

When I train, it produces the error:

1.15.0-rc3 WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630:
calling BaseResourceVariable.__init__ (from
tensorflow.python.ops.resource_variable_ops) with constraint is
deprecated and will be removed in a future version. Instructions for
updating: If using Keras pass *_constraint arguments to layers.
--------------------------------------------------------------------------- 
NotImplementedError                       Traceback (most recent call
last) <ipython-input-20-298384dd95ab> in <module>()
     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()

10 frames <ipython-input-19-df29e9865105> in __init__(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 = {}

<ipython-input-19-df29e9865105> in 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: Cannot convert a symbolic Tensor (2nd_target:0)
to a numpy array.

请帮忙,谢谢!

推荐答案

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

I found the solution to this problem:

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

It was because I mixed symbolic tensor with a non-symbolic type, such as a numpy. For example. It is NOT recommended to have something like this:

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

相反,您应该像这样将所有内容转换为符号张量:

Instead, you should convert all to symbolic tensors like this:

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

希望对您有所帮助!

这篇关于NotImplementedError:无法将符号张量 (2nd_target:0) 转换为 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆