2022-03-09
tf.nn.(输入, , , , =True, =”NHWC”, =[1,1,1,1], name=None)
参数:具体实现
输入形状:[batch, , , ]
形状:[, , , ]
计算过程:
1.将展开为 2-,shape: [**, ]
2.从输入中提取,形成一个,形状:[batch, , , **]
3.对于每个补丁,在右边乘以 1。即[batch, , , **] x [* * , ],那么输出shape:[batch, , , ]
[注意:必须有 [0] = [3] = 1]。在大多数情况下,水平与垂直相同,即 = [1, , , 1]。
输出结果的形状计算:
在caffe里是这样的:
=floor(+2*pad-)/+1;向下取整
=地板(+2*pad-)/+1
它看起来像这样:
“相同”类型:
= ceil(/[1]); ceil 向上取整
= ceil(/[2])
“有效”类型:
= ceil(( – + 1) / [1])
= ceil(( – + 1) / [2])
验证码
# -*- coding:utf-8 -*- from __future__ import division import tensorflow as tf import numpy as np import math import pandas as pd input_arr = np.zeros((12, 15), dtype=np.float32) number = 0 for row_idx in range(input_arr.shape[0]): for col_idx in range(input_arr.shape[1]): input_arr[row_idx][col_idx] = number number +=1 number = 6 w_arr = np.zeros((2, 3), dtype=np.float32) for row_idx in range(w_arr.shape[0]): for col_idx in range(w_arr.shape[1]): w_arr[row_idx][col_idx] = number number += 1 stride = [1, 1, 1, 1] # 从卷积的定义【实际上不是卷积,而是cross-correlation】进行计算验证---对VALID类型卷积进行 res_shape_h = int(math.ceil((input_arr.shape[0] - w_arr.shape[0] + 1) / stride[1])) res_shape_w = int(math.ceil(input_arr.shape[1] - w_arr.shape[1] + 1) / stride[2]) validation_res = np.zeros(shape=(res_shape_h, res_shape_w), dtype=np.float32) for row_idx in range(validation_res.shape[0]): for col_idx in range(validation_res.shape[1]): patch = input_arr[row_idx : row_idx+w_arr.shape[0], col_idx : col_idx+w_arr.shape[1]] # 这里的 * 实际上代表的是点积,即对应元素位置相乘 res = np.sum(patch * w_arr) validation_res[row_idx][col_idx] = res print('result of convolution from its definition: validation_res') print(validation_res) pd.DataFrame(validation_res).to_csv('validation_res.csv', index = False, header=False) # 从TensorFlow实现出发 input_arr = np.reshape(input_arr, [1, input_arr.shape[0], input_arr.shape[1], 1]) w_arr = np.reshape(w_arr, [w_arr.shape[0], w_arr.shape[1], 1, 1]) # 输入Tensor, shape: [1, 12, 15, 1] net_in = tf.constant(value=input_arr, dtype=tf.float32) # filter, shape: [2, 3, 1, 1] W = tf.constant(value=w_arr, dtype=tf.float32) # TensorFlow卷积的计算结果 # valid卷积结果, shape: [1, 11, 13, 1] result_conv_valid = tf.nn.conv2d(net_in, W, stride, 'VALID') # same卷积结果, shape: [1, 12, 15, 1] result_conv_smae = tf.nn.conv2d(net_in, W, stride, 'SAME') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) valid_conv_res, same_conv_res = sess.run([result_conv_valid, result_conv_smae]) print(valid_conv_res.shape) valid_conv_res = np.reshape(valid_conv_res, [valid_conv_res.shape[1], valid_conv_res.shape[2]]) same_conv_res = np.reshape(same_conv_res, [same_conv_res.shape[1], same_conv_res.shape[2]]) print('TensorFlow con res: valid_conv_res') print(valid_conv_res) pd.DataFrame(valid_conv_res).to_csv('conv_res.csv', index=False, header=False) pd.DataFrame(same_conv_res).to_csv('same_res.csv', index=False, header=False)
分类:
技术要点:
相关文章:
© 版权声明
本站下载的源码均来自公开网络收集转发二次开发而来,
若侵犯了您的合法权益,请来信通知我们1413333033@qq.com,
我们会及时删除,给您带来的不便,我们深表歉意。
下载用户仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,访问及下载者下载默认同意本站声明的免责申明,请合理使用切勿商用。
THE END
暂无评论内容