[英]Python building object instance: object() takes no parameters
I'm writing a tensorflow program similar to their MNIST LSTM example code. 我正在写一个类似于他们的MNIST LSTM示例代码的tensorflow程序。 I'm building my data file, and I am unable to build an object instance.
我正在构建数据文件,但是无法构建对象实例。
Essentially the process goes: define an empyty instance data_sets = DataSet()
and then build the object data_sets.train = DataSet(arg1, arg2...)
and data_sets.test = DataSet(arg1, arg2...)
and so on 从本质上讲,该过程是这样的:定义一个Emptyty实例
data_sets = DataSet()
,然后构建对象data_sets.train = DataSet(arg1, arg2...)
和data_sets.test = DataSet(arg1, arg2...)
等
I'm getting the error (exact error at the bottom) when I try to build data_sets.train = DataSet(arg1, arg2...)
当我尝试构建
data_sets.train = DataSet(arg1, arg2...)
时出现错误(底部完全错误data_sets.train = DataSet(arg1, arg2...)
The MNIST code looks like this: MNIST代码如下所示:
class DataSet(object):
def __init__(self, images, labels, fake_data=False, one_hot=False,
dtype=tf.float32):
"""Construct a DataSet.
one_hot arg is used only if fake_data is true. `dtype` can be either
`uint8` to leave the input as `[0, 255]`, or `float32` to rescale into
`[0, 1]`.
"""
dtype = tf.as_dtype(dtype).base_dtype
#pdb.set_trace()
if dtype not in (tf.uint8, tf.float32):
raise TypeError('Invalid image dtype %r, expected uint8 or float32' %
dtype)
if fake_data:
self._num_examples = 10000
self.one_hot = one_hot
else:
pdb.set_trace()
assert images.shape[0] == labels.shape[0], (
'images.shape: %s labels.shape: %s' % (images.shape,
labels.shape))
self._num_examples = images.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
assert images.shape[3] == 1
images = images.reshape(images.shape[0],
images.shape[1] * images.shape[2])
if dtype == tf.float32:
# Convert from [0, 255] -> [0.0, 1.0].
images = images.astype(numpy.float32)
images = numpy.multiply(images, 1.0 / 255.0)
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
So then in the same file they have a function that defines an instance without arguments (after a pass
), builds the dataset (I have left that part out), and then builds the object data_set
with data_set.train, data_set.validation, and data_set.test
-- each time they call the class constructor again, but this time they include arguments. 所以后来在同一文件中,他们有一个定义(一个后无参数的实例函数
pass
),建立数据集(我已经离开的那部分),然后生成对象data_set
与data_set.train, data_set.validation, and data_set.test
每次他们再次调用类构造函数时,但这一次它们包含参数。 As shown below 如下所示
def read_data_sets(train_dir, fake_data=False, one_hot=False, dtype=tf.float32):
class DataSets(object):
pass
pdb.set_trace()
data_sets = DataSets()
...(build dataset)...
data_sets.train = DataSet(train_images, train_labels, dtype=dtype)
data_sets.validation = DataSet(validation_images, validation_labels,
dtype=dtype)
data_sets.test = DataSet(test_images, test_labels, dtype=dtype)
pdb.set_trace()
return data_sets
I have essentially built the exact same thing, but with a different dataset 我基本上建立了完全相同的东西,但是使用了不同的数据集
Here is my class definition (ignore tabs, copy&paste messed up identation -- I don't think indentation is the problem) 这是我的类定义(忽略标签,复制和粘贴弄乱了标识-我认为缩进不是问题)
class ScrollData(object):
def __init__(self, images, labels, dtype=tf.float32):
dtype = tf.as_dtype(dtype).base_dtype
if dtype not in (tf.float64, tf.float32):
raise TypeError('Invalid image dtype %r, expected float64 or float32' %
dtype)
assert images.shape[0] == labels.shape[0], (
'images.shape: %s labels.shape: %s' % (images.shape,
labels.shape))
self._num_examples = images.shape[0]
pdb.set_trace()
assert images.shape[3] == 1
images = images.reshape(images.shape[0],
images.shape[1] * images.shape[2])
if dtype == tf.float32:
# Convert from [0, 255] -> [0.0, 1.0].
images = images.astype(numpy.float32)
images = numpy.multiply(images, 1.0 / 255.0)
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
And then I build the object in the following method: 然后,我通过以下方法构建对象:
def read_data(data_dir):
dtype=tf.float32
VALIDATION_SIZE = 1
TEST_SIZE = 1
class ScrollData(object):
pass
data_sets = ScrollData()
...(build dataset)...
data_sets.train = ScrollData(train_images, train_labels, dtype=tf.float32)
data_sets.validation = ScrollData(validation_images, validation_labels, dtype=tf.float32)
data_sets.test = ScrollData(testtest_images, test_labels, dtype=tf.float32)
return data_sets
I'm getting the following error: 我收到以下错误:
data_sets.train = ScrollData(train_images, train_labels, dtype=tf.float32)
TypeError: object() takes no parameters
You overwrite ScrollData
within you read_data
function with a class which doesn't take constructor arguments. 您可以使用不带构造函数参数的类覆盖
read_data
函数中的ScrollData
。
leave this redefinition out and either add arguments to the first call or define standard values in the constructor 保留此重新定义,并在第一个调用中添加参数或在构造函数中定义标准值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.