I am trying to a custom loss function for tensorflow. Because I was trying to see what was going on in the loss function, I tried to print the input of the loss function as a numpy array using .numpy()
, but I got an error. My code looks like this:
import tensorflow as tf
import numpy as np
import pandas as pd
def windowDataset(df, split=0.2, shuffleBuffer=7, batchSize=10):
dataset = tf.data.Dataset.from_tensor_slices(df).window(5, 1, drop_remainder=True)# sequence {{},...,{}}
dataset = dataset.flat_map(lambda x: x.batch(5)) # {[], ..., []}
dataset = dataset.map(lambda x: (x[:-1], x[-1])) # {[(,)],..., [(,)]}
split = round((1-split)*len(list(dataset)))
train = dataset.take(split).shuffle(shuffleBuffer).batch(batchSize)
val = dataset.skip(split)
return train, val
def customLoss(yTrue, yPredict):
print(yTrue)
print(yTrue.numpy())
return 0
model = tf.keras.Sequential([
tf.keras.layers.LSTM(8),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(4)
])
model.compile(loss=customLoss, optimizer='Adam')
df = pd.DataFrame(np.random.rand(100, 4))
train, val = windowDataset(df)
model.fit(train, validation_data=val, epochs=10)
This will print out:
Tensor("Cast:0", shape=(None, 4), dtype=float32)
but then there will be a error message:
...AttributeError: in user code:
C:\Users\119433\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
<ipython-input-22-f015b3791cf2>:16 customLoss *
print(yTrue.numpy())
AttributeError: 'Tensor' object has no attribute 'numpy'
Now if I try something like this:
test = tf.constant([1,2,3])
print(test)
print(test.numpy())
everything works fine and I get this return:
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
[1 2 3]
So Tensor does has .numpy() method. So why didn't it work for the custom loss function?