简体   繁体   中英

Python - AttributeError: 'dict' object has no attribute 'train'

I have this error: AttributeError: 'dict' object has no attribute 'train' in my code below :

def main(unused_argv):
# Load training and eval data
image_ind = 10
svhn = sio.loadmat('train_32x32.mat')
# access to the dict
x_train = svhn['X']
y_train = svhn['y']
# show sample
plt.imshow(x_train[:,:,:,image_ind])
print(y_train[image_ind])

train_data = svhn.train.images  # Returns np.array
train_labels = np.asarray(svhn.train.labels, dtype=np.int32)

eval_data = sio.loadmat('test_32x32.mat')
# access to the dict
x_test = eval_data['X']
y_test = eval_data['y']
eval_dataSVHN = eval_data.test.images  # Returns np.array
eval_labels = np.asarray(eval_data.test.labels, dtype=np.int32)

when executing, I am getting the error:

train_data = svhn.train.images  # Returns np.array
AttributeError: 'dict' object has no attribute 'train'

How can I fix this?

I don't see anything wrong... scipy.io.loadmat returns a dictionary , and base dictionaries don't have a "train" attribute. If there is a "train" variable in the matlab file, then it will be stored as dictionary key, so you can access it as svhn['train'] (as opposed to svhn.train ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM