How use to Feature crosses with Keras Functional API

Viewed 276

I'm trying to create a feature cross using latitude and longitude and use it as input to the DNN model created using Keras Functional API using this code.

#Create a feature cross of latitude and longitude.
latitude_x_longitude = tf.feature_column.crossed_column([latitude, longitude], hash_bucket_size=100)
latitude_x_longitude = tf.feature_column.indicator_column(latitude_x_longitude)
feature_columns.append(latitude_x_longitude)


# Add to Feature Input Layer
feature_layer_inputs['x'] = tf.keras.Input(shape=(1,), name='x')
feature_layer_inputs['y'] = tf.keras.Input(shape=(1,), name='y')
feature_layer_inputs['latitude_x_longitude'] = tf.keras.Input(shape=(2,), name='latitude_x_longitude')

yet got this error

AssertionError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 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:2585 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:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386 call
        inputs, training=training, mask=mask)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:517 _run_internal_graph
        assert x_id in tensor_dict, 'Could not compute output ' + str(x)

    AssertionError: Could not compute output Tensor("Output_layer/Softmax_5:0", shape=(None, 38), dtype=float32)

The error causing this due to naming of the feature layer input.

feature_layer_inputs['latitude_x_longitude'] = tf.keras.Input(shape=(1,), name='latitude_x_longitude')

So, How the feature input layer should be named to identify it is a crossed feature

0 Answers
Related