ValueError: Shapes (None, 1) and (None, None, 768) are incompatible - python

from transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = TFBertModel.from_pretrained("bert-base-uncased")
.
.
.
train = df2[:5000]
test = df2[5000:]
def convert_data_to_examples(train, test, text, Airline_Cat):
train_InputExamples = train.apply(lambda x: InputExample(guid=None,
text_a = x[text],
label = x[Airline_Cat]), axis = 1)
validation_InputExamples = test.apply(lambda x: InputExample(guid=None,
text_a = x[text],
label = x[Airline_Cat]), axis = 1)
return train_InputExamples, validation_InputExamples
train_InputExamples, validation_InputExamples = convert_data_to_examples(train, test, 'text', 'Airline_Cat')
def convert_examples_to_tf_dataset(examples, tokenizer, max_length=128):
features = []
for e in tqdm(examples):
input_dict = tokenizer.encode_plus(
e.text_a,
add_special_tokens=True,
max_length=max_length,
return_token_type_ids=True,
return_attention_mask=True,
pad_to_max_length=True,
truncation=True
)
input_ids, token_type_ids, attention_mask = (input_dict["input_ids"],input_dict["token_type_ids"], input_dict['attention_mask'])
features.append(InputFeatures( input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, label=e.label) )
def gen():
for f in features:
yield (
{
"input_ids": f.input_ids,
"attention_mask": f.attention_mask,
"token_type_ids": f.token_type_ids,
},
f.label,
)
return tf.data.Dataset.from_generator(
gen,
({"input_ids": tf.int32, "attention_mask": tf.int32, "token_type_ids": tf.int32}, tf.int64),
(
{
"input_ids": tf.TensorShape([None]),
"attention_mask": tf.TensorShape([None]),
"token_type_ids": tf.TensorShape([None]),
},
tf.TensorShape([]),
),
)
DATA_COLUMN = 'text'
LABEL_COLUMN = 'Airline_Cat'
train_data = convert_examples_to_tf_dataset(list(train_InputExamples), tokenizer)
train_data = train_data.shuffle(100).batch(32).repeat(2)
validation_data = convert_examples_to_tf_dataset(list (validation_InputExamples), tokenizer)
validation_data = validation_data.batch(32)
This is my code. I've used bert model for sentiment analysis and the next cell keeps throwing this error,
code:
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3, epsilon=1e-08, clipnorm=1.0)
loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.CategoricalAccuracy('accuracy')
model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
model.fit(train_data, epochs=2, validation_data=validation_data)
output:
Epoch 1/2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [41], in <cell line: 6>()
3 metric = tf.keras.metrics.CategoricalAccuracy('accuracy')
4 model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
----> 6 model.fit(train_data, epochs=2, validation_data=validation_data)
File ~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File C:\Users\JAYALA~1\AppData\Local\Temp\__autograph_generated_fileb_1mantj.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
File ~\anaconda3\lib\site-packages\transformers\modeling_tf_utils.py:1437, in TFPreTrainedModel.train_step(self, data)
1434 y_pred = y_pred[0]
1436 if loss is None:
-> 1437 loss = self.compiled_loss(y, y_pred, sample_weight, regularization_losses=self.losses)
1439 # Run backwards pass.
1440 self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
ValueError: in user code:
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\engine\training.py", line 1160, in train_function *
return step_function(self, iterator)
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\engine\training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\engine\training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\transformers\modeling_tf_utils.py", line 1437, in train_step
loss = self.compiled_loss(y, y_pred, sample_weight, regularization_losses=self.losses)
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\losses.py", line 272, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\losses.py", line 1990, in categorical_crossentropy
return backend.categorical_crossentropy(
File "C:\Users\Jayalakshmi\anaconda3\lib\site-packages\keras\backend.py", line 5529, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 1) and (None, None, 768) are incompatible
The airline sentiment column has three values positive, negative and neutral which I changed to numerics as 0,1 and 2. I tried many things like changing the loss type changing the sentiment values to only positive and negative by dropping neutral, etc. But it wouldn't work. And I also tried reshaping but couldn't find a way to reshape a bert model. I'm a very beginner to the domain and would like to get some help. Thanks in advance.

Related

ValueError: `logits` and `labels` must have the same shape, received ((None, 1) vs (None, 26328))

I'm trying to train a model for 2 classes. However, an error occurs: ValueError: logits and labels must have the same shape, received ((None, 1) vs (None, 26328)). I'm still looking for a solution and trying different options with Stackoverflow. Can you help me?
My past attempts at a solution consisted of changing one label size to another. In different variations.
The code is briefly below. Код кратко ниже. Ошибка возникает при работе history = model.fit(X_train, y_train, batch_size = 256, epochs = 200, validation_data = (X_valid, y_valid), callbacks = [es])
import os
import numpy as np
from scipy.io import loadmat
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
data = np.zeros((11, 26328, 6))
labels = np.zeros((11, 26328))
X = data
y = labels
#print('X', data.shape, '| y', labels.shape)
#Out: X (11, 26328, 6) | y (11, 26328)
n_samples = np.zeros((26328, 6)).shape[0]
n_channels = np.zeros((26328, 6)).shape[1]
n_filters = 32
model = PCNN1D(Chans = n_channels, Samples = n_samples, Filters = n_filters)
model.compile(optimizer = 'adam', loss = 'binary_crossentropy')
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.30)
#Train dataset size: (7, 26328, 6), (7, 26328)
#Valid dataset size: (4, 26328, 6), (4, 26328)
es = EarlyStopping(monitor = 'val_loss', mode = 'min', patience = 50, restore_best_weights = True)
history = model.fit(X_train, y_train, batch_size = 256, epochs = 200, validation_data = (X_valid, y_valid), callbacks = [es])
The error is completely:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3508\1516422158.py in <module>
89 lossfn = tf.keras.losses.BinaryCrossentropy(from_logits=False)
90 es = EarlyStopping(monitor = 'val_loss', mode = 'min', patience = 50, restore_best_weights = True)
---> 91 history = model.fit(X_train, y_train, batch_size = 256, epochs = 200, validation_data = (X_valid, y_valid), callbacks = [es])
92
93 # model.load_weights('model.h5')
~\Anaconda\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\Anaconda\lib\site-packages\keras\engine\training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\engine\training.py", line 1051, in train_function *
return step_function(self, iterator)
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\engine\training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\engine\training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\engine\training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\engine\training.py", line 948, in compute_loss
return self.compiled_loss(
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\engine\compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\losses.py", line 243, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\losses.py", line 1930, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "C:\Users\alesh\Anaconda\lib\site-packages\keras\backend.py", line 5283, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 1) vs (None, 26328)).
Now I'm trying to figure out the `logits'. What is it and whether it can be disabled.
If it helps, then I have such a model:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow.keras.regularizers import l2
def PCNN1D (Chans = 6, Samples = 206, Filters = 32):
eeg_input = Input(shape = (Samples, Chans))
padded = ZeroPadding1D(padding = 4)(eeg_input)
block1 = SeparableConv1D(Filters, 12, strides = 8,
padding = 'valid',
data_format = 'channels_last',
kernel_initializer = 'glorot_uniform',
bias_initializer = 'zeros',
use_bias = True)(padded)
block1 = Activation('tanh')(block1)
flatten = Flatten(name = 'flatten')(block1)
prediction = Dense(1, activation = 'sigmoid')(flatten)
return Model(inputs = eeg_input, outputs = prediction, name='PCNN-1D')

I'm new to MLP and currently, I'm working on a skin cancer dataset. I am facing this error and I am unable to make a model for the same

This is the code which I wrote for the building the model:
lr = 0.01
comms_round = 100
loss='categorical_crossentropy'
metrics = ['accuracy']
optimizer = SGD(lr=lr,
decay=lr / comms_round,
momentum=0.9
)
#initialize global model
smlp_global = SimpleMLP()
global_model = smlp_global.build(90000, 2)
#commence global training loop
for comm_round in range(comms_round):
# get the global model's weights - will serve as the initial weights for all local models
global_weights = global_model.get_weights()
#initial list to collect local model weights after scalling
scaled_local_weight_list = list()
#randomize client data - using keys
client_names= list(clients_batched.keys())
random.shuffle(client_names)
#loop through each client and create new local model
for client in client_names:
smlp_local = SimpleMLP()
local_model = smlp_local.build(90000, 2)
local_model.compile(loss=loss,
optimizer=optimizer,
metrics=metrics)
#set local model weight to the weight of the global model
local_model.set_weights(global_weights)
#fit local model with client's data
local_model.fit(clients_batched[client], epochs=1, verbose=0)
#scale the model weights and add to list
scaling_factor = weight_scalling_factor(clients_batched, client)
scaled_weights = scale_model_weights(local_model.get_weights(), scaling_factor)
scaled_local_weight_list.append(scaled_weights)
#clear session to free memory after each communication round
K.clear_session()
#to get the average over all the local model, we simply take the sum of the scaled weights
average_weights = sum_scaled_weights(scaled_local_weight_list)
#update global model
global_model.set_weights(average_weights)
#test global model and print out metrics after each communications round
for(X_test, Y_test) in test_batched:
global_acc, global_loss = test_model(X_test, Y_test, global_model, comm_round)
THESE ARE THE ERROR MESSAGES which I can see:-
ValueError Traceback (most recent call last)
in
28
29 #fit local model with client's data
---> 30 local_model.fit(clients_batched[client], epochs=1, verbose=0)
31
32 #scale the model weights and add to list
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy
target.shape.assert_is_compatible_with(output your text.shape)
ValueError: Shapes (None, 1) and (None, 2) are incompatible
I tried to make a model with the following shape :( 90000,2 ) , where in 90000 is the product of the 300*300 dimension of the image and 2 is the number of classes/categories, I have in my dataset.

Exception encountered when calling layer "model_5" (type Functional)

This is the following code :
Hyper parameters
batch_size = 64
nb_epoch = 50
Parameters for dataset
img_rows, img_cols = 160, 160
nb_classes = 10
Parameters for network
nb_lstm_outputs = 30
nb_time_steps = img_rows
dim_input_vector = img_cols
print('X_train original shape:', trainX.shape)
input_shape = (nb_time_steps, dim_input_vector)
trainX = trainX.astype('float32') / 255.
testX = testX.astype('float32') / 255.
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
print('X_train shape:', trainX.shape)
print(trainX.shape[0], 'train samples')
print(testX.shape[0], 'test samples')
Utility for our sequence model.
def get_sequence_model():
class_vocab = label_processor.get_vocabulary()
frame_features_input = keras.Input((input_shape))
x = keras.layers.GRU(16, return_sequences=True)(frame_features_input)
x = keras.layers.Dropout(0.4)(x)
x = keras.layers.GRU(8)(x)
x = keras.layers.Dropout(0.4)(x)
x = keras.layers.Dense(8, activation="relu")(x)
output = keras.layers.Dense(len(class_vocab), activation="softmax")(x)
rnn_model = keras.Model(frame_features_input, output)
rnn_model.compile(
loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"]
)
return rnn_model
Utility for running experiments.
def run_experiment():
seq_model = get_sequence_model()
history = seq_model.fit(
x = trainX,
y = trainY,
batch_size=64,
epochs=500,
validation_data=(testX, testY)
)
# seq_model.load_weights(filepath)
_, accuracy = seq_model.evaluate([test_data[0], test_data[1]], test_labels)
print(f"Test accuracy: {round(accuracy * 100, 2)}%")
return history, seq_model
_, sequence_model = run_experiment()
I am getting the following error :
enter image description here
Why am I getting the following error and how can I solve it?
Epoch 1/500
WARNING:tensorflow:Model was constructed with shape (None, 160, 160) for input KerasTensor(type_spec=TensorSpec(shape=(None, 160, 160), dtype=tf.float32, name='input_32'), name='input_32', description="created by layer 'input_32'"), but it was called on an input with incompatible shape (None, 160, 160, 3).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-138-a1d4870dee01> in <module>
40
41
---> 42 _, sequence_model = run_experiment()
2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 214, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "model_9" (type Functional).
Input 0 of layer "gru_29" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 160, 160, 3)
Call arguments received by layer "model_9" (type Functional):
• inputs=tf.Tensor(shape=(None, 160, 160, 3), dtype=float32)
• training=True
• mask=None
Can't solve this problem can someone help?

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 128

I am trying to build a transformer neural network for a text classification problem. I am following this tutorial but tweaking the encoder to include a layer with a softmax activation function.
This is the code that I changed:
class Encoder(tf.keras.layers.Layer):
def __init__(self,
*,
num_layers,
d_model, # Input/output dimensionality.
num_attention_heads,
dff, # Inner-layer dimensionality.
input_vocab_size, # Input vocabulary size.
dropout_rate=0.1
):
super().__init__()
self.d_model = d_model
self.num_layers = num_layers
# Embeddings + Positional encoding
self.pos_embedding = PositionalEmbedding(input_vocab_size, d_model)
# Encoder layers.
self.enc_layers = [
EncoderLayer(
d_model=d_model,
num_attention_heads=num_attention_heads,
dff=dff,
dropout_rate=dropout_rate)
for _ in range(num_layers)]
# Dropout.
self.dropout = tf.keras.layers.Dropout(dropout_rate)
self.flatten_layer = tf.keras.layers.GlobalAveragePooling1D()
self.dense_layer_1 = tf.keras.layers.Dense(256, activation='relu')
self.dense_layer_2 = tf.keras.layers.Dense(32, activation='relu')
self.final_layer = tf.keras.layers.Dense(2, activation='softmax')
# Masking.
def compute_mask(self, x, previous_mask=None):
return self.pos_embedding.compute_mask(x, previous_mask)
def call(self, x, training):
seq_len = tf.shape(x)[1]
# Sum up embeddings and positional encoding.
mask = self.compute_mask(x)
x = self.pos_embedding(x) # Shape `(batch_size, input_seq_len, d_model)`.
# Add dropout.
x = self.dropout(x, training=training)
# N encoder layers.
for i in range(self.num_layers):
x = self.enc_layers[i](x, training, mask)
# experimental thing, may not work
x = self.flatten_layer(x)
x = self.dense_layer_1(x)
x = self.dense_layer_2(x)
# x = tf.keras.layers.Dense(32, activation='relu')(x)
x = self.final_layer(x)
return x # Shape `(batch_size, input_seq_len, d_model)`.
I am using the Twitter sentiment analysis dataset to classify positive or negative sentences.
My sequence length is 128.
The shape of my features is (43012, 128), and the shape of the labels is (43012, 1).
The model definition is:
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(128)),
Encoder(num_layers = 4, d_model = 128, dff = 512, num_attention_heads = 8, input_vocab_size=tokenizers.en.get_vocab_size())
])
Which I have compiled as:
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
But when I try to train my model
history = model.fit(
x=new_features,
y=new_labels,
epochs=200,
batch_size=64,
validation_split=0.2
)
it is giving me the following error:
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1160, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1053, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 159, in __call__
losses, sample_weight, reduction=reduction
File "/usr/local/lib/python3.7/dist-packages/keras/utils/losses_utils.py", line 350, in compute_weighted_loss
) = squeeze_or_expand_dimensions(losses, None, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/losses_utils.py", line 224, in squeeze_or_expand_dimensions
sample_weight = tf.squeeze(sample_weight, [-1])
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 128 for '{{node sparse_categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [?,128].
I tested the model with a random input without training and it works fine.
input = np.random.rand(5, 128)
x = model((input))
x.shape # (5, 2) as expected
Why am I getting this error and how can I fix this?

problem with dimensions when using keras text_dataset_from_directory i get (None,) and can't lode it in to a model

I am using keras.utils.text_dataset_from_directory (see code). when I reach model.fit I get a warning about input dimentions and a error (that I think has something to do with the output).
code:
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Rescaling
MAX_VAL = 0.5
num_classes = 2
train_ds = keras.utils.text_dataset_from_directory(
directory='.../training_data/',
labels='inferred',
label_mode='categorical',
class_names=None,
batch_size=32,
max_length=None,
shuffle=True,
seed=None,
validation_split=None,
subset=None,
follow_links=False)
validation_ds = keras.utils.text_dataset_from_directory(
directory='.../validation_data/',
labels='inferred',
label_mode='categorical',
class_names=None,
batch_size=32,
max_length=None,
shuffle=True,
seed=None,
validation_split=None,
subset=None,
follow_links=False)
inputs = keras.Input(shape=(None,))
x = layers.Reshape((-1, 1))(inputs)
x = Rescaling(scale=1.0 / MAX_VAL)(x)
x = layers.Dense(32, activation="softmax")(x)
outputs = layers.Dense(num_classes, activation="softmax")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.summary()
keras.utils.plot_model(model, "my_first_model.png")
model.compile(optimizer='Adam', loss='categorical_crossentropy')
history = model.fit(train_ds, epochs=10, validation_data=validation_ds)
output:
Epoch 1/10
WARNING:tensorflow:Model was constructed with shape (None, None) for input KerasTensor(type_spec=TensorSpec(shape=(None, None), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None,).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-3656f32a72a9> in <module>
1 model.compile(optimizer='Adam', loss='categorical_crossentropy')
----> 2 history = model.fit(train_ds, epochs=10, validation_data=validation_ds)
~\AppData\Roaming\Python\Python38\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "...\Python\Python38\site-packages\keras\engine\training.py", line 1021, in train_function *
return step_function(self, iterator)
File "...\Python\Python38\site-packages\keras\engine\training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "...\Python\Python38\site-packages\keras\engine\training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "...\Python\Python38\site-packages\keras\engine\training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "...\Python\Python38\site-packages\keras\engine\training.py", line 918, in compute_loss
return self.compiled_loss(
File "...\Python\Python38\site-packages\keras\engine\compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "...\Python\Python38\site-packages\keras\losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "...\Python\Python38\site-packages\keras\losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "...\Python\Python38\site-packages\keras\losses.py", line 1789, in categorical_crossentropy
return backend.categorical_crossentropy(
File "...\Python\Python38\site-packages\keras\backend.py", line 5083, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 2) and (None, 1, 2) are incompatible
The inputs to the model are 1d vectors (of length ~27000) saved in .txt files:
0.101471743,0.099917953,0.103334975,0.099364908,0.099035715,...,0.097369999,0.099680934
readin dataset frome dir formated as:
/training_data/
...class_a/
......a_text_1.txt
......a_text_2.txt
...class_b/
......b_text_1.txt
......b_text_2.txt
/validation_data/
...class_a/
......a_text_1.txt
......a_text_2.txt
...class_b/
......b_text_1.txt
......b_text_2.txt
How can I get the dimensions right?
EDIT:
I saved the data as a .jpg file and loaded it using image_dataset_from_directory. this fixed the issue. but I would still like to understand why I cant get the data from .txt files properly. (I lose a lot of data transferring from float data to 8bit int in .jpg. and using image_dataset_from_directory requires all img size to be the same length, I wand my data to be different sizes).

Categories