I am trying to build a Wide and Deep Neural Network using Keras Functional API. I am getting a value to shape mismatch error. I don't understand where I am wrong. I am implementing this on the Fashion MNIST dataset.The X_train shape is (60000,28,28) and Y_train is (60000,). I am guessing that the error is because of the line : input_ = keras.layers.... but I don't understand how to resolve it.
Code :
# Building a Non Sequnetial Model using Functional API One Use of it is in Wide and Deep Neural Networks
input_ = keras.layers.Input(shape=X_train.shape[1:]) # This will return shape of the input [28,28],remeber we dont have to set it to the number of neurons in the layer
hidden1 = keras.layers.Dense(100,activation = "relu")(input_) # We have to call it as a function
hidden2 = keras.layers.Dense(100,activation = "relu")(hidden1)
concat_layer = keras.layers.concatenate([input_,hidden2])
output = keras.layers.Dense(10,activation="softmax")(concat_layer)
model = keras.models.Model(inputs=[input_], outputs=[output])
model.compile(loss = keras.losses.sparse_categorical_crossentropy,optimizer = keras.optimizers.SGD(lr = 0.8),metrics= ["accuracy"])
Tensorboard_cb = keras.callbacks.TensorBoard(Path_Tensor)
model.fit(X_train,Y_train,validation_split=0.2,epochs=100,callbacks=[Tensorboard_cb])
Error :
Epoch 1/100
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-97-675a2b302d27> in <module>
----> 1 model.fit(X_train,Y_train,validation_split=0.2,epochs=100,callbacks=[Tensorboard_cb])
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1096 batch_size=batch_size):
1097 callbacks.on_train_batch_begin(step)
-> 1098 tmp_logs = train_function(iterator)
1099 if data_handler.should_sync:
1100 context.async_wait()
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
821 # This is the first call of __call__, so we have to initialize.
822 initializers = []
--> 823 self._initialize(args, kwds, add_initializers_to=initializers)
824 finally:
825 # At this point we know that the initialization is complete (or less
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
695 self._concrete_stateful_fn = (
696 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 697 *args, **kwds))
698
699 def invalid_creator_scope(*unused_args, **unused_kwds):
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2853 args, kwargs = None, None
2854 with self._lock:
-> 2855 graph_function, _, _ = self._maybe_define_function(args, kwargs)
2856 return graph_function
2857
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
3211
3212 self._function_cache.missed.add(call_context_key)
-> 3213 graph_function = self._create_graph_function(args, kwargs)
3214 self._function_cache.primary[cache_key] = graph_function
3215 return graph_function, args, kwargs
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3073 arg_names=arg_names,
3074 override_flat_arg_shapes=override_flat_arg_shapes,
-> 3075 capture_by_value=self._capture_by_value),
3076 self._function_attributes,
3077 function_spec=self.function_spec,
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
984 _, original_func = tf_decorator.unwrap(python_func)
985
--> 986 func_outputs = python_func(*func_args, **func_kwargs)
987
988 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args, **kwds)
598 # __wrapped__ allows AutoGraph to swap in a converted function. We give
599 # the function a weak reference to itself to avoid a reference cycle.
--> 600 return weak_wrapped_fn().__wrapped__(*args, **kwds)
601 weak_wrapped_fn = weakref.ref(wrapped_fn)
602
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\func_graph.py in wrapper(*args, **kwargs)
971 except Exception as e: # pylint:disable=broad-except
972 if hasattr(e, "ag_error_metadata"):
--> 973 raise e.ag_error_metadata.to_exception(e)
974 else:
975 raise
ValueError: in user code:
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\training.py:749 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\losses.py:149 __call__
losses = ag_call(y_true, y_pred)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\losses.py:253 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\losses.py:1567 sparse_categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\keras\backend.py:4783 sparse_categorical_crossentropy
labels=target, logits=output)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\ops\nn_ops.py:4176 sparse_softmax_cross_entropy_with_logits_v2
labels=labels, logits=logits, name=name)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
c:\users\na462\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\ops\nn_ops.py:4091 sparse_softmax_cross_entropy_with_logits
logits.get_shape()))
ValueError: Shape mismatch: The shape of labels (received (32, 1)) should equal the shape of logits except for the last dimension (received (32, 28, 10)).
you forgot to add Flatten Layer, you input is 3D (None,28,28) but your output is only 2D (None,10), since you did not Flatten so you are getting (None, 28, 10) as out which is wrong.
also i think you did not use one hot encoding, because i can see (32,1) as your output shape rather then (32, 10). you need to change your last Dense layer neuron to 1, or use one hot encoding.
if you want to use Dense(1) then you have to make sure that your mnist dataset output is numeric, such as 0,1,2,3,4..9. but if you want to use Dense(10) then you have to use onehot encoding, your output will be [1,0,0,0,0,0,0,0,0,0] this is equal to 0 in decimal number system. [0,1,0,0,0,0,0,0,0,0] this is equal to 1 in decimal number system. it will have ten columns, each represent single decimal number, if a number in a column is 1 which mean it is that decimal number.
1st column mean 0, [1,0,0,0,0,0,0,0,0,0]
2nd column mean 1 [0,1,0,0,0,0,0,0,0,0]
3rd column mean 2 [0,0,1,0,0,0,0,0,0,0]
so on
last column mean 9 [0,0,0,0,0,0,0,0,0,1]
as for flatten layer, if you input is image then image has width, height and color, which are 3 dimensions. but as you can see above your output is either a single number or 10 columns of 0s and 1s. in order to make 3 dimension in your 1 dimension output you have to use flatten layer. also there is another hidden dimension which represent your data samples, or batch size. in your case it is 32. so your input is (32, 28, 28). and your output is (32, 10).
another thing is you can also use flatten layer first after you feed input. you can give input in the form of (None, 784) here 784 is (28 multiple 28)
Related
I'm trying to rerun code from this github link, when it comes to the chunck of code below:
tf.keras.backend.clear_session()
# SET SEED FOR REPRODUCIBILITY
np.random.seed(seed)
n_past = 30
batch_size = 64
n_dims = input_df.shape[1]
mat_X_train, mat_y_train = windowed_dataset(X_train, y_train, n_past)
# CONSTRUCTING MULTIVARIATE BIDIRECTIONAL LSTM NN
lstm_5 = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=[n_past, n_dims]),
# BATCH NORMALIZATION
tf.keras.layers.BatchNormalization(),
# ADDING 1st LSTM LAYER
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)),
tf.keras.layers.Dropout(0.1),
# ADDING 2nd LSTM LAYER
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16)),
tf.keras.layers.Dropout(0.1),
# DENSE OUTPUT LAYER
tf.keras.layers.Dense(1)
])
lstm_5.compile(loss='mse',
optimizer="adam",
metrics=[rmspe])
checkpoint_cb = ModelCheckpoint('lstm_5.h5',
save_best_only=True,
monitor='val_rmspe')
# STOPPING THE TRAINING IF VALIDATION RMSPE IS NOT IMPROVING
early_stopping_cb = EarlyStopping(patience=30,
restore_best_weights=True,
monitor='val_rmspe')
print(lstm_5.summary())
It raises the following type error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-345-9065b1b40a1c> in <module>
11
12 # CONSTRUCTING MULTIVARIATE BIDIRECTIONAL LSTM NN
---> 13 lstm_5 = tf.keras.models.Sequential([
14 tf.keras.layers.InputLayer(input_shape=[n_past, n_dims]),
15 # BATCH NORMALIZATION
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
585 with no_automatic_dependency_tracking_scope(model):
586 model.arr2 = [] # Creates a regular, untracked python list
--> 587 ```
588
589 Args:
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\sequential.py in __init__(self, layers, name)
132 layers = [layers]
133 for layer in layers:
--> 134 self.add(layer)
135
136 #property
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
585 with no_automatic_dependency_tracking_scope(model):
586 model.arr2 = [] # Creates a regular, untracked python list
--> 587 ```
588
589 Args:
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\sequential.py in add(self, layer)
215 # If the model is being built continuously on top of an input layer:
216 # refresh its output.
--> 217 output_tensor = layer(self.outputs[0])
218 if len(tf.nest.flatten(output_tensor)) != 1:
219 raise ValueError(SINGLE_LAYER_OUTPUT_ERROR_MSG)
~\AppData\Roaming\Python\Python38\site-packages\keras\layers\wrappers.py in __call__(self, inputs, initial_state, constants, **kwargs)
581
582 if initial_state is None and constants is None:
--> 583 return super(Bidirectional, self).__call__(inputs, **kwargs)
584
585 # Applies the same workaround as in `RNN.__call__`
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
974 # >> model = tf.keras.Model(inputs, outputs)
975 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 976 return self._functional_construction_call(inputs, args, kwargs,
977 input_list)
978
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1112 layer=self, inputs=inputs, build_graph=True, training=training_value):
1113 # Check input assumptions set after layer building, e.g. input shape.
-> 1114 outputs = self._keras_tensor_symbolic_call(
1115 inputs, input_masks, args, kwargs)
1116
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
846 return tf.nest.map_structure(keras_tensor.KerasTensor, output_signature)
847 else:
--> 848 return self._infer_output_signature(inputs, args, kwargs, input_masks)
849
850 def _infer_output_signature(self, inputs, args, kwargs, input_masks):
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
886 self._maybe_build(inputs)
887 inputs = self._maybe_cast_inputs(inputs)
--> 888 outputs = call_fn(inputs, *args, **kwargs)
889
890 self._handle_activity_regularization(inputs, outputs)
~\AppData\Roaming\Python\Python38\site-packages\keras\layers\wrappers.py in call(self, inputs, training, mask, initial_state, constants)
696 forward_state, backward_state = None, None
697
--> 698 y = self.forward_layer(forward_inputs,
699 initial_state=forward_state, **kwargs)
700 y_rev = self.backward_layer(backward_inputs,
~\AppData\Roaming\Python\Python38\site-packages\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
657
658 if initial_state is None and constants is None:
--> 659 return super(RNN, self).__call__(inputs, **kwargs)
660
661 # If any of `initial_state` or `constants` are specified and are Keras
~\AppData\Roaming\Python\Python38\site-packages\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
1035 with autocast_variable.enable_auto_cast_variables(
1036 self._compute_dtype_object):
-> 1037 outputs = call_fn(inputs, *args, **kwargs)
1038
1039 if self._activity_regularizer:
~\AppData\Roaming\Python\Python38\site-packages\keras\layers\recurrent_v2.py in call(self, inputs, mask, training, initial_state)
1263
1264 if self.return_sequences:
-> 1265 output = backend.maybe_convert_to_ragged(
1266 is_ragged_input, outputs, row_lengths, go_backwards=self.go_backwards)
1267 else:
TypeError: maybe_convert_to_ragged() got an unexpected keyword argument 'go_backwards'
Does someone could help to deal with this issue? I think it may be related to tensorflow version (I installed tensorflow==2.6.0 and keras==2.6.0). Thanks a lot.
I am creating an encoder on the fashion MNIST dataset. The encoder consists of three layers, Each input image is flattened into a dimensionality of 784. The three encoder layers are with output dimensionality of 128, 64, 32. But after fitting the model, it throws a value error - ValueError: Input 0 is incompatible with layer model_7: expected shape=(None, 784), found shape=(32, 28, 28)
the code:-
#Encoder
input1 = Input(shape = (784,))
hidden1 = Dense(128, activation = 'relu')(input1)
hidden2 = Dense(64, activation = 'relu')(hidden1)
hidden3 = Dense(32, activation = 'relu')(hidden2)
model = Model(inputs = input1, outputs = hidden3)
model summary:
Model: "model_7"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_17 (InputLayer) [(None, 784)] 0
_________________________________________________________________
dense_43 (Dense) (None, 128) 100480
_________________________________________________________________
dense_44 (Dense) (None, 64) 8256
_________________________________________________________________
dense_45 (Dense) (None, 32) 2080
=================================================================
Total params: 110,816
Trainable params: 110,816
Non-trainable params: 0
The error after fitting the model:-
Epoch 1/3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-42-3295f6ac1688> in <module>
----> 1 model.fit(x_train, y_train, epochs = 3)
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1098 _r=1):
1099 callbacks.on_train_batch_begin(step)
-> 1100 tmp_logs = self.train_function(iterator)
1101 if data_handler.should_sync:
1102 context.async_wait()
C:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
826 tracing_count = self.experimental_get_tracing_count()
827 with trace.Trace(self._name) as tm:
--> 828 result = self._call(*args, **kwds)
829 compiler = "xla" if self._experimental_compile else "nonXla"
830 new_tracing_count = self.experimental_get_tracing_count()
C:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
869 # This is the first call of __call__, so we have to initialize.
870 initializers = []
--> 871 self._initialize(args, kwds, add_initializers_to=initializers)
872 finally:
873 # At this point we know that the initialization is complete (or less
C:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
723 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
724 self._concrete_stateful_fn = (
--> 725 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
726 *args, **kwds))
727
C:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2967 args, kwargs = None, None
2968 with self._lock:
-> 2969 graph_function, _ = self._maybe_define_function(args, kwargs)
2970 return graph_function
2971
C:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
3359
3360 self._function_cache.missed.add(call_context_key)
-> 3361 graph_function = self._create_graph_function(args, kwargs)
3362 self._function_cache.primary[cache_key] = graph_function
3363
C:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3194 arg_names = base_arg_names + missing_arg_names
3195 graph_function = ConcreteFunction(
-> 3196 func_graph_module.func_graph_from_py_func(
3197 self._name,
3198 self._python_function,
C:\Anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
988 _, original_func = tf_decorator.unwrap(python_func)
989
--> 990 func_outputs = python_func(*func_args, **func_kwargs)
991
992 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
C:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py in wrapped_fn(*args, **kwds)
632 xla_context.Exit()
633 else:
--> 634 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
635 return out
636
C:\Anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function *
return step_function(self, iterator)
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Anaconda\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Anaconda\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Anaconda\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step **
outputs = model.train_step(data)
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:754 train_step
y_pred = self(x, training=True)
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:271 assert_input_compatibility
raise ValueError('Input ' + str(input_index) +
ValueError: Input 0 is incompatible with layer model_7: expected shape=(None, 784), found shape=(32, 28, 28)
What's the meaning of this error, do I need to change the input dimension. If yes then what will be my input dimension?
Shape of the train and test data-
Thnaks in advance.
Your input images are 28 x 28 images, that need to converted to a vector of 784 values. While there are multiple ways to do this, the easiest would be to use the Flatten layer provided by Keras. In this case, your input to the model will be the 28 x 28 images:
input1 = Input(shape = (28, 28))
flattened_input = Flatten()(input1)
hidden1 = Dense(128, activation = 'relu')(flattened_input)
hidden2 = Dense(64, activation = 'relu')(hidden1)
hidden3 = Dense(32, activation = 'relu')(hidden2)
model = Model(inputs = input1, outputs = hidden3)
I am running the new apple native tensorflow package 2.4, and ran into a problem I did not have before. This jupyter notebook code works in old intel based environment where an older tensorflow version was used. But with M1 apple MLcomputer TensorFlow2.4 it is not compatible
with Numpy 1.20 or 1.18(I downgraded numpy to try). The error log:
NotImplementedError: Cannot convert a symbolic Tensor (lstm_1/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-20-73358e637fe3> in <module>
4 model = Sequential()
5 model.add(Embedding(vocab_size+1, W2V_SIZE, weights=[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False))
----> 6 model.add(LSTM(500, dropout=0.2, recurrent_dropout=0.2))
7 model.add(Dense(units = 10000, kernel_initializer = 'glorot_uniform', activation = 'relu'))
8 model.add(Dropout(0.35))
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
515 self._self_setattr_tracking = False # pylint: disable=protected-access
516 try:
--> 517 result = method(self, *args, **kwargs)
518 finally:
519 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
221 # If the model is being built continuously on top of an input layer:
222 # refresh its output.
--> 223 output_tensor = layer(self.outputs[0])
224 if len(nest.flatten(output_tensor)) != 1:
225 raise ValueError(SINGLE_LAYER_OUTPUT_ERROR_MSG)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
658
659 if initial_state is None and constants is None:
--> 660 return super(RNN, self).__call__(inputs, **kwargs)
661
662 # If any of `initial_state` or `constants` are specified and are Keras
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
944 # >> model = tf.keras.Model(inputs, outputs)
945 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 946 return self._functional_construction_call(inputs, args, kwargs,
947 input_list)
948
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1083 layer=self, inputs=inputs, build_graph=True, training=training_value):
1084 # Check input assumptions set after layer building, e.g. input shape.
-> 1085 outputs = self._keras_tensor_symbolic_call(
1086 inputs, input_masks, args, kwargs)
1087
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
815 return nest.map_structure(keras_tensor.KerasTensor, output_signature)
816 else:
--> 817 return self._infer_output_signature(inputs, args, kwargs, input_masks)
818
819 def _infer_output_signature(self, inputs, args, kwargs, input_masks):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
856 # TODO(kaftan): do we maybe_build here, or have we already done it?
857 self._maybe_build(inputs)
--> 858 outputs = call_fn(inputs, *args, **kwargs)
859
860 self._handle_activity_regularization(inputs, outputs)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent_v2.py in call(self, inputs, mask, training, initial_state)
1161 # LSTM does not support constants. Ignore it during process.
1162 orig_initial_state = initial_state
-> 1163 inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
1164
1165 if isinstance(mask, list):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in _process_inputs(self, inputs, initial_state, constants)
857 initial_state = self.states
858 elif initial_state is None:
--> 859 initial_state = self.get_initial_state(inputs)
860
861 if len(initial_state) != len(self.states):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in get_initial_state(self, inputs)
640 dtype = inputs.dtype
641 if get_initial_state_fn:
--> 642 init_state = get_initial_state_fn(
643 inputs=None, batch_size=batch_size, dtype=dtype)
644 else:
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in get_initial_state(self, inputs, batch_size, dtype)
2504
2505 def get_initial_state(self, inputs=None, batch_size=None, dtype=None):
-> 2506 return list(_generate_zero_filled_state_for_cell(
2507 self, inputs, batch_size, dtype))
2508
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in _generate_zero_filled_state_for_cell(cell, inputs, batch_size, dtype)
2985 batch_size = array_ops.shape(inputs)[0]
2986 dtype = inputs.dtype
-> 2987 return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
2988
2989
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in _generate_zero_filled_state(batch_size_tensor, state_size, dtype)
3001
3002 if nest.is_nested(state_size):
-> 3003 return nest.map_structure(create_zeros, state_size)
3004 else:
3005 return create_zeros(state_size)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
657
658 return pack_sequence_as(
--> 659 structure[0], [func(*x) for x in entries],
660 expand_composites=expand_composites)
661
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
657
658 return pack_sequence_as(
--> 659 structure[0], [func(*x) for x in entries],
660 expand_composites=expand_composites)
661
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in create_zeros(unnested_state_size)
2998 flat_dims = tensor_shape.TensorShape(unnested_state_size).as_list()
2999 init_state_size = [batch_size_tensor] + flat_dims
-> 3000 return array_ops.zeros(init_state_size, dtype=dtype)
3001
3002 if nest.is_nested(state_size):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py in wrapped(*args, **kwargs)
2817
2818 def wrapped(*args, **kwargs):
-> 2819 tensor = fun(*args, **kwargs)
2820 tensor._is_zeros_tensor = True
2821 return tensor
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py in zeros(shape, dtype, name)
2866 # Create a constant if it won't be very big. Otherwise create a fill
2867 # op to prevent serialized GraphDefs from becoming too large.
-> 2868 output = _constant_if_small(zero, shape, dtype, name)
2869 if output is not None:
2870 return output
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py in _constant_if_small(value, shape, dtype, name)
2802 def _constant_if_small(value, shape, dtype, name):
2803 try:
-> 2804 if np.prod(shape) < 1000:
2805 return constant(value, shape=shape, dtype=dtype, name=name)
2806 except TypeError:
<__array_function__ internals> in prod(*args, **kwargs)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/numpy/core/fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
3028 10
3029 """
-> 3030 return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
3031 keepdims=keepdims, initial=initial, where=where)
3032
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
85 return reduction(axis=axis, out=out, **passkwargs)
86
---> 87 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
88
89
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in __array__(self)
850
851 def __array__(self):
--> 852 raise NotImplementedError(
853 "Cannot convert a symbolic Tensor ({}) to a numpy array."
854 " This error may indicate that you're trying to pass a Tensor to"
NotImplementedError: Cannot convert a symbolic Tensor (lstm_1/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
I am extremely new to Tensorflow hence I won't be sure exactly what will you need to solve my issue. So do let me know if you need any additional information.
Basically I'm trying to run images through Sequential. Based on the tutorial on https://www.tensorflow.org/tutorials/images/classification, I am trying to plug and play onto my own dataset.
I'm currently stuck at the running my model using model.fit() where it gave me the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-90-85c03bda7f8f> in <module>
16
17 epochs=1
---> 18 history = model.fit(
19 train_data,
20 validation_data=test_data,
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1132 _r=1):
1133 callbacks.on_train_batch_begin(step)
-> 1134 tmp_logs = self.train_function(iterator)
1135 if data_handler.should_sync:
1136 context.async_wait()
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
816 tracing_count = self.experimental_get_tracing_count()
817 with trace.Trace(self._name) as tm:
--> 818 result = self._call(*args, **kwds)
819 compiler = "xla" if self._jit_compile else "nonXla"
820 new_tracing_count = self.experimental_get_tracing_count()
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
860 # This is the first call of __call__, so we have to initialize.
861 initializers = []
--> 862 self._initialize(args, kwds, add_initializers_to=initializers)
863 finally:
864 # At this point we know that the initialization is complete (or less
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
701 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
702 self._concrete_stateful_fn = (
--> 703 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
704 *args, **kwds))
705
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
3018 args, kwargs = None, None
3019 with self._lock:
-> 3020 graph_function, _ = self._maybe_define_function(args, kwargs)
3021 return graph_function
3022
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
3412
3413 self._function_cache.missed.add(call_context_key)
-> 3414 graph_function = self._create_graph_function(args, kwargs)
3415 self._function_cache.primary[cache_key] = graph_function
3416
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3247 arg_names = base_arg_names + missing_arg_names
3248 graph_function = ConcreteFunction(
-> 3249 func_graph_module.func_graph_from_py_func(
3250 self._name,
3251 self._python_function,
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
996 _, original_func = tf_decorator.unwrap(python_func)
997
--> 998 func_outputs = python_func(*func_args, **func_kwargs)
999
1000 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
610 xla_context.Exit()
611 else:
--> 612 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
613 return out
614
~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
983 except Exception as e: # pylint:disable=broad-except
984 if hasattr(e, "ag_error_metadata"):
--> 985 raise e.ag_error_metadata.to_exception(e)
986 else:
987 raise
ValueError: in user code:
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:839 train_function *
return step_function(self, iterator)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:829 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1262 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2734 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3423 _call_for_each_replica
return fn(*args, **kwargs)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:822 run_step **
outputs = model.train_step(data)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:788 train_step
y_pred = self(x, training=True)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1032 __call__
outputs = call_fn(inputs, *args, **kwargs)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py:398 call
outputs = layer(inputs, **kwargs)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1028 __call__
self._maybe_build(inputs)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:2722 _maybe_build
self.build(input_shapes) # pylint:disable=not-callable
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py:188 build
input_channel = self._get_input_channel(input_shape)
/Users/mongchanghsi/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py:367 _get_input_channel
raise ValueError('The channel dimension of the inputs '
ValueError: The channel dimension of the inputs should be defined. Found `None`.
Here is my code for the model:
model = Sequential([
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(4)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
epochs=10
history = model.fit(
train_data,
validation_data=test_data,
epochs=epochs
)
I understand that in the tutorial they used a inbuilt preprocessing function however I tried to build my own preprocessing function to facilitate my learning as well.
def preprocessing(image, target_size):
# Extracting labels
parts = tf.strings.split(image, os.sep)
label = parts[-2]
# Decoding image file
path = tf.io.read_file(image)
image = tf.image.decode_jpeg(path)
# Cropping
image = tf.image.crop_to_bounding_box(image, offset_height=25, offset_width=25, target_height=image_size, target_width=image_size)
# Normalizing
image = image / 255
return image, label
list_ds = tf.data.Dataset.list_files(DATA_DIR + '/*/*')
preprocess_function = partial(preprocessing, target_size=image_size)
processed_data = list_ds.map(preprocess_function)
train_data = processed_data.take(8000).batch(batch_size)
test_data = processed_data.skip(8000).batch(batch_size)
Other information that I can provide is that the images are of grey-scale hence 1 channel and I have normalized it /255 in my preprocessing function and the image_size is 300 and batch_size is 100.
Try this:
image = tf.image.decode_jpeg(path, channels=1)
I am trying to use a part of the VGG16 model for transfer learning using the Fashion MNIST dataset. The data is processed and the model is specified as per below:
data = keras.datasets.fashion_mnist
(train_img, train_labels), (test_img, test_labels) = data.load_data()
train_img.shape, train_labels.shape, test_img.shape, test_labels.shape
#((60000, 28, 28), (60000,), (10000, 28, 28), (10000,))
# transform to rgb as required by VGG
train_img=tf.image.grayscale_to_rgb(tf.expand_dims(train_img, axis=3))
test_img=tf.image.grayscale_to_rgb(tf.expand_dims(test_img, axis=3))
#resize to minimum size of (32x32
train_img=tf.image.resize_with_pad(train_img,32,32)
test_img=tf.image.resize_with_pad(train_img,32,32)
train_img = train_img / 255.
test_img = test_img / 255.
from keras.applications.vgg16 import preprocess_input
train_img = tf.expand_dims(train_img, axis=0)
test_img = tf.expand_dims(test_img, axis=0)
#preprocessing as required by VGG16
train_img=preprocess_input(train_img)
test_img=preprocess_input(test_img)
#using model without last layers
vgg16=tf.keras.applications.VGG16(include_top=False, weights='imagenet', input_shape=(32,32,3))
layer_dict = dict([(layer.name, layer) for layer in vgg16.layers])
#stop at block3_pool and get output
output = layer_dict['block3_pool'].output
x = keras.layers.Flatten()(output)
...add some fully connected layers here...
x = keras.layers.Dense(10, activation='softmax')(x)
final = keras.models.Model(inputs=vgg16.input, outputs=model)
for layer in final.layers[:7]:
layer.trainable = False
final.fit(train_img, train_labels, epochs=50, validation_split=0.2)
When I try to fit the model I get the following error:
UnboundLocalError Traceback (most recent call last)
<ipython-input-65-6a0b99b56337> in <module>()
1 early_stopping_cb=keras.callbacks.EarlyStopping(patience=3, verbose=1,restore_best_weights=True)
----> 2 vgg16_1.fit(train_img, train_labels, epochs=50, validation_split=0.2, callbacks=[early_stopping_cb])
1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
857 logs = tmp_logs # No error, now safe to assign to logs.
858 callbacks.on_train_batch_end(step, logs)
--> 859 epoch_logs = copy.copy(logs)
860
861 # Run validation.
UnboundLocalError: local variable 'logs' referenced before assignment
I thought this might be due to the training set shape being faulty, but then if I use train_img[0] instead, which has shape (60000,32,32,3), then I get the following error instead:
ValueError Traceback (most recent call last)
<ipython-input-66-2b893ccd9ac9> in <module>()
1 early_stopping_cb=keras.callbacks.EarlyStopping(patience=3, verbose=1,restore_best_weights=True)
----> 2 vgg16_1.fit(train_img[0], train_labels, epochs=50, validation_split=0.2, callbacks=[early_stopping_cb])
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside `run_distribute_coordinator` already.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
849 batch_size=batch_size):
850 callbacks.on_train_batch_begin(step)
--> 851 tmp_logs = train_function(iterator)
852 # Catch OutOfRangeError for Datasets of unknown size.
853 # This blocks until the batch has finished executing.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
578 xla_context.Exit()
579 else:
--> 580 result = self._call(*args, **kwds)
581
582 if tracing_count == self._get_tracing_count():
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
625 # This is the first call of __call__, so we have to initialize.
626 initializers = []
--> 627 self._initialize(args, kwds, add_initializers_to=initializers)
628 finally:
629 # At this point we know that the initialization is complete (or less
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
504 self._concrete_stateful_fn = (
505 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 506 *args, **kwds))
507
508 def invalid_creator_scope(*unused_args, **unused_kwds):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2444 args, kwargs = None, None
2445 with self._lock:
-> 2446 graph_function, _, _ = self._maybe_define_function(args, kwargs)
2447 return graph_function
2448
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2775
2776 self._function_cache.missed.add(call_context_key)
-> 2777 graph_function = self._create_graph_function(args, kwargs)
2778 self._function_cache.primary[cache_key] = graph_function
2779 return graph_function, args, kwargs
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2665 arg_names=arg_names,
2666 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667 capture_by_value=self._capture_by_value),
2668 self._function_attributes,
2669 # Tell the ConcreteFunction to clean up its graph once it goes out of
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
979 _, original_func = tf_decorator.unwrap(python_func)
980
--> 981 func_outputs = python_func(*func_args, **func_kwargs)
982
983 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
439 # __wrapped__ allows AutoGraph to swap in a converted function. We give
440 # the function a weak reference to itself to avoid a reference cycle.
--> 441 return weak_wrapped_fn().__wrapped__(*args, **kwds)
442 weak_wrapped_fn = weakref.ref(wrapped_fn)
443
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:533 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/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.6/dist-packages/tensorflow/python/keras/losses.py:143 __call__
losses = self.call(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:246 call
return self.fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:1527 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py:4561 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py:1117 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (32, 1) and (32, 10) are incompatible
Any clues where these errors come from and what I am doing wrong? It feels like I might have missed something obvious, but being a Keras novice I can't get my head around what it is. Help much appreciated.
You need to comment two lines on expanding dims as follows. What happens is that it updates the shape of train_img to (1,60000,32,32,3) and model.fit complains that you are using single image for training.
#train_img = tf.expand_dims(train_img, axis=0)
#test_img = tf.expand_dims(test_img, axis=0)
I updated your code and shared Here. You need to update the architecture to improve it for better accuracy. Follow transfer learning approach mentioned here and update your code for better accuacy. Thanks!
Seems the issue was that I had a dense output layer of size 10, while the labels have size 1. Solution was to use sparse categorical cross-entropy loss function instead of simple categorical.