How to unpack mnist dataset? - python

I want to unpack a pkl file from a MNIST dataset. I have used this code to do that:
import cPickle, gzip, numpy
# Load the dataset
f = gzip.open(’mnist.pkl.gz’, ’rb’)
train_set, valid_set, test_set = cPickle.load(f)
f.close()
But I am getting this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/gzip.py", line 34, in open
return GzipFile(filename, mode, compresslevel)
File "/usr/lib/python2.7/gzip.py", line 94, in __init__
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: 'mnist.pkl.gz'
The mnist.pkl.gz file is on the desktop. How to specify the path in the code?

Put /home/<username>/Desktop/ in front of mnist.pkl.gz or launch it with the terminal working directory being the Desktop.

Related

Getting filenotfound error when trying to open a h5 file

I have a h5 file that contains the "catvnoncat" dataset. when I try to run the following code, I get an error which I will include at the bottom. I have tried getting the dataset from three different sources to exclude to possibility of a corrupted file.
What I would like to know is what is causing the problem?
# My Code
import h5py
train_dataset = h5py.File('train_catvnoncat.h5', "r")
# The Error
Traceback (most recent call last):
File "f:\Downloads\Compressed\coursera-deep-learning-specialization-master_2\coursera-deep-learning-specialization-master\C1 - Neural Networks and Deep Learning\Week 2\Logistic Regression as a Neural Network\lr_utils.py", line 4, in <module>
train_dataset = h5py.File('train_catvnoncat.h5', "r")
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\h5py\_hl\files.py", line 507, in __init__
fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\h5py\_hl\files.py", line 220, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 106, in h5py.h5f.open
FileNotFoundError: [Errno 2] Unable to open file (unable to open file: name = 'train_catvnoncat.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Your code is looking in the current directory which is not where the file is.
Based on the error message, it looks like you are on windows. Is the file 'train_catvnoncat.h5' in your Downloads folder? Find that file on your system and copy the full path. You can then update this:
train_dataset = h5py.File('train_catvnoncat.h5', "r")
to something that contains the full path. Similar to this:
train_dataset = h5py.File('C:/Users/Moshen/Downloads/train_catvnoncat.h5', "r")
Pay attention to the slashes and make sure you update the file path to the actual value.

cannot read simple txt file in python

I am a beginner. I have a simple txt file which I need to read (using numpy). I have the program in the same directory as the .txt file.
I have checked the cwd and it's the right one. Also, I've written a text file in order to see if python wants to open that one - that file opens just fine.
import os
import numpy as np
np.loadtxt("test2.txt")
The code above gives me the error.
The code below works just fine.
import os
import numpy as np
x = np.array([1, 2, 3])
np.savetxt("test.txt", x)
y = np.loadtxt("test.txt")
print(y)
The error I get is:
Traceback (most recent call last):
File "D:\detest\admi.py", line 5, in <module>
np.loadtxt("test2.txt")
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\npyio.py", line 962, in loadtxt
fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\_datasource.py", line 266, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\Mircea\AppData\Roaming\Python\Python37\site-packages\numpy\lib\_datasource.py", line 624, in open
raise IOError("%s not found." % path)
OSError: test2.txt not found.
Can you use the Python read file instead?
path = '' # location of your file
openfile = open(path, 'r') # open file
openfile.read() # return all content of file
openfile.close() # close file

Pytorch Torch.save FileNotFoundError

When I try to call "torch.save" to save my model in a "tmp_file", it rises a FileNotFoundError. the trace back is as follow:
Traceback (most recent call last):
File “C:/Users/Haoran/Documents/GitHub/dose-response/python/simulations/hdr.py”, line 234, in
test_hdr_continuous()
File “C:/Users/Haoran/Documents/GitHub/dose-response/python/simulations/hdr.py”, line 195, in test_hdr_continuous
model = fit_mdn(X[:split], y[:split], nepochs=20)
File “C:\Users\Haoran\Documents\GitHub\dose-response\python\simulations\continuous.py”, line 192, in fit_mdn
torch.save(model, tmp_file)
File “C:\Users\Haoran\Documents\GitHub\dose-response\python\venv\lib\site-packages\torch\serialization.py”, line 161, in save
return _with_file_like(f, “wb”, lambda f: _save(obj, f, pickle_module, pickle_protocol))
File “C:\Users\Haoran\Documents\GitHub\dose-response\python\venv\lib\site-packages\torch\serialization.py”, line 116, in _with_file_like
f = open(f, mode)
FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/tmp_file_4358f298-a1d9-4c81-9e44-db4d8f1b4319’
It is weird that everything works perfectly on my mac, but I got this error on my Windows desktop.
As shmee observed, you are trying to write to /tmp/[...] on a Windows machine. Therefore you get FileNotFoundError.
To make your code OS agnostic, you may find python's tempfile package useful, especially NamedTemporaryFile: this function creates a temporary file and returns its name, so you can access/use it in your program.

HDF4Error: SD (59): HDF Internal error

I am trying to download a hdf file and read it python as follows
from pyhdf import SD
file = open("temp.hdf", 'w')
ftp.retrbinary('RETR '+ filename, file.write)
file.close()
hdf=SD.SD('temp.hdf')
It works but soon after the I am getting the following Error:
Traceback (most recent call last):
File "<ipython-input-46-55805a9d569b>", line 6, in <module>
hdf=SD.SD('temp.hdf')
File "/usr/local/lib/python2.7/dist-packages/pyhdf/SD.py", line 1444, in __init__
_checkErr('SD', id, "cannot open %s" % path)
File "/usr/local/lib/python2.7/dist-packages/pyhdf/error.py", line 23, in _checkErr
raise HDF4Error(err)
HDF4Error: SD (59): HDF Internal error
You need to open the output file in binary mode:
file = open("temp.hdf", 'wb') # was 'w'
Better would be to use with to automatically close the file:
with open("temp.hdf", 'wb') as out:
ftp.retrbinary('RETR '+ filename, out.write)

IOError: [Errno 2] No such file or directory: 'Book1.xls'

Traceback (most recent call last):
File "C:/my_python/read1.py", line 3, in <module>
book = open_workbook('Book1.xls',on_demand=True)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 449, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 941, in biff2_8_load
f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'Book1.xls'
I am getting this error message in python while trying to open an excel sheet.....How can i solve this ?
Either make sure Book1.xls is in "C:\my_python\", which is also where your script is, or use the full path to where Bool1.xls is located. For example:
book = open_workbook('C:\\Users\\Jeeva\\Documents\\Book1.xls',on_demand=True)

Categories