Grokbase Groups Perl ai August 2004
FAQ
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.

Search Discussions

  • Ken Williams at Sep 1, 2004 at 3:02 am
    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
    lists wrote:
    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.
  • Greggallen at Dec 7, 2006 at 10:45 pm
    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 at Dec 8, 2006 at 2:38 pm
    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

    greggallen@gmail.com wrote:
    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.

  • Benjamin Tucker at Dec 8, 2006 at 3:13 pm
    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
    On Dec 8, 2006, at 9:37 AM, Brad Larsen wrote:

    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

    greggallen@gmail.com wrote:
    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.
  • Ala Qumsieh at Dec 8, 2006 at 3:56 pm

    --- Benjamin Tucker 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 at Dec 9, 2006 at 12:12 am
    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.



    On Dec 8, 2006, at 8:56 AM, Ala Qumsieh wrote:


    --- Benjamin Tucker 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupai @
categoriesperl
postedAug 31, '04 at 11:12a
activeDec 9, '06 at 12:12a
posts7
users6
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase