<s> """<STR_LIT>""" <EOL> from numpy import zeros , inner , diag , allclose , fill_diagonal <EOL> import numpy <EOL> from kameleon_mcmc . distribution . Distribution import Distribution <EOL> from kameleon_mcmc . tools . GenericTests import GenericTests <EOL> class Hopfield ( Distribution ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , W , bias ) : <EOL> GenericTests . check_type ( W , '<STR_LIT>' , numpy . ndarray , <NUM_LIT:2> ) <EOL> GenericTests . check_type ( bias , '<STR_LIT>' , numpy . ndarray , <NUM_LIT:1> ) <EOL> if not W . shape [ <NUM_LIT:0> ] == W . shape [ <NUM_LIT:1> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not bias . shape [ <NUM_LIT:0> ] == W . shape [ <NUM_LIT:0> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not all ( diag ( W ) == <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not allclose ( W , W . T ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> Distribution . __init__ ( self , W . shape [ <NUM_LIT:0> ] ) <EOL> self . W = W <EOL> self . bias = bias <EOL> def __str__ ( self ) : <EOL> s = self . __class__ . __name__ + "<STR_LIT>" <EOL> s += "<STR_LIT>" + str ( self . W ) <EOL> s += "<STR_LIT>" + str ( self . bias ) <EOL> s += "<STR_LIT:U+002CU+0020>" + Distribution . __str__ ( self ) <EOL> s += "<STR_LIT:]>" <EOL> return s <EOL> def sample ( self , n = <NUM_LIT:1> ) : <EOL> if not type ( n ) is int : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> raise NotImplementedError ( ) <EOL> def log_pdf ( self , X ) : <EOL> GenericTests . check_type ( X , '<STR_LIT:X>' , numpy . ndarray , <NUM_LIT:2> ) <EOL> if X . dtype != numpy . bool8 : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not X . shape [ <NUM_LIT:1> ] == self . dimension : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> result = zeros ( len ( X ) ) <EOL> for i in range ( len ( X ) ) : <EOL> result [ i ] = inner ( X [ i ] , self . bias + self . W . dot ( X [ i ] ) ) <EOL> return result <EOL> @ staticmethod <EOL> def weights_from_patterns ( P ) : <EOL> """<STR_LIT>""" <EOL> GenericTests . check_type ( P , "<STR_LIT:P>" , numpy . ndarray , required_shapelen = <NUM_LIT:2> ) <EOL> dim = P . shape [ <NUM_LIT:1> ] <EOL> n = P . shape [ <NUM_LIT:0> ] <EOL> if n <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> W = zeros ( ( dim , dim ) ) <EOL> for i in range ( dim ) : <EOL> for j in range ( i , dim ) : <EOL> for mu in range ( n ) : <EOL> W [ i , j ] += ( <NUM_LIT:2> * P [ mu ] [ i ] - <NUM_LIT:1> ) * ( <NUM_LIT:2> * P [ mu ] [ j ] - <NUM_LIT:1> ) <EOL> W [ j , i ] = W [ i , j ] <EOL> fill_diagonal ( W , <NUM_LIT:0> ) <EOL> return W </s>
