我写了一个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'
我写了许多其他的类,但是我还没有看到这个错误 有人可以帮忙吗? 谢谢!
答案 0 :(得分:0)
您的缩进是错误的:
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):
在使用时,也强烈考虑将代码拆分为多个类和方法。它过于复杂,很容易犯错。