首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python类找不到其函数

Python类找不到其函数
EN

Stack Overflow用户
提问于 2018-08-20 07:30:45
回答 1查看 48关注 0票数 -1

我写了一个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

下面是我用来创建对象的运行中的warpper:

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'

我已经写了很多其他的类,但是我还没有看到这个错误,有谁可以帮助我吗?谢谢!!

EN

回答 1

Stack Overflow用户

发布于 2018-08-20 07:39:08

缩进错误:

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代码从上到下执行。当您调用self.generator时,甚至不会执行def generator(self,reuse=False):。使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):

同时,还要强烈地考虑将代码拆分成多个类和方法;它过于复杂,很容易出错。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51922614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档