. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
Traceback (most recent call last):
File "C:\Users\nehad\Desktop\Neha\Non-School\Python\Handwritten Digits
Recognition.py", line 38, in <module>
X_train, y_train, X_test, y_test = load_dataset()
TypeError: cannot unpack non-iterable NoneType object
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “typeerror: cannot unpack non-iterable nonetype object” problem, pls let me know. Here is what I do:
import numpy as np
def load_dataset():
def download(filename, source="http://yaan.lecun.com/exdb/mnist/"):
print ("Downloading ",filename)
import urllib
urllib.urlretrieve(source+filename,filename)
import gzip
def load_mnist_images(filename):
if not os.path.exists(filename):
download(filename)
with gzip.open(filename,"rb") as f:
data=np.frombuffer(f.read(), np.uint8, offset=16)
data = data.reshape(-1,1,28,28)
return data/np.float32(256)
def load_mnist_labels(filename):
if not os.path.exists(filename):
download(filename)
with gzip.open(filename,"rb") as f:
data = np.frombuffer(f.read(), np.uint8, offset=8)
return data
X_train = load_mnist_images("train-images-idx3-ubyte.gz")
y_train = load_mnist_labels("train-labels-idx1-ubyte.gz")
X_test = load_mnist_images("t10k-images-idx3-ubyte.gz")
y_test = load_mnist_labels("t10k-labels-idx1-ubyte.gz")
return X_train, y_train, X_test, y_test
X_train, y_train, X_test, y_test = load_dataset()
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
plt.show(plt.imshow(X_train[3][0]))
Thanks!
The cause: You get this error because
X_train, y_train, X_test, y_test
are defined inside in yourload_mnist_images
function and hence are not declared in yourload_dataset
function.Solution: De-indent your 5 lines from
X_train = ...
toreturn X_train, ...
to fix this error.This error occurs when you attempt to perform multiple assignments to
None
(which, by the way, isNoneType
). Take this example:If you receive this, it is likely that you are wrong about the right-hand portion of the assignment. It is nothing.