InvalidArgumentError:-1不在0到3之间

时间:2019-09-02 11:46:42

标签: tensorflow keras tf.keras

我已经编译了以下模型。

  Uri u = new Uri("http://localhost:31404/Api/Customers");
        var payload = "{\"CustomerId\": 5,\"CustomerName\": \"Pepsi\"}";

        HttpContent c = new StringContent(payload, Encoding.UTF8, "application/json");
        var t = Task.Run(() => PostURI(u, c));
        t.Wait();

        Console.WriteLine(t.Result);
        Console.ReadLine();

但是,在拟合数据时出现错误。

_________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input1 (InputLayer)             (None, 2, 128)       0                                            
__________________________________________________________________________________________________
input2 (InputLayer)             (None, 2, 128)       0                                            
__________________________________________________________________________________________________
selfattnlayer1 (SelfAttn)       (None, 2, 128)       49152       input1[0][0]                     
__________________________________________________________________________________________________
selfattnlayer2 (SelfAttn)       (None, 2, 128)       49152       input2[0][0]                     
__________________________________________________________________________________________________
lambda_24 (Lambda)              (None, 2, 768)       0           input1[0][0]                     
                                                                 input2[0][0]                     
                                                                 selfattnlayer1[0][0]             
                                                                 selfattnlayer2[0][0]             
__________________________________________________________________________________________________
encoder_lstm (GRU)              (None, 2, 256)       787200      lambda_24[0][0]                  
__________________________________________________________________________________________________
time_distributed_18 (TimeDistri (None, 2, 3)         771         encoder_lstm[0][0]               
==================================================================================================
Total params: 886,275
Trainable params: 886,275
Non-trainable params: 0
__________________________________________________________________________________________________
None
Train on 1350 samples, validate on 150 samples
Epoch 1/10

我要输入的数据

   ---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-90-04168b02e9ca> in <module>
      5 y=np.array([[1,0,1] for _ in range(1500)])
      6 
----> 7 decomp.fit(X,y)

<ipython-input-86-74a9247c4023> in fit(self, X, y, entails)
     76 
     77 
---> 78         self.model.fit(x=[xg,xe], y=np.expand_dims(y, axis=1), validation_split = 0.1,batch_size=self.batch_size , epochs= self.epochs)

~/ckm/py3/lib/python3.5/site-packages/tensorflow/python/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, max_queue_size, workers, use_multiprocessing, **kwargs)
   1637           initial_epoch=initial_epoch,
   1638           steps_per_epoch=steps_per_epoch,
-> 1639           validation_steps=validation_steps)
   1640 
   1641   def evaluate(self,

~/ckm/py3/lib/python3.5/site-packages/tensorflow/python/keras/engine/training_arrays.py in fit_loop(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps)
    213           ins_batch[i] = ins_batch[i].toarray()
    214 
--> 215         outs = f(ins_batch)
    216         if not isinstance(outs, list):
    217           outs = [outs]

~/ckm/py3/lib/python3.5/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
   2984 
   2985     fetched = self._callable_fn(*array_vals,
-> 2986                                 run_metadata=self.run_metadata)
   2987     self._call_fetch_callbacks(fetched[-len(self._fetches):])
   2988     return fetched[:len(self.outputs)]

~/ckm/py3/lib/python3.5/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1437           ret = tf_session.TF_SessionRunCallable(
   1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
   1440         if run_metadata:
   1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/ckm/py3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: -1 is not between 0 and 3
     [[{{node training_11/RMSprop/gradients/selfattnlayer2_6/transpose_2_grad/InvertPermutation}} = InvertPermutation[T=DT_INT32, _class=["loc:@training_11/RMSprop/gradients/selfattnlayer2_6/transpose_2_grad/transpose"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](selfattnlayer2_6/transpose_2/perm)]]

有人可以指出我在哪里寻找错误吗?如果需要,我将共享代码(现在,该代码分为3个文件,这就是为什么我没有附加文件的原因)。我要解决的问题是使用某种自我注意的多标签分类问题。

编辑: 我发现了错误。

decomp = DecomposableAttention()
d1 = np.random.randint(0,10, size=[1500,2,128])
d2 = np.random.randint(0,4, size=[1500,2,128])
X=np.array([d1,d2])
y=np.array([[1,0,1] for _ in range(1500)])

decomp.fit(X,y)

是违规行。指定暗淡时不能使用K.permute_dimensions(K.dot(x, self.WK),(0,-1,1)) 。但是只能使用正整数,如下所示:

-1

1 个答案:

答案 0 :(得分:0)

permute_dimensions基本上是指轴的顺序。因此,您必须指定要生成的所有轴。

您可能会对reshape之类的操作感到困惑,在这些操作中您可以将一个轴指定为-1。在这些操作中,它表示您希望TensorFlow为您隐式评估尺寸。