'CifarData' object has no attribute '_data'

来源:2-7 神经元实现(二分类逻辑斯蒂回归模型实现)

算法工程大神

2021-05-11


AttributeError Traceback (most recent call last)
in
20 all_test_acc_val=[]
21 for j in range (test_steps):
—> 22 test_batch_data,test_batch_labels =test_data.next_batch(batch_size)
23 test_acc_val=sess.run(
24 [accuracy],feed_dict=

in next_batch(self, batch_size)
46 if end_indicator>self._num_examples:
47 raise Exception(“batch_size is larger than all examples”)
—> 48 batch_data=self._data[self.indicator: end_indicator]
49 batch_labels=self._labels[self.indicator: end_indicator]
50 self.indicator=end_indicator

AttributeError: ‘CifarData’ object has no attribute '_data

def load_data(filenames):
""“read data from data file”""
with open(filenames,‘rb’) as f:
data=pk.load(f,encoding=‘bytes’)
return data[b’data’],data[b’labels’]

class CifarData:
def init(self,filenames,need_shuffle):
all_data=[]
all_labels=[]
for filename in filenames:
data,labels=load_data(filename)
for item,label in zip(data,labels):
if label in [0,1]:
all_data.append(item)
all_labels.append(label)
self.data=np.vstack(all_data)
self.data=self.data/127.5-1
self.labels=np.hstack(all_labels)
print(self.data.shape)
print(self.labels.shape)
self._num_examples=self.data.shape[0]
self._need_shuffle=need_shuffle
self.indicator=0
if self._need_shuffle:
self._shuffle_data()

def _shuffle_data(self):
    p=np.random.permutation(self._num_examples)
    self._data=self.data[p]
    self._labels=self.labels[p]

def next_batch(self,batch_size):
    """return batch_size examples as batch"""
    end_indicator = self.indicator + batch_size
    if end_indicator>self._num_examples:
        if self._need_shuffle:
            self._shuffle_data()
            self.indicator=0
            end_indicator=batch_size
        else:
            raise Exception("have no more examples")
        
        
    if end_indicator>self._num_examples:
        raise Exception("batch_size is larger than all examples")
    batch_data=self._data[self.indicator: end_indicator]
    batch_labels=self._labels[self.indicator: end_indicator]
    self.indicator=end_indicator
    return batch_data,batch_labels

train_filenames=[os.path.join(CIFAR_DIR,‘data_batch_%d’ % i)for i in range(1,6)]
test_filenames=[os.path.join(CIFAR_DIR,‘test_batch’)]

train_data=CifarData(train_filenames,True)
test_data=CifarData(test_filenames,False)

batch_data,batch_labels=train_data.next_batch(10)
print(batch_data)
print(batch_labels)

init=tf.global_variables_initializer()
batch_size=20
train_steps=100000
test_steps=100
with tf.Session() as sess:
sess.run(init)
for i in range(train_steps):
batch_data,batch_labels=train_data.next_batch(batch_size)
loss_val,acc_val,_=sess.run(
[loss,accuracy,train_op],
feed_dict={
x: batch_data,
y: batch_labels})
if (i+1)%500==0:
print(’[Train] Step: %d,loss: %4.5f,acc: %4.5f’
% (i+1, loss_val,acc_val) )

    if (i+1)%5000==0:
        test_data=CifarData(test_filenames,False)
        all_test_acc_val=[]
        for j in range (test_steps):
            test_batch_data,test_batch_labels =test_data.next_batch(batch_size)
            test_acc_val=sess.run(
                [accuracy],feed_dict=
                {
                    x: test_batch_data,
                    y: test_batch_labels
                })
            all_test_acc_val.append(test_acc_val)
            test_acc=np.mean(all_test_acc_val)
            print('[Test]Step: %d,acc: %4.5f' % (i+1,test_acc))
写回答

1回答

正十七

2021-05-12

同学你好,可以直接参考咱们的源码,看看你的code不同点在哪里:https://git.imooc.com/coding-259/coding-259/src/master/py3/02-nn-basic/neuron.ipynb

0
0

深度学习之神经网络(CNN/RNN/GAN)算法原理+实战

深度学习算法工程师必学,深入理解深度学习核心算法CNN RNN GAN

2617 学习 · 935 问题

查看课程