代码之家  ›  专栏  ›  技术社区  ›  Jay Bhie Santos

剩余GRU:ValueError:输入具有不兼容的形状。收到的形状(5)和(24)

  •  0
  • Jay Bhie Santos  · 技术社区  · 2 年前

    我在尝试多元输入时遇到了一个值错误。 这是我得到的错误:

    ValueError: Inputs have incompatible shapes. Received shapes (5,) and (24,)
    

    这是我输入的形状:

    X shape (2100, 84, 5)
    y shape (2100, 24)
    

    这是我的模型摘要:

    Layer (type)                   Output Shape         Param #     Connected to                     
    ==================================================================================================
     input_2 (InputLayer)           [(None, 80, 5)]      0           []                               
                                                                                                      
     lstm_1 (LSTM)                  (None, 80, 24)       2880        ['input_2[0][0]']                
                                                                                                      
     lstm_2 (LSTM)                  (None, 80, 24)       4704        ['lstm_1[0][0]']                 
                                                                                                      
     add (Add)                      (None, 80, 24)       0           ['lstm_1[0][0]',                 
                                                                      'lstm_2[0][0]']                 
                                                                                                      
     lambda (Lambda)                (None, 24)           0           ['add[0][0]']                    
                                                                                                      
     lstm_3 (LSTM)                  (None, 24)           4704        ['add[0][0]']                    
                                                                                                      
     add_1 (Add)                    (None, 24)           0           ['lambda[0][0]',                 
                                                                      'lstm_3[0][0]']                 
                                                                                                      
    ==================================================================================================
    Total params: 12,288
    Trainable params: 12,288
    Non-trainable params: 0
    

    这是我的模型代码:

    def make_residual_lstm_layers(input, rnn_width, rnn_depth, rnn_dropout):
        
        x = input
        for i in range(rnn_depth):
            return_sequences = i < rnn_depth - 1
            x_rnn = LSTM(rnn_width, recurrent_dropout=rnn_dropout, dropout=rnn_dropout, return_sequences=return_sequences)(x)
            if return_sequences:
               
                if i > 0 or input.shape[-1] == rnn_width:
                    x = add([x, x_rnn])
                else:
                   
                    x = x_rnn
            else:
               
                def slice_last(x):
                    return x[..., -1, :]
                x = add([Lambda(slice_last)(x), x_rnn])
        return x
    

    我是这样定义输入的:

    input = Input(shape=(X_train.shape[1], X_train.shape[2]))
    output = make_residual_lstm_layers(input, rnn_width=24, rnn_depth=d, rnn_dropout=e)
    model = Model(inputs=input, outputs=output)
      
    

    这是我收到的全部错误:

    ValueError                                Traceback (most recent call last)
    <ipython-input-16-973dead00adb> in <module>
          1 hyper = Hyperactive()
          2 hyper.add_search(rlstm, search_space, optimizer=optimizer, n_iter=100)
    ----> 3 hyper.run()
    
    ~\anaconda3\lib\site-packages\hyperactive\hyperactive.py in run(self, max_time)
        136             opt.max_time = max_time
        137 
    --> 138         self.results_list = run_search(
        139             self.opt_pros, self.distribution, self.n_processes
        140         )
    
    ~\anaconda3\lib\site-packages\hyperactive\run_search.py in run_search(opt_pros, distribution, n_processes)
         49 
         50     if n_processes == 1:
    ---> 51         results_list = single_process(_process_, process_infos)
         52     else:
         53         (distribution, process_func), dist_paras = _get_distribution(distribution)
    
    ~\anaconda3\lib\site-packages\hyperactive\distribution.py in single_process(process_func, process_infos)
          9 
         10 def single_process(process_func, process_infos):
    ---> 11     results = [process_func(*info) for info in process_infos]
         12 
         13     return results
    
    ~\anaconda3\lib\site-packages\hyperactive\distribution.py in <listcomp>(.0)
          9 
         10 def single_process(process_func, process_infos):
    ---> 11     results = [process_func(*info) for info in process_infos]
         12 
         13     return results
    
    ~\anaconda3\lib\site-packages\hyperactive\process.py in _process_(nth_process, optimizer)
          5 
          6 def _process_(nth_process, optimizer):
    ----> 7     optimizer.search(nth_process)
          8 
          9     return {
    
    ~\anaconda3\lib\site-packages\hyperactive\optimizers\gfo_wrapper.py in search(self, nth_process)
        137         gfo_objective_function = gfo_wrapper_model(self.s_space())
        138 
    --> 139         self._optimizer.search(
        140             objective_function=gfo_objective_function,
        141             n_iter=self.n_iter,
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\search.py in search(self, objective_function, n_iter, max_time, max_score, early_stopping, memory, memory_warm_start, verbosity)
        133             if self.stop.check(self.start_time, self.p_bar.score_best, self.score_l):
        134                 break
    --> 135             self._initialization(init_pos, nth_iter)
        136 
        137         self.finish_initialization()
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\times_tracker.py in wrapper(self, *args, **kwargs)
         25         def wrapper(self, *args, **kwargs):
         26             t = time.time()
    ---> 27             res = func(self, *args, **kwargs)
         28             self.iter_times.append(time.time() - t)
         29             return res
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\search.py in _initialization(self, init_pos, nth_iter)
         55         self.init_pos(init_pos)
         56 
    ---> 57         score_new = self._score(init_pos)
         58         self.evaluate(score_new)
         59 
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\times_tracker.py in wrapper(self, *args, **kwargs)
         16         def wrapper(self, *args, **kwargs):
         17             t = time.time()
    ---> 18             res = func(self, *args, **kwargs)
         19             self.eval_times.append(time.time() - t)
         20             return res
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\search.py in _score(self, pos)
         46     @TimesTracker.eval_time
         47     def _score(self, pos):
    ---> 48         return self.score(pos)
         49 
         50     @TimesTracker.iter_time
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\results_manager.py in _wrapper(pos)
         29             value = self.conv.position2value(pos)
         30             para = self.conv.value2para(value)
    ---> 31             results_dict = self._obj_func_results(objective_function, para)
         32 
         33             self.results_list.append({**results_dict, **para})
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\results_manager.py in _obj_func_results(self, objective_function, para)
         12 
         13     def _obj_func_results(self, objective_function, para):
    ---> 14         results = objective_function(para)
         15 
         16         if isinstance(results, tuple):
    
    ~\anaconda3\lib\site-packages\gradient_free_optimizers\memory.py in wrapper(para)
         43                 return self.memory_dict[pos_tuple]
         44             else:
    ---> 45                 score = objective_function(para)
         46 
         47                 self.memory_dict[pos_tuple] = score
    
    ~\anaconda3\lib\site-packages\hyperactive\optimizers\objective_function.py in _model(para)
         35             para = gfo2hyper(search_space, para)
         36             self.para_dict = para
    ---> 37             results = self.objective_function(self)
         38 
         39             return results
    
    <ipython-input-13-5b4490c109de> in rlstm(opt)
         50     print("q shape",q.shape)
         51     input = Input(shape=(X_train.shape[1], X_train.shape[2]))
    ---> 52     output = make_residual_lstm_layers(input, rnn_width=24, rnn_depth=d, rnn_dropout=e)
         53     model = Model(inputs=input, outputs=output)
         54     model.summary()
    
    <ipython-input-4-8c85b2bfe824> in make_residual_lstm_layers(input, rnn_width, rnn_depth, rnn_dropout)
         22             def slice_last(x):
         23                 return x[..., -1, :]
    ---> 24             x = add([Lambda(slice_last)(x), x_rnn])
         25     return x
    
    ~\anaconda3\lib\site-packages\keras\layers\merge.py in add(inputs, **kwargs)
        786 
        787   """
    --> 788   return Add(**kwargs)(inputs)
        789 
        790 
    
    ~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
         65     except Exception as e:  # pylint: disable=broad-except
         66       filtered_tb = _process_traceback_frames(e.__traceback__)
    ---> 67       raise e.with_traceback(filtered_tb) from None
         68     finally:
         69       del filtered_tb
    
    ~\anaconda3\lib\site-packages\keras\layers\merge.py in _compute_elemwise_op_output_shape(self, shape1, shape2)
         76       else:
         77         if i != j:
    ---> 78           raise ValueError(
         79               'Inputs have incompatible shapes. '
         80               f'Received shapes {shape1} and {shape2}')
    
    ValueError: Inputs have incompatible shapes. Received shapes (5,) and (24,)
    

    ​

    0 回复  |  直到 2 年前