Error arising during implementing BERT on classifying text problem - matlab

Recently Matlab has launched BERT in their git repository. There's an example of classifying text data using BERT. I am trying to run this code as it is.
Here, I am struck at a point where data fails to partitioned into test data.
The code block I am trying is following:
%%
mdl = bert;
%%
tokenizer = mdl.Tokenizer
%%
filename = "factoryReports.csv";
data = readtable(filename,"TextType","string");
head(data)
%%
data.Category = categorical(data.Category);
classes = categories(data.Category);
numClasses = numel(classes)
%%
data.Tokens = encode(tokenizer, data.Description);
%%
cvp = cvpartition(data.Category,"Holdout",0.2);
dataTrain = data(training(cvp),:);
dataValidation = data(test(cvp),:);
The error is showing on the last line of the execution and the error is following:
Unable to use a value of type cvpartition as an index.
Error in Untitled (line 18)
dataValidation = data(test(cvp),:);
What might be the reason of this and how to resolve this issue? I am looking for your advice.

Try closing Matlab and opening it again. It worked for me.

Related

How to get the audioread.m file again in the matlab?

I have edited the audioread.m file
I have used
which('audioread', '-all')
rmpath(fileparts(which('audioread')))
savepath
Now I have got an error
Undefined function or variable 'audioread'.
I have checked with
[d,sr] = audioread('sadness22.wav');
% Plot the spectrogram
subplot(311)
specgram(d(:,1),1024,sr);
% Read in a different format
[d2,sr] = audioread('piano.mp3');
subplot(312)
specgram(d2(:,1),1024,sr);
Can you please give any suggestions regarding this issue?

Loading saved cv.DescriptorMatcher in MATLAB (mexopencv)

I have recently started working in mexopencv and I am new to it. However, I have made a database of keypoints and features of images for ORB and SURF and saved it in a .mat file along with the trained matchers.
Matcher used:
FLANN based matcher (for SURF points)
BruteForce - Hamming (for ORB points)
It works fine if I train the data in the same script and try to find the image from the databse. However, when I try to find the image from the database in another script by loading the keypoints data and the matcher, it fails.
The code for making database is:
%% OPTIONS
SURF_Threshold = 40000;
%% SETUP
SURF = cv.SURF('HessianThreshold', SURF_Threshold);
ORB = cv.ORB;
opts = {'KDTree', 'Trees',5}; %Dictionary required for FLANN
matcherFLANN = cv.DescriptorMatcher('FlannBasedMatcher', 'Index',opts);
matcherBF = cv.DescriptorMatcher('BruteForce-Hamming');
foldername = uigetdir;
oldFolder = cd(foldername);
files = [dir('*.jpg');dir('*.png')]; %Look for jpg files in the directory.
N = size(files,1); %numel the files
%Create empty training sets
trainDataORB = struct('name', cell(N,1), 'pts',cell(N,1), 'feat',cell(N,1));
trainDataSURF = struct('name', cell(N,1), 'pts',cell(N,1), 'feat',cell(N,1));
%% TRAINING
for i=1:N
% read image
img = imread((files(i).name));
%Extracting keypoints and descriptors (features) for both
[trainDataSURF(i).pts, trainDataSURF(i).feat] = SURF.detectAndCompute(img);
[trainDataORB(i).pts, trainDataORB(i).feat] = SURF.detectAndCompute(img);
trainDataSURF(i).name = files(i).name;
trainDataORB(i).name = files(i).name;
% add to training set to match against for each of descriptor
matcherFLANN.add(trainDataSURF(i).feat);
matcherBF.add(trainDataORB(i).feat);
end
% build index
matcherFLANN.train();
matcherBF.train();
%% SAVING DATA
%Navigate back to old folder
cd(oldFolder);
save('training_data.mat', 'matcherFLANN', 'matcherBF', 'trainDataSURF','trainDataORB');
The matcher objects are then loaded with in another scrip (with cleared workspace):
load('training_data.mat');
Which returns the error:
Error using DescriptorMatcher_ Object not found id=109
Error in cv.DescriptorMatcher/match (line 461)
matches = DescriptorMatcher_(this.id, 'match', queryDescriptors, varargin{:});
Error in detection_Test (line 27)
matches = matcherFLANN.match(feat);
Any help would be gladly welcomed.

Matlab: Working for-loop breaks in parfor while fitting curves

Hoping you may be able to assist me with this error. I am running some code to fit curves to ages using a cross validation regime. I iterate the curve fitting 1000 times to assess the best fit.
I define my models as:
linear_ft = fittype({'x', '1'});
monotonic_ft= fittype({'-1/x', '1'});
quadratic_ft = fittype('poly2');
I then run the following to iterate through different selections of data splitting, recording the residuals following the curve fit...
Data = randn(4,300,10,10);
Ages = randn(300,1);
for thisDim1 = 1:4
for thisDim2 = 1:10
for thisDim3 = 1:10
for nIts = 1:1000
RandomOrder = randperm(300,300);
Fit_Subs = RandomOrder(1:length(Ages)/2); % Take random subs to fit to
Test_Subs = RandomOrder(length(Ages)/2+1:300); % Take random subs to test fit to
Fit_Data = squeeze(Data(thisDim1,Fit_Subs,thisDim2,thisDim3)); % Take data to fit to
Test_Data = squeeze(Data(thisDim1,Test_Subs,thisDim2,thisDim3)); % Take data to test fit
Fit_Ages = Ages;
Fit_Ages(Fit_Subs) = []; %Take ages of Fit Subs only
Test_Ages = Ages;
Test_Ages(Test_Subs) = []; % Take ages of Test Subs only
Nsubs = (length(Ages)/2);
% Model Data using Curves
fFit_Lin = fit(Fit_Ages,Fit_Data',linear_ft);
fFit_Mon = fit(Fit_Ages,Fit_Data',monotonic_ft);
fFit_Quad = fit(Fit_Ages,Fit_Data',quadratic_ft);
% Fit Modelled Data to Test Data
tFit_Lin = fFit_Lin(Test_Ages);
tFit_Mon = fFit_Mon(Test_Ages);
tFit_Quad = fFit_Quad(Test_Ages);
% Calculate Median Residual
Lin_Med_Resid(nIts) = median(tFit_Lin - Test_Data');
Mon_Med_Resid(nIts) = median(tFit_Mon - Test_Data');
Quad_Med_Resid(nIts) = median(tFit_Quad - Test_Data');
end
end
end
end
If you run this with the fourth loop (nIts) as a for-loop it will run. If you run it as a parfor-loop it won't stating the error:
Error using fit>iFit (line 264)
The name 'lower' is not an accessible property for an instance of class
'llsqoptions'.
Error in fit (line 108) [fitobj, goodness, output, convmsg] = iFit(
xdatain, ydatain, fittypeobj, ...
Does anyone have any idea how to fix this? I would be most grateful for any advice!!
Thanks,
Ben
Try restarting MATLAB or typing clear all to see if it clears things up for you.
Your code works for me, but the parallel toolbox can be a bit finicky in my experience.

Issue with parfor in Matlab; "UndefinedFunction error thrown to workers" and Matlab crashing

I've got some code that works perfectly fine in series, but I'm trying to speed it up by putting it in parallel, and it's giving me fits in multiple places. Here's my code:
parfor q = 1:num_ranges;
timenumber = squeeze(DATA(q,:,:));
timenumber_shift = circshift(timenumber, [0 1]);
for m = 1:total_working_channels;
timenumberm = timenumber(m,:);
for n = 1:total_working_channels;
R_P(m,n,q) = mean(timenumberm.*conj(timenumber(n,:)),2);
R_V(m,n,q) = mean(timenumberm.*conj(timenumber_shift(n,:)),2);
end
end
end
DATA is a complex double of size 1716x32x400. When I try running it like this, Matlab just crashes ("Matlab has encountered an internal problem and needs to close"). Is the array too big to be sent? I have a feeling that that's the issue, but I'm not sure.
To get around that, I've tried changing the location of the parfor command; here's attempt #2:
for q = 1:num_ranges;
timenumber = squeeze(DATA(q,:,:));
timenumber_shift = circshift(timenumber, [0 1]);
parfor m = 1:total_working_channels; %this is line 145!
timenumberm = timenumber(m,:);
for n = 1:total_working_channels;
R_P(m,n,q) = mean(timenumberm.*conj(timenumber(n,:)),2);
R_V(m,n,q) = mean(timenumberm.*conj(timenumber_shift(n,:)),2);
end
end
end
When I do this, Matlab doesn't crash. However, in the command line, it says "Analyzing and transferring files to workers...done.", followed by an error. Here's the error message:
Error using DBFCode12 (line 145). An UndefinedFunction error was thrown on the workers for ''. This might be because the file containing '' is not accessible on the
workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for
'parallel.Pool/addAttachedFiles' for more details. Caused by: Undefined function or variable "".
I'm pretty well stumped on this one. Why is it saying there's an undefined function (with no name!) on the parfor line?
I'm using version R2014a.

MATLAB - error using imtransform()

I got an error when trying to run the imtransform() line in my code. I got:
Error using imtransform>parse_inputs (line 438)
XData and YData could not be automatically determined.
Try specifying XData and YData explicitly in the call to
IMTRANSFORM.
Error in imtransform (line 265)
args = parse_inputs(varargin{:});
Error in main (line 9)
newImage = imtransform(rgbImage,tf); %# Transform the image
I used the function tf = maketform() to generate the transformation itself.
So, in order to figure out what wrong with my code i simplified as much as I can the function:
function U = fisheye_inverse(X)
U = X;
end
and my code looked like this:
rgbImage = imread('IMG-20150622-WA0046.jpg'); %# Read the image
tf = maketform('custom',2,2,[],... %# Make the transformation structure
#fisheye_inverse,options);
newImage = imtransform(rgbImage,tf); %# Transform the image
imshow(newImage);
and still the code yell at me this error again.
I looked at a similar problem Here that posted here in the past, but no one answer the question in the end.
Thanks in advance, Gal :)