I want to include a pre-trained XLNet (or possibly another state of the art transformer) in a model to fine-tune it.
However, it doesn't work when I include it with keras layers.
import tensorflow as tf
from transformers import AutoTokenizer, TFAutoModel
inputs = tf.keras.Input(shape=2000, dtype='int32')
x = inputs
xlnetPretrainedModel = TFAutoModel.from_pretrained("xlnet-base-cased")
x = xlnetPretrainedModel(x)
x = tf.keras.layers.GlobalAveragePooling1D()(x)
x = tf.keras.layers.Dense(32, activation='relu')(x)
x = tf.keras.layers.Dense(32, activation=None)(x)
model = tf.keras.Model(inputs=inputs, outputs=x)
model.compile(optimizer='adam',
loss='mean_squared_error')
model.summary()
The bug is
AttributeError: 'NoneType' object has no attribute 'shape'
at the line
x = xlnetPretrainedModel(x)
So when the model is used on the input layer.
The XLNet model works if used on a numpy array, but then I wouldn't be able to train it.
The full error message is:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-d543506f9697> in <module>
5 x = inputs
6 xlnetPretrainedModel = TFAutoModel.from_pretrained("xlnet-base-cased")
----> 7 x = xlnetPretrainedModel(x)
8 x = tf.keras.layers.GlobalAveragePooling1D()(x)
9 x = tf.keras.layers.Dense(32, activation='relu')(x)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
771 not base_layer_utils.is_in_eager_or_tf_function()):
772 with auto_control_deps.AutomaticControlDependencies() as acd:
--> 773 outputs = call_fn(cast_inputs, *args, **kwargs)
774 # Wrap Tensors in `outputs` in `tf.identity` to avoid
775 # circular dependencies.
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py in wrapper(*args, **kwargs)
235 except Exception as e: # pylint:disable=broad-except
236 if hasattr(e, 'ag_error_metadata'):
--> 237 raise e.ag_error_metadata.to_exception(e)
238 else:
239 raise
AttributeError: in converted code:
/opt/conda/lib/python3.7/site-packages/transformers/modeling_tf_xlnet.py:810 call *
outputs = self.transformer(inputs, **kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:805 __call__
inputs, outputs, args, kwargs)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:2014 _set_connectivity_metadata_
input_tensors=inputs, output_tensors=outputs, arguments=arguments)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:2044 _add_inbound_node
arguments=arguments)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/node.py:110 __init__
self.output_shapes = nest.map_structure(backend.int_shape, output_tensors)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py:568 map_structure
structure[0], [func(*x) for x in entries],
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py:568 <listcomp>
structure[0], [func(*x) for x in entries],
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py:1172 int_shape
shape = x.shape
AttributeError: 'NoneType' object has no attribute 'shape'
or after trying a solution presented here https://github.com/huggingface/transformers/issues/1350 by decoring the call by a tf.function
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-16-c852fba5aa15> in <module>
8 xlnetPretrainedModel = TFAutoModel.from_pretrained("xlnet-base-cased")
9 xlnetPretrainedModel.call = tf.function(xlnetPretrainedModel.transformer.call)
---> 10 x = xlnetPretrainedModel(x)
11
12 x = tf.keras.layers.GlobalAveragePooling1D()(x)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
803 kwargs.pop('mask')
804 inputs, outputs = self._set_connectivity_metadata_(
--> 805 inputs, outputs, args, kwargs)
806 self._handle_activity_regularization(inputs, outputs)
807 self._set_mask_metadata(inputs, outputs, input_masks)
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
2012 # This updates the layer history of the output tensor(s).
2013 self._add_inbound_node(
-> 2014 input_tensors=inputs, output_tensors=outputs, arguments=arguments)
2015 return inputs, outputs
2016
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
2042 input_tensors=input_tensors,
2043 output_tensors=output_tensors,
-> 2044 arguments=arguments)
2045
2046 # Update tensor history metadata.
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/node.py in __init__(self, outbound_layer, inbound_layers, node_indices, tensor_indices, input_tensors, output_tensors, arguments)
108 self.input_shapes = nest.map_structure(backend.int_shape, input_tensors)
109 # Nested structure of shape tuples, shapes of output_tensors.
--> 110 self.output_shapes = nest.map_structure(backend.int_shape, output_tensors)
111
112 # Optional keyword arguments to layer's `call`.
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py in map_structure(func, *structure, **kwargs)
566
567 return pack_sequence_as(
--> 568 structure[0], [func(*x) for x in entries],
569 expand_composites=expand_composites)
570
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/util/nest.py in <listcomp>(.0)
566
567 return pack_sequence_as(
--> 568 structure[0], [func(*x) for x in entries],
569 expand_composites=expand_composites)
570
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in int_shape(x)
1170 """
1171 try:
-> 1172 shape = x.shape
1173 if not isinstance(shape, tuple):
1174 shape = tuple(shape.as_list())
AttributeError: 'NoneType' object has no attribute 'shape'
Please, can anyone help me fix this error?
Generally the model output differs from model to model in the Huggingface transformers library. Check the documentation for what values are returned from the XLNet's "call()" function.
The last hidden state can generally be accessed in the following way:
model= TFAutoModel.from_pretrained("xlnet-base-cased")
# 'last_hidden_state' seems to be common to most of the transformer models
# refer: https://huggingface.co/transformers/model_doc/bert.html#tfbertmodel
output = model(tokenizer_ouput).last_hidden_state
x = tf.keras.layers.Dense(32, activation='relu')(x)
# The rest of you model
Making this change should solve your issue.
The XLNet Model takes in input which has been passed through the XLNet tokenizer.
Given:
from transformers import XLNetTokenizer
xlnet_tokenizer = XLNetTokenizer.from_pretrained("xlnet-base-cased")
For a single forward pass, you can then generate your x as something like:
x = xlnet_tokenizer('Hello world', padding=True, return_tensors="tf")
After that, you should be able to proceed with:
xlnet_output = xlnetPretrainedModel(x)
To use x = xlnetPretrainedModel(x) in the functional API definition of your model, the input x will need to be in a format identical to output from xlnet_tokenizer.
Which is:
{'input_ids': <Tensor>, 'token_type_ids':<Tensor>, 'attention_mask':<Tensor>}.
You can do that with the functional API like:
input_ids = tf.keras.layers.Input(shape=(max_length,), dtype=tf.int32, name="input_ids")
token_type_ids = tf.keras.layers.Input(shape=(max_length,), dtype=tf.int32, name="token_type_ids")
attention_masks = tf.keras.layers.Input(shape=(max_length,), dtype=tf.int32, name="attention_mask")
x = xlnetPretrainedModel({
'input_ids': input_ids,
'token_type_ids':token_type_ids,
'attention_mask':attention_mask})
...
You should be able to proceed like that without any more of such problems.
Related
I have searched through stackoverflow and read through documentation but somehow still my trained model is not being loaded!
I have checked following links and many more so please do not mark as duplicate straight away. thank you
How to save final model using keras?
https://www.tensorflow.org/guide/keras/save_and_serialize
https://www.tensorflow.org/tutorials/distribute/save_and_load
https://www.tensorflow.org/guide/keras/functional
How to load a model from an HDF5 file in Keras?
here is my model:
def get_model(train=True):
set_seed(33)
pre_process = Lambda(preprocess_input)
vgg = VGG16(weights = 'imagenet', include_top = True, input_shape = SHAPE)
vgg = Model(vgg.input, vgg.layers[-3].output)
vgg.trainable = False
inp = Input(SHAPE)
vgg_16_process = pre_process(GaussianNoise(0.1)(inp))
vgg_out = vgg(vgg_16_process)
noise = Lambda(tf.zeros_like)(vgg_out)
noise = GaussianNoise(0.1)(noise)
if train:
x = Lambda(lambda z: tf.concat(z, axis=0))([vgg_out,noise])
x = Activation('relu')(x)
else:
x = vgg_out
x = Dense(512, activation='relu')(x)
x = Dense(128, activation='relu')(x)
out = Dense(2, activation='softmax')(x)
model = Model(inp, out)
model.compile(Adam(learning_rate=1e-4), loss='binary_crossentropy')
return model
And After that I have some test and train generator as follows,
The dataset is classical dog-vs-cat dataset and I'm trying to achieve one-class classification task.
train_datagen = ImageDataGenerator()
test_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_directory(
base_path + 'training_set/training_set/',
target_size = (SHAPE[0], SHAPE[1]),
batch_size = batch_size,
class_mode = 'categorical',
shuffle = True,
seed = 33,
classes = ['cats']
)
test_generator = test_datagen.flow_from_directory(
base_path + 'test_set/test_set/',
target_size = (SHAPE[0], SHAPE[1]),
batch_size = batch_size,
class_mode = 'categorical',
shuffle = True,
seed = 33,
classes = ['dogs','cats']
)
Then finally, I do compile the model
model = get_model()
model.fit(wrap_generator(train_generator), steps_per_epoch=train_generator.samples/train_generator.batch_size, epochs=1)
Then I do save model as follow
model.save('my_custom_model')
Either I do save in that format or 'my_custom_model.h5' it saves perfectly without any error.
If I use just save, then a folder is created with 'assets, variables, .pb files' that is expected as mentioned in posts of stackoverflow.
** Then here comes the problem **
from keras.models import load_model
loaded_model = load_model('my_custom_model.h5')
#or
loaded_model = tf.keras.models.load_model('my_custom_model.h5')
#or
loaded_model = load_model('my_custom_model') # as from folder
All throw same error
TypeError: 'module' object is not callable
I know I'm doing some mistake or model is bit different, please guide me to the right direction where I can look for solutions.
Python: 3.7.2
Tensorflow: 2.6.0
Keras: 2.6.0
Full Stack Trace
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\ops\array_ops.py in wrapper(*args, **kwargs)
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
TypeError: 'str' object is not callable
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-10-cd6655ece7c5> in <module>
7 # loaded_model = tf.keras.models.load_model("my_custom_model2")
8
----> 9 loaded_model = tf.keras.models.load_model('model_notebook.h5')
10 # print(loaded_model)
11 # print("------------")
~\AppData\Roaming\Python\Python37\site-packages\keras\saving\save.py in load_model(filepath, custom_objects, compile, options)
199 (isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
200 return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
--> 201 compile)
202
203 filepath = path_to_string(filepath)
~\AppData\Roaming\Python\Python37\site-packages\keras\saving\hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
179 model_config = json_utils.decode(model_config)
180 model = model_config_lib.model_from_config(model_config,
--> 181 custom_objects=custom_objects)
182
183 # set weights
~\AppData\Roaming\Python\Python37\site-packages\keras\saving\model_config.py in model_from_config(config, custom_objects)
50 '`Sequential.from_config(config)`?')
51 from keras.layers import deserialize # pylint: disable=g-import-not-at-top
---> 52 return deserialize(config, custom_objects=custom_objects)
53
54
~\AppData\Roaming\Python\Python37\site-packages\keras\layers\serialization.py in deserialize(config, custom_objects)
210 module_objects=LOCAL.ALL_OBJECTS,
211 custom_objects=custom_objects,
--> 212 printable_module_name='layer')
~\AppData\Roaming\Python\Python37\site-packages\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
676 custom_objects=dict(
677 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 678 list(custom_objects.items())))
679 else:
680 with CustomObjectScope(custom_objects):
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\functional.py in from_config(cls, config, custom_objects)
661 with generic_utils.SharedObjectLoadingScope():
662 input_tensors, output_tensors, created_layers = reconstruct_from_config(
--> 663 config, custom_objects)
664 model = cls(inputs=input_tensors, outputs=output_tensors,
665 name=config.get('name'))
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\functional.py in reconstruct_from_config(config, custom_objects, created_layers)
1281 if layer in unprocessed_nodes:
1282 for node_data in unprocessed_nodes.pop(layer):
-> 1283 process_node(layer, node_data)
1284
1285 input_tensors = []
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\functional.py in process_node(layer, node_data)
1229 input_tensors = (
1230 base_layer_utils.unnest_if_single_tensor(input_tensors))
-> 1231 output_tensors = layer(input_tensors, **kwargs)
1232
1233 # Update node index map.
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
975 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
976 return self._functional_construction_call(inputs, args, kwargs,
--> 977 input_list)
978
979 # Maintains info about the `Layer.call` stack.
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1113 # Check input assumptions set after layer building, e.g. input shape.
1114 outputs = self._keras_tensor_symbolic_call(
-> 1115 inputs, input_masks, args, kwargs)
1116
1117 if outputs is None:
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
846 return tf.nest.map_structure(keras_tensor.KerasTensor, output_signature)
847 else:
--> 848 return self._infer_output_signature(inputs, args, kwargs, input_masks)
849
850 def _infer_output_signature(self, inputs, args, kwargs, input_masks):
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
886 self._maybe_build(inputs)
887 inputs = self._maybe_cast_inputs(inputs)
--> 888 outputs = call_fn(inputs, *args, **kwargs)
889
890 self._handle_activity_regularization(inputs, outputs)
~\AppData\Roaming\Python\Python37\site-packages\keras\layers\core.py in call(self, inputs, mask, training)
901 with tf.GradientTape(watch_accessed_variables=True) as tape,\
902 tf.variable_creator_scope(_variable_creator):
--> 903 result = self.function(inputs, **kwargs)
904 self._check_variables(created_variables, tape.watched_variables())
905 return result
~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\ops\array_ops.py in wrapper(*args, **kwargs)
208 # Note: convert_to_eager_tensor currently raises a ValueError, not a
209 # TypeError, when given unexpected types. So we need to catch both.
--> 210 result = dispatch(wrapper, args, kwargs)
211 if result is not OpDispatcher.NOT_SUPPORTED:
212 return result
TypeError: 'module' object is not callable
The dataset is classical dog-vs-cat dataset and I'm trying to achieve
one-class classification task.
Then the model should have only one output node because this is an example of Binary Classification. Your model has the last Dense layer with two output units and the softmax as an activation function, which is a possible solution, but for this kind of problem my suggestion is to specify on the last Dense layer only one output unit and the sigmoid as the activation function.
It seems you forgot to add the string dog in the classes list on the train_generator.
train_generator = train_datagen.flow_from_directory(
base_path + 'training_set/training_set/',
target_size = (SHAPE[0], SHAPE[1]),
batch_size = batch_size,
class_mode = 'categorical',
shuffle = True,
seed = 33,
classes = ['cats']
)
Now, regards on the main topic of your question.
from keras.models import load_model
loaded_model = load_model('my_custom_model.h5')
#or
loaded_model = tf.keras.models.load_model('my_custom_model.h5')
#or
loaded_model = load_model('my_custom_model') # as from folder
I think, but I'm not 100% sure about it, the problem is from this line of code from keras.models import load_model. Starting from Tensorflow 2.x, which includes also the Keras module, every single function, class etc. must be imported by specifying always tensorflow as the first name for import each Keras module. In short, you should write this line of code: from tensorflow.keras.models import load_model and it should work because it calls the function from the Keras module with the Tensorflow as the back engine.
I am trying to build a vector quantized variational autoencoder in tensorflow. A very cryptic error occurs when I build my model and start training.
I have gone through the code to find an mistakes but I could not find one. The error is very unhelpful in finding the cause. Below is part of the code for model
def call(self, inputs):
z = self.encode(inputs)
vq_output = self.vector_quantizer(z)
reconstructed_image = self.decode(vq_output['quantize'])
return reconstructed_image, vq_output
def encode(self, image):
encoded = Conv2D(int(self._num_hidden/2), kernel_size=(4,4), strides=(2,2), activation='relu', name='enc1')(image)
encoded = Conv2D(self._num_hidden, kernel_size=(4,4), strides=(2,2), activation='relu', name='enc2')(encoded)
encoded = Conv2D(self._num_hidden, kernel_size=(3,3), strides=(1,1), name='enc2')(encoded)
encoded = residual_stack(encoded, self._num_hidden, self._num_residual_layers, self._num_residual_hiddens)
return Conv2D(self._embeding_dim, kernel_size=(1,1) ,strides=(1,1), name='enc3')(encoded)
def decode(self, encoded_input):
decoded = Conv2D(self._decoder_num_hidden, kernel_size=(3,3), strides=(1,1), name='dec1')(encoded_input)
decoded = residual_stack(decoded, self._decoder_num_hidden, self._decoder_num_residual_layers, self._decoder_num_residual_hiddens)
decoded = Conv2DTranspose(int(self._decoder_num_hidden/2), kernel_size=(4,4), strides=(2,2), activation='relu', name='dec2')(decoded)
return Conv2DTranspose(3, kernel_size=(4,4), strides=(2,2), name='dec3')(decoded)
The following error occurs:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-90a0a1fb38f6> in <module>()
15
16 for batch in train_batches:
---> 17 _loss, _perplexity = train(vq_vae, batch['images'], optimizer)
18 loss.update_state(_loss)
19 perplexity.update_state(_perplexity)
14 frames
<ipython-input-45-0025b05fe5c9> in train(model, inputs, optimizer, outputs)
1 def train(model, inputs, optimizer, outputs=None):
2 with tf.GradientTape() as tape:
----> 3 loss, perplexity = compute_loss(model, inputs)
4 gradients = tape.gradient(loss, model.trainable_variables)
5 optimizer.apply_gradients(zip(gradients, model.trainable_variables))
<ipython-input-44-242959fe043f> in compute_loss(model, inputs, outputs)
1 def compute_loss(model, inputs, outputs=None):
----> 2 recon, vq_outputs = model(inputs)
3 recon_loss = tf.reduce_mean((recon - inputs)**2) / data_variance
4 loss = recon_loss + vq_outputs['loss']
5 return loss, vq_outputs['perplexity']
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
677 with base_layer_utils.autocast_context_manager(
678 input_list, self._mixed_precision_policy.should_cast_variables):
--> 679 outputs = self.call(inputs, *args, **kwargs)
680 self._handle_activity_regularization(inputs, outputs)
681 self._set_mask_metadata(inputs, outputs, previous_mask)
<ipython-input-43-f28d8aaad600> in call(self, inputs)
40 vq_output = self.vector_quantizer(z)
41
---> 42 reconstructed_image = self.decode(vq_output['quantize'])
43
44 return reconstructed_image, vq_output
<ipython-input-43-f28d8aaad600> in decode(self, encoded_input)
55
56 def decode(self, encoded_input):
---> 57 decoded = Conv2D(self._decoder_num_hidden, kernel_size=(3,3), strides=(1,1), name='dec1')(encoded_input)
58
59 decoded = residual_stack(decoded, self._decoder_num_hidden, self._decoder_num_residual_layers, self._decoder_num_residual_hiddens)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
674 # Eager execution on data tensors.
675 with backend.name_scope(self._name_scope()):
--> 676 self._maybe_build(inputs)
677 with base_layer_utils.autocast_context_manager(
678 input_list, self._mixed_precision_policy.should_cast_variables):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
1879 # operations.
1880 with tf_utils.maybe_init_scope(self):
-> 1881 self.build(input_shapes)
1882 # We must set self.built since user defined build functions are not
1883 # constrained to set self.built.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/convolutional.py in build(self, input_shape)
163 constraint=self.kernel_constraint,
164 trainable=True,
--> 165 dtype=self.dtype)
166 if self.use_bias:
167 self.bias = self.add_weight(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, synchronization, aggregation, **kwargs)
382 collections=collections_arg,
383 synchronization=synchronization,
--> 384 aggregation=aggregation)
385 backend.track_variable(variable)
386
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
661 dtype=dtype,
662 initializer=initializer,
--> 663 **kwargs_for_getter)
664
665 # If we set an initializer and the variable processed it, tracking will not
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer_utils.py in make_variable(name, shape, dtype, initializer, trainable, caching_device, validate_shape, constraint, use_resource, collections, synchronization, aggregation, partitioner)
140 # TODO(apassos,rohanj) figure out how to remove collections from here so we
141 # can remove the V1.
--> 142 variable_shape = tensor_shape.TensorShape(shape)
143 return tf_variables.VariableV1(
144 initial_value=init_val,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, dims)
772 else:
773 # Got a list of dimensions
--> 774 self._dims = [as_dimension(d) for d in dims_iter]
775
776 #property
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in <listcomp>(.0)
772 else:
773 # Got a list of dimensions
--> 774 self._dims = [as_dimension(d) for d in dims_iter]
775
776 #property
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in as_dimension(value)
714 return value
715 else:
--> 716 return Dimension(value)
717
718
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, value)
183 raise TypeError("Cannot convert %s to Dimension" % value)
184 else:
--> 185 self._value = int(value)
186 if (not isinstance(value, compat.bytes_or_text_types) and
187 self._value != value):
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
I cannot understand where I made a mistake by looking at this error.
I have started working on Keras, my level is very basic, I'm getting the invalid argument error in my code I'm using Keras to load the weights and do some prediction. I tried to use Keras to update because I thought it was a Keras version issue but it is not because Keras version issues arise in previous versions that were quite exhausting. Then I tried updating my TensorFlow and other libraries because from tutorials I got to know that these issues can be arises. I found this link https://github.com/raghakot/keras-vis/pull/120/files/74f2d086feb9556cbb2f1cfd90d6d4f8c21f520d but didn't get enough from it that how to fix this error. I want you to help me in solving this invalid argument error there is no information I got so far.
Kindly help me out if you can Because I really need to do this I'm stuck here.
img_width, img_height=300,300
# predicting images
img = image.load_img('omnglot.jpg', target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
print(images)
classes = model.predict_classes(images, batch_size=32)
#print(classes)
# predicting multiple images at once
img = image.load_img('omnglot2.jpg', target_size=(img_width, img_height))
y = image.img_to_array(img)
y = np.expand_dims(y, axis=0)
# pass the list of multiple images np.vstack()
images = np.vstack([x, y])
classes = model.predict_classes(images, batch_size=32)
# print the classes, the images belong to
print(classes)
print(classes[0])
print(classes[0][0])
InvalidArgumentError Traceback (most recent call last)
<ipython-input-10-4fda4804f4b7> in <module>
7 images = np.vstack([x])
8 print(images)
----> 9 classes = model.predict_classes(images, batch_size=32)
10 #print(classes)
11
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py in predict_classes(self, x, batch_size, verbose)
265 A numpy array of class predictions.
266 """
--> 267 proba = self.predict(x, batch_size=batch_size, verbose=verbose)
268 if proba.shape[-1] > 1:
269 return proba.argmax(axis=-1)
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in predict(self, x, batch_size, verbose, steps)
1167 batch_size=batch_size,
1168 verbose=verbose,
-> 1169 steps=steps)
1170
1171 def train_on_batch(self, x, y,
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps)
292 ins_batch[i] = ins_batch[i].toarray()
293
--> 294 batch_outs = f(ins_batch)
295 batch_outs = to_list(batch_outs)
296 if batch_index == 0:
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
2669 feed_symbols,
2670 symbol_vals,
-> 2671 session)
2672 if self.run_metadata:
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _make_callable(self, feed_arrays, feed_symbols, symbol_vals, session)
2621 callable_opts.run_options.CopyFrom(self.run_options)
2622 # Create callable.
-> 2623 callable_fn = session._make_callable_from_options(callable_opts)
2624 # Cache parameters corresponding to the generated callable, so that
2625 # we can detect future mismatches and refresh the callable.
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _make_callable_from_options(self, callable_options)
1469 """
1470 self._extend_graph()
-> 1471 return BaseSession._Callable(self, callable_options)
1472
1473
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __init__(self, session, callable_options)
1423 with errors.raise_exception_on_not_ok_status() as status:
1424 self._handle = tf_session.TF_SessionMakeCallable(
-> 1425 session._session, options_ptr, status)
1426 finally:
1427 tf_session.TF_DeleteBuffer(options_ptr)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: sequential_6_input:0 is both fed and fetched.
I would like to implement a convolutional recurrent neural net with lstm in keras. I am a only a beginner in Machine Learning therefore I struggle understanding everything. Here is my code:
def initialize_model():
'Function to initialize the Convolutional Neural Network'
seed=123
# define CNN model
model = Sequential()
model.add(TimeDistributed(Conv2D(input_shape=(None,1,2048,1),kernel_size=(256,1),strides=(32,1),filters=16
,activation=LeakyReLU(0.01),data_format='channels_first')))
#cnn.add(MaxPooling2D(...))
model.add(TimeDistributed(Flatten()))
# define LSTM model
#model = Sequential()
#model.add(TimeDistribu(cnn))
model.add(LSTM(1024,recurrent_activation=LeakyReLU(0.01),recurrent_initializer=glorot_normal(seed)
,recurrent_regularizer=regularizers.l2(0.005)))
model.add(Dense(2048))
return model
def compile_model(model,learning_rate,loss_function):
#optimizer
optimizer = Adam(lr=learning_rate,beta_1=0.9,beta_2=0.999,epsilon=1e-8)
#Compile the model
model.compile(optimizer=optimizer,loss=loss_function,metrics=["accuracy"])
return model
# import and split data
data = get_data_from_files(10)
print(data.shape)
X_train, X_test, y_train, y_test = train_test_split(data[0],data[1], test_size=0.2, random_state=123)
print(X_train.shape) -> 160000,2048
print(y_train.shape) -> 160000,2048
print(X_test.shape) -> 40000,2048
print(y_test.shape) -> 40000,2048
X_tr = np.expand_dims(np.expand_dims(X_train,axis=2),axis=3)
X_te = np.expand_dims(np.expand_dims(X_test,axis=2),axis=3)
#X_tr shape -->(160000, 2048, 1, 1)
#X_te shape -->(40000, 2048, 1, 1)
model = initialize_model()
model =compile_model(model,0.001,"cosine_proximity")
hist = model.fit(X_tr,y_train
,validation_data=[X_te,y_test]
,epochs=5,batch_size=1000)
It returns the following error:
ValueError Traceback (most recent call last)
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
666 try:
--> 667 self.assert_same_rank(other)
668 new_dims = []
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in assert_same_rank(self, other)
711 raise ValueError("Shapes %s and %s must have the same rank" % (self,
--> 712 other))
713
ValueError: Shapes (256, 1, 2048, 16) and (?, ?, ?) must have the same rank
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
741 try:
--> 742 return self.merge_with(unknown_shape(ndims=rank))
743 except ValueError:
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
672 except ValueError:
--> 673 raise ValueError("Shapes %s and %s are not compatible" % (self, other))
674
ValueError: Shapes (256, 1, 2048, 16) and (?, ?, ?) are not compatible
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-36-1633649265a0> in <module>()
6 hist = model.fit(X_tr,y_train
7 ,validation_data=[X_te,y_test]
----> 8 ,epochs=5,batch_size=1000)
9
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
953 sample_weight=sample_weight,
954 class_weight=class_weight,
--> 955 batch_size=batch_size)
956 # Prepare validation data.
957 do_validation = False
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
674 # to match the value shapes.
675 if not self.inputs:
--> 676 self._set_inputs(x)
677
678 if y is not None:
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
574 assert len(inputs) == 1
575 inputs = inputs[0]
--> 576 self.build(input_shape=(None,) + inputs.shape[1:])
577 return
578
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/sequential.py in build(self, input_shape)
225 self.inputs = [x]
226 for layer in self._layers:
--> 227 x = layer(x)
228 self.outputs = [x]
229
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
458 # Actually call the layer,
459 # collecting output(s), mask(s), and shape(s).
--> 460 output = self.call(inputs, **kwargs)
461 output_mask = self.compute_mask(inputs, previous_mask)
462
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/layers/wrappers.py in call(self, inputs, training, mask)
246 inner_mask_shape = self._get_shape_tuple((-1,), mask, 2)
247 kwargs['mask'] = K.reshape(mask, inner_mask_shape)
--> 248 y = self.layer.call(inputs, **kwargs)
249 if hasattr(y, '_uses_learning_phase'):
250 uses_learning_phase = y._uses_learning_phase
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/layers/convolutional.py in call(self, inputs)
166 padding=self.padding,
167 data_format=self.data_format,
--> 168 dilation_rate=self.dilation_rate)
169 if self.rank == 3:
170 outputs = K.conv3d(
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in conv2d(x, kernel, strides, padding, data_format, dilation_rate)
3564 strides=strides,
3565 padding=padding,
-> 3566 data_format=tf_data_format)
3567
3568 if data_format == 'channels_first' and tf_data_format == 'NHWC':
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in convolution(input, filter, padding, strides, dilation_rate, name, data_format)
777 dilation_rate=dilation_rate,
778 name=name,
--> 779 data_format=data_format)
780 return op(input, filter)
781
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, strides, dilation_rate, name, data_format)
854 filter_shape=filter_shape,
855 spatial_dims=spatial_dims,
--> 856 data_format=data_format)
857
858 def _build_op(self, _, padding):
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, dilation_rate, padding, build_op, filter_shape, spatial_dims, data_format)
437 raise ValueError("dilation_rate must be positive")
438 if np.all(const_rate == 1):
--> 439 self.call = build_op(num_spatial_dims, padding)
440 return
441
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in _build_op(self, _, padding)
863 data_format=self.data_format,
864 strides=self.strides,
--> 865 name=self.name)
866
867 def __call__(self, inp, filter): # pylint: disable=redefined-builtin
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, data_format, strides, name)
134 strides=None,
135 name=None):
--> 136 filter_shape = filter_shape.with_rank(input_shape.ndims)
137 self.padding = padding
138 self.name = name
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
742 return self.merge_with(unknown_shape(ndims=rank))
743 except ValueError:
--> 744 raise ValueError("Shape %s must have rank %d" % (self, rank))
745
746 def with_rank_at_least(self, rank):
ValueError: Shape (256, 1, 2048, 16) must have rank 3
I am completely stuck and have no idea how to resolve the problem.
EDIT:
I updated my code with the new error which is now a error of shape.
Fo anyone with a similar problem, I changed the trained and test data shape as follow :
X_tr = np.expand_dims(np.expand_dims(np.expand_dims(X_train,axis=1),axis=3),axis=1)
X_te = np.expand_dims(np.expand_dims(np.expand_dims(X_test,axis=1),axis=3),axis=1)
and the first layer :
model.add(TimeDistributed(Conv2D(kernel_size=(256,1),strides=(32,1),filters=16,activation=LeakyReLU(0.01),data_format='channels_first'),input_shape=(None,1,2048,1)))
It know run very well.
I am fairly confident that I have found the solution so this answer is edited to reflect that. According to keras documentation, when ising TimeDistribution with conv2d the input_shape must be a touple of length 4
Below is a part of the keras documentation.
TimeDistributed can be used with arbitrary layers, not just Dense, for instance with a Conv2D layer:
model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3, 3)),
input_shape=(10, 299, 299, 3)))
I am trying to use DynamicRnnEstimator but I am getting a "'list' object has no attribute 'key'" Error.
Code:
feature_names = [
'FeatureA',
'FeatureB',
'FeatureC',
'FeatureD',
'FeatureE',
'FeatureF']
...
feature_columns = [tf.feature_column.numeric_column(k) for k in feature_names]
print (feature_columns)
estimator = tf.contrib.learn.DynamicRnnEstimator(problem_type = constants.ProblemType.CLASSIFICATION,
prediction_type = rnn_common.PredictionType.SINGLE_VALUE,
sequence_feature_columns = [feature_columns],
context_feature_columns = None,
num_units = 5,
num_classes = 11,
cell_type = 'lstm',
optimizer = 'SGD',
model_dir = "model",
learning_rate = 0.1)
estimator.fit(input_fn=lambda: input_fn("train.csv"), steps=STEPS)
Here's the output:
[_NumericColumn(key='FeatureA', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='FeatureB', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='FeatureC', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='FeatureD', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='FeatureE', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='FeatureF', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)]
...
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-83-bea117372333> in <module>()
26 learning_rate = 0.1)
27
---> 28 estimator.fit(input_fn=lambda: input_fn("train.csv"), steps=STEPS)
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/python/util/deprecation.pyc in new_func(*args, **kwargs)
314 'in a future version' if date is None else ('after %s' % date),
315 instructions)
--> 316 return func(*args, **kwargs)
317 return tf_decorator.make_decorator(func, new_func, 'deprecated',
318 _add_deprecated_arg_notice_to_docstring(
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in fit(self, x, y, input_fn, steps, batch_size, monitors, max_steps)
478 hooks.append(basic_session_run_hooks.StopAtStepHook(steps, max_steps))
479
--> 480 loss = self._train_model(input_fn=input_fn, hooks=hooks)
481 logging.info('Loss for final step: %s.', loss)
482 return self
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in _train_model(self, input_fn, hooks)
984 global_step_read_tensor = training_util._get_or_create_global_step_read() # pylint: disable=protected-access
985 with ops.control_dependencies([global_step_read_tensor]):
--> 986 model_fn_ops = self._get_train_ops(features, labels)
987 ops.add_to_collection(ops.GraphKeys.LOSSES, model_fn_ops.loss)
988 all_hooks.extend(hooks)
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in _get_train_ops(self, features, labels)
1200 `ModelFnOps` object.
1201 """
-> 1202 return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
1203
1204 def _get_eval_ops(self, features, labels, metrics):
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in _call_model_fn(self, features, labels, mode, metrics)
1164 if 'model_dir' in model_fn_args:
1165 kwargs['model_dir'] = self.model_dir
-> 1166 model_fn_results = self._model_fn(features, labels, **kwargs)
1167
1168 if isinstance(model_fn_results, model_fn_lib.ModelFnOps):
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dynamic_rnn_estimator.pyc in _dynamic_rnn_model_fn(features, labels, mode)
478 sequence_input = build_sequence_input(features,
479 sequence_feature_columns,
--> 480 context_feature_columns)
481 dropout = (dropout_keep_probabilities
482 if mode == model_fn.ModeKeys.TRAIN
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dynamic_rnn_estimator.pyc in build_sequence_input(features, sequence_feature_columns, context_feature_columns, weight_collections, scope)
190 features.update(layers.transform_features(
191 features,
--> 192 list(sequence_feature_columns) + list(context_feature_columns or [])))
193 sequence_input = layers.sequence_input_from_feature_columns(
194 columns_to_tensors=features,
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in transform_features(features, feature_columns)
642 """
643 columns_to_tensor = features.copy()
--> 644 check_feature_columns(feature_columns)
645 transformer = _Transformer(columns_to_tensor)
646 for column in sorted(set(feature_columns), key=lambda x: x.key):
/home/judge/anaconda3/envs/ipykernel_py2/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in check_feature_columns(feature_columns)
765 seen_keys = set()
766 for f in feature_columns:
--> 767 key = f.key
768 if key in seen_keys:
769 raise ValueError('Duplicate feature column key found for column: {}. '
AttributeError: 'list' object has no attribute 'key'
Looking at the trace, it concatenates the sequence_feature_columns and context_feature_columns. It starts looking at the result but doesn't find a key. I have printed out the feature_names and they have keys.
It looks like you've wrapped feature_columns in a list a second time (giving [[...]] rather than [...]):
sequence_feature_columns = [feature_columns],