I'm experiencing a "ValueError: Shapes (None, None) and (None, 8, 8, 7) are incompatible" anytime I am training my model in Tensorflow. So far:
history = model.fit(train_batches,
steps_per_epoch=train_steps,
class_weight=class_weights,
validation_data=validation_batches,
validation_steps=val_steps,
epochs=30,
verbose=1,
callbacks=callbacks_list
)
gives this stacktrace:
Traceback (most recent call last):
File "/home/brian/Desktop/381-deep-learning/main.py", line 410, in <module>
epochs=30
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1479, in fit_generator
initial_epoch=initial_epoch)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 627, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 506, in _initialize
*args, **kwds))
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/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 "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2777, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2667, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/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 "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 441, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:533 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:143 __call__
losses = self.call(y_true, y_pred)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:246 call
return self.fn(y_true, y_pred, **self._fn_kwargs)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:1527 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/backend.py:4561 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/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, 8, 8, 7) are incompatible
Process finished with exit code 1
after making it to Epoch 1/30.
Here is my model definition in case anyone in wondering:
# Create Inception Res Net model as used in paper
resnet = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()
print("Layers of ResNet: "+str(len(resnet.layers))) //782 layers
x = resnet.layers[-28].output
x = tf.keras.layers.Dropout(0.25)(x)
# Make a prediction layer with 7 nodes for the 7 dir in our train_dir.
predictions_layer = tf.keras.layers.Dense(7, activation='softmax')(x)
# print(resnet.input)
# inputs=resnet.input selects the input layer, outputs=predictions refers to the
# dense layer we created above.
model = tf.keras.Model(inputs=resnet.input, outputs=predictions_layer)
What I believe may be the cause of my issue is my model declaration because when I observe my model.summary() what I see is this (with all in between layers excluded of course):
Output of model.summary()
input_1 (InputLayer) [(None, 299, 299, 3) 0
__________________________________________________________________________________________________
dropout (Dropout) (None, 8, 8, 192) 0 batch_normalization_195[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 8, 8, 7) 1351 dropout[0][0]
==================================================================================================
Total params: 47,465,959
Trainable params: 47,411,559
Non-trainable params: 54,400
I included a pastebin of my entire file in case I missed anything: https://pastebin.com/raw/E0VQ83JQ
I understand that it is expecting type (None, None) and my output layer is sent to a Dense Layer of shape (None, 8, 8, 7), but how would I do the reshaping?
Any help is appreciated, including documentation which you'd think I'd find useful on the subject.
There should be a flatten layer between output from ResNet and the Dense layer.
# Create Inception Res Net model as used in paper
resnet = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()
print("Layers of ResNet: "+str(len(resnet.layers))) //782 layers
x = resnet.layers[-28].output
x = tf.keras.layers.Dropout(0.25)(x)
### Edit here.
x = tf.keras.layers.Flatten()(x)
# Make a prediction layer with 7 nodes for the 7 dir in our train_dir.
predictions_layer = tf.keras.layers.Dense(7, activation='softmax')(x)
# print(resnet.input)
# inputs=resnet.input selects the input layer, outputs=predictions refers to the
# dense layer we created above.
model = tf.keras.Model(inputs=resnet.input, outputs=predictions_layer)
Also, make sure that train_batches is valid.
First Convert X and Y into numpy array then convert x_train,y_train into the 'Standardscale' which coverts large values in small values
X=X.values
y=y.values
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
X_train=scaler.fit_transform(X_train)
X_test=scaler.fit_transform(X_test)
Now apply model.fit
Related
I have been trying to stack a single LSTM layer on top of Bert embeddings, but whilst my model starts to train it fails on the last batch and throws the following error message:
Node: 'model/tf.reshape/Reshape'
Input to reshape is a tensor with 59136 values, but the requested shape has 98304
[[{{node model/tf.reshape/Reshape}}]] [Op:__inference_train_function_70500]
This is how I build the model and I honestly cannot figure out what is going wrong here:
batch_size = 128
bert_preprocess = hub.load('https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3')
bert_encoder = hub.KerasLayer('https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4', trainable=True)
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
preprocessed_text = bert_preprocess(text_input)
outputs = bert_encoder(preprocessed_text) #shape=(None, 768)
bert_output = outputs['pooled_output']
l = tf.reshape(bert_output, [batch_size, 1, 768])
l = tf.keras.layers.LSTM(32, activation='relu')(l)
l = tf.keras.layers.Dropout(0.1, name='dropout')(l)
l = tf.keras.layers.Dense(8, activation='softmax', name="output")(l)
model = tf.keras.Model(inputs=[text_input], outputs = [l])
print(model.summary())
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
this is the full output:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
text (InputLayer) [(None,)] 0 []
keras_layer (KerasLayer) {'input_mask': (Non 0 ['text[0][0]']
e, 128),
'input_word_ids':
(None, 128),
'input_type_ids':
(None, 128)}
keras_layer_1 (KerasLayer) {'sequence_output': 109482241 ['keras_layer[0][0]',
(None, 128, 768), 'keras_layer[0][1]',
'default': (None, 'keras_layer[0][2]']
768),
'encoder_outputs':
[(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768),
(None, 128, 768)],
'pooled_output': (
None, 768)}
tf.reshape (TFOpLambda) (128, 1, 768) 0 ['keras_layer_1[0][13]']
lstm (LSTM) (128, 32) 102528 ['tf.reshape[0][0]']
dropout (Dropout) (128, 32) 0 ['lstm[0][0]']
output (Dense) (128, 8) 264 ['dropout[0][0]']
==================================================================================================
Total params: 109,585,033
Trainable params: 102,792
Non-trainable params: 109,482,241
__________________________________________________________________________________________________
None
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fc4ff809440> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fc4ff809440> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
18/19 [===========================>..] - ETA: 25s - loss: 1.5747 - accuracy: 0.5456Traceback (most recent call last):
File "bert-test-lstm.py", line 62, in <module>
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 55, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'model/tf.reshape/Reshape' defined at (most recent call last):
File "bert-test-lstm.py", line 62, in <module>
model.fit(x_train, y_train, epochs=1, batch_size = batch_size)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1021, in train_function
return step_function(self, iterator)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1010, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 1000, in run_step
outputs = model.train_step(data)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/functional.py", line 452, in call
inputs, training=training, mask=mask)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/functional.py", line 589, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1096, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/layers/core/tf_op_layer.py", line 226, in _call_wrapper
return self._call_wrapper(*args, **kwargs)
File "/Users/user/opt/anaconda3/envs/project/lib/python3.7/site-packages/keras/layers/core/tf_op_layer.py", line 261, in _call_wrapper
result = self.function(*args, **kwargs)
Node: 'model/tf.reshape/Reshape'
Input to reshape is a tensor with 59136 values, but the requested shape has 98304
The code runs perfectly fine if I just drop the LSTM and reshape layers - any help is appreciated.
You should use tf.keras.layers.Reshape in order to reshape bert_output into a 3D tensor and automatically taking into account the batch dimension.
Simply changing:
l = tf.reshape(bert_output, [batch_size, 1, 768])
into:
l = tf.keras.layers.Reshape((1,768))(bert_output)
should work.
I am working on training a neural network using keras and keras_tuner for hyper parameter tuning and ran into an error that isn't similar to other questions asked in the website before.
Here's the code that is relevant according to my knowledge:
Splitting my Dataset
x = data.drop('label', axis=1).values
X = normalize(x)
y = pd.get_dummies(data['label']).values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=40)
print("Length of train set: ", X_train.shape, "y:", y_train.shape)
print("Length of test set: ", X_test.shape, "y:", y_test.shape)
which resulted like this:
Length of train set: (7922, 26) y: (7922, 6)
Length of test set: (3396, 26) y: (3396, 6)
Building my Model
def build_model(hp):
model = Sequential()
step = 100
for i in range(hp.Int('num_layers', 3, 4)):
if i == 1:
model.add(
Dense(
units=hp.Int('units_'+str(i), min_value=step*2, max_value=step*5, default=step*2, step=step),
input_dim=26,
activation=hp.Choice('dense_activation_'+str(i), values=['relu', 'tanh', 'sigmoid'])
)
)
else:
model.add(
Dense(units=hp.Int('units_' + str(i), min_value=step*2, max_value=step*5, default=step*2, step=step),
activation=hp.Choice('dense_activation_'+str(i), values=['relu', 'tanh', 'sigmoid'])
)
)
model.add(
Dropout(
hp.Float('dropout_' + str(i), min_value=0.0, max_value=0.1, default=0.1, step=0.02)
)
)
if step > 20:
step = step/2
else:
step = 10
model.add(Dense(6, activation='softmax'))
# optimizer = optimizers.Adam(learning_rate=0.001)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
Using Hyperband to hypertune the Parameters
tuner = kt.Hyperband(
build_model,
max_epochs=100,
objective='accuracy',
seed=42,
executions_per_trial=2
)
tuner.search(X_train, y_train, epochs=500, validation_data=(X_test, y_test))
best_model = tuner.get_best_models()[0]
and Finally after all that i got an error which is as shown:
Traceback (most recent call last):
File "PycharmProjects\FYP_Project\Keras training\Keras Training.py", line 75, in <module>
best_model.evaluate(X_test, y_test)[1] * 100)
File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py", line 1489, in evaluate
tmp_logs = self.test_function(iterator)
File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
result = self._call(*args, **kwds)
File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 763, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "AppData\Roaming\Python\Python39\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 "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\function.py", line 3279, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "AppData\Roaming\Python\Python39\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 "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1323 test_function *
return step_function(self, iterator)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1314 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1307 run_step **
outputs = model.test_step(data)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1266 test_step
y_pred = self(x, training=False)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:1030 __call__
outputs = call_fn(inputs, *args, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\sequential.py:394 call
outputs = layer(inputs, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:1023 __call__
self._maybe_build(inputs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:2625 _maybe_build
self.build(input_shapes) # pylint:disable=not-callable
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\layers\core.py:1191 build
self.kernel = self.add_weight(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:639 add_weight
variable = self._add_variable_with_custom_getter(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\training\tracking\base.py:810 _add_variable_with_custom_getter
new_variable = getter(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer_utils.py:127 make_variable
return tf_variables.VariableV1(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:260 __call__
return cls._variable_v1_call(*args, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:206 _variable_v1_call
return previous_getter(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
return captured_getter(captured_previous, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3523 creator
return next_creator(**kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
return captured_getter(captured_previous, **kwargs)
C:\Users\Axell\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3523 creator
return next_creator(**kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
return captured_getter(captured_previous, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3523 creator
return next_creator(**kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
return captured_getter(captured_previous, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py:750 variable_capturing_scope
v = UnliftedInitializerVariable(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:264 __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py:293 __init__
initial_value = initial_value()
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\training\tracking\base.py:86 __call__
return CheckpointInitialValue(
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\training\tracking\base.py:122 __init__
self.wrapped_value.set_shape(shape)
AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\ops.py:1238 set_shape
raise ValueError(
ValueError: Tensor's shape (26, 400) is not compatible with supplied shape [26, 200]
I'm not even sure where the 400 came from, and i realized that i have a zero on one of my layer, but when i remove that, the error still stayed the same.
the new changes i've made are:
def build_model(hp):
model = Sequential()
for i in range(hp.Int('num_layers', 4, 5)):
# if i == 1:
# model.add(Dense(units=hp.Int('units_' + str(i),
# min_value=20,
# max_value=500,
# step=20),
# input_dim=26,
# activation='relu'))
# else:
model.add(Dense(units=hp.Int('units_' + str(i),
min_value=20,
max_value=500,
step=20),
activation='relu'))
model.add(Dense(6, activation='softmax'))
# optimizer = optimizers.Adam(learning_rate=0.001)
model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(
hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])), metrics=['accuracy'])
return model
Still had the 400 from somewhere, can someone enlighten me of where it comes from?
I think there is bug with Keras tuner, I found the similar issue when getting the best model as below:
models = tuner.get_best_models(num_models=2)
best_model = models[0]
best_model.build(input_shape=(INPUT_FEATURE_SIZE, ))
This problem can be solved by using get_best_hyperparameters() method as below:
best_hp = tuner.get_best_hyperparameters()[0]
best_model = tuner.hypermodel.build(best_hp)
print("Best Model summary is as below: ")
best_model.summary()
So I am trying to build a neural network with multiple outputs. I want to recognize gender and age using a face image and then I will further add more outputs once this issue is resolved.
Input-type = Image (originally 200200, resized to 6464)
Output-type = Array(len = 2)
(Pdb) x_train.shape <br/>
(18965, 64, 64, 1)<br/>
(Pdb) y_train.shape <br/>
(18965, 2)<br/>
(Pdb) x_test.shape<br/>
(4742, 64, 64, 1)<br/>
(Pdb) y_test.shape <br/>
(4742, 2)<br/>
Neural Network:-
input_layer = Input((x_train[0].shape))
conv1 = Conv2D(64,(4,4),activation="relu",strides=(2,2))(input_layer)
batchnorm1 = BatchNormalization()(conv1)
maxpool1 = MaxPooling2D((2,2))(batchnorm1)
drop1 = Dropout(0.3)(maxpool1)
conv2 = Conv2D(64,(4,4),activation="relu",strides=(2,2))(drop1)
batchnorm2 = BatchNormalization()(conv2)
maxpool2 = MaxPooling2D((2,2))(batchnorm2)
drop2 = Dropout(0.3)(maxpool2)
conv3 = Conv2D(64,(2,2), activation='relu',strides=(1,1))(drop2)
dense = Dense(32,activation = 'relu')(conv3)
flat = Flatten()(dense)
dense1 = Dense(32,activation = 'relu')(flat)
dense2 = Dense(32,activation = 'relu')(flat)
age_out = Dense(1,activation = 'relu',name = 'age')(dense1)
gen_out = Dense(1,activation= 'sigmoid', name = 'gen')(dense2)
def scheduler(epoch, lr):
if epoch<25:
return lr
elif epoch%25==0:
return lr * 0.5
learning_rate = tf.keras.callbacks.LearningRateScheduler(scheduler)
adam = Adam(learning_rate=5e-5)
model = Model(inputs=input_layer, outputs = [gen_out,age_out])
# model._set_output_names
model.compile(loss={'gen':"binary_crossentropy",'age':"mae"},optimizer = adam, metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=50,validation_data = (x_test,y_test),epochs=100, callbacks=[learning_rate])
Error:-
Traceback (most recent call last):
File "F:\projects\Ultimate Project\age.py", line 86, in <module>
neural_network()
File "F:\projects\Ultimate Project\age.py", line 78, in neural_network
model.fit(x_train,y_train,batch_size=50,validation_data = (x_test,y_test),epochs=100, callbacks=[learning_rate])
File "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit
tmp_logs = self.train_function(iterator)
File "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\eager\def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\eager\def_function.py", line 726, in _initialize
*args, **kwds))
File "F:\projects\Ultimate Project\env\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 "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\eager\function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\eager\function.py", line 3206, in _create_graph_function
capture_by_value=self._capture_by_value),
File "F:\projects\Ultimate Project\env\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 "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\eager\def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\framework\func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function *
return step_function(self, iterator)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step **
outputs = model.train_step(data)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\engine\training.py:756 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:203 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\losses.py:152 __call__
losses = call_fn(y_true, y_pred)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\losses.py:256 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\losses.py:1608 binary_crossentropy
K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\keras\backend.py:4979 binary_crossentropy
return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
F:\projects\Ultimate Project\env\lib\site-packages\tensorflow\python\ops\nn_impl.py:174 sigmoid_cross_entropy_with_logits
(logits.get_shape(), labels.get_shape()))
ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2))
As far as I can understand, this error means that the neural network is giving output in the shape of (none,1) but my actual output is of (none,2). If I am using this config for loss model.compile(loss=["binary_crossentropy","mae"],optimizer = adam, metrics=['accuracy']) with model = Model(inputs=input_layer, outputs = tf.keras.layers.concatenate([gen_out,age_out], axis=-1)) then it runs but it gives me only a single loss instead of separate losses for both output layers and that too goes negative after some epochs.
I tried my best to explain my problem if there is any confusion then my apologies, let me know and I will add it.
You should also split the y_train and y_test like this:
model.fit(x_train, [y_train[:, 0], y_train[:, 1]], batch_size=50,
validation_data = (x_test, [y_test[:, 0], y_test[:, 1]] ),
epochs=100, callbacks=[learning_rate])
am new to ML so i don't really know what am doing i don't know what logits means in the code i haven't even written logits i just followed a YouTube tutorial to get my self familiar with the environment.. this is the entire code thanks for your help.. i am aware that there is already this kind of post on stackoverflow but i don't think it applies to my situation maybe it does i don't know but i still don't know how to implement it even if it does so please help me out here am struggling :) tnx
the code:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import RMSprop
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
import os
import numpy as np
img = cv2.imread("/content/drive/MyDrive/data/train/ha/2.jpg").shape
print(img)
imgg = image.load_img("/content/drive/MyDrive/data/train/ha/2.jpg")
plt.imshow(imgg)
train = ImageDataGenerator(rescale=1/255)
validation = ImageDataGenerator(rescale=1/255)
train_dataset = train.flow_from_directory("/content/drive/MyDrive/data/train/", target_size = (100,100),
batch_size = 3,
class_mode ="binary")
print(train_dataset.class_indices)
validation_dataset = train.flow_from_directory("/content/drive/MyDrive/data/validate/",
target_size = (100,100),
batch_size = 3,
class_mode ="binary")
model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(3,3),activation = 'relu',input_shape =(200,200,3)),
tf.keras.layers.MaxPool2D(2,2),
#
tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),
tf.keras.layers.MaxPool2D(2,2),
#
tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),
tf.keras.layers.MaxPool2D(2,2),
##
tf.keras.layers.Dense(134,activation = 'relu'),
##
tf.keras.layers.Dense(1,activation = 'sigmoid')
])
model.compile(loss = 'binary_crossentropy',
optimizer = RMSprop(lr=0.001),
metrics =['accuracy'])
model_fit = model.fit(train_dataset,
steps_per_epoch = 3,
epochs = 1,
validation_data = validation_dataset)
the error:
2021-01-01 13:39:18.588397: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1
(51, 51, 3)
Found 9 images belonging to 2 classes.
{'ha': 0, 'hu': 1}
Found 4 images belonging to 2 classes.
2021-01-01 13:39:22.999078: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-01-01 13:39:23.026197: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.1
2021-01-01 13:39:23.092853: E tensorflow/stream_executor/cuda/cuda_driver.cc:328] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2021-01-01 13:39:23.092917: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (6e4fde799083): /proc/driver/nvidia/version does not exist
2021-01-01 13:39:23.093374: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-01-01 13:39:23.846859: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-01-01 13:39:23.850373: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 2300000000 Hz
Traceback (most recent call last):
File "/content/drive/MyDrive/main.py", line 48, in <module>
validation_data = validation_dataset)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1100, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 726, in _initialize
*args, **kwds))
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 3206, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 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:2730 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:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:756 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:203 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:152 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:256 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:1608 binary_crossentropy
K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py:4979 binary_crossentropy
return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_impl.py:174 sigmoid_cross_entropy_with_logits
(logits.get_shape(), labels.get_shape()))
ValueError: logits and labels must have the same shape ((None, 23, 23, 1) vs (None, 1))
Your problem is that the input to the dense layer has to be a vector. To achieve that
you can
replace tf.keras.layers.MaxPool2D(2,2)
with tf.keras.layers.GlobalMaxPooling2D()
or just add
tf.keras.layers.GlobalMaxPooling2D() after tf.keras.layers.MaxPool2D(2,2)
I construct a LSTM network, and my input's dimension is 100*100*83 ( batch_size=100, steps = 100, char_vector = 83). I build a two LSTM layers which has 512 hidden units.
# coding: utf-8
from __future__ import print_function
import tensorflow as tf
import numpy as np
import time
class CharRNN:
def __init__(self, num_classes, batch_size=64, num_steps=50, lstm_size=128, num_layers =2,\
learning_rate = 0.001, grad_clip=5, keep_prob=0.001,sampling= False):
# True for SGD
if sampling == True:
self.batch_size, self.num_steps = 1,1
else:
self.batch_size, self.num_steps = batch_size, num_steps
tf.reset_default_graph()
self.inputs, self.targets, self.keep_prob = self.build_inputs(self.batch_size,self.num_steps)
self.keep_prob = keep_prob
self.cell, self.initial_state = self.build_lstm(lstm_size,num_layers,self.batch_size,self.keep_prob)
# print(self.cell.state_size)
x_one_hot = tf.one_hot(self.inputs, num_classes)
print("cell state size: ",self.cell.state_size)
print("cell initial state: ",self.initial_state)
print("this is inputs", self.inputs)
print("x_one_hot: ",x_one_hot)
outputs, state = tf.nn.dynamic_rnn(self.cell, x_one_hot, initial_state= self.initial_state)
def build_inputs(self, num_seqs, num_steps):
inputs = tf.placeholder(tf.int32, shape=(num_seqs, num_steps), name = "inputs")
targets = tf.placeholder(tf.int32, shape= (num_seqs, num_steps), name="targets")
print('inputs shape: ',inputs.shape)
keep_prob = tf.placeholder(tf.float32, name="keep_prob")
return inputs, targets, keep_prob
def build_lstm(self, lstm_size, num_layers, batch_size, keep_prob):
# construct lstm cell
lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
# add dropout
drop = tf.nn.rnn_cell.DropoutWrapper(lstm, output_keep_prob= keep_prob)
# stack multiple rnn cells
cell = tf.nn.rnn_cell.MultiRNNCell([drop for _ in range(num_layers)])
initial_state = cell.zero_state(batch_size, tf.float32)
return cell, initial_state
if __name__ == '__main__':
len_vocab = 83
batch_size = 100
num_steps = 100
lstm_size = 512
num_layers = 2
learning_rate = 0.001
keep_prob = 0.5
epochs = 20
save_every_n = 200
print("h1")
model = CharRNN(len_vocab, batch_size = batch_size, num_steps=num_steps, lstm_size = lstm_size,num_layers=num_layers\
,learning_rate=learning_rate,sampling= False,keep_prob = keep_prob
I get a dimension not match error at tf.nn.dynamic_rnn.
error message is like this:
inputs shape: (100, 100)
cell state size: (LSTMStateTuple(c=512, h=512), LSTMStateTuple(c=512, h=512))
cell initial state: (LSTMStateTuple(c=<tf.Tensor 'MultiRNNCellZeroState/DropoutWrapperZeroState/BasicLSTMCellZeroState/zeros:0' shape=(100, 512) dtype=float32>, h=<tf.Tensor 'MultiRNNCellZeroState/DropoutWrapperZeroState/BasicLSTMCellZeroState/zeros_1:0' shape=(100, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'MultiRNNCellZeroState/DropoutWrapperZeroState_1/BasicLSTMCellZeroState/zeros:0' shape=(100, 512) dtype=float32>, h=<tf.Tensor 'MultiRNNCellZeroState/DropoutWrapperZeroState_1/BasicLSTMCellZeroState/zeros_1:0' shape=(100, 512) dtype=float32>))
this is inputs Tensor("inputs:0", shape=(100, 100), dtype=int32)
x_one_hot: Tensor("one_hot:0", shape=(100, 100, 83), dtype=float32)
Traceback (most recent call last):
File "./seq2_minimal.py", line 70, in <module>
,learning_rate=learning_rate,sampling= False,keep_prob = keep_prob)
File "./seq2_minimal.py", line 32, in __init__
outputs, state = tf.nn.dynamic_rnn(self.cell, x_one_hot, initial_state= self.initial_state)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 614, in dynamic_rnn
dtype=dtype)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 777, in _dynamic_rnn_loop
swap_memory=swap_memory)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2816, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2640, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2590, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 762, in _time_step
(output, new_state) = call_cell()
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 748, in <lambda>
call_cell = lambda: cell(input_t, state)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 183, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 575, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1066, in call
cur_inp, new_state = cell(cur_inp, cur_state)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 891, in __call__
output, new_state = self._cell(inputs, state, scope)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 183, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 575, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 441, in call
value=self._linear([inputs, h]), num_or_size_splits=4, axis=1)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1189, in __call__
res = math_ops.matmul(array_ops.concat(args, 1), self._weights)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1891, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2437, in _mat_mul
name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2958, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2209, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2159, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn
require_shape_fn)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 691, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 1024 and 595 for 'rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [100,1024], [595,2048].
I search that and find that tensorflow's lstm cell should adjust its input size automatically. But error message said this.
It shows
input size is [100, 1024] and lstm is [595, 2048].
Thanks firstly.
cell = tf.nn.rnn_cell.MultiRNNCell([drop for _ in range(num_layers)])
TO
cell = tf.nn.rnn_cell.MultiRNNCell([drop])
because your given input tensor and produces tensor are not the same.