TensorFlow2 - Model subclassing ValueError - python

I am trying to experiment creating a LeNet-300-100 dense neural network using TensorFlow 2's model sub-classing. The code that I have is as follows:
batch_size = 32
num_epochs = 20
# Load MNIST dataset-
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0
# Convert class vectors/target to binary class matrices or one-hot encoded values-
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
X_train.shape, y_train.shape
# ((60000, 28, 28), (60000, 10))
X_test.shape, y_test.shape
# ((10000, 28, 28), (10000, 10))
class LeNet300(Model):
def __init__(self, **kwargs):
super(LeNet300, self).__init__(**kwargs)
self.flatten = Flatten()
self.dense1 = Dense(units = 300, activation = 'relu')
self.dense2 = Dense(units = 100, activation = 'relu')
self.op = Dense(units = 10, activation = 'softmax')
def call(self, inputs):
x = self.flatten(inputs)
x = self.dense1(x)
x = self.dense2(x)
return self.op(x)
# Instantiate an object using LeNet-300-100 dense model-
model = LeNet300()
# Compile the defined model-
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy']
)
# Define early stopping callback-
early_stopping_callback = tf.keras.callbacks.EarlyStopping(
monitor = 'val_loss', min_delta = 0.001,
patience = 3)
# Train defined and compiled model-
history = model.fit(
x = X_train, y = y_train,
batch_size = batch_size, shuffle = True,
epochs = num_epochs,
callbacks = [early_stopping_callback],
validation_data = (X_test, y_test)
)
On calling "model.fit()", it gives the following error:
ValueError: Shape mismatch: The shape of labels (received (320,))
should equal the shape of logits except for the last dimension
(received (32, 10)).
What's going wrong?
Thanks

The loss SparseCategoricalCrossentropy doesn't take one-hot encoding to calculate loss. In the documentation, they mention that
Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided as integers. If you want to provide labels using one-hot representation, please use CategoricalCrossentropy loss. There should be # classes floating point values per feature for y_pred and a single floating point value per feature for y_true.
As a result of this you are getting the error. If you observe the stacktrace the error arises in the loss function,
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:1569 sparse_categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py:4941 sparse_categorical_crossentropy
labels=target, logits=output)
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py:4241 sparse_softmax_cross_entropy_with_logits_v2
labels=labels, logits=logits, name=name)
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py:4156 sparse_softmax_cross_entropy_with_logits
logits.get_shape()))
ValueError: Shape mismatch: The shape of labels (received (320,)) should equal the shape of logits except for the last dimension (received (32, 10)).
I would suggest using CategoricalCrossentropy.

This is because input to first Dense layer should be flattened. MNIST data has 28x28 grid/image for every digit. This 28x28 data should be flattened to 784 input numbers.
So just before first Dense(...) layer insert Flatten() keras layer i.e. do Flatten()(inputs).
See this doc of Flatten layer for reference.

Related

Why I am getting an input shape error after I specified the input shape?

I am trying to train a CNN in anaconda, jupyter notebook. The TensorFlow version is 1.14. And I am experimenting the mobilenet_v2. Here is my code:
from tensorflow.keras.models import Model
base_model=tf.keras.applications.mobilenet_v2.MobileNetV2(include_top=False,weights=None,input_shape=(150,150,3))
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
predictions = tf.keras.layers.Dense(9,activation='softmax')(x)
model = Model(inputs = base_model.input,outputs=predictions)
model.compile(loss='categorical_crossentropy',metrics=["accuracy"],optimizer = tf.keras.optimizers.Adam())
history = model.fit(train_data,
epochs=5,steps_per_epoch=len(train_data),
validation_steps=0.2*len(train_data))
The input shape of the images is 150x150x3, and I double-checked the input image size to ensure it is correct.
image size of a random image from imagedata
After I fit the model, I got an error said( Error when checking input: expected input_8 to have shape (150, 150, 3) but got array with shape (256, 256, 3) )
error message screenshot
Here is a screenshot of the model summary; the inputlayer has the correct shape, so I am not sure where the 256 comes from.
first few layers of the model
ps: I also tried to build a custom model with just a few layers, but the same error still occurred:
custom_model = tf.keras.Sequential([
tf.keras.layers.Conv2D(10,10,activation='relu',input_shape=(150,150,3)),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Conv2D(10,10,activation='relu'),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(9,activation='softmax')
])
custom_model.summary()
custom_model.compile(loss="categorical_crossentropy",
optimizer=tf.keras.optimizers.Adam(),
metrics=["accuracy"])
custom_model_history = custom_model.fit(train_data,
epochs=5,
steps_per_epoch=len(train_data),
validation_data=val_data,
validation_steps=len(val_data))
Here is the summary of the custom model: custom model
And here is the error message: error message from custom model
please look into your train, test splits (look into this example)
num_classes = 10
input_shape = (28, 28, 1)
Load the data and split it between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
Scale images to the [0, 1] range
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")
convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
also while training you have to mention both x and y in your training data
i think you didn't mentioned x_train and y_train instead of that you mentioned only x_train
batch_size = 128
epochs = 15
custom_model_history = custom_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

Shape error of Tensorflow GRU while doing slot filling

I'm trying to train slot filling by LSTM in tensorflow, but received a shape error after first epoch.
Error mes: "Input to reshape is a tensor with 5632 values, but the requested shape has 12800"
I used sequences model and defined timestep = 128, which is the length of the input data sequence(after padding).
input data in model.fit : x_train.shape = (7244, 128) , y_train.shape = (7244, 128)
I have no idea why the model work on in the first epoch but fail in the latter , appreciate to any suggestion!
batch_size = 100
sequence_input = Input(shape=(128,), dtype='int32',batch_size=batch_size)
embedding_layer = Embedding(embeddings.shape[0],
embedding_dim,
weights = [embeddings],
input_length = 128,
name = 'embeddings',mask_zero=True)
# embedding layer use a dict according to glove 300d,
# and I set input_length =128 because I gauss this parameter also mentioned time step? not pretty sure.
embedded_sequences = embedding_layer(sequence_input)
x = Bidirectional(GRU(90,
stateful=True,
return_sequences=True,
name='lstm_layer',
go_backwards=True))(embedded_sequences)
x = Dense(50, activation="sigmoid")(x)
pred = Dense(9, activation="sigmoid")(x)
_model = Model(sequence_input, pred)
_model.compile(loss = 'SparseCategoricalCrossentropy',
optimizer='adam',
metrics = ['accuracy'])
_model.summary()
history = _model.fit(x_train, y_train,
epochs =10, batch_size =batch_size, shuffle = False,
validation_data=(sequences['train'], labels['train']))
model summary

ValueError: Found input variables with inconsistent numbers of samples: [1, 74]

I want to apply LSTM.
I have 12 features and 74 rows
my data shape after dropping the targeted variable and reshape it for 3d arrays:(1, 74, 12)
and my targeted shape: (74,)
when I split the data using this code:
x_train, x_test, y_train, y_test = train_test_split(data_1, target, test_size = 0.2,random_state =25)
I got this error:
ValueError: Found input variables with inconsistent numbers of samples: [1, 74]
I defined the model well but when I fit the model also I have another error
defining the model:
model = Sequential()
model.add(LSTM(1, batch_input_shape=(1, 74, 12), return_sequences = True))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accurecy'])
model.summary()
fitting the model:
history = model.fit(x_train, y_train, epochs = 100, validation_data= (x_test, y_test))
here I have also this error:
ValueError: Input 0 of layer sequential_14 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 12)
How can I resolve this error?
tf.keras.layers.LSTM expects inputs: A 3D tensor with shape [batch, timesteps, feature].
import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)
Output
(1, 74, 4)
If your input shape is of 2D, use tf.expand_dims(input, axis=0) to add extra dimension.

(ValueError) How to set data shape in RNN?

I have a problem with data shape in RNN model.
y_pred = model.predict(X_test_re) # X_test_re.shape (35,1,1)
It returned an error like below.
ValueError: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 35 samples. Batch size: 32.
first Question
I can't understand because I defined batch_size=10, but why error msg says batch size:32?
Second Question
when I modified the code as below
model.predict(X_test_re[:32])
I also got an error msg but I don't know what it means.
InvalidArgumentError: Incompatible shapes: [32,20] vs. [10,20]
[[{{node lstm_1/while/add_1}}]]
I built a model and fit it as below.
features = 1
timesteps = 1
batch_size = 10
K.clear_session()
model=Sequential()
model.add(LSTM(20, return_sequences=True, stateful=True,
batch_input_shape=(batch_size, timesteps, features)))
model.add(LSTM(20, stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
earyl_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=1)
hist = model.fit(X_train_re, y_train, # X_train_re.shape (70,1,1), y_train(70,)
batch_size=batch_size,
epochs=100,
verbose=1,
shuffle=False,
callbacks=[earyl_stop])
Until fit model, it works without any problem.
+) source code
first, df looks like,
# split_train_test from dataframe
train,test = df[:-35],df[-35:]
# print(train.shape, test.shape) (70, 2) (35, 2)
# scaling
sc = MinMaxScaler(feature_range=(-1,1))
train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)
# Split X,y (column t-1 is X)
X_train, X_test, y_train, y_test = train_sc[:,1], test_sc[:,1], train_sc[:,0], test_sc[:,0]
# reshape X_train
X_train_re = X_train.reshape(X_train.shape[0],1,1)
X_test_re = X_test.reshape(X_test.shape[0],1,1)

TensorFlow/TFLearn: ValueError: Cannot feed value of shape (64,10) for Tensor u'target/Y:0', which has shape '(?, 2)'

I have been trying to train a dataset using TFLearn to implement a convolutional neural network.
I have a dataset of 10 classes with image size is 64*32, 3 channels of input and 2 outputs i.e image detected/not detected.
Here is my code.
# Load the data set
def read_data():
with open("deep_logo.pickle", 'rb') as f:
save = pickle.load(f)
X = save['train_dataset']
Y = save['train_labels']
X_test = save['test_dataset']
Y_test = save['test_labels']
del save
return [X, X_test], [Y, Y_test]
def reformat(dataset, labels):
dataset = dataset.reshape((-1, 64, 32,3)).astype(np.float32)
labels = (np.arange(10) == labels[:, None]).astype(np.float32)
return dataset, labels
dataset, labels = read_data()
X,Y = reformat(dataset[0], labels[0])
X_test, Y_test = reformat(dataset[2], labels[2])
print('Training set', X.shape, Y.shape)
print('Test set', X_test.shape, Y_test.shape)
#building convolutional layers
network = input_data(shape=[None, 64, 32, 3],data_preprocessing=img_prep,
data_augmentation=img_aug)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 128, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
# Step 8: Fully-connected neural network with two outputs to make the final
prediction
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
# Wrap the network in a model object
model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='logo-
classifier.tfl.ckpt')
# Training it . 100 training passes and monitor it as it goes.
model.fit(X,Y, n_epoch=100, shuffle=True, validation_set=(X_test, Y_test),
show_metric=True, batch_size=64,
snapshot_epoch=True,
run_id='logo-classifier')
# Save model when training is complete to a file
model.save("logo-classifier.tfl")
print("Network trained and saved as logo-classifier.tfl!")
I get the following error
ValueError: Cannot feed value of shape (64, 10) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'
I have X and X_test with parameters of images and Y and Y_test with labeles in the pickle file. I have tried solutions from similar question, but the didn't work for me.
Any help would be appericiated.
Thanks.
Youve specified your output tensor shape as (?,2) and your labels is of the shape (?,10). Your label and output tensor shape must be the same.
You are getting that error because there is a mismatch between the shape of what you are feeding and what the tensorflow is expecting. To fix the issue, you might want to reshape your Y which is currently shaped at (64,10) to (?, 2). For example, you would do the following:
Y = np.reshape(Y, (-1, 2))

Categories