Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AI::Categorizer restore problem

2 views
Skip to first unread message

PerlDiscuss - Perl Newsgroups and mailing lists

unread,
Aug 31, 2004, 7:12:08 AM8/31/04
to per...@perl.org
Hi folks,

I'm trying to implement AI:Categorizer. Everything works fine
until I try to save and restore the state of the learner.

From the synopsis...

my $nb = new
AI::Categorizer::Learner::NaiveBayes(...parameters...);
$nb->train(knowledge_set => $k);
$nb->save_state ('filename');

--- later ----

$nb =
AI::Categorizer::Learner::NaiveBayes->restore_state('filename');
my $c = new AI::Categorizer::Collection::Files( path => ... );
while (my $document = $c->next) {
my $hypothesis = $nb->categorize($document);
print "Best assigned category: ",
$hypothesis->best_category, "\n";
print "All assigned categories: ", join(', ',
$hypothesis->categories), "\n";

However when the line
my $hypothesis = $nb->categorize($document);
is executed I get the following error.

Can't locate object method "predict" via package
"Algorithm::NaiveBayes::Model::Frequency"
at /usr/lib/perl5/site_perl/5.8.3/AI/Categorizer/Learner/NaiveBayes.pm
line 28.

It seems like inheritance is lost between save and restore
states. Any ideas?

Thanks in advance.

PS: CC to email highly appreciated.

Ken Williams

unread,
Aug 31, 2004, 11:02:29 PM8/31/04
to PerlDiscuss - Perl Newsgroups and mailing lists, per...@perl.org
Hi PerlDiscuss - Perl Newsgroups and mailing lists,

It's not really inheritance that's lost, it's the actual module code.
You should be able to solve this by just adding a 'use
Algorithm::NaiveBayes::Model::Frequency;' to the second script.

Or if you don't want to hard-code that, you can do:

$nb = AI::Categorizer::Learner::NaiveBayes->restore_state('filename');

eval( 'use ' . ref($nb) );

To solve this correctly, I think I'd have to add this to the
restore_state() method in the NaiveBayes learner class.

-Ken


On Aug 31, 2004, at 6:12 AM, PerlDiscuss - Perl Newsgroups and mailing

gregg...@gmail.com

unread,
Dec 7, 2006, 5:45:30 PM12/7/06
to per...@perl.org

I know this is going to turn out to be a stupid question, but could
someone tell me the easiest way to store and retrieve the state of
the entire AI::Genetic colony, and parameters, to a disk file so it
can be read in and out at will?

I'm doing some constrained optimization experiments that can take
several days, even a week, to run in the background, but I have a
computer (Mac OS X 10.4.8) that is shared, and I need to install
software and restart it almost daily.

I would like to save the entire thing about every hour, but I can
handle the timing part myself.


Sincerely,

Gregg Allen
Cerebra, Inc.


Brad Larsen

unread,
Dec 8, 2006, 9:37:47 AM12/8/06
to gregg...@gmail.com, per...@perl.org
One (possibly stupid) suggestion is to look at Data::Dumper. It should
work, but may be very slow if the object in question is large. Let us
know if you find anything better.

Cheers,
Brad Larsen

Benjamin Tucker

unread,
Dec 8, 2006, 10:13:17 AM12/8/06
to gregg...@gmail.com, per...@perl.org
I don't actually have any experience with AI::Genetic, but
Storable.pm is probably your best bet. Take a look at how
AI::Categorizer interfaces with it:
http://search.cpan.org/src/KWILLIAMS/AI-Categorizer-0.07/lib/AI/
Categorizer/Storable.pm

If you throw something like this into the bottom of one of your perl
files, you should be able just to call
$gen->store_state('filename') and then $gen->restore_state
('filename') (where $gen is an instance of AI::Genetic)

package AI::Genetic;

use strict;
use Storable;
use File::Spec ();
use File::Path ();

sub save_state {
my ($self, $path) = @_;
if (-e $path) {
File::Path::rmtree($path) or die "Couldn't overwrite $path: $!";
}
mkdir($path, 0777) or die "Can't create $path: $!";
Storable::nstore($self, File::Spec->catfile($path, 'self'));
}

sub restore_state {
my ($package, $path) = @_;
return Storable::retrieve(File::Spec->catfile($path, 'self'));
}

1;

Ben

Ala Qumsieh

unread,
Dec 8, 2006, 10:56:16 AM12/8/06
to Benjamin Tucker, gregg...@gmail.com, per...@perl.org

--- Benjamin Tucker <b...@greenriver.org> wrote:

> I don't actually have any experience with
> AI::Genetic, but
> Storable.pm is probably your best bet. Take a look
> at how
> AI::Categorizer interfaces with it:
>
http://search.cpan.org/src/KWILLIAMS/AI-Categorizer-0.07/lib/AI/
>
> Categorizer/Storable.pm
>
> If you throw something like this into the bottom of
> one of your perl
> files, you should be able just to call
> $gen->store_state('filename') and then
> $gen->restore_state
> ('filename') (where $gen is an instance of
> AI::Genetic)

[snip code]

Thanks. That's an excellent suggestion. I'll add that
to AI::Genetic and upload a new version soon.

Thanks,
--Ala


____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

Gregg Allen

unread,
Dec 8, 2006, 7:11:51 PM12/8/06
to Ala Qumsieh, Benjamin Tucker, per...@perl.org
Awesome! That helped a lot. I'm looking forward to your new and
improved module.

Thanks for creating that module, in the first place, also. When I
first discovered the module about a year ago, I ran about a dozen
random optimization problems from my graduate level operations
research textbook from 25 years ago.

I didn't find a single one it couldn't solve in less than a few
minutes. (Mere seconds in most cases.)

Thanks!

Gregg Allen
Cerebra, Inc.

0 new messages