When I try to run ‘# Test your preprocessing_fn’ , I got 'AttributeError: ‘Tensor’ object has no attribute ‘indices’
My long code:
def preprocessing_fn(inputs):
"""tf.transform's callback function for preprocessing inputs.
Args:
inputs: map from feature keys to raw not-yet-transformed features.
Returns:
Map from string feature key to transformed feature operations.
"""
outputs = {}
### START CODE HERE
# Scale these features to the z-score.
for key in _DENSE_FLOAT_FEATURE_KEYS:
# Scale these features to the z-score.
outputs[_transformed_name(key)] = tft.scale_to_z_score(inputs[key])
# Scale these feature/s from 0 to 1
for key in _RANGE_FEATURE_KEYS:
outputs[_transformed_name(key)] = tft.scale_to_0_1(inputs[key])
# Transform the strings into indices
# hint: use the VOCAB_SIZE and OOV_SIZE to define the top_k and num_oov parameters
for key in _VOCAB_FEATURE_KEYS:
outputs[_transformed_name(key)] = tft.compute_and_apply_vocabulary(
inputs[key],
top_k=_VOCAB_SIZE,
num_oov_buckets=_OOV_SIZE)
# Bucketize the feature
for key in _BUCKET_FEATURE_KEYS:
outputs[_transformed_name(key)] = tft.bucketize(
inputs[key], _FEATURE_BUCKET_COUNT[key])
# Keep as is. No tft function needed.
for key in _CATEGORICAL_FEATURE_KEYS:
outputs[_transformed_name(key)] = inputs[key]
# Use `tf.cast` to cast the label key to float32 and fill in the missing values.
traffic_volume = tf.cast(_fill_in_missing(inputs[_VOLUME_KEY]), tf.float32)
# Create a feature that shows if the traffic volume is greater than the mean and cast to an int
outputs[_transformed_name(_VOLUME_KEY)] = tf.cast(
# Use `tf.greater` to check if the traffic volume in a row is greater than the mean of the entire traffic volumn column
tf.greater(traffic_volume, tft.mean(tf.cast(inputs[_VOLUME_KEY], tf.float32))),
tf.int64)
### END CODE HERE
return outputs
def _fill_in_missing(x):
default_value = '' if x.dtype == tf.string else 0
return tf.squeeze(
tf.sparse.to_dense(
tf.SparseTensor(x.indices, x.values, [x.dense_shape[0], 1]),
default_value),
axis=1)
So over here just make sure that you are passing x correctly, i.e. x should be a Numpy array and not a tensorflow type.
TensorFlow type doesnot have an object ‘indices’ and that’s exactly what the error states. ‘.indices’ is used in Numpy arrays. And here is the documentation for the same:-
The error states that bucketize() function has no argument named ‘always_return_num_quantiles’.
When I check the official documentation of bucketize() function of TensorFlow (tft.bucketize | TFX | TensorFlow), and indeed there is no argument with that name.
So you would have to change the file traffic_transform.py where you have used the tft.bucketize() function.
Let me know if this works for you. If not, let me know some more details about the code, and we will solve it. Happy to help !
This is code that were run and any recommendation and pls help me
Bucketize the feature # Noted
for key in _BUCKET_FEATURE_KEYS:
outputs[_transformed_name(key)] = tft.bucketize(
inputs[key], _FEATURE_BUCKET_COUNT[key],
always_return_num_quantiles=False)
Best Regards and Thank in advance
Request you to please open a new thread for resolving any of your doubts. The errors which you have posted are different. Request you to post the issue in different thread and we can take up from there. (There are many other people as well eho can help.you out in the same).