torch语句和tf语句

 pytorch用得到的语句

torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) 

input -> 输入数据
k -> 前k个
dim -> 默认为输入tensor的最后一个维度
sorted -> 是否排序
largest -> False表示第k个最小值,True表示第k个最大值

nn.Linear(in_features, out_features, bias) 
nn.Sequential(*args)
   

tensorflow转pytorch

整体结构

#################   Tensorflow    ###############
class Decoder(tf.keras.layers.Layer):
    def __init__(self, intermediate_dim, original_dim):
        super(Decoder, self).__init__()
            self.xxx = xxxxx

    def call(self, input):
        y=xxxxxxx
        return


class Autoencoder(tf.keras.Model):
    def __init__(self, intermediate_dim, original_dim):
        super(Autoencoder, self).__init__()
            self.xxx = Decoder(intermediate_dim, original_dim)

    def call(self, input):
        y=xxxxxxx
        return


#################   Pytorch    ###############


class Encoder(nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder_hidden_layer = nn.Linear(784, 128)
        self.encoder_output_layer = nn.Linear(128, 128)
 
    def forward(self, features):
        activation = self.encoder_hidden_layer(features)
        activation = F.relu(activation)
        code = self.encoder_output_layer(activation)
        code = F.relu(code)
        return code
 

 
class Autoencoder(nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder = Encoder()
        self.decoder = Decoder()
 
    def forward(self, features):
        code = self.encoder(features)
        reconstructed = self.decoder(code)
        return reconstructed

tensorflow的Dense(全连接层)

#########         tensorflow     ###################
Dense(units,#代表该层的输出维度
 activation=None, #激活函数.但是默认 liner
 use_bias=True, #是否使用b 直线 y=ax+b 中的 b
 kernel_initializer='glorot_uniform', 
 bias_initializer='zeros', 
 kernel_regularizer=None, 
 bias_regularizer=None, 
 activity_regularizer=None, 
 kernel_constraint=None, bias_constraint=None)


tf.layers.dense(
inputs, #该层的输入
units, #输出的大小(维数)
activation=None, #**函数
use_bias=True, #偏置参数,默认使用
kernel_initializer=None, #权重矩阵的初始化函数。如果为None,则使用tf.get_variable使用的默认初始化程序初始化权重
bias_initializer=tf.zeros_initializer(), #bias的初始化函数
kernel_regularizer=None, #权重矩阵的正则函数
bias_regularizer=None, #bias的正则函数
activity_regularizer=None, #输出的正则函数
kernel_constraint=None, #优化器更新后应用于内核的可选投影函数(例如,用于实现层权重的范数约束或值约束)。 该函数必须将未投影的变量作为输入,并且必须返回投影变量(必须具有相同的形状)。 在进行异步分布式培训时,使用约束是不安全的。
bias_constraint:由优化器更新后应用于偏差的可选投影函数。
trainable=None, #如果为True,还将变量添加到图集
name=None, #名字
reuse=None, #是否以同一名称重用前一层的权重
)



layer_one = Dense(feature_size // reduction,   #输出维数
                  kernel_initializer='he_normal',   # 核权重矩阵的初始值设定项
                  activation='relu', # 激活函数,不指定则没有
                  use_bias=True,  # 布尔值,是否使用偏移向量
                  bias_initializer='zeros') # 偏差向量的初始值设定项
avg_pool = layer_one(x)


#########     pytorch    ##################

nn.Linear(in_features, out_features, bias=True)

linear_layer = nn.Linear(input_size, output_size)
output = linear_layer(x)

conv2d

 

################    tensorflow        ##############
x = Conv2D(feature_size, kernel_size=3, kernel_initializer='he_uniform', padding='same')(x)


padding有两个方式可以选择:“SAME” and “VALID”
1.padding=‘SAME‘

        卷积核在进行卷积时候,假设原图是3X3,卷积核为2x2,步长为2,当向右滑动两步之后,VALID方式发现余下的窗口不到2×2所以直接将第三列舍弃,而SAME方式并不会把多出的一列丢弃,但是只有一列了就填充一列0

2.padding=‘VALID’

        卷积核在进行卷积时候,假设原图是3X3,卷积核为2x2,步长为2,当向右滑动两步之后,VALID方式发现余下的窗口不到2×2时候将第三列舍弃
————————————————
版权声明:本文为CSDN博主「smiling0927」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/smiling614/article/details/86618797



SAME与VALID
在操作过程中过滤器可能不能将某个方向上的数据刚好处理完,‘SAME’ 为对输入的图像矩阵边缘补零,‘VALID’ 则不补



################     pytorch   ######################

torch.nn.Conv2d(
	in_channels, 
	out_channels, 
	kernel_size, 
	stride=1, 
	padding=0, 
	dilation=1, 
	groups=1, 
	bias=True, 
	padding_mode='zeros', 
	device=None, 
	dtype=None
)

in_channels:输入的通道数,RGB 图像的输入通道数为 3
out_channels:输出的通道数
kernel_size:卷积核的大小,一般我们会使用 5x5、3x3 这种左右两个数相同的卷积核,因此这种情况只需要写 kernel_size = 5这样的就行了。如果左右两个数不同,比如3x5的卷积核,那么写作kernel_size = (3, 5),注意需要写一个 tuple,而不能写一个 list。
stride = 1:卷积核在图像窗口上每次平移的间隔,即所谓的步长。
padding:指图像填充,后面的int型常数代表填充的多少(行数、列数),默认为0。需要注意的是这里的填充包括图像的上下左右,以padding=1为例,若原始图像大小为[32, 32],那么padding后的图像大小就变成了[34, 34]
dilation:是否采用空洞卷积,默认为1(不采用)。从中文上来讲,这个参数的意义从卷积核上的一个参数到另一个参数需要走过的距离,那当然默认是1了,毕竟不可能两个不同的参数占同一个地方吧(为0)。更形象和直观的图示可以观察Github上的Dilated convolution animations,展示了dilation=2的情况。
groups:决定了是否采用分组卷积,groups参数可以参考groups参数详解
bias:即是否要添加偏置参数作为可学习参数的一个,默认为True。
padding_mode:即padding的模式,默认采用零填充。
————————————————
版权声明:本文为CSDN博主「望天边星宿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/See_Star/article/details/127560160

全局池化 

########### tensorflow  ##############
    avg_pool = GlobalAveragePooling2D()(x)

    max_pool = GlobalMaxPooling2D()(x)



##########     pytorch   ##########
global_avg_pool = nn.AdaptiveAvgPool2d((1, 1))
out = global_avg_pool(x)



global_max_pool = nn.AdaptiveMaxPool2d((1, 1))
out = global_max_pool(x)


最大池化

# Torch 实现
torch.nn.MaxPool1d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)


平均池化
torch.nn.AvgPool1d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)

sobel找边缘 

import cv2 
o = cv2.imread('Sobel4.bmp', cv2.IMREAD_GRAYSCALE) 
Sobelx = cv2.Sobel(o, cv2.CV_64F,1,0) 
Sobely = cv2.Sobel(o, cv2.CV_64F,0,1) 
Sobelx = cv2.convertScaleAbs(Sobelx) 
Sobely = cv2.convertScaleAbs(Sobely) 
Sobelxy =  cv2.addWeighted(Sobelx,0.5, Sobely,0.5,0) 

激活函数 

https://blog.csdn.net/jiebaoshayebuhui/article/details/130441213

https://blog.csdn.net/qq_64741315/article/details/132480866

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值