卷积神经网络
作者:互联网
layers.Conv2D
In [1]: import tensorflow as tf
...: from tensorflow.keras import layers
########## x必须ndim=4, shape:[b, h, w, c]
In [2]: x = tf.random.normal([1, 32, 32, 3])
In [3]: layer = layers.Conv2D(4, kernel_size=5, strides=1, padding='valid')
In [4]: out = layer(x)
In [5]: out.shape
Out[5]: TensorShape([1, 28, 28, 4])
In [6]: layer = layers.Conv2D(4, kernel_size=5, strides=1, padding='same')
In [7]: out = layer(x)
In [8]: out.shape
Out[8]: TensorShape([1, 32, 32, 4])
In [9]: layer = layers.Conv2D(4, kernel_size=5, strides=2, padding='same')
In [10]: out = layer(x)
In [11]: out.shape
Out[11]: TensorShape([1, 16, 16, 4])
# weight & bias
In [12]: layer.kernel
Out[12]:
<tf.Variable 'conv2d_2/kernel:0' shape=(5, 5, 3, 4) dtype=float32, numpy=
array([[[[ 0.07923736, 0.14217006, -0.0109257 , 0.08888994],
[-0.01091294, -0.16224794, 0.18029307, -0.01917681],
[ 0.1477335 , 0.13419123, 0.13923149, -0.18107778]],...
nn.conv2d
In [14]: w = tf.random.normal([5, 5, 3, 4])
In [15]: b = tf.zeros([4])
In [16]: x.shape
Out[16]: TensorShape([1, 32, 32, 3])
In [17]: out = tf.nn.conv2d(x, w, strides=1, padding='VALID')
In [18]: out.shape
Out[18]: TensorShape([1, 28, 28, 4])
In [19]: out = out + b
In [20]: out.shape
Out[20]: TensorShape([1, 28, 28, 4])
In [21]: out = tf.nn.conv2d(x, w, strides=2, padding='VALID')
In [22]: out.shape
Out[22]: TensorShape([1, 14, 14, 4])
Gradient
标签:layer,卷积,shape,神经网络,TensorShape,tf,out,Out 来源: https://blog.csdn.net/qq_46456049/article/details/112995733