Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在tensorflow/keras中使用常数滤波器进行卷积_Python_Tensorflow_Keras_Conv Neural Network - Fatal编程技术网

Python 如何在tensorflow/keras中使用常数滤波器进行卷积

Python 如何在tensorflow/keras中使用常数滤波器进行卷积,python,tensorflow,keras,conv-neural-network,Python,Tensorflow,Keras,Conv Neural Network,在resnet的某个阶段,我每个图像有6个特征,即每个示例的形状为1x8x6,我希望每个特征都包含4个大小为1x2x1x1的常量过滤器(DWT),步幅为2,以便在下一层中获得24个特征,并将图像变为1x4x24。但是,我无法使用tf.nn.conv2d或tf.nn.convolution实现此目的,conv2d表示输入的第四维等于滤波器的第三维,但我如何做到这一点,我尝试了第一个滤波器,但即使这样也不起作用: x_in = np.random.randn(1,8,8,6) kernel_in =

在resnet的某个阶段,我每个图像有6个特征,即每个示例的形状为1x8x6,我希望每个特征都包含4个大小为1x2x1x1的常量过滤器(DWT),步幅为2,以便在下一层中获得24个特征,并将图像变为1x4x24。但是,我无法使用tf.nn.conv2d或tf.nn.convolution实现此目的,conv2d表示输入的第四维等于滤波器的第三维,但我如何做到这一点,我尝试了第一个滤波器,但即使这样也不起作用:

x_in = np.random.randn(1,8,8,6)
kernel_in = np.array([[[[1],[1]],[[1],[1]]]])
kernel_in.shape
x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)
tf.nn.convolution(x, kernel, strides=[1, 1, 1, 1], padding='VALID')
这样试试

x_in = np.random.randn(1,8,8,6) # [batch, in_height, in_width, in_channels]
kernel_in = np.ones((2,2,6,24)) # [filter_height, filter_width, in_channels, out_channels]

x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)

tf.nn.conv2d(x, kernel, strides=[1, 2, 2, 1], padding='VALID')
# <tf.Tensor: shape=(1, 4, 4, 24), dtype=float32, numpy=....>
x_in=np.random.randn(1,8,8,6)#[批次,单位高度,单位宽度,单位通道]
内核_in=np.one((2,2,6,24))#[过滤器高度,过滤器宽度,输入通道,输出通道]
x=tf.constant(x_in,dtype=tf.float32)
kernel=tf.constant(kernel_in,dtype=tf.float32)
conv2d(x,内核,步幅=[1,2,2,1],padding='VALID')
# 
用这种方法试试

x_in = np.random.randn(1,8,8,6) # [batch, in_height, in_width, in_channels]
kernel_in = np.ones((2,2,6,24)) # [filter_height, filter_width, in_channels, out_channels]

x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)

tf.nn.conv2d(x, kernel, strides=[1, 2, 2, 1], padding='VALID')
# <tf.Tensor: shape=(1, 4, 4, 24), dtype=float32, numpy=....>
x_in=np.random.randn(1,8,8,6)#[批次,单位高度,单位宽度,单位通道]
内核_in=np.one((2,2,6,24))#[过滤器高度,过滤器宽度,输入通道,输出通道]
x=tf.constant(x_in,dtype=tf.float32)
kernel=tf.constant(kernel_in,dtype=tf.float32)
conv2d(x,内核,步幅=[1,2,2,1],padding='VALID')
# 

如何在TF2中的
Keras.conv2d
层中为过滤器填充预定义值的简单示例:

model=models.Sequential()
#一个3x3过滤器
添加(layers.Conv2D(1,(3,3),input_shape=(无,无,1)))
#访问目标层
图层=模型。图层[0]
current_w,current_bias=图层。获取_权重()#查看当前权重
new_w=tf.常数([[1,2,3],
[4, 5, 6],
[7, 8, 9]])
new_w=tf.重塑(new_w,custom_w.形状)#固定形状
新偏差=tf.常数([0])
层。设置权重([new\u w,new\u bias])
model.summary()
#让我想想。。
tf.print(model.layers[0]。获取权重()

如何在TF2中的
Keras.conv2d
层中为过滤器填充预定义值的简单示例:

model=models.Sequential()
#一个3x3过滤器
添加(layers.Conv2D(1,(3,3),input_shape=(无,无,1)))
#访问目标层
图层=模型。图层[0]
current_w,current_bias=图层。获取_权重()#查看当前权重
new_w=tf.常数([[1,2,3],
[4, 5, 6],
[7, 8, 9]])
new_w=tf.重塑(new_w,custom_w.形状)#固定形状
新偏差=tf.常数([0])
层。设置权重([new\u w,new\u bias])
model.summary()
#让我想想。。
tf.print(model.layers[0]。获取权重()