Issue
I'm practicing Dense Neural Network on Google Colab and had this error when doing model.fit.
This is the whole code:
I imported my data from Google drive and was able to passed in the data to panda.
import functools
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from google.colab import drive
drive.mount('/content/drive')
train_file_path = "/content/drive/My Drive/Colab Notebooks/1111_train.csv"
test_file_path = "/content/drive/My Drive/Colab Notebooks/1111_test.csv"
df_train = pd.read_csv(train_file_path)
df_test = pd.read_csv(test_file_path)
I then sliced in inorder to create tensorslice
train_target = df_train.pop('22')
test_target = df_test.pop('22')
train_dataset = tf.data.Dataset.from_tensor_slices((df_train.values, train_target.values))
test_dataset = tf.data.Dataset.from_tensor_slices((df_test.values, test_target.values))
After this, I built my model
model = tf.keras.Sequential([
tf.keras.layers.Dense(22, activation='relu'),
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(1)])
model.compile(optimizer='adam',
loss = tf.keras.losses.BinaryCrossentropy,
metrics=['accuracy'])
model.fit(train_dataset, epochs=15)
This is the whole error message when runnning model.fit.
WARNING:tensorflow:Layer dense_6 is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because it's dtype defaults to floatx.
If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.
To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-19-d6feee4cfcc8> in <module>()
----> 1 model.fit(train_dataset, epochs=15)
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
TypeError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:533 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:145 __call__
losses, sample_weight, reduction=self._get_reduction())
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/losses_utils.py:104 compute_weighted_loss
losses = ops.convert_to_tensor_v2(losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1283 convert_to_tensor_v2
as_ref=False)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1341 convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:321 _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:262 constant
allow_broadcast=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:300 _constant_impl
allow_broadcast=allow_broadcast))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:547 make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'tensorflow.python.keras.losses.BinaryCrossentropy'> to Tensor. Contents: <tensorflow.python.keras.losses.BinaryCrossentropy object at 0x7f76215279b0>. Consider casting elements to a supported type.
Solution
Nevermind. I found a solution.
I just change the loss function on model.compile
model.compile(optimizer='adam',
loss = tf.keras.losses.binary_crossentropy,
metrics=['accuracy'])
Answered By - Rudolf Pablico
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.