首页 > 解决方案 > Python类找不到它的功能

问题描述

我写了一个python类,但是在使用的时候却找不到里面的任何函数。我在 windows 和 MAC 上测试过,两者都不起作用。这是我定义的类的一些代码:(我这里不是完整代码,因为代码太多,这里不允许复制)

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt



class VanillaInfoGAN(object):
    def __init__(self,learning_rate,batch_size,num_epoches,z_dim,c_cat_dim,c_cont_dim,model_num,print_every=30,show_every=50,
                 store_path = './ckpt/',training_picture_path='./pictures/'):
        self.learning_rate = learning_rate
        self.batch_size = batch_size
        self.num_epoches = num_epoches
        self.z_dim = z_dim
        self.c_cat_dim = c_cat_dim
        self.c_cont_dim = c_cont_dim
        self.print_every = print_every
        self.show_every = show_every
        self.data =input_data.read_data_sets('MNIST',one_hot=True)
        self.data_dim = 784
        self.store_path = store_path
        self.model_num = model_num
        self.training_picture_path = training_picture_path

        self.real_data_placeholder = tf.placeholder(tf.float32,[None,self.data_dim])
        self.z_placeholder = tf.placeholder(tf.float32,[None,self.z_dim])
        self.c_cat_placeholder = tf.placeholder(tf.float32,[None,self.c_cat_dim])
        self.c_cont_placeholder = tf.placeholder(tf.float32,[None,self.c_cont_dim])
        self.c = tf.concat([self.c_cat_placeholder,self.c_cont_placeholder],axis=1)
        self.z_c = tf.concat([self.z_placeholder,self.c_cat_placeholder,self.c_cont_placeholder],axis=1)


        self.g_out = self.generator()
        d_out_real = self.discriminator(self.real_data_placeholder)
        d_out_fake = self.discriminator(self.g_out,reuse=True)
        q_out = self.q_net(self.g_out)
        self.g_loss,self.d_loss,self.q_loss = self.build_loss(self.g_out,d_out_real,d_out_fake,q_out)

        self.g_opt,self.d_opt,self.q_opt = self.optimizer(self.g_loss,self.d_loss,self.q_loss)

        self.saver= tf.train.Saver()
        print('Model graph has built')





        def generator(self,reuse=False):
            with tf.variable_scope('generator',reuse=reuse):
                layer = tf.layers.dense(self.z_c,128,activation = tf.nn.relu)
                layer = tf.layers.dense(layer,self.data_dim,activation = tf.nn.sigmoid)

                return layer

        def discriminator(self,d_input,reuse=False):
            with tf.variable_scope('discriminator',reuse=reuse):
                layer = tf.layers.dense(d_input,128,activation = tf.nn.relu)
                layer = tf.layers.dense(layer,1,activation = tf.nn.sigmoid)

                return layer

        def q_net(self,g_out,reuse=False):
            with tf.variable_scope('Q',reuse=reuse):
                layer = tf.layers.dense(g_out,128,activation = tf.nn.relu)
                layer = tf.layers.dense(layer,self.c_cat_dim+self.c_cont_dim,activation = None)
                layer_cat = tf.nn.softmax(layer[:,:self.c_cat_dim])
                layer_cont  =tf.nn.sogmoid(layer[:,self.c_cat_dim:])
                q_out = tf.concat([layer_cat,layer_cont],axis=1)

                return q_out

这是我用来创建对象的运行变形器:

from VanillaInfoGAN import  VanillaInfoGAN
    gan =  VanillaInfoGAN(learning_rate = learning_rate,batch_size=batch_size,num_epoches=num_epoches,z_dim=z_dim,c_cat_dim=c_cat_dim,c_cont_dim=c_cont_dim,
                      model_num=model_num,print_every=print_every,show_every=show_every)

我收到错误:

File "<ipython-input-2-5252a711a145>", line 1, in <module>
    runfile('/Users/shiyanpei/Documents/embeddings/infogan/test/run.py', wdir='/Users/shiyanpei/Documents/embeddings/infogan/test')

  File "/Users/shiyanpei/Applications/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 692, in runfile
    execfile(filename, namespace)

  File "/Users/shiyanpei/Applications/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/shiyanpei/Documents/embeddings/infogan/test/run.py", line 28, in <module>
    model_num=model_num,print_every=print_every,show_every=show_every)

  File "/Users/shiyanpei/Documents/embeddings/infogan/test/VanillaInfoGAN.py", line 40, in __init__
    self.g_out = self.generator()

AttributeError: 'VanillaInfoGAN' object has no attribute 'generator'

我写了很多其他的类,但我还没有看到这个错误有人可以帮忙吗?谢谢!!

标签: pythonpython-3.x

解决方案


你的缩进是错误的:

class VanillaInfoGAN(object):
    def __init__(self,learning_rate,batch_size,num_epoches,z_dim,c_cat_dim,c_cont_dim,model_num,print_every=30,show_every=50,
                 store_path = './ckpt/',training_picture_path='./pictures/'):

        # a lot of lines skipped
        self.g_out = self.generator()
        # a lot of lines skipped

        def generator(self,reuse=False):
            with tf.variable_scope('generator',reuse=reuse):

Python 代码从上到下执行。def generator(self,reuse=False):调用时甚至不会执行self.generator. generator通过突出它来创建类的属性:

class VanillaInfoGAN(object):
    def __init__(self,learning_rate,batch_size,num_epoches,z_dim,c_cat_dim,c_cont_dim,model_num,print_every=30,show_every=50,
                 store_path = './ckpt/',training_picture_path='./pictures/'):

        # a lot of line skipped
        self.g_out = self.generator()
        # a lot of line skipped

    def generator(self,reuse=False):
        with tf.variable_scope('generator',reuse=reuse):

当你在做的时候,也要强烈考虑将代码拆分成多个类和方法;它的过度复杂性使得它很容易犯错误。


推荐阅读