在自定义图层中使用model.fit时出现Keras batch_size问题

时间:2019-02-23 10:11:28

标签: python tensorflow keras deep-learning

我有一个自定义层,可重塑输入张量,对内核执行一些点积运算,并返回具有相同维数的张量。 我网络的输入是图像,例如尺寸为61x80。当火车图像的数量是batch_size的倍数时,model.fit可以正常工作。例如,火车总图像= 2700,batch_size = 10。 但是,当火车总图像= 2701,batch_size()无效时,将引发类似以下的错误-

  
    

Epoch 1/5 2520/2701 [========================= ......]-ETA:0秒-损失:2.7465 -acc:0.2516Traceback(最近通话最近):

  
     

文件“”,第5行,在       历史= model.fit(x_train,y_train,batch_size = 10,epochs = 5)

     

文件   “ /home/eee/anaconda3/lib/python3.6/site-packages/keras/engine/training.py”,   1039行,适合       validate_steps = validation_steps)

     

文件   “ /home/eee/anaconda3/lib/python3.6/site-packages/keras/engine/training_arrays.py”,   在fit_loop中的第199行       outs = f(ins_batch)

     

文件   “ /home/eee/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”,   第2715行,在致电中       返回self._call(inputs)

     

文件   “ /home/eee/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”,   _call中的第2675行       提取= self._callable_fn(* array_vals)

     

文件   “ /home/eee/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”,   第1439行,在致电中       run_metadata_ptr)

     

文件   “ /home/eee/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py”,   第528行,位于退出中       c_api.TF_GetCode(self.status.status))

     

InvalidArgumentError:要重塑的输入是具有4880值的张量,   但请求的形状为48800 [[{{node my_layer_3 / Reshape}} =   重塑[T = DT_FLOAT,Tshape = DT_INT32,   _device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”](_ arg_input_2_0_0,   my_layer_3 / stack)]]

请帮助解决该问题。

编辑:-添加自定义图层的代码

class MyLayer(Layer):

def __init__(self, output_dim, **kwargs):
    self.output_dim = output_dim
    super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):
    print(len(input_shape))
    # Create a trainable weight variable for this layer.
    assert len(input_shape) >= 3
    input_dim = input_shape[1:]
    print(input_shape)


    self.kernel1 = self.add_weight(shape=self.output_dim[0],input_dim[0]),
                                   name = 'kernel1',
                                  initializer='uniform',
                                  trainable=True)
    print(self.kernel1)

    self.kernel2 = self.add_weight(shape=self.output_dim[1],input_dim[1]),
                                   name = 'kernel2',
                                  initializer='uniform',
                                  trainable=True)
    print(self.kernel2)


    super(MyLayer, self).build(input_shape)  

def call(self, x):
     print(x.shape)
     input_shape=x.shape

     mat1_shape =K.int_shape(self.kernel1)     
     mat2_shape =K.int_shape(self.kernel2)

     output1 = Myoperation(x,self.kernel1,1)
     output2 = Myoperation(output1,self.kernel2,2)                

     return output2


def compute_output_shape(self, input_shape):

    return (input_shape[0],self.output_dim[0],self.output_dim[1])

Myoperation函数的代码是-

def Myoperation(x,mat,mode):
    shape1 = K.shape(x) 
    mode_list =[0,1,2]
    mode_list.remove(mode)
    mode_shape= shape1[mode]

    new_shape = tf.stack( [mode_shape,(shape1[mode_list[0]]*shape1[mode_list[1]])])                    
    input_reshaped = K.reshape(x, new_shape)
    ten_mul = K.dot(mat,input_reshaped)
    out_mode=K.int_shape(mat)
    if (mode==1):
       out_shape =  tf.stack([shape1[mode_list[0]],out_mode[0],shape1[mode_list[1]]])
    if (mode==2):
       out_shape = tf.stack([shape1[mode_list[0]],shape1[mode_list[1]],out_mode[0]])

    output_reshaped = K.reshape(ten_mul,out_shape)    

   return output_reshaped

当火车图像集不是batch_size的倍数时,问题在于重新调整张量。

0 个答案:

没有答案