ValueError building a neural network with 2 outputs in Keras - python

I tried to build a network having a single input X (a 2-dimensions matrix of size Xa*Xb) and 2 outputs Y1 and Y2 (both in 1 dimension). Even though it isn't the case in the code I posted below, Y1 is supposed to be a classifier that outputs a one-hot vector and Y2 is supposed to be for regression (the original code raised the same error).
When training the network I get the following error:
ValueError: Shapes (None, None) and (None, 17, 29) are incompatible
Obviously, (None, 17, 29) translates to (None, size_Xa, size_Y1), and I don't understand why Xa and Y1 should be related (independantly from Xb) in the first place.
Here is my code. I tried to reduce it to the minimum in order to make it easier to understand.
import numpy as np
from keras.layers import Dense, LSTM, Input
from keras.models import Model
def dataGenerator():
while True:
yield makeBatch()
def makeBatch():
"""generates a batch of artificial training data"""
x_batch, y_batch = [], {}
x_batch = np.random.rand(batch_size, size_Xa, size_Xb)
#x_batch = np.random.rand(batch_size, size_Xa)
y_batch['output1'] = np.random.rand(batch_size, size_Y1)
y_batch['output2'] = np.random.rand(batch_size, size_Y2)
return x_batch, y_batch
def generate_model():
input_layer = Input(shape=(size_Xa, size_Xb))
#input_layer = Input(shape=(size_Xa))
common_branch = Dense(128, activation='relu')(input_layer)
branch_1 = Dense(size_Y1, activation='softmax', name='output1')(common_branch)
branch_2 = Dense(size_Y2, activation='relu', name='output2')(common_branch)
model = Model(inputs=input_layer,outputs=[branch_1,branch_2])
losses = {"output1":"categorical_crossentropy", "output2":"mean_absolute_error"}
model.compile(optimizer="adam",
loss=losses,
metrics=['accuracy'])
return model
batch_size=5
size_Xa = 17
size_Xb = 13
size_Y2 = 100
size_Y1 = 29
model = generate_model()
model.fit( x=dataGenerator(),
steps_per_epoch=50,
epochs=15,
validation_data=dataGenerator(), validation_steps=50, verbose=1)
If I uncomment the 2 commented lines in makeBatch and generate_model, the error disappears. So if the input X is in 1 dimension it runs, but when I change it to 2 dimensions (keeping everything else the same) the error appears.
Is this related to the architecture with 2 outputs? I think there is something I'm missing here, any help is welcome.
I add the full error log for reference:
Epoch 1/15
Traceback (most recent call last):
File "neuralnet_minimal.py", line 41, in <module>
model.fit( x=dataGenerator(),
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 627, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 505, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2446, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2777, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2657, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 981, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 441, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:532 train_step **
loss = self.compiled_loss(
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:143 __call__
losses = self.call(y_true, y_pred)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:246 call
return self.fn(y_true, y_pred, **self._fn_kwargs)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:1527 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/keras/backend.py:4561 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/path/of/my/project/venv/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py:1117 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, None) and (None, 17, 29) are incompatible

Strangely enough, the error disappears when I add a Flatten() layer before the network splits... It has to do with the shape of the network but I still don't get the real reason behind all of this.
I will mark this as correct answer as it solves the problem, unless someone else posts something. Please tell me if this is not the right way to do it.

Related

how to solve Value Error in tensorflow.keras?

I have some project about korean NLP. my project's purpose is classfiying sentence by three category(none, offensive, hate). input data was padded by 45 length. so i created simple DL model and input preprocessed data to model. i want to create DL model to classify cursed_sentence
so i'm using keras of tensorflow-cpu(version:2.5.0 / python version = 3.7.9). i was faced some problem with using keras. i created very simple LSTM model by using Keras. i created embedding layer. input_dim of embedding layer is vocab_size + 1 (vocab_size is 24844) and created LSTM layers and final layers that used softmax for activation function
but i checked occuring 'ValueError: Shapes (None, 3) and (None, 1) are incompatible.' i submit some code and error message. i can't understand why this error occurs and which part of errors occured
import pickle
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
METRICS = [
keras.metrics.TruePositives(name='tp'),
keras.metrics.FalsePositives(name='fp'),
keras.metrics.TrueNegatives(name='tn'),
keras.metrics.FalseNegatives(name='fn'),
keras.metrics.BinaryAccuracy(name='accuracy'),
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall'),
keras.metrics.AUC(name='auc')
]
model = keras.Sequential()
model.add(layers.Embedding(len(tk.word_index)+1, 100, input_length=45))
model.add(layers.LSTM(100))
model.add(layers.Dense(3, activation='softmax'))
model.summary()
early_stopping = keras.callbacks.EarlyStopping(
monitor = 'val_auc',
verbose = 1,
patience = 10,
mode = 'max',
restore_best_weights=True)
model.compile(optimizer=keras.optimizers.RMSprop(), loss='sparse_categorical_crossentropy', metrics=METRICS)
baseline_history = model.fit(train_data, train_label, batch_size = 8192, epochs = 100, callbacks = [early_stopping], validation_split = 0.2, class_weight = class_weight)
below content is summary that created simple model
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 45, 100) 2484500
_________________________________________________________________
lstm (LSTM) (None, 100) 80400
_________________________________________________________________
dense (Dense) (None, 3) 303
=================================================================
Total params: 2,565,203
Trainable params: 2,565,203
Non-trainable params: 0
and below content is occured error message
Traceback (most recent call last):
File "learning.py", line 85, in <module>
baseline_history = model.fit(train_data, train_label, batch_size = 8192, epochs = 100, callbacks = [early_stopping], validation_split = 0.2, class_weight = class_weight)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1183, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
result = self._call(*args, **kwds)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 764, in _initialize
*args, **kwds))
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3050, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3289, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 999, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:855 train_function *
return step_function(self, iterator)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:838 run_step **
outputs = model.train_step(data)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:800 train_step
self.compiled_metrics.update_state(y, y_pred, sample_weight)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:460 update_state
metric_obj.update_state(y_t, y_p, sample_weight=mask)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py:86 decorated
update_op = update_state_fn(*args, **kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\metrics.py:177 update_state_fn
return ag_update_state(*args, **kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\metrics.py:1005 update_state **
sample_weight=sample_weight)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py:366 update_confusion_matrix_variables
y_pred.shape.assert_is_compatible_with(y_true.shape)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-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
i tried to solve this problem but i can't find appropriate answers. sorry for my english and please give me some advice for this error.
I'm not really into ML, but try changing
model.add(layers.Dense(3, activation='softmax'))
to
model.add(layers.Dense(1, activation='softmax'))
and see if the error still occured
Edit:
or check the elements of train_label. I think it was supposed to have 3 label values (because your last layer has 3 outputs)

Tensorflow 2 :NotImplementedError: numpy() is only available when eager execution is enabled

There is a question in this code, I delete SeBlock class and just run CNN class, then all is well. If I plug SeBlock to CNN class the error will occur, and display NotImplementedError. I don't know cause this problem, I try to solve this problem, but what method I searched all is not working. Can somebody help me, thanks very much !
import tensorflow as tf
class SeBlock(tf.keras.Model):
def __init__(self, ratio, channel):
super(SeBlock, self).__init__()
self.kernel_initializer = tf.keras.initializers.VarianceScaling()
self.bias_initializer = tf.constant_initializer(value=0.0)
self.ratio = ratio
self.ReduceMean = tf.keras.layers.GlobalAveragePooling2D()
self.DenseCut = tf.keras.Sequential([
tf.keras.layers.Dense(units=channel,
activation=tf.nn.relu, kernel_initializer=self.kernel_initializer,
bias_constraint=self.bias_initializer),
tf.keras.layers.Dense(units=channel,
activation=tf.nn.sigmoid,
kernel_initializer=self.kernel_initializer,
bias_constraint=self.bias_initializer)
])
self.flatten = tf.keras.layers.Reshape(target_shape=(1, 1, channel,))
def call(self, inputs, training=True):
if training:print("training network")
x = self.ReduceMean(inputs)
x = self.DenseCut(x, training)
scale = self.flatten(x)
scale = tf.keras.layers.multiply([inputs,scale])
# scale *= inputs
return scale
class CNN(tf.keras.Model):
def __init__(self, se_block):
super(CNN, self).__init__()
self.conv1 = tf.keras.layers.Conv2D(
filters=32, #
kernel_size=[5, 5], #
padding='same', #
activation=tf.nn.relu #
)
self.seblock1 = self._make_layer(se_block= se_block, ratio=1, input_channel=32)
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2)
self.conv2 = tf.keras.layers.Conv2D(
filters=64,
kernel_size=[5, 5],
padding='same',
activation=tf.nn.relu
)
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2)
self.flatten = tf.keras.layers.Reshape(target_shape=(112 * 112 * 64,))
self.dense2 = tf.keras.layers.Dense(units=10)
def _make_layer(self, se_block, ratio, input_channel):
return tf.keras.Sequential([se_block(ratio=ratio,channel=input_channel)])
def call(self, inputs, training=True):
print("1",inputs.get_shape().as_list())
x = self.conv1(inputs) # [batch_size, 28, 28, 32]
# print("start se-block")
x = self.seblock1(x, training)
# print("end se-block")
x = self.pool1(x) # [batch_size, 14, 14, 32]
x = self.conv2(x) # [batch_size, 14, 14, 64]
x = self.pool2(x) # [batch_size, 7, 7, 64]
x = self.flatten(x) # [batch_size, 7 * 7 * 64]
x = self.dense2(x) # [batch_size, 10]
return tf.nn.softmax(x)
def CNNDense():
return CNN(SeBlock)
The main code is below.
import tensorflow as tf
import LoadImage as readimage
import DenseBSE
tf.keras.backend.clear_session()
train_path = r"E:\BaiduNetdiskDownload\板角\boardtrain"
test_path = r"E:\BaiduNetdiskDownload\板角\boardtest"
BatchSize = 4
Epoch = 60
lr = 0.001
ds_train, train_count = readimage.load_tensor_img(train_path,
batch_size=BatchSize,
epoch=Epoch)
ds_test, test_count = readimage.load_tensor_img(test_path,
batch_size=BatchSize,
epoch=Epoch)
model = DenseBSE.CNNDense()
model.build(input_shape=(BatchSize, 448, 448, 3))
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
epoch_steps = train_count // BatchSize
val_steps = test_count // BatchSize
model.fit(ds_train, epochs=Epoch, steps_per_epoch = epoch_steps,
validation_data=ds_test, validation_steps = val_steps)
And the error information is displayed below.
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-41c34ae2b3b4>", line 1, in <module>
runfile('E:/PythonProject/CNN_training.py', wdir='E:/PythonProject')
File "C:\Program Files\JetBrains\PyCharm 2020.3.5\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2020.3.5\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/PythonProject/CNN_training.py", line 35, in <module>
model.fit(ds_train, epochs=Epoch, steps_per_epoch = epoch_steps,
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit
tmp_logs = self.train_function(iterator)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 725, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3196, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
NotImplementedError: in user code:
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function *
return step_function(self, iterator)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step **
outputs = model.train_step(data)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:757 train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:498 minimize
return self.apply_gradients(grads_and_vars, name=name)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:631 apply_gradients
return distribute_ctx.get_replica_context().merge_call(
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2941 merge_call
return self._merge_call(merge_fn, args, kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2948 _merge_call
return merge_fn(self._strategy, *args, **kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:682 _distributed_apply **
update_ops.extend(distribution.extended.update(
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2494 update
return self._update(var, fn, args, kwargs, group)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3431 _update
return self._update_non_slot(var, fn, (var,) + tuple(args), kwargs, group)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3437 _update_non_slot
result = fn(*args, **kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:661 apply_grad_to_update_var **
return var.assign(var.constraint(var))
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\init_ops_v2.py:290 __call__
return constant_op.constant(self.value, dtype=dtype, shape=shape)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py:264 constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py:281 _constant_impl
tensor_util.make_tensor_proto(
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_util.py:454 make_tensor_proto
if shape is not None and np.prod(shape, dtype=np.int64) == 0:
<__array_function__ internals>:5 prod
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:2961 prod
return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:90 _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:483 __array__
return np.asarray(self.numpy())
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:619 numpy
raise NotImplementedError(
NotImplementedError: numpy() is only available when eager execution is enabled.
This notebook should help to upgrade, check, and enable. Good luck!

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

EDIT: I got past that error message by reshaping my data as follows:
train_x = np.array(train_x)
train_y = np.array(train_y)
x_size = train_x.shape[0] * train_x.shape[1]
train_x = train_x.reshape(x_size, train_x.shape[2])
train_x = np.expand_dims(train_x, 1)
train_x = train_x.transpose(0,2,1)
train_y = train_y.flatten()
shape = train_x.shape # 3D: number of texts * number of padded paragraphs, number of features, 1
time_steps = shape[0] # number of padded pars * number of texts
features = shape[1] # number of features
model = Sequential()
model.add(layers.Masking(mask_value=0, input_shape=(time_steps, features)))
model.add(layers.LSTM(128, return_sequences=True, return_state=False, input_shape=(time_steps, features))) # 128 internal units
model.add(layers.TimeDistributed(layers.Dense(1, activation='sigmoid')))
#model.add(layers.Dense(len(train_y))) # Dense layer
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(train_x, train_y, batch_size=train_y.shape[0])
predictions = model.predict(test_x)
I get a new error message:
ValueError: Input 0 is incompatible with layer lstm: expected shape=(None, None, 3), found shape=[288, 3, 1]
I'll keep updating this question in case someone runs into a similiar problem.
Still happy about any input.
Original question:
I want to buil a sequential LSTM model that predicts binary classification at every time step. More exactly, I want to predict an output for every paragraph in my texts (48 is the number of paragraphs). This is my code:
shape = np.shape(train_x) # 3D: number of texts, number of padded paragraphs, number of features
n = shape[0] # number of texts
time_steps = shape[1] # number of padded pars
features = shape[2] # number of features
model = Sequential()
model.add(layers.Masking(mask_value=0.0, input_shape=(time_steps, features)))
model.add(layers.LSTM(128, return_sequences=True, return_state=False))
model.add(layers.TimeDistributed(layers.Dense(1)))
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()
#train_x = np.array(train_x).reshape(2, input_shape, 3)
train_x = tf.convert_to_tensor(train_x) # data needs to be tensor object
train_y = tf.convert_to_tensor(train_y)
model.fit(train_x, train_y, batch_size=2)
predictions = model.predict(test_x)
This is the error message I get:
ValueError: Can not squeeze dim[1], expected a dimension of 1,
got 48 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT,
squeeze_dims=[-1]](Cast)' with input shapes: [2,48].
I don't really know what to do with this, do I need to reshape my data? How? Or do I need to change something in the model?
Thanks!
(changing the loss function to 'binary_crossentropy' raises the same error)
This is the entire traceback:
Traceback (most recent call last):
File "program.py", line 247, in <module>
eval_scores = train_classifier(x_train, y_train_sc, x_test, y_test_sc)
File "program.py", line 201, in train_classifier
model.fit(train_x, train_y, batch_size=2)
File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:748 train_step
loss = self.compiled_loss(
C:\Python38\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
C:\Python38\lib\site-packages\tensorflow\python\keras\losses.py:150 __call__
return losses_utils.compute_weighted_loss(
C:\Python38\lib\site-packages\tensorflow\python\keras\utils\losses_utils.py:111 compute_weighted_loss
weighted_losses = tf_losses_utils.scale_losses_by_sample_weight(
C:\Python38\lib\site-packages\tensorflow\python\ops\losses\util.py:142 scale_losses_by_sample_weight
losses, _, sample_weight = squeeze_or_expand_dimensions(
C:\Python38\lib\site-packages\tensorflow\python\ops\losses\util.py:95 squeeze_or_expand_dimensions
sample_weight = array_ops.squeeze(sample_weight, [-1])
C:\Python38\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
C:\Python38\lib\site-packages\tensorflow\python\util\deprecation.py:507 new_func
return func(*args, **kwargs)
C:\Python38\lib\site-packages\tensorflow\python\ops\array_ops.py:4259 squeeze
return gen_array_ops.squeeze(input, axis, name)
C:\Python38\lib\site-packages\tensorflow\python\ops\gen_array_ops.py:10043 squeeze
_, _, _op, _outputs = _op_def_library._apply_op_helper(
C:\Python38\lib\site-packages\tensorflow\python\framework\op_def_library.py:742 _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
C:\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py:591 _create_op_internal
return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
C:\Python38\lib\site-packages\tensorflow\python\framework\ops.py:3477 _create_op_internal
ret = Operation(
C:\Python38\lib\site-packages\tensorflow\python\framework\ops.py:1974 __init__
self._c_op = _create_c_op(self._graph, node_def, inputs,
C:\Python38\lib\site-packages\tensorflow\python\framework\ops.py:1815 _create_c_op
raise ValueError(str(e))
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 48 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast)' with input shapes: [2,48].

For the value of custom regularization parameters (tensorflow2.3.x keras)

I'm doing simple machine learning in tensorflow 2.3.x.
I would like to create and implement a custom regularization here.
I would like to create a loss by computing the weight values in 1D and a matrix that I created myself.
However, even if I create a matrix with the weights made 1D using the parameter x, it does not seem to contain any values. Naturally this results in a value error.
What if I want to calculate with the values of the weights in a custom regularization?
Here is the code that causes the error.
The return statement will be rewritten later.
#import datasets
from tensorflow.keras.datasets import cifar10
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
from tensorflow.keras.utils import to_categorical
X_train = X_train/255.
X_test = X_test/255.
Y_train = to_categorical(Y_train, 10)
Y_test = to_categorical(Y_test, 10)
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
import numpy as np
import tensorflow as tf
import random
import os
from tensorflow.keras import regularizers
def set_seed(seed=200):
tf.random.set_seed(seed)
# optional
# for numpy.random
np.random.seed(seed)
# for built-in random
random.seed(seed)
# for hash seed
os.environ["PYTHONHASHSEED"] = str(seed)
set_seed(0)
from tensorflow.python.ops import math_ops
from tensorflow.python.keras import backend
import math
class Costom(regularizers.Regularizer):
def __init__(self, costom):
self.costom = costom
def __call__(self, x):
w = tf.reduce_mean(tf.reduce_mean(tf.reduce_mean(x,0),0),0)
print(x.shape[3])
SK = [[0] * 256 for i in range(x.shape[3])]
i = 0
while i < x.shape[3]:
SK[i][i] = 1
i += 1
tf.constant(SK)
#tf.matmul(w ,SK)
return self.costom * tf.reduce_sum(x)
def get_config(self):
return {'costom': float(self.costom)}
model = Sequential([
Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=X_train.shape[1:],kernel_regularizer=Costom(0.01)),
Conv2D(32, (3, 3), padding='same', activation='relu'),
MaxPooling2D(2, 2),
Dropout(0.25),
Conv2D(64, (3, 3), padding='same', activation='relu'),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Dropout(0.25),
Flatten(),
Dense(512, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax'),
])
model.compile(loss='categorical_crossentropy',optimizer='SGD',metrics=['accuracy'])
history = model.fit(X_train, Y_train, epochs=1)
model.save('./CIFAR-10_reg.h5')
print(model.evaluate(X_test, Y_test))
The following is the error message output.
Traceback (most recent call last):
File "train.py", line 112, in <module>
history = model.fit(X_train, Y_train, epochs=1)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\81805\anaconda3\envs\tf23\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\81805\anaconda3\envs\tf23\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\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\training.py:749 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1433 losses
loss_tensor = regularizer()
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1509 _tag_callable
loss = loss()
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:2433 _loss_for_variable
regularization = regularizer(v)
train.py:61 __call__
tf.matmul(w ,SK)
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\ops\math_ops.py:3253 matmul
return gen_math_ops.mat_mul(
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\ops\gen_math_ops.py:5640 mat_mul
_, _, _op, _outputs = _op_def_library._apply_op_helper(
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\op_def_library.py:742 _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\func_graph.py:591 _create_op_internal
return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\ops.py:3477 _create_op_internal
ret = Operation(
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\ops.py:1974 __init__
self._c_op = _create_c_op(self._graph, node_def, inputs,
C:\Users\81805\anaconda3\envs\tf23\lib\site-packages\tensorflow\python\framework\ops.py:1815 _create_c_op
raise ValueError(str(e))
ValueError: Shape must be rank 2 but is rank 1 for '{{node conv2d/kernel/Regularizer/MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](conv2d/kernel/Regularizer/Mean_2, conv2d/kernel/Regularizer/MatMul/b)' with input shapes: [32], [32,256].

Keras ValueError: Dimensions must be equal, but are 9 and 400 for '{{node Equal}}' with input shapes: [?,9], [?,300,400]

I'm trying to train a very simple Keras network to classify some one-hot encoded images saved as np.array. The input data structure is made of a .npy file, with 500 images (3 arrays each one, as it's RGB) and a one-hot encoded array with each image to determine it's classification. Each image is 400x300 pixels (Width x Height), and the target output should be of 9 classes. Hence, each image has a shape of (300, 400, 3) and each one-hot encoded label list has a length of 9.
This is the code that I am currently using:
import numpy as np
import cv2
import time
import os
import pandas as pd
from collections import deque
from random import shuffle
import pickle
# Do not display following messages:
# 0 = all messages are logged (default behavior)
# 1 = INFO messages are not printed
# 2 = INFO and WARNING messages are not printed
# 3 = INFO, WARNING, and ERROR messages are not printed
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0],True)
#os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
from keras import Input
from keras.models import load_model
lr = 0.01 # Learning Rate
WIDTH = 400
HEIGHT = 300
file_name = 'path/to/training_data.npy'
train_data = np.load(file_name, allow_pickle=True)
SAMPLE = len(train_data)
print('training_data.npy - Sample Size: {}'.format(SAMPLE))
X = np.array([i[0] for i in train_data]) / 255.0 # Divide to normalize values between 0 and 1
print('X shape: {}'.format(str(X.shape)))
Y = np.array([i[1] for i in train_data])
print("============================")
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(WIDTH, HEIGHT, 3)))
model.add(tf.keras.layers.Dense(512, activation='relu'))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(9, activation='softmax'))
model.compile(
optimizer = tf.keras.optimizers.SGD(lr=lr),
loss = 'mse',
metrics = ['acc']
)
model.summary()
model.fit(X, Y, epochs=5)
print("============================")
However, when I try to run model.fit(), I always face the same error:
WARNING:tensorflow:Model was constructed with shape (None, 400, 300, 3) for input Tensor("input_1:0", shape=(None, 400, 300, 3), dtype=float32), but it was called on an input with incompatible shape (None, 300, 400, 3).
Traceback (most recent call last):
File "test.py", line 78, in <module>
model.fit(X, Y, epochs=5)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\def_function.py", line 697, in _initialize
*args, **kwds))
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py:759 train_step
self.compiled_metrics.update_state(y, y_pred, sample_weight)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:409 update_state
metric_obj.update_state(y_t, y_p, sample_weight=mask)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py:90 decorated
update_op = update_state_fn(*args, **kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\metrics.py:176 update_state_fn
return ag_update_state(*args, **kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\metrics.py:612 update_state **
matches = ag_fn(y_true, y_pred, **self._fn_kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\keras\metrics.py:3309 sparse_categorical_accuracy
return math_ops.cast(math_ops.equal(y_true, y_pred), K.floatx())
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\ops\math_ops.py:1613 equal
return gen_math_ops.equal(x, y, name=name)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\ops\gen_math_ops.py:3224 equal
name=name)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\op_def_library.py:744 _apply_op_helper
attrs=attr_protos, op_def=op_def)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\func_graph.py:593 _create_op_internal
compute_device)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\ops.py:3485 _create_op_internal
op_def=op_def)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\ops.py:1975 __init__
control_input_ops, op_def)
D:\Anaconda3\envs\pygta5_env_tf2.0\lib\site-packages\tensorflow\python\framework\ops.py:1815 _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 9 and 400 for '{{node Equal}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](Cast_1, Cast_2)' with input shapes: [?,9], [?,300,400].
I have been reading Keras documentation and lots of SO questions, but I can't figure out what's wrong with the code. I think that the problem might be located in the definiton of the input layer, but I have tried other configurations and returns errors as well.
Thanks in advance!!
Finally, I've found a solution that works for the system. It's not very efficient as it consumes a lot of memory while running, but at least it runs. The problem was the model itself, as the shape of the input (4D array. 3 dimensions for RGB channels and 1 dimension for target labels) was incompatible with the dense layer.
To define a Dense layer in this case, it's necessary to create previously a Flatten layer. Besides this, I've added a Conv2D layer as first hidden layer. Thus, the model looks like this:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters=2, kernel_size=2, input_shape=(HEIGHT,WIDTH,3)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(9, activation='relu'))
And whole script like this:
import numpy as np
import cv2
import time
import os
import pandas as pd
from collections import deque
from random import shuffle
import pickle
# Do not display following messages:
# 0 = all messages are logged (default behavior)
# 1 = INFO messages are not printed
# 2 = INFO and WARNING messages are not printed
# 3 = INFO, WARNING, and ERROR messages are not printed
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0],True)
#os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
from keras import Input
from keras.models import load_model
lr = 0.01 # Learning Rate
WIDTH = 400
HEIGHT = 300
file_name = 'path/to/training_data.npy'
train_data = np.load(file_name, allow_pickle=True)
SAMPLE = len(train_data)
print('training_data.npy - Sample Size: {}'.format(SAMPLE))
X = np.array([i[0] for i in train_data]) / 255.0 # Divide to normalize values between 0 and 1
print('X shape: {}'.format(str(X.shape)))
Y = np.array([i[1] for i in train_data])
print("============================")
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters=2, kernel_size=2, input_shape=(HEIGHT,WIDTH,3)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(9, activation='relu'))
model.compile(
optimizer = tf.keras.optimizers.SGD(lr=lr),
loss = 'mse',
metrics = ['acc']
)
model.summary()
model.fit(X, Y, epochs=5)
print("============================")

Categories

Resources