Reputation: 1823
I am a totally newcomer to TensorFlow. I have followed some of the tutorials and worked out my first multi-class classification model.
I am not sure my layers are reasonably designed, anyway, the accuracy in the test is 0.98 or so.
The thing is I cannot use my model to predict a new input. Here is my code and the data I used to train the model.
The data has 10 columns, the last one is the class names. The model is to use a row of 9 values to predict which class is the row fits in.
All the codes were run in colab.
!pip install sklearn
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import feature_column
from tensorflow.keras import layers
from tensorflow.keras import Sequential
from sklearn.model_selection import train_test_split
index_col = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'r']
dataframe = pd.read_csv('drive/MyDrive/Book2.csv', names=index_col)
train, test = train_test_split(dataframe, test_size=0.2)
train, val = train_test_split(train, test_size=0.2)
train_labels = train.filter('r')
train = train.drop('r', axis=1)
test_labels = test.filter('r')
test = test.drop('r', axis=1)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1),
tf.keras.layers.Dense(100, activation='relu'),
tf.keras.layers.Dense(4)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train, train_labels, epochs=20)
test_loss, test_acc = model.evaluate(test, test_labels, verbose=2)
result = model.predict(pd.DataFrame([1, 3, 0, 3, 3, 1, 2, 3, 2]))
Here is the console error I got.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-942b3f127f67> in <module>()
----> 1 result = model.predict([1, 3, 0, 3, 3, 1, 2, 3, 2])
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1569 predict_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1559 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1552 run_step **
outputs = model.predict_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1525 predict_step
return self(x, training=False)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1013 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:255 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 9 but received input with shape (None, 1)
Book2.csv is here.
Upvotes: 0
Views: 927
Reputation: 4960
Your dataframe passed to predict
has shape (9,1)
. It's shape should be like the shape of train dataset (except first dimension) you have passed.
Simply transpose your data to change shape from (9,1)
to (1,9)
:
result = model.predict(pd.DataFrame([1, 3, 0, 3, 3, 1, 2, 3, 2]).T)
P.S: (9,1)
means 9 samples, each of which with 1 feature which is incompatible with your model expectation. But (1,9)
means 1 sample with 9 features.
Upvotes: 1