卷积神经网络的一些知识:
import tensorflow as tf
import numpy as np
a=np.array([[1,1,0,1],[0,0,0,0]])
b=a.reshape(2,2,1,2)#input batch为2,通道为1时 ,输出果然是2*3*3*2
filter=tf.Variable(b,dtype=tf.float32)
a1=np.arange(32)
#b1=a1.reshape(1,4,4,2)
b1=a1.reshape(2,4,4,1) #batch为2
input=tf.Variable(b1,dtype=tf.float32) #还要加个float32
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1],padding='VALID')
sess=tf.Session()
sess.run(tf.global_variables_initializer())
bb=(sess.run(op))
#cc=bb.reshape(2,3,3)
#dd=bb.reshape(3,3,2)
ee=sess.run(tf.squeeze(op))
print(bb.size)
1.对于卷积函数来说,输入变量shape为(2,4,4,1),2张图片,通道为1,图像为4*4,输入到
tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1],padding='VALID') 函数里,输出果然是(2,3,3,2),也就是第一个猜想成立: batch值总是在那的,对于每一层网络,都是那么大,都是(batch,...)作为第一位。 2.第二个猜想如果一张图像作为输入shaoe:(1,4,4,1),卷积核为(2,2,1,3) ,输出为(1,2,2,3) 。然后这层输出作为输入,输入到第二个 卷积层,那么就相当于通道数为3的一张2*2大小的图片了。 因此下一个卷积核 通道数为3. 3.看到这样的代码
conv1 = tf.contrib.layers.conv2d(
X, 32, 8, 4, activation_fn=tf.nn.relu)
conv2 = tf.contrib.layers.conv2d(
conv1, 64, 4, 2, activation_fn=tf.nn.relu)
conv3 = tf.contrib.layers.conv2d(
conv2, 64, 3, 1, activation_fn=tf.nn.relu)
我觉得这里的卷积核的通道数是自动的生成那样的卷积核 如:第二层conv2的卷积核通道就为32。
4.卷积核其实是权重,因此很多网络都是随机生成卷积核,到时候就是学习修改这些权重。也因此conv2d()函数的卷积操作 要不要翻转180度
都是无所谓的。反正都要改。
5.经过试验,卷积核与图像输入 是直接相乘的,没有翻转
试验代码如下:最困难的地方是卷积核不知道是如何的,导致话费好多时间,才弄清楚
#懂了,卷积层是随机的,因为那些都是权重,需要更新
#conv_filter_w1 = tf.Variable(tf.random_normal([3, 3, 1, 10]))这个竟然是这样的
#conv_filter_w2 = tf.Variable(tf.random_normal([3, 3, 10, 5]))参数对应长宽,通道数,输出数
#tf.nn.conv2d 是直接相乘的,没有翻转。然后我想要多个核的时候,把数字一变化(卷积核a),每次答案都莫名其妙.终于找到规律了,还是reshape的事。因为最后为2,也就是说,第一个在A矩阵,第2个在B矩阵,这样排列
#例如
# a=np.array([[1,1,0,0],[0,0,0,0]])
#b=a.reshape(2,2,1,2) 最后2个卷积核其实为A=[1,0;0,0] 与B=[1,0;0;0]
#第二个试验:多个通道即 第一个卷积层作为输入,此时feature map 有多个,确实是类似多通道那样计算,每个卷积核 卷积对应的feature map然后相加
'''
a=np.array([[1,1,0,1],[0,0,0,0]])
b=a.reshape(2,2,2,1)
A=[[1,0],[0,0]] ; B=[[1,1],[0,0]]
a1=np.arange(32)
b1=a1.reshape(1,4,4,2)
C=[[0,2,4,6],[8,10,12,14],...,[24,26,28,30]] D=[[1,3,5,7],...,[25,27,29,31]]
'''
import tensorflow as tf
import numpy as np
a=np.array([[1,1,0,1],[0,0,0,0]])
b=a.reshape(2,2,2,1)
#b=a.reshape(2,2,1,2)#input batch为2,通道为1时 ,输出果然是2*3*3*2
filter=tf.Variable(b,dtype=tf.float32)
a1=np.arange(32)
b1=a1.reshape(1,4,4,2)
#b1=a1.reshape(2,4,4,1) #batch为2
input=tf.Variable(b1,dtype=tf.float32) #还要加个float32
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1],padding='VALID')
sess=tf.Session()
sess.run(tf.global_variables_initializer())
bb=(sess.run(op))
#cc=bb.reshape(2,3,3)
#dd=bb.reshape(3,3,2)
ee=sess.run(tf.squeeze(op))
print(bb.size)