Gary-McC / TraderbotFinal

Mozilla Public License 2.0
3 stars 2 forks source link

incompatible input shape #1

Open icarus3 opened 3 years ago

icarus3 commented 3 years ago
I am getting following WARNING and exception at line tomorrow_pred=model.predict(today_pred) in 24-hour daily predictor all data.py
The input is expected to be 7 rows (ticks) with 5 values in every row.  For example model.predict(X_test) looks like a correct call. However today_pred is just and array

(Note, line numbers from following stack trace may not match the code that is checked in. I have couple of print statement added in my copy)

WARNING:tensorflow:Model was constructed with shape (None, 7, 5) for input KerasTensor(type_spec=TensorSpec(shape=(None, 7, 5), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None, 1).

`
    Traceback (most recent call last):
      File "C:\Users\Naktya\Documents\code\quant\TraderbotFinal\Predictive Model\24-hour daily predictor all data.py", line 548, in <module>
        tomorrow_pred=model.predict(today_pred)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py", line 1751, in predict
        tmp_batch_outputs = self.predict_function(iterator)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 885, in __call__
        result = self._call(*args, **kwds)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 924, in _call
        results = self._stateful_fn(*args, **kwds)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3038, in __call__
        filtered_flat_args) = self._maybe_define_function(args, kwargs)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3459, in _maybe_define_function
        return self._define_function_with_shape_relaxation(
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3381, in _define_function_with_shape_relaxation
        graph_function = self._create_graph_function(
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3298, in _create_graph_function
        func_graph_module.func_graph_from_py_func(
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1007, in func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 668, in wrapped_fn
        out = weak_wrapped_fn().__wrapped__(*args, **kwds)
      File "C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py", line 994, in wrapper
        raise e.ag_error_metadata.to_exception(e)
    ValueError: in user code:

    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:1586 predict_function  *
        return step_function(self, iterator)
    C:\Users\Naktya\Documents\code\quant\TraderbotFinal\Predictive Model\24-hour daily predictor all data.py:153 call  *
        x = tf.math.reduce_mean(x[:,:,:4], axis=-1)
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper
        return target(*args, **kwargs)
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\ops\array_ops.py:1041 _slice_helper
        return strided_slice(
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper
        return target(*args, **kwargs)
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\ops\array_ops.py:1214 strided_slice
        op = gen_array_ops.strided_slice(
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\ops\gen_array_ops.py:10537 strided_slice
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\op_def_library.py:748 _apply_op_helper
        op = g._create_op_internal(op_type_name, inputs, dtypes=None,
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py:599 _create_op_internal
        return super(FuncGraph, self)._create_op_internal(  # pylint: disable=protected-access
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py:3561 _create_op_internal
        ret = Operation(
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py:2041 __init__
        self._c_op = _create_c_op(self._graph, node_def, inputs,
    C:\Users\Naktya\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py:1883 _create_c_op
        raise ValueError(str(e))

    ValueError: Index out of range using input dim 2; input has only 2 dims for '{{node model/time2_vector_1/strided_slice}} = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=7, ellipsis_mask=0, end_mask=3, new_axis_mask=0, shrink_axis_mask=0](ExpandDims, model/time2_vector_1/strided_slice/stack, model/time2_vector_1/strided_slice/stack_1, model/time2_vector_1/strided_slice/stack_2)' with input shapes: [?,1], [3], [3], [3] and with computed input tensors: input[3] = <1 1 1>.

`
lucasa commented 3 years ago

Hi! Any solution?

Gary-McC commented 3 years ago

Sorry for not getting back to you earlier, I've been pre-occupied with a few other things. The error comes about because it expects a list of data points from the previous days and tomorrow_pred is only a value of a single day. It was completely my mistake in writing the code; I added that in there at the last second to show people that you need to make two prediction commands to get tomorrow's values, but never really tested it because I use a slightly different method to get them with the predictor in the actual agent.

To get the proper shape, it should be tomorrow_pred=model.predict(test_data[-seq_len:])

I just want to emphasize that you don't actually need the final few lines of code regarding tomorrow_pred to get your stuff up and running. The final server-deployable bot uses a much more straightforward method since it doesn't need to split the data up.