Problem implementing straight through estimator in tensorflow - python

I want to implement a straight through estimator in tensorflow. I am using tf.stop_gradient() method for this. When I implement in the inner layers it works fine but when used on the last layer it throws an error. Below are my code and error.
def generator():
model = Sequential([
Embedding(21, 100, input_shape = (50, )),
Conv1D(100, 3,2, padding = "same", activation = "relu"),
Conv1D(100, 3,2, padding = "same", activation = "relu"),
Conv1D(21, 3,1, padding = "same", activation = "relu"),
Flatten(),
Dense(1,"sigmoid", trainable = False),
Lambda(lambda x: tf.stop_gradient(x)),
])
return model
gen = generator()
def dis_inputs():
ind = np.random.randint(0, len(x_train), batch_size * 2)
real = x_train[ind]
out = np.ones((batch_size * 2, ))
out[: batch_size ] = 0
return real, out
batch_size = 128
x, y = dis_inputs()
gen.compile("adam", "binary_crossentropy")
gen.fit(x, y, epochs = 100)
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 816, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 532, in minimize
return self.apply_gradients(grads_and_vars, name=name)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 633, in apply_gradients
grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/utils.py", line 73, in filter_empty_gradients
raise ValueError(f"No gradients provided for any variable: {variable}. "
ValueError: No gradients provided for any variable: (['embedding_4/embeddings:0', 'conv1d_12/kernel:0', 'conv1d_12/bias:0', 'conv1d_13/kernel:0', 'conv1d_13/bias:0', 'conv1d_14/kernel:0', 'conv1d_14/bias:0'],). Provided `grads_and_vars` is ((None, <tf.Variable 'embedding_4/embeddings:0' shape=(21, 100) dtype=float32>), (None, <tf.Variable 'conv1d_12/kernel:0' shape=(3, 100, 100) dtype=float32>), (None, <tf.Variable 'conv1d_12/bias:0' shape=(100,) dtype=float32>), (None, <tf.Variable 'conv1d_13/kernel:0' shape=(3, 100, 100) dtype=float32>), (None, <tf.Variable 'conv1d_13/bias:0' shape=(100,) dtype=float32>), (None, <tf.Variable 'conv1d_14/kernel:0' shape=(3, 100, 21) dtype=float32>), (None, <tf.Variable 'conv1d_14/bias:0' shape=(21,) dtype=float32>)).

Related

Python - Shapes (None, 1) and (None, 25) are incompatible of CNN model

I'm trying to train the model but I am stuck with this error
ValueError: Shapes (None, 1) and (None, 24) are incompatible
My code
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(25, activation=tf.nn.softmax))
model.summary()
Model Summary
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_18 (Conv2D) (None, 26, 26, 32) 320
conv2d_19 (Conv2D) (None, 24, 24, 64) 18496
conv2d_20 (Conv2D) (None, 22, 22, 128) 73856
max_pooling2d_8 (MaxPooling (None, 11, 11, 128) 0
2D)
dropout_8 (Dropout) (None, 11, 11, 128) 0
flatten_5 (Flatten) (None, 15488) 0
dense_10 (Dense) (None, 128) 1982592
dropout_9 (Dropout) (None, 128) 0
dense_11 (Dense) (None, 25) 3225
=================================================================
Total params: 2,078,489
Trainable params: 2,078,489
Non-trainable params: 0
Complier
model.compile(loss=tf.keras.losses.categorical_crossentropy,
optimizer=tf.keras.optimizers.Adadelta(), metrics=['accuracy'])
Fit the model
model.fit(train_img, train_y_values, batch_size=64, epochs=5,
verbose=1, validation_data=(test_img, test_y_values))
Error
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-1ace0d4f685d> in <module>
1 model.fit(train_img, train_y_values, batch_size=64, epochs=5,
----> 2 verbose=1, validation_data=(test_img, test_y_values))
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.shape)
ValueError: Shapes (None, 1) and (None, 24) are incompatible
train_img shape = (27455, 28, 28)
train_y_value shape = (27455,)
test_img shape = (7172, 28, 28)
test_y_values shape = (7172,)
Somebody help me please
The problem comes from a mismatch of the last layer of your model (None, 25) and the labels (None, 1).
This can be fixed by changing the loss function to "sparse_categorical_crossentropy"
See What is the difference between sparse_categorical_crossentropy and categorical_crossentropy? for more details.

stacking LSTM layer on top of BERT encoder in Keras

I have been trying to stack a single LSTM layer on top of Bert embeddings, but whilst my model starts to train it fails on the last batch and throws the following error message:
Node: 'model/tf.reshape/Reshape'
Input to reshape is a tensor with 59136 values, but the requested shape has 98304
[[{{node model/tf.reshape/Reshape}}]] [Op:__inference_train_function_70500]
This is how I build the model and I honestly cannot figure out what is going wrong here:
batch_size = 128
bert_preprocess = hub.load('https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3')
bert_encoder = hub.KerasLayer('https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4', trainable=True)
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
preprocessed_text = bert_preprocess(text_input)
outputs = bert_encoder(preprocessed_text) #shape=(None, 768)
bert_output = outputs['pooled_output']
l = tf.reshape(bert_output, [batch_size, 1, 768])
l = tf.keras.layers.LSTM(32, activation='relu')(l)
l = tf.keras.layers.Dropout(0.1, name='dropout')(l)
l = tf.keras.layers.Dense(8, activation='softmax', name="output")(l)
model = tf.keras.Model(inputs=[text_input], outputs = [l])
print(model.summary())
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
this is the full output:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
text (InputLayer) [(None,)] 0 []
keras_layer (KerasLayer) {'input_mask': (Non 0 ['text[0][0]']
e, 128),
'input_word_ids':
(None, 128),
'input_type_ids':
(None, 128)}
keras_layer_1 (KerasLayer) {'sequence_output': 109482241 ['keras_layer[0][0]',
(None, 128, 768), 'keras_layer[0][1]',
'default': (None, 'keras_layer[0][2]']
768),
'encoder_outputs':
[(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768)],
'pooled_output': (
None, 768)}
tf.reshape (TFOpLambda) (128, 1, 768) 0 ['keras_layer_1[0][13]']
lstm (LSTM) (128, 32) 102528 ['tf.reshape[0][0]']
dropout (Dropout) (128, 32) 0 ['lstm[0][0]']
output (Dense) (128, 8) 264 ['dropout[0][0]']
==================================================================================================
Total params: 109,585,033
Trainable params: 102,792
Non-trainable params: 109,482,241
__________________________________________________________________________________________________
None
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fc4ff809440> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fc4ff809440> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
18/19 [===========================>..] - ETA: 25s - loss: 1.5747 - accuracy: 0.5456Traceback (most recent call last):
File "bert-test-lstm.py", line 62, in <module>
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 55, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'model/tf.reshape/Reshape' defined at (most recent call last):
File "bert-test-lstm.py", line 62, in <module>
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1021, in train_function
return step_function(self, iterator)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1010, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1000, in run_step
outputs = model.train_step(data)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/functional.py", line 452, in call
inputs, training=training, mask=mask)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/functional.py", line 589, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/layers/core/tf_op_layer.py", line 226, in _call_wrapper
return self._call_wrapper(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/layers/core/tf_op_layer.py", line 261, in _call_wrapper
result = self.function(*args, **kwargs)
Node: 'model/tf.reshape/Reshape'
Input to reshape is a tensor with 59136 values, but the requested shape has 98304
The code runs perfectly fine if I just drop the LSTM and reshape layers - any help is appreciated.
You should use tf.keras.layers.Reshape in order to reshape bert_output into a 3D tensor and automatically taking into account the batch dimension.
Simply changing:
l = tf.reshape(bert_output, [batch_size, 1, 768])
into:
l = tf.keras.layers.Reshape((1,768))(bert_output)
should work.

CNN Resnet Classification

I am a total newbie here so if I write something wrong sorry in advance.
I am trying to use Resnet50 model to classify a skin illness.
When I try to run the model there is an error occurring. First, I thought about the cause of the data type of images I convert them to and array to tensor but didn't work.
Here is my code
directory_infection_negative = glob.glob("/content/drive/MyDrive/DFU_Classification_Datasets/DFU_Classification_Datasets/PartB_DFU_Dataset/Infection/Aug-Negative/*")
for img in directory_infection_negative:
image = cv.imread(img)
imageRGB = cv.cvtColor(image, cv.COLOR_BGR2RGB)
img = cv2.resize(imageRGB, (size,size), interpolation = cv2.INTER_AREA)
imgT = tf.convert_to_tensor(img)
datasets.append(imgT)
labels.append(0)
def resnet(input_shape, n_classes):
def conv_bn_rl(x, f, k=1, s=1, p='same'):
x = Conv2D(f, k, strides=s, padding=p)(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
def identity_block(tensor, f):
x = conv_bn_rl(tensor, f)
x = conv_bn_rl(x, f, 3)
x = Conv2D(4*f, 1)(x)
x = BatchNormalization()(x)
x = Add()([x, tensor])
output = ReLU()(x)
return output
def conv_block(tensor, f, s):
x = conv_bn_rl(tensor, f)
x = conv_bn_rl(x, f, 3, s)
x = Conv2D(4*f, 1)(x)
x = BatchNormalization()(x)
shortcut = Conv2D(4*f, 1, strides=s)(tensor)
shortcut = BatchNormalization()(shortcut)
x = Add()([x, shortcut])
output = ReLU()(x)
return output
def resnet_block(x, f, r, s=2):
x = conv_block(x, f, s)
for _ in range(r-1):
x = identity_block(x, f)
return x
input = Input(input_shape)
x = conv_bn_rl(input, 64, 7, 2)
x = MaxPool2D(3, strides=2, padding='same')(x)
x = resnet_block(x, 64, 3, 1)
x = resnet_block(x, 128, 4)
x = resnet_block(x, 256, 6)
x = resnet_block(x, 512, 3)
x = GlobalAvgPool2D()(x)
output = Dense(n_classes, activation='softmax')(x)
model = Model(input, output)
return model
INPUT_SHAPE = 224,224,3
N_CLASSES = 4
K.clear_session()
model = resnet(INPUT_SHAPE, N_CLASSES)
model.compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics=['accuracy'])
model.summary()
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
X_train, X_test, y_train, y_test = train_test_split(datasets, to_categorical(np.array(labels)), test_size=0.2, random_state=0)
y_train = to_categorical(y_train, 4)
y_test = to_categorical(y_test, 4)
y_train.shape
print(len(X_train))
print(len(X_test))
print(len(y_train))
print(len(y_test))
print(type(X_train))
print(type(X_test))
print(type(X_train[0]))
history = model.fit((X_train), (y_train), batch_size = 32, verbose = 1, epochs= 50, validation_split=0.1, shuffle = False)
print('Test Accuracy: {:.2f}%'.format(model.evaluate(np.array(X_test), np.array(y_test))[1]*100))
f,(ax1, ax2) = plt.subplots(1,2, figsize=(12,4))
t = f.suptitle('CNN Performance', fontsize=12)
f.subplots_adjust(top =0.85, wspace=0.3)
max_epoch = len(history.history['accuracy'])+1
epoch_list = list(range(1,max_epoch))
ax1.plot(epoch_list, history.history['accuracy'], label='Train Accuracy')
ax1.plot(epoch_list, history.history['val_accuracy'], label='Validation Accuracy')
ax1.set_xticks(np.arange(1, max_epoch,5))
ax1.set_ylabel('Accuracy Value')
ax1.set_xlabel('Epoch')
ax1.set_title('Accuracy')
l1 = ax1.legend(loc="best")
ax2.plot(epoch_list, history.history['loss'], label='Train Loss')
ax2.plot(epoch_list, history.history['val_loss'], label='Validation Loss')
ax2.set_xticks(np.arange(1, max_epoch,5))
ax2.set_ylabel('Loss Value')
ax2.set_xlabel('Epoch')
ax2.set_title('Loss')
l2 = ax2.legend(loc="best")
model.save('DFU Classifier - Resnet50')
Error Says :
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1127 except Exception as e: # pylint:disable=broad-except
1128 if hasattr(e, "ag_error_metadata"):
-> 1129 raise e.ag_error_metadata.to_exception(e)
1130 else:
1131 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, 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 199, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "model" expects 1 input(s), but it received 12608 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:2' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:3' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:4' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:5' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:6' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:7' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:8' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:9' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:10' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:11' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:12' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:13' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:14' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:15' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:16' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:17' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:18' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:19' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:20' shape=(None, 224, 3) dtype=uin...
Thank you so much in advance
So, i will count the issues :
You do not need ( should not) convert images to tensor if you are using fit. So, delete:
imgT = tf.convert_to_tensor(img)
After that you should convert datasets or at least whatever you are inputting to the network np.array or list of np.array. Fit expects list of arrays or np.array itself otherwise it causes trouble by going against their algorithmic logic .So, converting datasets to a suitable format would solve that many inputs issue:
datasets = np.array(datasets, dtype=np.float32)

CLDNN Compile and Training Issue

I am running into an issue running a CLDNN model in Python 3. I ran model.summary() and I did not see an issue. I am receiving a sizing/shaping error between Keras and Tensorflow.
Below is the code with the output error. Any input would be greatly apricated.
Update: reformatted the code and supplied the output as text.
def cldnn():
model = Sequential()
#input layer
model.add(keras.layers.InputLayer(input_shape=(2, 128, 1)))
#first layer
model.add(Conv2D(256, (1, 3), activation='relu', use_bias=True))
model.add(Dropout(0.2))
#second layer
model.add(Conv2D(256, (2, 3), activation='relu', use_bias=True))
#third layer
model.add(Conv2D(80, (1, 3), activation='relu', use_bias=True))
model.add(Dropout(0.2))
#fourth layer
model.add(Conv2D(80, (1, 3), activation='relu', use_bias=True))
#reshape data
model.add(Reshape((120,80)))
#fifth layer
model.add(LSTM(50, activation='tanh', return_sequences=True))
#sixth layer
model.add(Dense(128,
activation='relu',
use_bias=True,
kernel_initializer='normal'))
#output layer
model.add(Dense(10, activation='softmax', use_bias=True))
model.summary()
return model
#Create instance of CNN model graph
cldnn = cldnn()
#Compile model using an appropriate loss and optimizer algorithm
cldnn.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
cldnn_mdata = cldnn.fit(data_train, labels_train,
validation_data=(data_test, labels_test),
epochs=15,
batch_size=1024,
shuffle=True)
#Print accuracy of model on testing set after training
scores = cldnn.evaluate(data_test, labels_test)
print("Accuracy: %.2f%%" %(scores[1]*100))
#OUTPUT
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param # =================================================================
conv2d_16 (Conv2D) (None, 2, 126, 256) 1024
_________________________________________________________________
dropout_8 (Dropout) (None, 2, 126, 256) 0
_________________________________________________________________
conv2d_17 (Conv2D) (None, 1, 124, 256) 393472
_________________________________________________________________
conv2d_18 (Conv2D) (None, 1, 122, 80) 61520
_________________________________________________________________
dropout_9 (Dropout) (None, 1, 122, 80) 0
_________________________________________________________________
conv2d_19 (Conv2D) (None, 1, 120, 80) 19280
_________________________________________________________________
reshape_3 (Reshape) (None, 120, 80) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 120, 50) 26200
_________________________________________________________________
dense_6 (Dense) (None, 120, 128) 6528
_________________________________________________________________
dense_7 (Dense) (None, 120, 10) 1290
=================================================================
Total params: 509,314
Trainable params: 509,314
Non-trainable params: 0
_________________________________________________________________
Epoch 1/15
WARNING:tensorflow:Model was constructed with shape (None, 2, 128, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 2, 128, 1), dtype=tf.float32, name='input_5'), name='input_5', description="created by layer 'input_5'"), but it was called on an input with incompatible shape (None, 28, 28, 1).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-0e0ee3a37c65> in <module>()
52 epochs=15,
53 batch_size=1024,
---> 54 shuffle=True)
55
56 #Print accuracy of model on testing set after training
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 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:2849 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:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:787 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py:369 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:415 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:550 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:539 call
result.set_shape(self.compute_output_shape(inputs.shape))
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:530 compute_output_shape
self.target_shape)
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:518 _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged, input_shape = [27, 20, 80], output_shape = [120, 80]

ValueError: Shapes (None, 3) and (None, 1) are incompatible

I have a problem with my machine learning code
here is the model :
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=input_shape),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(128, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(512, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='softmax')])
model.summary()
This is my model.summary() result :
Model: "sequential_32"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_128 (Conv2D) (None, 148, 148, 32) 896
_________________________________________________________________
max_pooling2d_128 (MaxPoolin (None, 74, 74, 32) 0
_________________________________________________________________
conv2d_129 (Conv2D) (None, 72, 72, 64) 18496
_________________________________________________________________
max_pooling2d_129 (MaxPoolin (None, 36, 36, 64) 0
_________________________________________________________________
conv2d_130 (Conv2D) (None, 34, 34, 128) 73856
_________________________________________________________________
max_pooling2d_130 (MaxPoolin (None, 17, 17, 128) 0
_________________________________________________________________
conv2d_131 (Conv2D) (None, 15, 15, 512) 590336
_________________________________________________________________
max_pooling2d_131 (MaxPoolin (None, 7, 7, 512) 0
_________________________________________________________________
flatten_32 (Flatten) (None, 25088) 0
_________________________________________________________________
dense_79 (Dense) (None, 128) 3211392
_________________________________________________________________
dense_80 (Dense) (None, 1) 129
=================================================================
Total params: 3,895,105
Trainable params: 3,895,105
Non-trainable params: 0
And this is the compile setting i use :
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
And this is the fit model code :
EPOCH = 100
history = model.fit(train_data,
steps_per_epoch=len(train_generator),
epochs=EPOCH,
validation_data=val_data,
validation_steps=len(val_generator),
shuffle=True,
verbose = 1)
For the train_data i create use tensorflow tf.data cause i think it is more compatible with tf.keras. this is the tf.data generator function code :
def tf_data_generator(generator, input_shape):
num_class = generator.num_classes
tf_generator = tf.data.Dataset.from_generator(
lambda: generator,
output_types=(tf.float32, tf.float32),
output_shapes=([None
, input_shape[0]
, input_shape[1]
, input_shape[2]]
,[None, num_class])
)
return tf_generator
train_data = tf_data_generator(train_generator, input_shape)
val_data = tf_data_generator(val_generator, input_shape)
actually i got that function from medium.com as source. But i got error when try to train my machine learning code, can someone help me solve the error, this is the error message :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-448faadd058c> in <module>()
6 validation_steps=len(val_generator),
7 shuffle=True,
----> 8 verbose = 1)
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:855 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 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:838 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:797 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:155 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:259 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1644 categorical_crossentropy
y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4862 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 3) and (None, 1) are incompatible
Sorry if my question is confusing, i still new in machine learning field. Thanks for helping me
I guess you intend to get a multi-class classifier, for 3 classes. If that is the case, you wrongly assigned the last layer to a DENSE of size 1. You can solve the issue by replacing this line:
tf.keras.layers.Dense(1, activation='softmax')])
by this:
tf.keras.layers.Dense(3, activation='softmax')])

Categories