Upload
flint
View
65
Download
0
Embed Size (px)
DESCRIPTION
Изкуствен интелект Упражнение № 10. спец. Информатика, ФМИ 200 4 /200 5. Машинно самообучение. Формиране на описания на понятия (хипотези): примери положителни отрицателни обучаваща извадка тестова извадка Обучение за: едно понятие много понятия. Машинно самообучение. - PowerPoint PPT Presentation
Citation preview
Изкуствен интелектИзкуствен интелект
Упражнение № 10
спец. Информатика, ФМИ
2004/2005
Машинно самообучениеМашинно самообучениеФормиране на описания на понятия
(хипотези): примери
• положителни• отрицателни
обучаваща извадка тестова извадка
Обучение за: едно понятие много понятия
Машинно самообучениеМашинно самообучениеЕзици за представяне на примерите и
хипотезите атрибут-стойност релационен език други
Език атрибут стойностЕзик атрибут стойностПредставяне на примерите:%example (Id, Class, AVS)%example (Id, Class, AVS)example(a4,mammal,[has_covering = hair, example(a4,mammal,[has_covering = hair,
milk = t, homeothermic = t, milk = t, homeothermic = t,
habitat = air, eggs = f, gills = habitat = air, eggs = f, gills = f]). f]).
example(a5,fish,[has_covering = scales, example(a5,fish,[has_covering = scales,
milk = f, homeothermic = f, milk = f, homeothermic = f,
habitat = sea, eggs = t, gills = habitat = sea, eggs = t, gills = t]). t]).
example(a6,reptile,[has_covering = scales, example(a6,reptile,[has_covering = scales,
milk = f, homeothermic = f, milk = f, homeothermic = f,
habitat = land, eggs = t, gills = habitat = land, eggs = t, gills = f]). f]).
Език атрибут стойностЕзик атрибут стойностПредставяне на хипотезите:
IF Ai=Vij & … & Ak=Vkm THEN Classp
…
IF As=Vst & … & An=Vnl THEN Classq
Типове атрибути и стойностиТипове атрибути и стойностиНоминалниЛинейниСтруктурни
learn(H):-learn(H):-
findall([Class|AVS], findall([Class|AVS],
example(_,Class,AVS),example(_,Class,AVS),
Ex),Ex),
lrn(Ex,H).lrn(Ex,H).
lrn(E,lrn(E,HH) :- ) :-
del([Cdel([Classlass|A],E,E1),|A],E,E1),
del([Cdel([Classlass|B],E1,E2),|B],E1,E2),
lgg(A,B,LGG,Clgg(A,B,LGG,Classlass),),
\+((member([C1|LGG],E2),\+((member([C1|LGG],E2),
\+(C\+(Classlass=C1))),=C1))),
lrn([[Clrn([[Classlass|LGG]|E2],H).|LGG]|E2],H).
lrn(Hlrn(H,H,H).).
lgg([],[],[],Clgg([],[],[],Classlass).).
lgg([A=Vlgg([A=V11|R|Rest1est1],[A=V],[A=V22|R|Rest2est2],],[A=H[A=H11|H|Hypyp],C],Classlass) :-) :-
lgg1(A,Vlgg1(A,V1,V21,V2,H,H11,C,Classlass),),
lgg(Rlgg(Rest1,Rest2est1,Rest2,H,Hypyp,C,Classlass).).
lgg1(_,X,Y,X,_):- X==Y,!.lgg1(_,X,Y,X,_):- X==Y,!.
lgg1(A,X,Y,_,lgg1(A,X,Y,_,CC):- ):-
X \== Y,X \== Y,
nominal(C,N),member(A,N).nominal(C,N),member(A,N).
lgg1(A,X,Y,H,C):-lgg1(A,X,Y,H,C):-
X \==Y,X \==Y,
linear(C,L),member(A,L),linear(C,L),member(A,L),
cover(X,Y,H).cover(X,Y,H).
lgg1(A,X,Y,H,C):-lgg1(A,X,Y,H,C):-
X \==Y,X \==Y,
struct(C,L),member(A,L),struct(C,L),member(A,L),
common_parent(X,Y,H,C).common_parent(X,Y,H,C).
cover(X,X,X):-!.cover(X,X,X):-!.cover(X,Y,[Min,Max]):-cover(X,Y,[Min,Max]):- atom(X),atom(Y),!,atom(X),atom(Y),!, min(X,Y,Min),min(X,Y,Min), max(X,Y,Max).max(X,Y,Max).cover(X,[Y1,Y2],[Min,Max]):-cover(X,[Y1,Y2],[Min,Max]):- atom(X),!,atom(X),!, min(X,Y1,Min),min(X,Y1,Min), max(X,Y2,Max).max(X,Y2,Max).cover([X1,X2],Y,[Min,Max]):-cover([X1,X2],Y,[Min,Max]):- atom(Y),!,atom(Y),!, min(X1,Y,Min),min(X1,Y,Min), max(X2,Y,Max).max(X2,Y,Max).cover([X1,X2],[Y1,Y2],[Min,Max]):-cover([X1,X2],[Y1,Y2],[Min,Max]):- min(X1,Y1,Min),min(X1,Y1,Min), max(X2,Y2,Max).max(X2,Y2,Max).
min(X,X,X):-!.min(X,X,X):-!.
min(X,Y,X):-X<Y,!.min(X,Y,X):-X<Y,!.
min(X,Y,Y):-X>Y.min(X,Y,Y):-X>Y.
max(X,X,X):-!.max(X,X,X):-!.
max(X,Y,X):-X>Y,!.max(X,Y,X):-X>Y,!.
max(X,Y,Y):-X<Y.max(X,Y,Y):-X<Y.
common_parent(X,Y,H,C):-common_parent(X,Y,H,C):-
parent(H,X,C),parent(H,X,C),
parent(H,Y,C).parent(H,Y,C).
parent(X,X,C).parent(X,X,C).
parent(X,Y,C):-isa(C,Y,X).parent(X,Y,C):-isa(C,Y,X).
parent(X,Y,C):-isa(C,Y,Z),parent(X,Z,C).parent(X,Y,C):-isa(C,Y,Z),parent(X,Z,C).
del(X,[X|T],T).
del(X,[Y|T],[Y|V]) :-
del(X,T,V).
member(X,[X|_]).
member(X,[_|T]) :-
member(X,T).
write_list([]).
write_list([H|Rest]):-
write(H),nl,
write_list(Rest).
ПримерПримерexample(a1,mammal,[has_covering = hair, example(a1,mammal,[has_covering = hair, milk = t, homeothermic = t, habitat = land, milk = t, homeothermic = t, habitat = land, eggs = f, gills = f]).eggs = f, gills = f]).
example(a2,mammal,[has_covering = none, example(a2,mammal,[has_covering = none, milk = t, homeothermic = t, habitat = sea, milk = t, homeothermic = t, habitat = sea, eggs = f, gills = f]). eggs = f, gills = f]).
example(a3,mammal,[has_covering = hair, example(a3,mammal,[has_covering = hair, milk = t, homeothermic = t, habitat = sea, milk = t, homeothermic = t, habitat = sea, eggs = t, gills = f]). eggs = t, gills = f]).
example(a4,mammal,[has_covering = hair, example(a4,mammal,[has_covering = hair, milk = t, homeothermic = t, habitat = air, milk = t, homeothermic = t, habitat = air, eggs = f, gills = f]). eggs = f, gills = f]).
example(a5,fish,[has_covering = scales, example(a5,fish,[has_covering = scales, milk = f, homeothermic = f, habitat = sea, milk = f, homeothermic = f, habitat = sea, eggs = t, gills = t]). eggs = t, gills = t]).
ПримерПримерexample(a6,reptile,[has_covering = scales, example(a6,reptile,[has_covering = scales, milk = f, homeothermic = f, habitat = land, milk = f, homeothermic = f, habitat = land, eggs = t, gills = f]). eggs = t, gills = f]).
example(a7,reptile,[has_covering = scales, example(a7,reptile,[has_covering = scales, milk = f, homeothermic = f, habitat = sea, milk = f, homeothermic = f, habitat = sea, eggs = t, gills = f]). eggs = t, gills = f]).
example(a8,bird,[has_covering = feathers, example(a8,bird,[has_covering = feathers, milk = f, homeothermic = t, habitat = air, milk = f, homeothermic = t, habitat = air, eggs = t, gills = f]). eggs = t, gills = f]).
example(a9,bird,[has_covering = feathers, example(a9,bird,[has_covering = feathers, milk = f, homeothermic = t, habitat = land, milk = f, homeothermic = t, habitat = land, eggs = t, gills = f]). eggs = t, gills = f]).
example(a10,amphibian,[has_covering = none, example(a10,amphibian,[has_covering = none, milk = f, homeothermic = f, habitat = land, milk = f, homeothermic = f, habitat = land, eggs = t, gills = f]).eggs = t, gills = f]).
ПримерПримерstruct(_,[]).struct(_,[]).
linear(_,[]).linear(_,[]).
nominal(_,[has_covering,milk,homeothermic,habitat,eggs,gills])nominal(_,[has_covering,milk,homeothermic,habitat,eggs,gills])
ПримерПример| ?- learn(H),write_list(H). | ?- learn(H),write_list(H). [bird,has_covering=feathers,milk=f,homeothermic=t,[bird,has_covering=feathers,milk=f,homeothermic=t,habitat=_3699,eggs=t,gills=f]habitat=_3699,eggs=t,gills=f]
[reptile,has_covering=scales,milk=f,[reptile,has_covering=scales,milk=f,homeothermic=f,habitat=_3039,eggs=t,gills=f]homeothermic=f,habitat=_3039,eggs=t,gills=f]
[mammal,has_covering=_2095,milk=t,homeothermic=t,[mammal,has_covering=_2095,milk=t,homeothermic=t,habitat=_2324,eggs=_2464,gills=f]habitat=_2324,eggs=_2464,gills=f]
[fish,has_covering=scales,milk=f,homeothermic=f,[fish,has_covering=scales,milk=f,homeothermic=f,habitat=sea,eggs=t,gills=t]habitat=sea,eggs=t,gills=t]
[amphibian,has_covering=none,milk=f,homeothermic=f,h[amphibian,has_covering=none,milk=f,homeothermic=f,habitat=land,eggs=t,gills=f]abitat=land,eggs=t,gills=f]
ПримерПримерexample(1,pos,[hs = octagon, bs = octagon, sm = no, example(1,pos,[hs = octagon, bs = octagon, sm = no,
ho = sword, jc = red, ti = yes]).ho = sword, jc = red, ti = yes]).
example(2,pos,[hs = octagon, bs = octagon, sm = example(2,pos,[hs = octagon, bs = octagon, sm = yes, ho = sword, jc = red, ti = yes]).yes, ho = sword, jc = red, ti = yes]).
example(3,neg,[hs = round, bs = square, sm = yes, example(3,neg,[hs = round, bs = square, sm = yes, ho = sword, jc = blue, ti = no]).ho = sword, jc = blue, ti = no]).
example(4,neg,[hs = round, bs = square, sm = yes, example(4,neg,[hs = round, bs = square, sm = yes, ho = flag, jc = green, ti = yes]).ho = flag, jc = green, ti = yes]).
example(5,pos,[hs = octagon, bs = round, sm = yes, example(5,pos,[hs = octagon, bs = round, sm = yes, ho = sword, jc = red, ti = yes]).ho = sword, jc = red, ti = yes]).
example(6,pos,[hs = square, bs = octagon, sm = example(6,pos,[hs = square, bs = octagon, sm = yes, ho = sword, jc = red, ti = yes]).yes, ho = sword, jc = red, ti = yes]).
example(7,pos,[hs = square, bs = square, sm = no, example(7,pos,[hs = square, bs = square, sm = no, ho = sword, jc = red, ti = yes]).ho = sword, jc = red, ti = yes]).
Машинно самообучениеМашинно самообучение| ?- learn(H),write_list(H). | ?- learn(H),write_list(H).
[neg,hs=octagon,bs=square,sm=yes,ho=_125218,jc=blue,[neg,hs=octagon,bs=square,sm=yes,ho=_125218,jc=blue,ti=no]ti=no]
[neg,hs=square,bs=round,sm=_123819,ho=_123946,jc=green,[neg,hs=square,bs=round,sm=_123819,ho=_123946,jc=green,ti=_124150]ti=_124150]
[neg,hs=octagon,bs=round,sm=_120834,ho=_120961,jc=yellow,[neg,hs=octagon,bs=round,sm=_120834,ho=_120961,jc=yellow,ti=no]ti=no]
[neg,hs=square,bs=octagon,sm=_116815,ho=_116942,jc=green,[neg,hs=square,bs=octagon,sm=_116815,ho=_116942,jc=green,ti=_117146]ti=_117146]
[neg,hs=square,bs=round,sm=_113808,ho=_113935,jc=yellow,[neg,hs=square,bs=round,sm=_113808,ho=_113935,jc=yellow,ti=no]ti=no]
[neg,hs=square,bs=round,sm=_111648,ho=_111775,jc=blue,[neg,hs=square,bs=round,sm=_111648,ho=_111775,jc=blue,ti=_111979]ti=_111979]
[neg,hs=round,bs=octagon,sm=_106498,ho=_106625,jc=yellow,[neg,hs=round,bs=octagon,sm=_106498,ho=_106625,jc=yellow,ti=_106829]ti=_106829]
[neg,hs=round,bs=octagon,sm=_101722,ho=_101849,jc=green,[neg,hs=round,bs=octagon,sm=_101722,ho=_101849,jc=green,ti=_102053]ti=_102053]
[neg,hs=round,bs=octagon,sm=_100472,ho=_100599,jc=blue,[neg,hs=round,bs=octagon,sm=_100472,ho=_100599,jc=blue,ti=_100803]ti=_100803]
[pos,hs=round,bs=round,sm=_93315,ho=_93442,jc=_93582,[pos,hs=round,bs=round,sm=_93315,ho=_93442,jc=_93582,ti=_93735]ti=_93735]
[pos,hs=square,bs=square,sm=_84522,ho=_84649,jc=_84789,[pos,hs=square,bs=square,sm=_84522,ho=_84649,jc=_84789,ti=_84942]ti=_84942]
[pos,hs=octagon,bs=octagon,sm=_70090,ho=_70217,jc=_70357,[pos,hs=octagon,bs=octagon,sm=_70090,ho=_70217,jc=_70357,ti=_70510]ti=_70510]
[neg,hs=round,bs=square,sm=_55652,ho=_55779,jc=yellow,[neg,hs=round,bs=square,sm=_55652,ho=_55779,jc=yellow,ti=_55983]ti=_55983]
[neg,hs=round,bs=square,sm=_51710,ho=_51837,jc=green,[neg,hs=round,bs=square,sm=_51710,ho=_51837,jc=green,ti=_52041]ti=_52041]
[neg,hs=round,bs=square,sm=_45417,ho=_45544,jc=blue,[neg,hs=round,bs=square,sm=_45417,ho=_45544,jc=blue,ti=_45748]ti=_45748]
[pos,hs=_41557,bs=_41658,sm=_41772,ho=_41899,jc=red,[pos,hs=_41557,bs=_41658,sm=_41772,ho=_41899,jc=red,ti=_42103]ti=_42103]
[neg,hs=octagon,bs=round,sm=no,ho=flag,jc=green,ti=no][neg,hs=octagon,bs=round,sm=no,ho=flag,jc=green,ti=no][neg,hs=square,bs=octagon,sm=yes,ho=balloon,jc=blue,ti=no][neg,hs=square,bs=octagon,sm=yes,ho=balloon,jc=blue,ti=no][neg,hs=square,bs=octagon,sm=no,ho=flag,jc=yellow,ti=yes][neg,hs=square,bs=octagon,sm=no,ho=flag,jc=yellow,ti=yes][neg,hs=octagon,bs=square,sm=no,ho=balloon,jc=yellow,ti=yes][neg,hs=octagon,bs=square,sm=no,ho=balloon,jc=yellow,ti=yes][neg,hs=octagon,bs=square,sm=no,ho=sword,jc=green,ti=no][neg,hs=octagon,bs=square,sm=no,ho=sword,jc=green,ti=no]
Език атрибут стойностЕзик атрибут стойност
attribute(size,[small,large]).attribute(size,[small,large]).
attribute(shape,[long,compact,other]).attribute(shape,[long,compact,other]).
attribute(holes,[none,1,2,3,many]).attribute(holes,[none,1,2,3,many]).
Език атрибут стойностЕзик атрибут стойност Представяне на примерите %example (Class, AVS)%example (Class, AVS)
example(example(nutnut,[,[size=small,shape=compact,holes=1]).size=small,shape=compact,holes=1]).
example(example(screwscrew,[,[size=small,shape=long,holes=none]).size=small,shape=long,holes=none]).
example(example(keykey,[,[size=small,shape=long,holes=1]).size=small,shape=long,holes=1]).
example(example(nutnut,[,[size=small,shape=compact,holes=1]).size=small,shape=compact,holes=1]).
example(example(keykey,[,[size=large,shape=long,holes=1]).size=large,shape=long,holes=1]).
example(example(screwscrew,[,[size=small,shape=compact,holes=none]).size=small,shape=compact,holes=none]).
example(example(nutnut,[,[size=small,shape=compact,holes=1]).size=small,shape=compact,holes=1]).
example(example(penpen,[,[size=large,shape=long,holes=none]).size=large,shape=long,holes=none]).
example(example(scissorsscissors,[,[size=large,shape=long,holes=2]).size=large,shape=long,holes=2]).
example(example(penpen,[,[size=large,shape=long,holes=none]).size=large,shape=long,holes=none]).
example(example(scissorsscissors,[,[size=large,shape=other,holes=2]).size=large,shape=other,holes=2]).
example(example(keykey,[,[size=small,shape=other,holes=2]).size=small,shape=other,holes=2]).
match(Object,Description):-match(Object,Description):-
member(Conjunction,Description),member(Conjunction,Description),
satisfy(Object,Conjunction).satisfy(Object,Conjunction).
satisfy(Object,Conjunction):-satisfy(Object,Conjunction):-
not ( member(Att=Val,Conjunction),not ( member(Att=Val,Conjunction),
member(Att=ValX,Object),member(Att=ValX,Object),
ValX \== Val ).ValX \== Val ).
:-op(300,xfx,<==).:-op(300,xfx,<==).
learn(Class):-learn(Class):-
bagof(example(CalssX,Obj),bagof(example(CalssX,Obj),
example(CalssX,Obj),example(CalssX,Obj),
Examples),Examples),
learn(Examples,Class,Description),learn(Examples,Class,Description),
nl,write(Class),write(‘<==‘),nl,nl,write(Class),write(‘<==‘),nl,
writelist(Description),writelist(Description),
assert(Class<==Description).assert(Class<==Description).
learn(Examples,Class,[]):-learn(Examples,Class,[]):-
not member(example(Calss,_),Examples).not member(example(Calss,_),Examples).
learn(Examples,Class,[Conj|Conjs]):-learn(Examples,Class,[Conj|Conjs]):-
learn_conj(Examples,Class,Conj),learn_conj(Examples,Class,Conj),
remove(Examples,Conj,RestExamples),remove(Examples,Conj,RestExamples),
learn(REstExamples,Class,Conjs).learn(REstExamples,Class,Conjs).
learn_conj(Examples,Class,[]):-learn_conj(Examples,Class,[]):-
not (member(example(CalsX,_),Examples),not (member(example(CalsX,_),Examples),
ClasX \== Class).ClasX \== Class).
learn_conj(Examples,Class,[Cond|Conds]):-learn_conj(Examples,Class,[Cond|Conds]):-
choose_cond(Examples,Class,Cond),choose_cond(Examples,Class,Cond),
filter(Examples,[Cond],Examples1),filter(Examples,[Cond],Examples1),
learn_conj(Examples1,Class,Conds).learn_conj(Examples1,Class,Conds).
learn_conj(Examples,_,[]).learn_conj(Examples,_,[]).
choose_cond(Examples,Class,AttVal):-choose_cond(Examples,Class,AttVal):-
findall(AV/Score,findall(AV/Score,
score(Examples,Class,AV,Score),score(Examples,Class,AV,Score),
AVs),AVs),
best(AVs,AttVal).best(AVs,AttVal).
best([AttVal/_],AttVal).best([AttVal/_],AttVal).
best([AV0/S0, AV1/S1 |AVSlist],AttVal):-best([AV0/S0, AV1/S1 |AVSlist],AttVal):-
S1 > S0, !,S1 > S0, !,
best([AV1/S1 |AVSlist],AttVal)best([AV1/S1 |AVSlist],AttVal)
;;
best([AV0/S0 |AVSlist],AttVal).best([AV0/S0 |AVSlist],AttVal).
filter(Examples,Cond,Examples1):-filter(Examples,Cond,Examples1):-
findall(example(Calss,Obj),findall(example(Calss,Obj),
(member(example(Calss,Obj),Examples),(member(example(Calss,Obj),Examples),
satisfy(Obj,Cond)),satisfy(Obj,Cond)),
Examples1).Examples1).
remove([],_,[]).remove([],_,[]).
remove([example(Class,Obj)|Es],Conj,Es1):-remove([example(Class,Obj)|Es],Conj,Es1):-
satisfy(Obj,Conj),!,satisfy(Obj,Conj),!,
remove(Es,Conj,Es1).remove(Es,Conj,Es1).
remove([E|Es],Conj,[E|Es1]):-remove([E|Es],Conj,[E|Es1]):-
remove(Es,Conj,Es1). remove(Es,Conj,Es1).
satisfy(Object,Conjunction):-satisfy(Object,Conjunction):-
not ( member(Att=Val,Conjunction),not ( member(Att=Val,Conjunction),
member(Att=ValX,Object),member(Att=ValX,Object),
ValX \== Val ).ValX \== Val ).
score(Examples,Class,AttVal,Score):-score(Examples,Class,AttVal,Score):-
candidate(Examples,Class,AttVal),candidate(Examples,Class,AttVal),
filter(Examples,[AttVal],Examples1),filter(Examples,[AttVal],Examples1),
length(Examples1,N1),length(Examples1,N1),
count_pos(Examples1,Class,NPos1),count_pos(Examples1,Class,NPos1),
NPos1 >0,NPos1 >0,
Score is 2*NPos1 – N1.Score is 2*NPos1 – N1.
candidate(Examples,Class,Att=Val):-candidate(Examples,Class,Att=Val):-
attribute(Att,Values),attribute(Att,Values),
member(Val,Values),member(Val,Values),
suitable(Att=Val,Examples,Class).suitable(Att=Val,Examples,Class).
suitable(AttVal,Examples,Class):-suitable(AttVal,Examples,Class):-
member(example(ClassX,ObjX),Examples),member(example(ClassX,ObjX),Examples),
ClassX \== Class,ClassX \== Class,
not satisfy(ObjX, [AttVal]),!.not satisfy(ObjX, [AttVal]),!.
count_pos([],_,0).count_pos([],_,0).
count_pos([example(ClassX,_)|Ex],Class,N):-count_pos([example(ClassX,_)|Ex],Class,N):-
count_pos(Ex,Class,N1),count_pos(Ex,Class,N1),
(ClassX=Class,!, N is N1+1 ; N = N1).(ClassX=Class,!, N is N1+1 ; N = N1).
writelist([]).writelist([]).
writelist([X|L]):-writelist([X|L]):-
tab(2), write(X), nl,tab(2), write(X), nl,
writelist(L).writelist(L).
| ?- learn(pen).pen<== [holes=none,size=large] []yes| ?- learn(key). key<==
[shape=other,size=small] [holes=1,shape=long]
[]yes| ?- learn(nut).nut<== [shape=compact,holes=1] []yes
| ?- learn(screw).screw<== [holes=none,size=small] []yes| ?- learn(scissors).scissors<== [holes=2,size=large] []yes