0

The following example is based on this approach.

Similar to that approach, I am wanting to pass an additional parameter with y_true for my custom metric, as both will be used in the computation of that metric. Here this additional parameter is a vector of log-transformed probabilities. I follow Nickpick's approach suggested in the linked thread. The metric example provided here is different from my actual metric, but this is an equivalent example showing the inclusion of both the true response and the probability vector.

y_true = pd.concat([pd.DataFrame(y), pd.DataFrame(logZprob)], axis = 1)
y_true.columns = [0,1]

#custom metric def comp_log_like(y_true,y_pred): logZprob = pd.DataFrame.to_numpy(y_true)[:,1] y_true = pd.DataFrame.to_numpy(y_true)[:,0] cll = K.sum(y_true*logZprob) return cll #binary cross entropy loss custom def bce_custom(y_true, y_pred): #reassign y_true to only be true responses column y_true = pd.DataFrame.to_numpy(y_true)[:,0] #Call binary cross entropy method bce = tf.keras.losses.BinaryCrossentropy(from_logits=True) return tf.Tensor(bce(y_true, y_pred), dtype=float32)

After running the following code an error occurs.

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=10,input_shape=(10,), activation='relu'))
model.add(tf.keras.layers.Dense(32, activation='tanh'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam',
              loss = bce_custom,
              metrics=comp_log_like)

model.fit(Q,y,epochs=10, batch_size=2, validation_split=0.2) model.evaluate(testQ, testy)

The error seems related to the implementation of the custom loss function. This is the error printout (file locations modified for brevity).

>>> model.fit(Q,y,epochs=10, batch_size=2, validation_split=0.2)
Epoch 1/10
AttributeError: in user code:
File "keras\engine\training.py", line 1249, in train_function  *
    return step_function(self, iterator)
File "keras\engine\training.py", line 1233, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "keras\engine\training.py", line 1222, in run_step  **
    outputs = model.train_step(data)
File "keras\engine\training.py", line 1024, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "keras\engine\training.py", line 1082, in compute_loss
    return self.compiled_loss(
File "keras\engine\compile_utils.py", line 265, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "keras\losses.py", line 152, in __call__
    losses = call_fn(y_true, y_pred)
File "losses.py", line 284, in call  ** return ag_fn(y_true, y_pred,self._fn_kwargs)
File "<string>", line 3, in bce_custom  **

File "pandas\core\frame.py", line 1838, in to_numpy
    result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)

AttributeError: 'Tensor' object has no attribute '_mgr'

I do not understand this error or how to prevent it from occurring. Is there a way to follow this custom loss/metric approach that does not involve this error?

Example data:


y = np.array([1., 1., 0., 0., 1., 0., 0., 0., 0., 0.])
Q = np.array([[1., 0., 0., 0., 0., 1., 0., 0., 1., 0.],
       [0., 1., 0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 1., 0., 0., 1.],
       [0., 0., 0., 1., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 1., 0.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
testy = np.array([1., 1., 1., 1., 1., 0., 0., 0., 0., 1.])
testQ = np.array([[1., 0., 0., 0., 0., 1., 1., 1., 1., 0.],
       [0., 1., 0., 0., 0., 1., 0., 0., 0., 1.],
       [0., 0., 1., 0., 0., 0., 1., 0., 0., 1.],
       [0., 0., 0., 1., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 1., 0.],
       [1., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
logZprob = [-0.3963536643094933, -0.7125631306135873, -0.7951953025863644, 
-0.8887697591933559, -0.8293953059235406, -2.0795984093992654, 
-2.0418665328379717, -1.3570418533075643, -1.6522522259570047, -1.443180271147809]

David
  • 1
  • 1

0 Answers0