使用Keras的自定义过滤器CNN

时间:2019-05-02 00:17:52

标签: python tensorflow filter keras conv-neural-network

我正在尝试使用我自己的函数使用keras初始化过滤器,但出现错误,我不知道为该参数添加什么到代码中; 'partition_info'

这是我的代码的一部分:

m
'Rounds a number to the nearest unit, never exceeding the actual value
function RoundToNearestOrBelow(num, r)

    '@param         num         Long/Integer/Double     The number to be rounded
    '@param         r           Long                    The rounding value
    '@return        OUT         Long                    The rounded value

    'Example usage :
    '   Round 47 to the nearest 5 : it will return 45
    '   Response.Write RoundToNearestBelow(47, 5)

    Dim OUT : OUT = num

    Dim rounded : rounded = Round((((num)) / r), 0) * r

    if (rounded =< num) then
        OUT = rounded
    else
        OUT = rounded - r
    end if

    'Return
    RoundToNearestOrBelow = OUT

end function 'RoundToNearestOrBelow
  

TypeError:kernel_init()获得了意外的关键字参数'partition_info'

2 个答案:

答案 0 :(得分:0)

def kernel_init(self,shape, dtype=None, partition_info=None):应该解决此问题。

该错误是由于code中的此检查所致。

答案 1 :(得分:0)

谢谢,我添加了这一部分,它非常完美。

最终代码

def kernel_init(self, shape, dtype=tf.float64, partition_info=None):
            kernel = np.zeros(shape=shape)
            kernel[:,:,0,0] = np.array([[1,2,1],
                                        [0,0,0],
                                        [-1,-2,-1]])

            return kernel 


self.model = keras.Sequential([
             keras.layers.Conv2D(32, [3,3], kernel_initializer=self.kernel_init, 
             input_shape=(28,28,1), padding="valid"),
             keras.layers.MaxPooling2D(pool_size=(2, 2)),
             keras.layers.Dropout(0.25),
             keras.layers.Flatten(),
             keras.layers.Dense(batch_size, activation=tf.nn.relu),
             keras.layers.Dropout(0.5),
             keras.layers.Dense(num_classes, activation=tf.nn.softmax)
             ])