Simen Russnes
Simen Russnes

Reputation: 2260

WARNING:tensorflow:Model was constructed with shape (None, ....)

I have a classification problem (0 or 1) with 78 features. Let's say I have that data in a dataframe with 78 columns and 202 rows, each cell value holding an integer in the range [0, infinity).

Trying to achieve prediction with TensorFlow, when I fit my model I get the following warning:

WARNING:tensorflow:Model was constructed with shape (None, 101, 78) for input Tensor("input_2:0", shape=(None, 101, 78), dtype=float32), but it was called on an input with incompatible shape (101, 78).

I would have thought my shape definition was correct, so why am I getting this warning? Perhaps I'm misunderstanding how to use the framework.

X_train, X_test, y_train, y_test = train_test_split(df_x, series_y, random_state=1, test_size=0.5)
numpy_x_train = X_train.to_numpy()
numpy_y_train = y_train.to_numpy()
numpy_x_test = X_test.to_numpy()

shape_x = len(X_train)
shape_y = len(X_train.columns)
inputs = keras.Input(shape=(shape_x, shape_y))

x = Rescaling(scale=1.0 / 255)(inputs)

num_classes = 1
outputs = layers.Dense(num_classes, activation="softmax")(x)

model = keras.Model(inputs=inputs, outputs=outputs)
processed_data = model(numpy_x_train)

optimiser = keras.optimizers.RMSprop(learning_rate=1e-3)
loss = keras.losses.CategoricalCrossentropy()

model.compile(optimizer=optimiser, loss=loss)

history = model.fit(numpy_x_train, numpy_y_train, batch_size=32, epochs=10)

Upvotes: 2

Views: 6437

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

here a full example based on your problem. you have 2D data so in the input layer you have to specify only the feature dimension and not also the sample dimension. you also are carrying out a binary classification task so the best choice is to use a final dense layer with 1 dimension, a sigmoid activation function, and binary crossentropy as a loss. the predicted class will be 1 if the prob are > 0.5 otherwise it is 0

from tensorflow import keras
import numpy as np

# create dummy data
train_size, test_size = 101, 101
n_features = 78
num_classes = 2 # 0 or 1

numpy_x_train = np.random.uniform(0,256, (train_size,n_features))
numpy_y_train = np.random.randint(0,num_classes,train_size)
numpy_x_test = np.random.uniform(0,256, (test_size,n_features))
numpy_y_test = np.random.randint(0,num_classes,test_size)

# rescaling data
numpy_x_train = numpy_x_train / 255
numpy_x_test = numpy_x_test / 255

# define model
inputs = keras.layers.Input(shape=(numpy_x_train.shape[1],))
outputs = keras.layers.Dense(1, activation="sigmoid")(inputs)

optimiser = keras.optimizers.RMSprop(learning_rate=1e-3)
loss = keras.losses.BinaryCrossentropy()

model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer=optimiser, loss=loss)

history = model.fit(numpy_x_train, numpy_y_train, batch_size=32, epochs=10)

# get test predictions
test_prob = model.predict(numpy_x_test).ravel()
test_class = (test_prob>0.5)+0 # if prob > 0.5 is 1 else is 0

Upvotes: 4

Related Questions