Lostsoul
Lostsoul

Reputation: 26067

ValueError: Value tf.Tensor.. shape=(), dtype=float64) has insufficient rank for batching.?

I'm trying to take a dataframe and convert them to tensors to train a model in keras.

I think it's being triggered when I am converting my Y label to a tensor:

  X_train = df_train1.drop(variableToPredict, axis=1)
  y_train = df_train1[variableToPredict].copy()


X_train=tf.data.Dataset.from_tensor_slices(dict(X_train))
  y_train=tf.data.Dataset.from_tensor_slices(dict(y_train))

I'm getting the following error when casting y_train to tensor from slices:

ValueError: Value tf.Tensor(0.10559591064345274, shape=(), dtype=float64) has insufficient rank for batching.

In the tutorials this seems to work but I think those tutorials are doing multiclass classifications whereas I'm doing a regression so y_train is a series not multiple columns.

Any suggestions of what I can do?

Upvotes: 1

Views: 1393

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36714

Either use:

y = tf.data.Dataset.from_tensors(dict(y_train))

Or this:

y = tf.data.Dataset.from_tensor_slices(y_train)

Or just use double brackets so your dataframe remains a dataframe, then you won't need to change anything:

y_train = df[['height']].copy()

y = tf.data.Dataset.from_tensor_slices(dict(y_train))

Full code reproducing the issue, with alternatives:

import tensorflow as tf
import pandas as pd

df = pd.DataFrame(data={'integers': [1, 2, 3, 4], 'floats': [4., 3., 2., 1]})

y_train = df['floats'].copy()

y = tf.data.Dataset.from_tensor_slices(dict(y_train)) # not good

y = tf.data.Dataset.from_tensor_slices(y_train) # good
print(next(iter(y)))

y = tf.data.Dataset.from_tensors(y_train) # good
print(next(iter(y)))

y_train = df[['floats']].copy()
y = tf.data.Dataset.from_tensor_slices(dict(y_train)) # good
next(iter(y))
{'floats': <tf.Tensor: shape=(4,), dtype=float64, numpy=array([5., 4., 3., 2.])>}

Upvotes: 1

Related Questions