zoukankan      html  css  js  c++  java
  • Python Class

    Basic knowledge:

    1. Class VS Instance;

    class Student(object):
    
        def __init__(self, name, score):
            self.__name = name
            self.__score = score
        def print_score(self):
            print(self.__name,self.__score)

    2. variables

    私有变量private,__name, 双下划线: 只能内部访问,不能外部访问。

    _name,当为单下划线时, 这样的实例变量外部是可以访问的,但是,按照约定俗成的规定,当你看到这样的变量时,意思就是,“虽然我可以被访问,但是,请把我视为私有变量,不要随意访问”。

    特殊变量, __name__,可以直接访问,一般为库变量,比如tf.__version__; 库函数也可以加双下划线,比如def __int__(self, param1, param2);

    3. Class.functions

    相比于普通的函数,类函数的第一个参数永远是self,即实例变量本身,并且调用时不用传递该参数,程序会自动添加传递。

    4. self的仔细用法

    • self代表类的实例,而非类。self.__class__则指向类。
    • self 在函数定义时,不可以省略。因为程序会自动传递self参数,省略会导致参数报错。

    5. 在pytorch的网络结构类中,一般是下面的结构,先init,下边super  然后定义网络结构  再接着forward函数,把数据传进去 计算

    self(x)返回的是forward 函数的结果。

    class VRAE(BaseEstimator, nn.Module):
       
        def __init__(self, sequence_length, number_of_features, hidden_size=90, hidden_layer_depth=2, latent_length=20,
                     batch_size=32, learning_rate=0.005, block='LSTM',
                     n_epochs=5, dropout_rate=0.):
    
            super(VRAE, self).__init__()
    
            self.dtype = torch.FloatTensor
    
            self.encoder = Encoder(number_of_features = number_of_features,
                                   hidden_size=hidden_size,
                                   hidden_layer_depth=hidden_layer_depth,
                                   latent_length=latent_length,
                                   dropout=dropout_rate,
                                   block=block)
    
            self.lmbd = Lambda(hidden_size=hidden_size,
                               latent_length=latent_length)
    
            self.decoder = Decoder(sequence_length=sequence_length,
                                   batch_size = batch_size,
                                   hidden_size=hidden_size,
                                   hidden_layer_depth=hidden_layer_depth,
                                   latent_length=latent_length,
                                   output_size=number_of_features,
                                   block=block,
                                   dtype=self.dtype)
    
            self.sequence_length = sequence_length
            self.hidden_size = hidden_size
            self.hidden_layer_depth = hidden_layer_depth
            self.latent_length = latent_length
            self.batch_size = batch_size
    
        def forward(self, x):
            cell_output = self.encoder(x)
            latent = self.lmbd(cell_output)
            x_decoded = self.decoder(latent)
    
            return x_decoded, latent
  • 相关阅读:
    tomact与eclipces的配置
    IDEA中Flink环境pom.xml文件配置
    python读写mysql
    用pyspark实现Wordcount
    python计算相似度
    Spark读取mysql
    Spark之WordCount
    IDEA中Spark环境pom.xml文件配置
    Spark之HelloWorld
    (转)二叉树的中序遍历与后序遍历构造二叉树思路(递归方法,含图片说明)
  • 原文地址:https://www.cnblogs.com/dulun/p/12369947.html
Copyright © 2011-2022 走看看