C5 - Week 1 - Jazz with LSTM

I have been trying for a long time but couldn’t figure out what’s the argument for one_hot in the music_inference_model function :

    # Step 2.D: 
    # Select the next value according to "out",
    # Set "x" to be the one-hot representation of the selected value
    # See instructions above.
    x = tf.math.argmax(out,axis=1)
    x = Lambda(tf.one_hot)(out)

    # Step 3: Create model instance with the correct "inputs" and "outputs" (≈1 line)
    inference_model = Model(inputs=[x0, a0, c0], outputs=outputs)

The error message is :

   ValueError: Shapes must be equal rank, but are 2 and 0
   From merging shape 0 with other shapes. for '{{node Pack_2}} = Pack[N=2, T=DT_FLOAT, axis=0](dense/Softmax_82, Pack_2/values_1)' with input shapes: [?,90], [].
  1. In the description its told that, we need to get the maximum value of out along the last axis. You can use -1 to select the last axis.
  2. In the previous step, you’ve calculated the index of the note with max probability (its stored in x). Thus all you have to do now is to create a vector of size (depth) n_values with all 0's except the one at x. You can easily do that by calling tf.one_hot and passing those (index, size of the vector) as arguments to that function.

I tried it but I’m surely missing something :

    x = tf.math.argmax(out,axis=-1) #index of the note with max probability
    x = tf.one_hot(x,n_values)

Did you include Step 2.E?

Yes I have :

    x = RepeatVector(1)(x)

Can you paste the error message?

Here it is : (I have the same as before)

ValueError: Shapes must be equal rank, but are 2 and 0
From merging shape 0 with other shapes. for '{{node Pack_15}} = Pack[N=2, T=DT_FLOAT, axis=0](dense_1/Softmax_40, Pack_15/values_1)' with input shapes: [?,90], [].

sorry, could you post the entire Traceback?


InvalidArgumentError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1811 try:
→ 1812 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1813 except errors.InvalidArgumentError as e:

InvalidArgumentError: Shapes must be equal rank, but are 2 and 0
From merging shape 0 with other shapes. for ‘{{node Pack_16}} = Pack[N=2, T=DT_FLOAT, axis=0](dense_1/Softmax_41, Pack_16/values_1)’ with input shapes: [?,90], .

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in
----> 1 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

in music_inference_model(LSTM_cell, densor, Ty)
49 # Set “x” to be the one-hot representation of the selected value
50 # See instructions above.
—> 51 x = tf.math.argmax(out,axis=-1) #index of the note with max probability
52 x = tf.one_hot(x,n_values)
53 x = RepeatVector(1)(x)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”""
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in argmax_v2(input, axis, output_type, name)
293 if axis is None:
294 axis = 0
→ 295 return gen_math_ops.arg_max(input, axis, name=name, output_type=output_type)
296
297

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in arg_max(input, dimension, output_type, name)
856 try:
857 return arg_max_eager_fallback(
→ 858 input, dimension, output_type=output_type, name=name, ctx=_ctx)
859 except _core._SymbolicException:
860 pass # Add nodes to the TensorFlow graph.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in arg_max_eager_fallback(input, dimension, output_type, name, ctx)
884 output_type = _dtypes.int64
885 output_type = _execute.make_type(output_type, “output_type”)
→ 886 _attr_T, (input,) = _execute.args_to_matching_eager([input], ctx)
887 _attr_Tidx, (dimension,) = _execute.args_to_matching_eager([dimension], ctx, _dtypes.int32)
888 _inputs_flat = [input, dimension]

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in args_to_matching_eager(l, ctx, default_dtype)
261 ret.append(
262 ops.convert_to_tensor(
→ 263 t, dtype, preferred_dtype=default_dtype, ctx=ctx))
264 if dtype is None:
265 dtype = ret[-1].dtype

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1497
1498 if ret is None:
→ 1499 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500
1501 if ret is NotImplemented:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
1500 elif dtype != inferred_dtype:
1501 v = nest.map_structure(_cast_nested_seqs_to_dtype(dtype), v)
→ 1502 return _autopacking_helper(v, dtype, name or “packed”)
1503
1504

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
1436 elems_as_tensors.append(
1437 constant_op.constant(elem, dtype=dtype, name=str(i)))
→ 1438 return gen_array_ops.pack(elems_as_tensors, name=scope)
1439 else:
1440 return converted_elems

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in pack(values, axis, name)
6475 axis = _execute.make_int(axis, “axis”)
6476 _, _, _op, _outputs = _op_def_library._apply_op_helper(
→ 6477 “Pack”, values=values, axis=axis, name=name)
6478 _result = _outputs[:]
6479 if _execute.must_record_gradient():

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
742 op = g._create_op_internal(op_type_name, inputs, dtypes=None,
743 name=scope, input_types=input_types,
→ 744 attrs=attr_protos, op_def=op_def)
745
746 # outputs is returned as a separate return value so that the output

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
591 return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
592 op_type, inputs, dtypes, input_types, name, attrs, op_def,
→ 593 compute_device)
594
595 def capture(self, tensor, name=None, shape=None):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
3483 input_types=input_types,
3484 original_op=self._default_original_op,
→ 3485 op_def=op_def)
3486 self._create_op_helper(ret, compute_device=compute_device)
3487 return ret

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in init(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1973 op_def = self._graph._get_op_def(node_def.op)
1974 self._c_op = _create_c_op(self._graph, node_def, inputs,
→ 1975 control_input_ops, op_def)
1976 name = compat.as_str(node_def.name)
1977 # pylint: enable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1813 except errors.InvalidArgumentError as e:
1814 # Convert to ValueError for backwards compatibility.
→ 1815 raise ValueError(str(e))
1816
1817 return c_op

ValueError: Shapes must be equal rank, but are 2 and 0
From merging shape 0 with other shapes. for ‘{{node Pack_16}} = Pack[N=2, T=DT_FLOAT, axis=0](dense_1/Softmax_41, Pack_16/values_1)’ with input shapes: [?,90], .

I can’t seem to figure it out but the shape of x is (None,) for me.

Thanks anyways, I’ll keep on debugging

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Hey @marcus-waldman please refrain from giving the answers away, as it is against the honour code. If it is absolutely necessary to share code, when it sometimes can be, after your query is answered please delete them. Thank you.

My bad. Just deleted. Will be more careful going forward.

1 Like

I am stuck with the same problem. Did you find the solution?