I am trying to understand how to use keras for supply chain forecasting and i keep getting errors that i can't find help for elsewhere. I've tried to do similar tutorials; sunspot forecasting tutorial, pollution multivariate tutorial etc but i'm still not understanding how the input_shape argument works or how to organize my data to get it to be accepted by keras.
My dataset is a single time series describing the number of products we sold every month. I took that single time series, 107 months, and turned it into a 30 row, 77 column data set. I created a training set and test set from that.
but no matter what i do i can't get past even just creating a model without some kind of error.
Keras v#: 1.2.0
C:\Users\Ryan.B>python -c "import keras; print(keras.version)"
Using TensorFlow backend.
1.2.0
Python Version: 3.5.4
Here's the code and respective errors i'm getting.
model = Sequential()
model.add(LSTM(units=64, input_shape=(77, 1), output_dim=1))
C:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py in concatenate(tensors, axis)
1219 try:
-> 1220 return tf.concat_v2([to_dense(x) for x in tensors], axis)
1221 except AttributeError:
AttributeError: module 'tensorflow' has no attribute 'concat_v2'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-21-94f09519ff46> in <module>()
1 model = Sequential()
----> 2 model.add(LSTM(input_shape=(77, 1), output_dim = 1))
3 #model.add(Dense(10, activation = 'relu'))
4 #model.add(Dense(1, activation = 'softmax'))
C:\Python35\lib\site-packages\keras\models.py in add(self, layer)
292 else:
293 input_dtype = None
--> 294 layer.create_input_layer(batch_input_shape, input_dtype)
295
296 if len(layer.inbound_nodes) != 1:
C:\Python35\lib\site-packages\keras\engine\topology.py in create_input_layer(self, batch_input_shape, input_dtype, name)
396 # and create the node connecting the current layer
397 # to the input layer we just created.
--> 398 self(x)
399
400 def add_weight(self, shape, initializer, name=None,
C:\Python35\lib\site-packages\keras\engine\topology.py in __call__(self, x, mask)
541 '`layer.build(batch_input_shape)`')
542 if len(input_shapes) == 1:
--> 543 self.build(input_shapes[0])
544 else:
545 self.build(input_shapes)
C:\Python35\lib\site-packages\keras\layers\recurrent.py in build(self, input_shape)
761 self.W_f, self.U_f, self.b_f,
762 self.W_o, self.U_o, self.b_o]
--> 763 self.W = K.concatenate([self.W_i, self.W_f, self.W_c, self.W_o])
764 self.U = K.concatenate([self.U_i, self.U_f, self.U_c, self.U_o])
765 self.b = K.concatenate([self.b_i, self.b_f, self.b_c, self.b_o])
C:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py in concatenate(tensors, axis)
1220 return tf.concat_v2([to_dense(x) for x in tensors], axis)
1221 except AttributeError:
-> 1222 return tf.concat(axis, [to_dense(x) for x in tensors])
1223
1224
C:\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py in concat(values, axis, name)
1041 ops.convert_to_tensor(axis,
1042 name="concat_dim",
-> 1043 dtype=dtypes.int32).get_shape(
1044 ).assert_is_compatible_with(tensor_shape.scalar())
1045 return identity(values[0], name=scope)
C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
674 name=name,
675 preferred_dtype=preferred_dtype,
--> 676 as_ref=False)
677
678
C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
739
740 if ret is None:
--> 741 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
742
743 if ret is NotImplemented:
C:\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
111 as_ref=False):
112 _ = as_ref
--> 113 return constant(v, dtype=dtype, name=name)
114
115
C:\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name, verify_shape)
100 tensor_value = attr_value_pb2.AttrValue()
101 tensor_value.tensor.CopyFrom(
--> 102 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
103 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
104 const_tensor = g.create_op(
C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
372 nparray = np.empty(shape, dtype=np_dt)
373 else:
--> 374 _AssertCompatible(values, dtype)
375 nparray = np.array(values, dtype=np_dt)
376 # check to them.
C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py in _AssertCompatible(values, dtype)
300 else:
301 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302 (dtype.name, repr(mismatch), type(mismatch).__name__))
303
304
TypeError: Expected int32, got "<"tf.Variable 'lstm_3_W_i:0' shape=(1, 1) dtype=float32_ref">" of type 'Variable' instead.
Any help resolving these errors, and understanding about how input_shape and output_dim work would be appreciated!
Eventually I want to start to use things like monthly marketing budget/metrics and sales team metrics as external regressors for multivariate forecasting but one step at a time. Thank you for your time and input!
You should really upgrade to Keras 2; in Keras 1.x, units is not even a valid argument, hence your error:
import keras
from keras.models import Sequential
from keras.layers import LSTM
keras.__version__
# '2.2.4'
Your case still gives an error in Keras 2, albeit a different one:
model = Sequential()
model.add(LSTM(units=64, input_shape=(77, 1), output_dim=1))
[...]
TypeError: For the `units` argument, the layer received both the legacy keyword argument `output_dim` and the Keras 2 keyword argument `units`. Stick to the latter!
Omitting the legacy output_dim argument, as the message advises, we get it to work:
model = Sequential()
model.add(LSTM(units=64, input_shape=(77, 1)))
model.summary()
# result:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 64) 16896
=================================================================
Total params: 16,896
Trainable params: 16,896
Non-trainable params: 0
_________________________________________________________________
So, I seriously suggest you upgrade to Keras 2 (I highly doubt that Keras 1.x works OK with Tensorflow 1.2), and open a new question if you still have issues...
Related
Please I need help, I keep having this error message when I run my codes.
RuntimeError: Attempting to capture an EagerTensor without building a function.
I have a very little knowledge of programming and machine learning. Any help will be very much appreciated.
y=tf.convert_to_tensor(y)#convert pd.dataframe to tensorflow
The issue started when I ran the above code to convert from pandas dataframe to tensorflow since the former was also giving error message.
I can put the entire codes here if required
This is the error message I got
RuntimeError Traceback (most recent call last)
<ipython-input-24-4411ee976a0d> in <module>
8 cost = sess.run(cost_function, feed_dict={x: X_train, y_: y_train})
9 cost_history = np.append(cost_history, cost)
---> 10 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
11 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
12 # print("Accuracy: ", (sess.run(accuracy, feed_dict={x: test_x, y_: test_y})))
~\Anaconda3\lib\site-packages\tensorflow_core\python\util\deprecation.py in new_func(*args, **kwargs)
505 'in a future version' if date is None else ('after %s' % date),
506 instructions)
--> 507 return func(*args, **kwargs)
508
509 doc = _add_deprecated_arg_notice_to_docstring(
~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\math_ops.py in argmax(input, axis, name, dimension, output_type)
139 axis = deprecation.deprecated_argument_lookup("axis", axis, "dimension",
140 dimension)
--> 141 return argmax_v2(input, axis, output_type, name)
142
143
~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\math_ops.py in argmax_v2(input, axis, output_type, name)
185 if axis is None:
186 axis = 0
--> 187 return gen_math_ops.arg_max(input, axis, name=name, output_type=output_type)
188
189
~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py in arg_max(input, dimension, output_type, name)
846 _, _, _op, _outputs = _op_def_library._apply_op_helper(
847 "ArgMax", input=input, dimension=dimension, output_type=output_type,
--> 848 name=name)
849 _result = _outputs[:]
850 if _execute.must_record_gradient():
~\Anaconda3\lib\site-packages\tensorflow_core\python\framework\op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
466 dtype=dtype,
467 as_ref=input_arg.is_ref,
--> 468 preferred_dtype=default_dtype)
469 except TypeError as err:
470 if dtype is None:
~\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1278 graph = get_default_graph()
1279 if not graph.building_function:
-> 1280 raise RuntimeError("Attempting to capture an EagerTensor without "
1281 "building a function.")
1282 return graph.capture(value, name=name)
RuntimeError: Attempting to capture an EagerTensor without building a function.
tf.convert_to_tensor cannot convert Pandas dataframes to Tensors. To load data from a Pandas dataframe into TensorFlow, consult the following guide:
https://www.tensorflow.org/tutorials/load_data/pandas_dataframe
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.
Consider the following "hello world" of neural networks:
(X_train, y_train),(X_test, y_test) = keras.datasets.mnist.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0
net = keras.models.Sequential([
keras.layers.Flatten(),
keras.layers.Dense(512, activation=tf.nn.relu),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
net.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
net.fit(X_train, y_train, epochs=5, verbose=True)
Calling the fit method leads to the exception below.
Versions:
keras: 2.2.4
tensorflow: 1.13.1
I installed keras and tensorflow freshly with Anaconda:
conda install -c conda-forge keras tensorflow
The following warnings seem suspicious, but a solution is not clear:
On module import:
Using TensorFlow backend.
/Users/cls/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.6 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.7
return f(*args, **kwds)
On compile:
WARNING:tensorflow:From /Users/cls/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Somewhere in stdout:
2019-05-14 16:40:10.628107: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Error message:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<timed eval> in <module>
~/anaconda3/lib/python3.7/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)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
675 # to match the value shapes.
676 if not self.inputs:
--> 677 self._set_inputs(x)
678
679 if y is not None:
~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
587 assert len(inputs) == 1
588 inputs = inputs[0]
--> 589 self.build(input_shape=(None,) + inputs.shape[1:])
590 return
591
~/anaconda3/lib/python3.7/site-packages/keras/engine/sequential.py in build(self, input_shape)
219 self.inputs = [x]
220 for layer in self._layers:
--> 221 x = layer(x)
222 self.outputs = [x]
223 self._build_input_shape = input_shape
~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
455 # Actually call the layer,
456 # collecting output(s), mask(s), and shape(s).
--> 457 output = self.call(inputs, **kwargs)
458 output_mask = self.compute_mask(inputs, previous_mask)
459
~/anaconda3/lib/python3.7/site-packages/keras/layers/core.py in call(self, inputs, training)
124 seed=self.seed)
125 return K.in_train_phase(dropped_inputs, inputs,
--> 126 training=training)
127 return inputs
128
~/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in in_train_phase(x, alt, training)
3103 """
3104 if training is None:
-> 3105 training = learning_phase()
3106 uses_learning_phase = True
3107 else:
~/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in learning_phase()
133 phase = tf.placeholder_with_default(False,
134 shape=(),
--> 135 name='keras_learning_phase')
136 _GRAPH_LEARNING_PHASES[graph] = phase
137 return _GRAPH_LEARNING_PHASES[graph]
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in placeholder_with_default(input, shape, name)
2091 A `Tensor`. Has the same type as `input`.
2092 """
-> 2093 return gen_array_ops.placeholder_with_default(input, shape, name)
2094
2095
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in placeholder_with_default(input, shape, name)
5923 shape = _execute.make_shape(shape, "shape")
5924 _, _, _op = _op_def_lib._apply_op_helper(
-> 5925 "PlaceholderWithDefault", input=input, shape=shape, name=name)
5926 _result = _op.outputs[:]
5927 _inputs_flat = _op.inputs
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
509 dtype=dtype,
510 as_ref=input_arg.is_ref,
--> 511 preferred_dtype=default_dtype)
512 except TypeError as err:
513 if dtype is None:
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_symbolic_tensors)
1173
1174 if ret is None:
-> 1175 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1176
1177 if ret is NotImplemented:
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
302 as_ref=False):
303 _ = as_ref
--> 304 return constant(v, dtype=dtype, name=name)
305
306
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
243 """
244 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 245 allow_broadcast=True)
246
247
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
281 tensor_util.make_tensor_proto(
282 value, dtype=dtype, shape=shape, verify_shape=verify_shape,
--> 283 allow_broadcast=allow_broadcast))
284 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
285 const_tensor = g.create_op(
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
571 raise TypeError(
572 "Element type not supported in TensorProto: %s" % numpy_dtype.name)
--> 573 append_fn(tensor_proto, proto_values)
574
575 return tensor_proto
tensorflow/python/framework/fast_tensor_util.pyx in tensorflow.python.framework.fast_tensor_util.AppendBoolArrayToTensorProto()
~/anaconda3/lib/python3.7/site-packages/numpy/lib/type_check.py in asscalar(***failed resolving arguments***)
545 warnings.warn('np.asscalar(a) is deprecated since NumPy v1.16, use '
546 'a.item() instead', DeprecationWarning, stacklevel=1)
--> 547 return a.item()
548
549 #-----------------------------------------------------------------------------
UnboundLocalError: local variable 'a' referenced before assignment
As of Tensorflow 1.14 the official package distribution supports python 3.4 and over. Not sure if there is a sepparate wheel for each version of python, but the good idea would be to install tensorflow the recommended way though pip install.
The command to try to fix the issue:
pip install --upgrade --force-reinstall tensorflow
# pip install --upgrade --force-reinstall tensorflow-gpu
Update: Tensorflow 1.13 does not support python 3.7
This is a duplicate Question that i posted earlier today, in the other question i was using an old version of Keras. I've upgraded to Keras 2.0.0 and still was getting a lot of errors that i can't figure out on my own so i'm reposting the question mostly verbatim.
I am trying to understand how to use keras for supply chain forecasting and i keep getting errors that i can't find help for elsewhere. I've tried to do similar tutorials; sunspot forecasting tutorial, pollution multivariate tutorial etc but i'm still not understanding how the input_shape argument works or how to organize my data to get it to be accepted by keras.
My dataset is a single time series describing the number of products we sold every month. I took that single time series, 107 months, and turned it into a 30 row, 77 column data set. I created a training set and test set from that.
from command prompt:
Successfully uninstalled Keras-1.2.0
Successfully installed keras-2.0.0
Python Version: 3.5.4
Here's the code and respective errors i'm getting.
model = Sequential()
model.add(LSTM(input_shape=(77, 1), output_dim = 10))
Traceback
C:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py in concatenate(tensors, axis)
1219 A tensor.
-> 1220 """
1221 zero = _to_tensor(0., x.dtype.base_dtype)
AttributeError: module 'tensorflow' has no attribute 'concat_v2'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-42-ee393fff874d> in <module>()
1 model = Sequential()
----> 2 model.add(LSTM(input_shape=(77, 1), output_dim = 10))
3 #model.add(Dense(10, activation = 'relu'))
4 #model.add(Dense(1, activation = 'softmax'))
C:\Python35\lib\site-packages\keras\models.py in add(self, layer)
292 '`Sequential.from_config(config)`?')
293 return layer_module.deserialize(config, custom_objects=custom_objects)
--> 294
295
296 def model_from_yaml(yaml_string, custom_objects=None):
C:\Python35\lib\site-packages\keras\engine\topology.py in create_input_layer(self, batch_input_shape, input_dtype, name)
396
397 # Check ndim.
--> 398 if spec.ndim is not None:
399 if K.ndim(x) != spec.ndim:
400 raise ValueError('Input ' + str(input_index) +
C:\Python35\lib\site-packages\keras\engine\topology.py in __call__(self, x, mask)
541 # Handle automatic shape inference (only useful for Theano).
542 input_shape = _collect_input_shape(inputs)
--> 543
544 # Actually call the layer, collecting output(s), mask(s), and shape(s).
545 output = self.call(inputs, **kwargs)
C:\Python35\lib\site-packages\keras\layers\recurrent.py in build(self, input_shape)
761 constants.append(dp_mask)
762 else:
--> 763 constants.append([K.cast_to_floatx(1.) for _ in range(3)])
764
765 if 0 < self.recurrent_dropout < 1:
C:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py in concatenate(tensors, axis)
1220 """
1221 zero = _to_tensor(0., x.dtype.base_dtype)
-> 1222 inf = _to_tensor(np.inf, x.dtype.base_dtype)
1223 x = tf.clip_by_value(x, zero, inf)
1224 return tf.sqrt(x)
C:\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py in concat(values, axis, name)
1041 ops.convert_to_tensor(axis,
1042 name="concat_dim",
-> 1043 dtype=dtypes.int32).get_shape(
1044 ).assert_is_compatible_with(tensor_shape.scalar())
1045 return identity(values[0], name=scope)
C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
674 name=name,
675 preferred_dtype=preferred_dtype,
--> 676 as_ref=False)
677
678
C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
739
740 if ret is None:
--> 741 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
742
743 if ret is NotImplemented:
C:\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
111 as_ref=False):
112 _ = as_ref
--> 113 return constant(v, dtype=dtype, name=name)
114
115
C:\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name, verify_shape)
100 tensor_value = attr_value_pb2.AttrValue()
101 tensor_value.tensor.CopyFrom(
--> 102 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
103 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
104 const_tensor = g.create_op(
C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
372 nparray = np.empty(shape, dtype=np_dt)
373 else:
--> 374 _AssertCompatible(values, dtype)
375 nparray = np.array(values, dtype=np_dt)
376 # check to them.
C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py in _AssertCompatible(values, dtype)
300 else:
301 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302 (dtype.name, repr(mismatch), type(mismatch).__name__))
303
304
TypeError: Expected int32, got <tf.Variable 'lstm_7_W_i:0' shape=(1, 10) dtype=float32_ref> of type 'Variable' instead.
I think that the problem goes around TF version. Version compatibility between Keras and TF is a problem that probably anyone has faced, as TF API changes a lot in a small period of time.
I think that for Keras 2.2.X you need a TF version > 1.10.X
Try updating it and see if the problem is fixed!
I want to create a siamese model, defined lower for colloborative filtration. First one creates users' embeddings, second one creates items' embeddings.
import keras
from keras import backend as K
from keras.layers import Input, Embedding, Dense, Flatten, concatenate
from keras.models import Model
n_users, n_items = 100, 3000
users_input = Input(shape=(n_users,), dtype='int32', name='users')
users_embedding = Embedding(output_dim=6, input_dim=n_users, input_length=1)(users_input)
users_flatten = Flatten()(users_embedding)
items_input = Input(shape=(n_items,), dtype='int32', name='items')
items_embedding = Embedding(output_dim=6, input_dim=n_items, input_length=1)(items_input)
items_flatten = Flatten()(items_embedding)
layer_0 = concatenate([users_flatten, items_flatten])
layer_1 = Dense(8, activation='relu')(layer_0)
layer_2 = Dense(1, activation='relu')(layer_1)
model = Model(inputs=[users_input, items_input], outputs=[layer_2])
As you see, I have problems with concatenation. Here is my stack trace:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-bf475de5f9cc> in <module>()
----> 1 layer_0 = concatenate([users_flatten, items_flatten])
2 layer_1 = Dense(8, activation='relu')(layer_0)
3 layer_2 = Dense(1, activation='relu')(layer_1)
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/layers/merge.pyc in concatenate(inputs, axis, **kwargs)
506 A tensor, the concatenation of the inputs alongside axis `axis`.
507 """
--> 508 return Concatenate(axis=axis, **kwargs)(inputs)
509
510
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
583
584 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 585 output = self.call(inputs, **kwargs)
586 output_mask = self.compute_mask(inputs, previous_mask)
587
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/layers/merge.pyc in call(self, inputs)
281 raise ValueError('A `Concatenate` layer should be called '
282 'on a list of inputs.')
--> 283 return K.concatenate(inputs, axis=self.axis)
284
285 def compute_output_shape(self, input_shape):
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in concatenate(tensors, axis)
1679 return tf.sparse_concat(axis, tensors)
1680 else:
-> 1681 return tf.concat([to_dense(x) for x in tensors], axis)
1682
1683
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in concat(concat_dim, values, name)
998 ops.convert_to_tensor(concat_dim,
999 name="concat_dim",
-> 1000 dtype=dtypes.int32).get_shape(
1001 ).assert_is_compatible_with(tensor_shape.scalar())
1002 return identity(values[0], name=scope)
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
667
668 if ret is None:
--> 669 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
670
671 if ret is NotImplemented:
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
174 as_ref=False):
175 _ = as_ref
--> 176 return constant(v, dtype=dtype, name=name)
177
178
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
163 tensor_value = attr_value_pb2.AttrValue()
164 tensor_value.tensor.CopyFrom(
--> 165 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
166 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
167 const_tensor = g.create_op(
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape)
365 nparray = np.empty(shape, dtype=np_dt)
366 else:
--> 367 _AssertCompatible(values, dtype)
368 nparray = np.array(values, dtype=np_dt)
369 # check to them.
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype)
300 else:
301 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302 (dtype.name, repr(mismatch), type(mismatch).__name__))
303
304
TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
As an example I used Keras documenation for functional API. I use TensorFlow as backend.
Solution: update Keras and Tresorflow to the last versions.