How to get predictions and calculate accuracy for a given test set in fast ai?

2024/10/13 7:27:24

I'm trying to load a learner which was exported by learn.export() and I want to run it against a test set. I want my test set have labels so that I can measure its accuracy.

This is my code:

test_src = (TextList.from_df(df, path, cols='texts').split_by_rand_pct(0.1, seed=42).label_from_df(cols='recommend'))learn_fwd = load_learner(path + '/fwd_learn_c', test=test_src) #, tfm_y=False)pred_fwd,lbl_fwd = learn_fwd.get_preds(ds_type=DatasetType.Test,ordered=True) 
accuracy(pred_fwd, lbl_fwd)

And I got the following error, which apparently doesn't accept a labeled data set!!

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-7f52f2136d8e> in <module>6 7 learn_fwd = load_learner(path + '/fwd_learn_c', 
----> 8                          test=test_src) #, tfm_y=False)9 learn_bwd = load_learner(path + '/bwd_learn_c',10                          test=test_src) #, tfm_y=test_src)~/miniconda3/lib/python3.7/site-packages/fastai/basic_train.py in load_learner(path, file, test, tfm_y, **db_kwargs)622     model = state.pop('model')623     src = LabelLists.load_state(path, state.pop('data'))
--> 624     if test is not None: src.add_test(test, tfm_y=tfm_y)625     data = src.databunch(**db_kwargs)626     cb_state = state.pop('cb_state')~/miniconda3/lib/python3.7/site-packages/fastai/data_block.py in add_test(self, items, label, tfms, tfm_y)562         "Add test set containing `items` with an arbitrary `label`."563         # if no label passed, use label of first training item
--> 564         if label is None: labels = EmptyLabelList([0] * len(items))565         else: labels = self.valid.y.new([label] * len(items)).process()566         if isinstance(items, MixedItemList): items = self.valid.x.new(items.item_lists, inner_df=items.inner_df).process()TypeError: object of type 'LabelLists' has no len()
Answer

It seems that for the test set, it just accepts an ItemList (without lables). In the above example, I passed a LabelList to it which is the source of error. Anyway to get the accuracy for a test set I found the following solution:

# Create your test set:
data_test = (TextList.from_df(df, path, cols='texts').split_by_rand_pct(0.1, seed=42).label_from_df(cols='recommend'))data_test.valid = data_test.train
data_test=data_test.databunch()# Set the validation set of the learner by the test data you created
learn.data.valid_dl = data_test.valid_dl# Now y refers to the actual labels in the data set
preds, y = learn.get_preds(ds_type=DatasetType.Valid)
acc = accuracy(preds, y)# Alternatively you can call validate if you don't want the predictionsacc = learn.validate()[1]
https://en.xdnf.cn/q/69446.html

Related Q&A

Splitting the legend in matploblib

Is it possible to split up a single big legend into multiple (usually 2) smaller ones.from pylab import *t = arange(0.0, 2.0, 0.01) s = sin(2*pi*t) plot(t, s, linewidth=1.0, label="Graph1") g…

Python 3.x - iloc throws error - single positional indexer is out-of-bounds

I am scraping election data from a website and trying to store it in a dataframe import pandas as pd import bs4 import requestscolumns = [Candidate,Party,Criminal Cases,Education,Age,Total Assets,Liabi…

Supposed automatically threaded scipy and numpy functions arent making use of multiple cores

I am running Mac OS X 10.6.8 and am using the Enthought Python Distribution. I want for numpy functions to take advantage of both my cores. I am having a problem similar to that of this post: multithre…

Golang net.Listen binds to port thats already in use

Port 8888 is already bound on my (OS X 10.13.5) system, by a process running inside a docker container:$ netstat -an | grep 8888 tcp6 0 0 ::1.8888 *.* LISTE…

Aiohttp, Asyncio: RuntimeError: Event loop is closed

I have two scripts, scraper.py and db_control.py. In scraper.py I have something like this: ... def scrape(category, field, pages, search, use_proxy, proxy_file):...loop = asyncio.get_event_loop()to_do…

Python for ios interpreter [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Python or Ruby Interpreter on iOS I just discovered this apps pypad and python for ios They have like an interpreter an ed…

Detect when multiprocessing queue is empty and closed

Lets say I have two processes: a reader and a writer. How does the writer detect when the reader has finished writing values?The multiprocessing module has a queue with a close method that seems custo…

imshow and histogram2d: cant get them to work

Im learning Python and this is my first question here. Ive read other topics related to the usage of imshow but didnt find anything useful. Sorry for my bad English.I have plotted a set of points here,…

3D Waterfall Plot with Colored Heights

Im trying to visualise a dataset in 3D which consists of a time series (along y) of x-z data, using Python and Matplotlib.Id like to create a plot like the one below (which was made in Python: http://a…

python xlsxwriter: Keep header in excel when adding a table

I have a panda dataframe that I write to a xslx file, and would like to add a table over that data. I would also like to keep the headers that I have already written, instead of adding them again. Is t…