2

I'm using tensorflow for the first time and amusing it to classify data with 18 features into 4 classes.

The dimensions of X_train are: (14125,18).

This is my code:

dataset = tf.data.Dataset.from_tensor_slices((np.array(X_train.values, dtype=float),
                               np.array(y_train.pet_category.values, dtype=float)))
train_data = dataset.shuffle(len(X_train)).batch(32)

vdataset = tf.data.Dataset.from_tensor_slices((np.array(X_val.values, dtype=float)))
val_data = vdataset.batch(32)

tfmodel = tf.keras.Sequential([
                  tf.keras.layers.Dense(15, activation=tf.nn.relu, input_shape=(18,1)),
                  tf.keras.layers.Flatten(),
                  tf.keras.layers.Dense(10, activation=tf.nn.relu),
                  tf.keras.layers.Dense(4, activation=tf.nn.softmax)
])

tfmodel.compile(optimizer='adam',
                loss=tf.keras.losses.CategoricalCrossentropy(),
                metrics=['accuracy'])

On calling tfmodel.fit(dataset, epochs=15, validation_data=val_data), I'm getting the following error:

ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 270 but received input with shape [18, 15]

I tried looking for similar questions but couldn't find anything that'd help. Would be really helpful to solve this issue

Edit: The issue was with the version. It went away when I used a lower version of TensorFlow (v 2.1.0).

telekineser
  • 88
  • 1
  • 1
  • 9
  • Your input shape should be `(18,)` if the shape of your data is `(14125,18)` in which I'm assuming 14125 is the number of datapoints. – krxat Aug 07 '20 at 18:51
  • Tried it out, the error changed to `Input 0 of layer sequential is... input shape to have value 18 but received input with shape [18, 1]` – telekineser Aug 07 '20 at 19:30
  • Can you just try `train_data.element_spec` and see the shape of your dataset? – krxat Aug 07 '20 at 20:00
  • `(TensorSpec(shape=(None, 18), dtype=tf.float64, name=None), TensorSpec(shape=(None,), dtype=tf.float64, name=None))` – telekineser Aug 07 '20 at 20:04
  • Um can you try with `input_shape=(18)` since your dataset shape is also the same? – krxat Aug 07 '20 at 20:06
  • Input shape requires a tuple so that doesn't work. It just says int is not iterable – telekineser Aug 07 '20 at 20:11
  • Ideally, when you are feeding input to `Dense`, it should have a shape `(shape,)` which in your case is `(18,)`, so that should have worked. – krxat Aug 07 '20 at 20:40
  • Oh wait the error probably might be in the `Flatten` layer. Since you are already getting an output of shape `(batch_size, 18)` in your case, you don't need to flatten it out. You can remove the `Flatten` layer and it should work I guess – krxat Aug 07 '20 at 20:41
  • Sorry, doesn't work, `ValueError: Shapes () and (18, 1, 4) are incompatible` – telekineser Aug 07 '20 at 21:05

2 Answers2

2

You are using the dataset int fit instead of train_data. I assume you are using a DataFrame called X_train and y_train and I mimicked the same with numpy and it works now. See below.

import tensorflow as tf
import numpy as np

X_train = np.random.random((14125,18))
y_train = np.random.random((14125,1))

dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = dataset.shuffle(len(X_train)).batch(32)
train_data = train_data.prefetch(
        buffer_size=tf.data.experimental.AUTOTUNE)

tfmodel = tf.keras.Sequential([
                  tf.keras.layers.Dense(15, activation=tf.nn.relu, input_shape=(18,)),
                  tf.keras.layers.Flatten(),
                  tf.keras.layers.Dense(10, activation=tf.nn.relu),
                  tf.keras.layers.Dense(4, activation=tf.nn.softmax)
])

tfmodel.compile(optimizer='adam',
                loss=tf.keras.losses.CategoricalCrossentropy(),
                metrics=['accuracy'])

tfmodel.fit(train_data, epochs=5)

Note: I didn't use the val_data

Train for 442 steps
Epoch 1/5
442/442 [==============================] - 1s 2ms/step - loss: 7.8375 - accuracy: 1.4159e-04
Epoch 2/5
442/442 [==============================] - 1s 2ms/step - loss: 28.5034 - accuracy: 0.0000e+00
Epoch 3/5
442/442 [==============================] - 1s 1ms/step - loss: 17.8604 - accuracy: 0.0000e+00
Epoch 4/5
442/442 [==============================] - 1s 1ms/step - loss: 3.4244 - accuracy: 2.1239e-04
Epoch 5/5
442/442 [==============================] - 1s 2ms/step - loss: 3.2791 - accuracy: 0.0160
<tensorflow.python.keras.callbacks.History at 0x7f0d8c72d630>
krxat
  • 513
  • 4
  • 16
0

It seems that the issue was with the version of tensorflow I was using (2.3.0) I tried with the nightly build and it gave the same error. I downgraded to v2.1.0 and it worked

telekineser
  • 88
  • 1
  • 1
  • 9