Keras inheriting built in layers? - python

I am new to Keras.
Keras's docs show how to make a custom layer where you can have full control over the trainable weights.
My question is how can one just extend an existing layer?
For example, the BatchNormalization layer does not have an activation option, where in practice one may often add an activation function following batch normalization.
This attempt does not work:
class BatchNormalizationActivation(keras.layers.BatchNormalization):
def __init__(self, bn_params={}, activation=keras.activations.relu, act_params={}):
super(BatchNormalizationActivation, self).__init__(**bn_params)
self.act = activation
def call(x):
x = super(BatchNormalizationActivation, self).call(x)
return self.act(x, **act_params)
BatchNormalizationActivation()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-182-6e4f0495a112> in <module>()
----> 1 BatchNormalizationActivation()
<ipython-input-181-2d5c8337234a> in __init__(self, bn_params, activation, act_params)
3
4 def __init__(self, bn_params={}, activation=keras.activations.relu, act_params={}):
----> 5 super(BatchNormalizationActivation, self).__init__(**bn_params)
6 self.act = activation
7
/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/layers/normalization.py in __init__(self, axis, momentum, epsilon, center, scale, beta_initializer, gamma_initializer, moving_mean_initializer, moving_variance_initializer, beta_regularizer, gamma_regularizer, beta_constraint, gamma_constraint, **kwargs)
105 beta_constraint=constraints.get(beta_constraint),
106 gamma_constraint=constraints.get(gamma_constraint),
--> 107 **kwargs
108 )
109
/usr/local/lib/python3.6/site-packages/tensorflow/python/layers/normalization.py in __init__(self, axis, momentum, epsilon, center, scale, beta_initializer, gamma_initializer, moving_mean_initializer, moving_variance_initializer, beta_regularizer, gamma_regularizer, beta_constraint, gamma_constraint, renorm, renorm_clipping, renorm_momentum, fused, trainable, virtual_batch_size, adjustment, name, **kwargs)
144 **kwargs):
145 super(BatchNormalization, self).__init__(
--> 146 name=name, trainable=trainable, **kwargs)
147 if isinstance(axis, list):
148 self.axis = axis[:]
/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/base_layer.py in __init__(self, **kwargs)
147 super(Layer, self).__init__(
148 name=name, dtype=dtype, trainable=trainable,
--> 149 activity_regularizer=kwargs.get('activity_regularizer'))
150 self._uses_inputs_arg = True
151
/usr/local/lib/python3.6/site-packages/tensorflow/python/layers/base.py in __init__(self, trainable, name, dtype, activity_regularizer, **kwargs)
130 self._graph = None # Will be set at build time.
131 self._dtype = None if dtype is None else dtypes.as_dtype(dtype).name
--> 132 self._call_fn_args = estimator_util.fn_args(self.call)
133 self._compute_previous_mask = ('mask' in self._call_fn_args or
134 hasattr(self, 'compute_mask'))
/usr/local/lib/python3.6/site-packages/tensorflow/python/estimator/util.py in fn_args(fn)
60 args = tf_inspect.getfullargspec(fn).args
61 if _is_bounded_method(fn):
---> 62 args.remove('self')
63 return tuple(args)
64
ValueError: list.remove(x): x not in list

Related

TypeError when using tf.keras.models.save_model() to save multi-inputs TF 2.x subclass model

Following the training process of this tutorial, I tried to use the following codes to save a tf model:
tf.keras.models.save_model(decoder, 'path', save_format='tf')
but an error message came out:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-5a5dd79f753f> in <module>()
----> 1 tf.keras.models.save_model(decoder, './drive/My Drive/DeepLearning/decoder/kerasencoder', save_format='tf')
20 frames
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
113 else:
114 saved_model_save.save(model, filepath, overwrite, include_optimizer,
--> 115 signatures, options)
116
117
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/save.py in save(model, filepath, overwrite, include_optimizer, signatures, options)
76 # we use the default replica context here.
77 with distribution_strategy_context._get_default_replica_context(): # pylint: disable=protected-access
---> 78 save_lib.save(model, filepath, signatures, options)
79
80 if not include_optimizer:
/tensorflow-2.1.0/python3.6/tensorflow_core/python/saved_model/save.py in save(obj, export_dir, signatures, options)
884 if signatures is None:
885 signatures = signature_serialization.find_function_to_export(
--> 886 checkpoint_graph_view)
887
888 signatures = signature_serialization.canonicalize_signatures(signatures)
/tensorflow-2.1.0/python3.6/tensorflow_core/python/saved_model/signature_serialization.py in find_function_to_export(saveable_view)
72 # If the user did not specify signatures, check the root object for a function
73 # that can be made into a signature.
---> 74 functions = saveable_view.list_functions(saveable_view.root)
75 signature = functions.get(DEFAULT_SIGNATURE_ATTR, None)
76 if signature is not None:
/tensorflow-2.1.0/python3.6/tensorflow_core/python/saved_model/save.py in list_functions(self, obj)
140 if obj_functions is None:
141 obj_functions = obj._list_functions_for_serialization( # pylint: disable=protected-access
--> 142 self._serialization_cache)
143 self._functions[obj] = obj_functions
144 return obj_functions
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/base_layer.py in _list_functions_for_serialization(self, serialization_cache)
2418 def _list_functions_for_serialization(self, serialization_cache):
2419 return (self._trackable_saved_model_saver
-> 2420 .list_functions_for_serialization(serialization_cache))
2421
2422 def __getstate__(self):
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/base_serialization.py in list_functions_for_serialization(self, serialization_cache)
89 `ConcreteFunction`.
90 """
---> 91 fns = self.functions_to_serialize(serialization_cache)
92
93 # The parent AutoTrackable class saves all user-defined tf.functions, and
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in functions_to_serialize(self, serialization_cache)
78 def functions_to_serialize(self, serialization_cache):
79 return (self._get_serialized_attributes(
---> 80 serialization_cache).functions_to_serialize)
81
82 def _get_serialized_attributes(self, serialization_cache):
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes(self, serialization_cache)
93
94 object_dict, function_dict = self._get_serialized_attributes_internal(
---> 95 serialization_cache)
96
97 serialized_attr.set_and_validate_objects(object_dict)
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/model_serialization.py in _get_serialized_attributes_internal(self, serialization_cache)
45 # cache (i.e. this is the root level object).
46 if len(serialization_cache[constants.KERAS_CACHE_KEY]) == 1:
---> 47 default_signature = save_impl.default_save_signature(self.obj)
48
49 # Other than the default signature function, all other attributes match with
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/save_impl.py in default_save_signature(layer)
210 original_losses = _reset_layer_losses(layer)
211 fn = saving_utils.trace_model_call(layer)
--> 212 fn.get_concrete_function()
213 _restore_layer_losses(original_losses)
214 return fn
/tensorflow-2.1.0/python3.6/tensorflow_core/python/eager/def_function.py in get_concrete_function(self, *args, **kwargs)
907 if self._stateful_fn is None:
908 initializers = []
--> 909 self._initialize(args, kwargs, add_initializers_to=initializers)
910 self._initialize_uninitialized_variables(initializers)
911
/tensorflow-2.1.0/python3.6/tensorflow_core/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
495 self._concrete_stateful_fn = (
496 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 497 *args, **kwds))
498
499 def invalid_creator_scope(*unused_args, **unused_kwds):
/tensorflow-2.1.0/python3.6/tensorflow_core/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2387 args, kwargs = None, None
2388 with self._lock:
-> 2389 graph_function, _, _ = self._maybe_define_function(args, kwargs)
2390 return graph_function
2391
/tensorflow-2.1.0/python3.6/tensorflow_core/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2701
2702 self._function_cache.missed.add(call_context_key)
-> 2703 graph_function = self._create_graph_function(args, kwargs)
2704 self._function_cache.primary[cache_key] = graph_function
2705 return graph_function, args, kwargs
/tensorflow-2.1.0/python3.6/tensorflow_core/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2591 arg_names=arg_names,
2592 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2593 capture_by_value=self._capture_by_value),
2594 self._function_attributes,
2595 # Tell the ConcreteFunction to clean up its graph once it goes out of
/tensorflow-2.1.0/python3.6/tensorflow_core/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
976 converted_func)
977
--> 978 func_outputs = python_func(*func_args, **func_kwargs)
979
980 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
/tensorflow-2.1.0/python3.6/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args, **kwds)
437 # __wrapped__ allows AutoGraph to swap in a converted function. We give
438 # the function a weak reference to itself to avoid a reference cycle.
--> 439 return weak_wrapped_fn().__wrapped__(*args, **kwds)
440 weak_wrapped_fn = weakref.ref(wrapped_fn)
441
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saving_utils.py in _wrapped_model(*args)
148 with base_layer_utils.call_context().enter(
149 model, inputs=inputs, build_graph=False, training=False, saving=True):
--> 150 outputs_list = nest.flatten(model(inputs=inputs, training=False))
151
152 try:
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
776 outputs = base_layer_utils.mark_as_return(outputs, acd)
777 else:
--> 778 outputs = call_fn(cast_inputs, *args, **kwargs)
779
780 except errors.OperatorNotAllowedInGraphError as e:
/tensorflow-2.1.0/python3.6/tensorflow_core/python/autograph/impl/api.py in wrapper(*args, **kwargs)
290 def wrapper(*args, **kwargs):
291 with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.DISABLED):
--> 292 return func(*args, **kwargs)
293
294 if inspect.isfunction(func) or inspect.ismethod(func):
TypeError: call() missing 2 required positional arguments: 'hidden' and 'enc_output'
In the link, the decoder is defined as:
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz):
super(Decoder, self).__init__()
self.batch_sz = batch_sz
self.dec_units = dec_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.dec_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
self.fc = tf.keras.layers.Dense(vocab_size)
# used for attention
self.attention = BahdanauAttention(self.dec_units)
def call(self, x, hidden, enc_output):
# enc_output shape == (batch_size, max_length, hidden_size)
context_vector, attention_weights = self.attention(hidden, enc_output)
# x shape after passing through embedding == (batch_size, 1, embedding_dim)
x = self.embedding(x)
# x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)
x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)
# passing the concatenated vector to the GRU
output, state = self.gru(x)
# output shape == (batch_size * 1, hidden_size)
output = tf.reshape(output, (-1, output.shape[2]))
# output shape == (batch_size, vocab)
x = self.fc(output)
return x, state, attention_weights
How to solve this error?
The error might occur due to serialization issues.
There are a couple of ways to save Tensorflow Model. In the tutorial, you provided they use tf.Checkpoint, mostly because subclassed tf.keras.Model cannot be safely serialized via tf.keras.model.save_model or model.save.
Take a look at the docs (especially, custom object section) and this issue.
When using subclassing API it is much safer to save only model weights via model.save_weights.
Hope this helps.
You could save tf.keras.Model in the following way:
import tensorflow as tf
batch_size = 2
dim = 10
class Decoder(tf.keras.Model):
def __init__(self, fc_dim=dim * 2):
super(Decoder, self).__init__()
self.fc = tf.keras.layers.Dense(fc_dim)
signature_dict = {
# the first dimension can change, so we set it to None
't1': tf.TensorSpec(shape=(None, dim), dtype=tf.float32),
't2': tf.TensorSpec(shape=(None, dim), dtype=tf.float32),
}
#tf.function(input_signature=[signature_dict])
def call(self, input_dict):
# model which accept several arguments
t1 = input_dict['t1']
t2 = input_dict['t2']
con = tf.concat((t1, t2), axis=1)
res = self.fc(con)
return con, res
t1_ = tf.ones(shape=(batch_size, dim), dtype=tf.float32)
t2_ = tf.ones(shape=(batch_size, dim), dtype=tf.float32)
d_model = Decoder()
con_, res_ = d_model({'t1': t1_, 't2': t2_})
path = '/tmp/d_model'
tf.keras.models.save_model(d_model, path)
# this method of a model loading requires only path to weights
# and does not require model class instance
loaded_model = tf.keras.models.load_model(path)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple' when starting the training of my model

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.

Argument must be a dense tensor: got shape [2] but wanted [2, 563]

I was trying to train a language model with Keras on a corpus of 35 tweets.
I get the error mentioned in the title, with the following trace back:
ValueError Traceback (most recent call last)
<ipython-input-101-5ed366712809> in <module>()
----> 1 create_model(X, Y, max_len, total_words)
<ipython-input-100-798dd17a8b2b> in create_model(predictors, label, max_sequence_len, total_words)
3
4 model = Sequential()
----> 5 model.add(Embedding(total_words, 10))
6 model.add(LSTM(150))
7 model.add(Dropout(0.1))
/usr/local/lib/python3.6/dist-packages/keras/models.py in add(self, layer)
495 # and create the node connecting the current layer
496 # to the input layer we just created.
--> 497 layer(x)
498
499 if len(layer._inbound_nodes[-1].output_tensors) != 1:
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
590 '`layer.build(batch_input_shape)`')
591 if len(input_shapes) == 1:
--> 592 self.build(input_shapes[0])
593 else:
594 self.build(input_shapes)
/usr/local/lib/python3.6/dist-packages/keras/layers/embeddings.py in build(self, input_shape)
103 regularizer=self.embeddings_regularizer,
104 constraint=self.embeddings_constraint,
--> 105 dtype=self.dtype)
106 self.built = True
107
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
411 if dtype is None:
412 dtype = K.floatx()
--> 413 weight = K.variable(initializer(shape),
414 dtype=dtype,
415 name=name,
/usr/local/lib/python3.6/dist-packages/keras/initializers.py in __call__(self, shape, dtype)
110 def __call__(self, shape, dtype=None):
111 return K.random_uniform(shape, self.minval, self.maxval,
ā€”-> 112 dtype=dtype, seed=self.seed)
113
114 def get_config(self):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in random_uniform(shape, minval, maxval, dtype, seed)
3836 seed = np.random.randint(10e6)
3837 return tf.random_uniform(shape, minval=minval, maxval=maxval,
-> 3838 dtype=dtype, seed=seed)
3839
3840
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/random_ops.py in random_uniform(shape, minval, maxval, dtype, seed, name)
232 maxval = 1
233 with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name:
--> 234 shape = _ShapeTensor(shape)
235 minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
236 maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/random_ops.py in _ShapeTensor(shape)
41 else:
42 dtype = None
---> 43 return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
44
45
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
1009 name=name,
1010 preferred_dtype= preferred_dtype,
-> 1011 as_ref=False)
1012
1013
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1105
1106 if ret is None:
-> 1107 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1108
1109 if ret is NotImplemented:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
215 as_ref=False):
216 _ = as_ref
--> 217 return constant(v, dtype=dtype, name=name)
218
219
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
194 tensor_value.tensor.CopyFrom(
195 tensor_util.make_tensor_proto(
ā€”> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
198 const_tensor = g.create_op(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
443 """ - got shape %s, but wanted %s.""" %
444 (values, list(nparray.shape),
--> 445 _GetDenseDimensions(values)))
446
447 # python/numpy default float type is float64. We prefer float32 instead.
Here is all my code:
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, LSTM, Dense, Dropout
from keras.preprocessing.text import Tokenizer
from keras.callbacks import EarlyStopping
from keras.models import Sequential
import keras.utils as ku
import numpy as np
def dataset_prep(data):
corpus = data
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len,
padding='pre'))
predictors, next_word = input_sequences[:,:-1], input_sequences[:,-1]
label = ku.to_categorical(next_word, num_classes=total_words)
return input_sequences[:25], input_sequences[25:], max_sequence_len, list(input_sequences)
def create_model(predictors, label, max_sequence_len, total_words):
input_len = max_sequence_len
model = Sequential()
model.add(Embedding(total_words, 10))
model.add(LSTM(150))
model.add(Dropout(0.1))
model.add(Dense(total_words, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
history = model.fit(predictors, label, epochs=100, verbose=1)
print(history.history['loss'], history.history['val_loss'])
X, Y, max_len, total_words = dataset_prep(tweet_text)
create_model(X, Y, max_len, total_words)
Iā€™m using Google Colaboratory, with Keras v2.1.6 and TensorFlow 1.9.0
The code is all Python 3 with a Tesla K80 GPU for runtime compilation.

ValueError when running MaskedAutoregressiveFlow example

I am trying to run the example for MaskedAutoregressiveFlow at https://www.tensorflow.org/api_docs/python/tf/contrib/distributions/bijectors/MaskedAutoregressiveFlow. It's a plain copy from the docs but I receive the following error. I've tried event_shape=[dims, 1] but that doesn't seem to help (different error). I'm not sure what to make of it.
Has anyone seen this as well?
import tensorflow as tf
import tensorflow.contrib.distributions as tfd
from tensorflow.contrib.distributions import bijectors as tfb
dims = 5
# A common choice for a normalizing flow is to use a Gaussian for the base
# distribution. (However, any continuous distribution would work.) E.g.,
maf = tfd.TransformedDistribution(
distribution=tfd.Normal(loc=0., scale=1.),
bijector=tfb.MaskedAutoregressiveFlow(
shift_and_log_scale_fn=tfb.masked_autoregressive_default_template(
hidden_layers=[512, 512])),
event_shape=[dims])
x = maf.sample() # Expensive; uses `tf.while_loop`, no Bijector caching.
maf.log_prob(x) # Almost free; uses Bijector caching.
maf.log_prob(0.) # Cheap; no `tf.while_loop` despite no Bijector caching.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-3b2fcb2af309> in <module>()
11
12
---> 13 x = maf.sample() # Expensive; uses `tf.while_loop`, no Bijector caching.
14 maf.log_prob(x) # Almost free; uses Bijector caching.
15 maf.log_prob(0.) # Cheap; no `tf.while_loop` despite no Bijector caching.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/distributions/distribution.py in sample(self, sample_shape, seed, name)
687 samples: a `Tensor` with prepended dimensions `sample_shape`.
688 """
--> 689 return self._call_sample_n(sample_shape, seed, name)
690
691 def _log_prob(self, value):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/distributions/transformed_distribution.py in _call_sample_n(self, sample_shape, seed, name, **kwargs)
411 # work, it is imperative that this is the last modification to the
412 # returned result.
--> 413 y = self.bijector.forward(x, **kwargs)
414 y = self._set_sample_static_shape(y, sample_shape)
415
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/distributions/bijector_impl.py in forward(self, x, name)
618 NotImplementedError: if `_forward` is not implemented.
619 """
--> 620 return self._call_forward(x, name)
621
622 def _inverse(self, y):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/distributions/bijector_impl.py in _call_forward(self, x, name, **kwargs)
599 if mapping.y is not None:
600 return mapping.y
--> 601 mapping = mapping.merge(y=self._forward(x, **kwargs))
602 self._cache(mapping)
603 return mapping.y
/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/distributions/python/ops/bijectors/masked_autoregressive.py in _forward(self, x)
245 y0 = array_ops.zeros_like(x, name="y0")
246 # call the template once to ensure creation
--> 247 _ = self._shift_and_log_scale_fn(y0)
248 def _loop_body(index, y0):
249 """While-loop body for autoregression calculation."""
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/template.py in __call__(self, *args, **kwargs)
358 custom_getter=self._custom_getter) as vs:
359 self._variable_scope = vs
--> 360 result = self._call_func(args, kwargs)
361 return result
362
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/template.py in _call_func(self, args, kwargs)
300 trainable_at_start = len(
301 ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES))
--> 302 result = self._func(*args, **kwargs)
303
304 if self._variables_created:
/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/distributions/python/ops/bijectors/masked_autoregressive.py in _fn(x)
478 activation=activation,
479 *args,
--> 480 **kwargs)
481 x = masked_dense(
482 inputs=x,
/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/distributions/python/ops/bijectors/masked_autoregressive.py in masked_dense(inputs, units, num_blocks, exclusive, kernel_initializer, reuse, name, *args, **kwargs)
386 *args,
387 **kwargs)
--> 388 return layer.apply(inputs)
389
390
/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py in apply(self, inputs, *args, **kwargs)
807 Output tensor(s).
808 """
--> 809 return self.__call__(inputs, *args, **kwargs)
810
811 def _add_inbound_node(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py in __call__(self, inputs, *args, **kwargs)
671
672 # Check input assumptions set before layer building, e.g. input rank.
--> 673 self._assert_input_compatibility(inputs)
674 if input_list and self._dtype is None:
675 try:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py in _assert_input_compatibility(self, inputs)
1195 ', found ndim=' + str(ndim) +
1196 '. Full shape received: ' +
-> 1197 str(x.get_shape().as_list()))
1198 # Check dtype.
1199 if spec.dtype is not None:
ValueError: Input 0 of layer dense_1 is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [5]
originally defined at:
File "<ipython-input-2-3b2fcb2af309>", line 9, in <module>
hidden_layers=[512, 512])),
File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/distributions/python/ops/bijectors/masked_autoregressive.py", line 499, in masked_autoregressive_default_template
"masked_autoregressive_default_template", _fn)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/template.py", line 152, in make_template
**kwargs)

statsmodels ValueError

I am having problem for my project, i have extracted data from twitter, saved it as csv and also performed data sentiment analysis but when i am trying to plot them i am unable to.
#statmodels OLS first
y, X = dmatrices('retweet_count_l ~ surge_pricing + free_rides + promo+ driver + food + controversy + regulations', data=training, return_type='dataframe')
# Define the model from above Patsy-created variables, using Statsmodels
print sm.OLS(y,X).fit().summary()
print sm.OLS(y,X).fit().params
print 'r sqd is : ', sm.OLS(y,X).fit().rsquared
rainbow = sm.stats.linear_rainbow(sm.OLS(y,X).fit())
print 'Rainbow Test for Linearity is ', rainbow
y_hat, X_hat = dmatrices('retweet_count_l ~ surge_pricing + free_rides + promo + driver + food + controversy + regulations', data=testing, return_type='dataframe')
y_pred = sm.OLS(y,X).fit().predict(X_hat)
testing['retweet_pred_smols'] = pd.Series(y_pred)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-b5c392dca77b> in <module>()
2 y, X = dmatrices('retweet_count_l ~ surge_pricing + free_rides + promo + driver + food + controversy + regulations', data=training, return_type='dataframe')
3 # Define the model from above Patsy-created variables, using Statsmodels
----> 4 model = sm.OLS(y,X)
5 results = model.fit()
6 print(results.summary())
/usr/lib/python2.7/dist-packages/statsmodels/regression/linear_model.pyc in __init__(self, endog, exog, missing, hasconst)
481 def __init__(self, endog, exog=None, missing='none', hasconst=None):
482 super(OLS, self).__init__(endog, exog, missing=missing,
--> 483 hasconst=hasconst)
484
485 def loglike(self, params):
/usr/lib/python2.7/dist-packages/statsmodels/regression/linear_model.pyc in __init__(self, endog, exog, weights, missing, hasconst)
383 weights = weights.squeeze()
384 super(WLS, self).__init__(endog, exog, missing=missing,
--> 385 weights=weights, hasconst=hasconst)
386 nobs = self.exog.shape[0]
387 weights = self.weights
/usr/lib/python2.7/dist-packages/statsmodels/regression/linear_model.pyc in __init__(self, endog, exog, **kwargs)
77 """
78 def __init__(self, endog, exog, **kwargs):
---> 79 super(RegressionModel, self).__init__(endog, exog, **kwargs)
80 self._data_attr.extend(['pinv_wexog', 'wendog', 'wexog', 'weights'])
81
/usr/lib/python2.7/dist-packages/statsmodels/base/model.pyc in __init__(self, endog, exog, **kwargs)
135 def __init__(self, endog, exog=None, **kwargs):
136 super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
--> 137 self.initialize()
138
139 def initialize(self):
/usr/lib/python2.7/dist-packages/statsmodels/regression/linear_model.pyc in initialize(self)
86 # overwrite nobs from class Model:
87 self.nobs = float(self.wexog.shape[0])
---> 88 self.rank = rank(self.exog)
89 self.df_model = float(self.rank - self.k_constant)
90 self.df_resid = self.nobs - self.rank
/usr/lib/python2.7/dist-packages/statsmodels/tools/tools.pyc in rank(X, cond)
380 if len(X.shape) == 2:
381 D = svdvals(X)
--> 382 return int(np.add.reduce(np.greater(D / D.max(), cond).astype(np.int32)))
383 else:
384 return int(not np.alltrue(np.equal(X, 0.)))
/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.pyc in _amax(a, axis, out, keepdims)
24 # small reductions
25 def _amax(a, axis=None, out=None, keepdims=False):
---> 26 return umr_maximum(a, axis, None, out, keepdims)
27
28 def _amin(a, axis=None, out=None, keepdims=False):
ValueError: zero-size array to reduction operation maximum which has no identity
I can reproduce the error with:
In [224]: np.array([]).max()
...
ValueError: zero-size array to reduction operation maximum which has no identity
In [225]:
So tracing D.max() back ...
sm.OLS(y,X)
X must be be zero-size.
So you need to examine X. What is it's .shape? Try printing it.
I had the same issue. It was because I had some 'nan' values in my columns. After filling them with '0's the problem was solved.

Categories

Resources