Tensorflow和Pytorch函数转换对照表

最近做实验在用,这里存一些:
Tensorflow和Pytorch函数转换对照表
TENSORFLOW与PYTORCH:区别及函数习惯的对比
如侵必删

tensorflow的一个神经网络层:

#创建一个神经网络层
def add_layer(input,in_size,out_size,activation_function=None):
    """
    :param input: 数据输入
    :param in_size: 输入大小
    :param out_size: 输出大小
    :param activation_function: 激活函数(默认没有)
    :return:output:数据输出
    """
    Weight=tf.Variable(tf.random_normal([in_size,out_size]) )
    biases=tf.Variable(tf.zeros([1,out_size]) +0.1 )
    W_mul_x_plus_b=tf.matmul(input,Weight) + biases
    #根据是否有激活函数
    if activation_function == None:
        output=W_mul_x_plus_b
    else:
        output=activation_function(W_mul_x_plus_b)
    return output

batch_norm 层:

# batch_normalization层
def batch_norm(x):
    epsilon = 1e-5
    batch_mean, batch_var = tf.nn.moments(x, [0])
    return tf.nn.batch_normalization(x, batch_mean, batch_var,
                                     offset=None, scale=None,
                                     variance_epsilon=epsilon)

你可能感兴趣的:(pytorch,tensorflow)