{"id":296,"hash":"6847b8cf8f7f1f33d2396254e0e4d2c0cc15235275cdd33aa4baa0842116e00a","pattern":"NotImplementedError: Cannot convert a symbolic Tensor (2nd_target:0) to a numpy array","full_message":"I try to pass 2 loss functions to a model as Keras allows that.\n\nloss: String (name of objective function) or objective function or\nLoss instance. See losses. If the model has multiple outputs, you can\nuse a different loss on each output by passing a dictionary or a list\nof losses. The loss value that will be minimized by the model will\nthen be the sum of all individual losses.\n\nThe two loss functions:\n\ndef l_2nd(beta):\n    def loss_2nd(y_true, y_pred):\n        ...\n        return K.mean(t)\n\n    return loss_2nd\n\nand\n\ndef l_1st(alpha):\n    def loss_1st(y_true, y_pred):\n        ...\n        return alpha * 2 * tf.linalg.trace(tf.matmul(tf.matmul(Y, L, transpose_a=True), Y)) / batch_size\n\n    return loss_1st\n\nThen I build the model:\n\nl2 = K.eval(l_2nd(self.beta))\nl1 = K.eval(l_1st(self.alpha))\nself.model.compile(opt, [l2, l1])\n\nWhen I train, it produces the error:\n\n1.15.0-rc3 WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630:\ncalling BaseResourceVariable.__init__ (from\ntensorflow.python.ops.resource_variable_ops) with constraint is\ndeprecated and will be removed in a future version. Instructions for\nupdating: If using Keras pass *_constraint arguments to layers.\n--------------------------------------------------------------------------- \nNotImplementedError                       Traceback (most recent call\nlast) <ipython-input-20-298384dd95ab> in <module>()\n     47                          create_using=nx.DiGraph(), nodetype=None, data=[('weight', int)])\n     48 \n---> 49     model = SDNE(G, hidden_size=[256, 128],)\n     50     model.train(batch_size=100, epochs=40, verbose=2)\n     51     embeddings = model.get_embeddings()\n\n10 frames <ipython-input-19-df29e9865105> in __init__(self, graph,\nhidden_size, alpha, beta, nu1, nu2)\n     72         self.A, self.L = self._create_A_L(\n     73             self.graph, self.node2idx)  # Adj Matrix,L Matrix\n---> 74         self.reset_model()\n     75         self.inputs = [self.A, self.L]\n     76         self._embeddings = {}\n\n<ipython-input-19-df29e9865105> in reset_model(self, opt)\n\n---> 84         self.model.compile(opt, loss=[l2, l1])\n     85         self.get_embeddings()\n     86 \n\n/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/tracking/base.py\nin _method_wrapper(self, *args, **kwargs)\n    455     self._self_setattr_tracking = False  # pylint: disable=protected-access\n    456     try:\n--> 457       result = method(self, *args, **kwargs)\n    458     finally:\n    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access\n\nNotImplementedError: Cannot convert a symbolic Tensor (2nd_target:0)\nto a numpy array.\n\nPlease help, thanks!","ecosystem":"pypi","package_name":"tensorflow","package_version":null,"solution":"I found the solution to this problem:\n\nIt 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:\n\ndef my_mse_loss_b(b):\n     def mseb(y_true, y_pred):\n         ...\n         a = np.ones_like(y_true) #numpy array here is not recommended\n         return K.mean(K.square(y_pred - y_true)) + a\n     return mseb\n\nInstead, you should convert all to symbolic tensors like this:\n\ndef my_mse_loss_b(b):\n     def mseb(y_true, y_pred):\n         ...\n         a = K.ones_like(y_true) #use Keras instead so they are all symbolic\n         return K.mean(K.square(y_pred - y_true)) + a\n     return mseb\n\nHope this help!","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/58479556/notimplementederror-cannot-convert-a-symbolic-tensor-2nd-target0-to-a-numpy","votes":83,"created_at":"2026-04-19T04:41:46.114799+00:00","updated_at":"2026-04-19T04:51:57.736564+00:00"}