def init_models(num_models, input_num): global global_genes, global_num_nodes, current_pop global_num_nodes = input_num + 1 for i in range(num_models): model = Model() # model.add(Dense(output_dim=3, input_dim=input_num)) # model.add(Activation("sigmoid")) # model.add(Dense(output_dim=1)) # model.add(Activation("sigmoid")) for j in range(input_num): if len(global_genes) < input_num: global_genes.append(Gene(j, input_num, j)) model.add(global_genes[j]) model.set_num_nodes(input_num+1) # sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) # stochastic gradient descent, (lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) # model.compile(loss="mse", optimizer=sgd, metrics=["accuracy"]) current_pop.append(model) fitness.append(-100) if load_current_pool: current_pop = load_saved() for i in range(num_models): print(current_pop[i].get_weights())
class StructureBuilder: """ Deals with contructing the Structure object. The StructureBuilder class is used by the PDBParser classes to translate a file to a Structure object. """ def __init__(self): self.line_counter=0 self.header={} def _is_completely_disordered(self, residue): "Return 1 if all atoms in the residue have a non blank altloc." atom_list=residue.get_unpacked_list() for atom in atom_list: altloc=atom.get_altloc() if altloc==" ": return 0 return 1 # Public methods called by the Parser classes def set_header(self, header): self.header=header def set_line_counter(self, line_counter): """ The line counter keeps track of the line in the PDB file that is being parsed. Arguments: o line_counter - int """ self.line_counter=line_counter def init_structure(self, structure_id): """Initiate a new Structure object with given id. Arguments: o id - string """ self.structure=Structure(structure_id) def init_model(self, model_id): """Initiate a new Model object with given id. Arguments: o id - int """ self.model=Model(model_id) self.structure.add(self.model) def init_chain(self, chain_id): """Initiate a new Chain object with given id. Arguments: o chain_id - string """ if self.model.has_id(chain_id): self.chain=self.model[chain_id] if __debug__: warnings.warn("WARNING: Chain %s is discontinuous at line %i." % (chain_id, self.line_counter), PDBConstructionWarning) else: self.chain=Chain(chain_id) self.model.add(self.chain) def init_seg(self, segid): """Flag a change in segid. Arguments: o segid - string """ self.segid=segid def init_residue(self, resname, field, resseq, icode): """ Initiate a new Residue object. Arguments: o resname - string, e.g. "ASN" o field - hetero flag, "W" for waters, "H" for hetero residues, otherwise blank. o resseq - int, sequence identifier o icode - string, insertion code """ if field!=" ": if field=="H": # The hetero field consists of H_ + the residue name (e.g. H_FUC) field="H_"+resname res_id=(field, resseq, icode) if field==" ": if self.chain.has_id(res_id): # There already is a residue with the id (field, resseq, icode). # This only makes sense in the case of a point mutation. if __debug__: warnings.warn("WARNING: Residue ('%s', %i, '%s') " "redefined at line %i." % (field, resseq, icode, self.line_counter), PDBConstructionWarning) duplicate_residue=self.chain[res_id] if duplicate_residue.is_disordered()==2: # The residue in the chain is a DisorderedResidue object. # So just add the last Residue object. if duplicate_residue.disordered_has_id(resname): # The residue was already made self.residue=duplicate_residue duplicate_residue.disordered_select(resname) else: # Make a new residue and add it to the already # present DisorderedResidue new_residue=Residue(res_id, resname, self.segid) duplicate_residue.disordered_add(new_residue) self.residue=duplicate_residue return else: # Make a new DisorderedResidue object and put all # the Residue objects with the id (field, resseq, icode) in it. # These residues each should have non-blank altlocs for all their atoms. # If not, the PDB file probably contains an error. if not self._is_completely_disordered(duplicate_residue): # if this exception is ignored, a residue will be missing self.residue=None raise PDBConstructionException(\ "Blank altlocs in duplicate residue %s ('%s', %i, '%s')" \ % (resname, field, resseq, icode)) self.chain.detach_child(res_id) new_residue=Residue(res_id, resname, self.segid) disordered_residue=DisorderedResidue(res_id) self.chain.add(disordered_residue) disordered_residue.disordered_add(duplicate_residue) disordered_residue.disordered_add(new_residue) self.residue=disordered_residue return residue=Residue(res_id, resname, self.segid) self.chain.add(residue) self.residue=residue def init_atom(self, name, coord, b_factor, occupancy, altloc, fullname, serial_number=None, element=None): """ Initiate a new Atom object. Arguments: o name - string, atom name, e.g. CA, spaces should be stripped o coord - Numeric array (Float0, size 3), atomic coordinates o b_factor - float, B factor o occupancy - float o altloc - string, alternative location specifier o fullname - string, atom name including spaces, e.g. " CA " o element - string, upper case, e.g. "HG" for mercury """ residue=self.residue # if residue is None, an exception was generated during # the construction of the residue if residue is None: return # First check if this atom is already present in the residue. # If it is, it might be due to the fact that the two atoms have atom # names that differ only in spaces (e.g. "CA.." and ".CA.", # where the dots are spaces). If that is so, use all spaces # in the atom name of the current atom. if residue.has_id(name): duplicate_atom=residue[name] # atom name with spaces of duplicate atom duplicate_fullname=duplicate_atom.get_fullname() if duplicate_fullname!=fullname: # name of current atom now includes spaces name=fullname if __debug__: warnings.warn("WARNING: atom names %s and %s differ " "only in spaces at line %i." % (duplicate_fullname, fullname, self.line_counter), PDBConstructionWarning) atom=self.atom=Atom(name, coord, b_factor, occupancy, altloc, fullname, serial_number, element) if altloc!=" ": # The atom is disordered if residue.has_id(name): # Residue already contains this atom duplicate_atom=residue[name] if duplicate_atom.is_disordered()==2: duplicate_atom.disordered_add(atom) else: # This is an error in the PDB file: # a disordered atom is found with a blank altloc # Detach the duplicate atom, and put it in a # DisorderedAtom object together with the current # atom. residue.detach_child(name) disordered_atom=DisorderedAtom(name) residue.add(disordered_atom) disordered_atom.disordered_add(atom) disordered_atom.disordered_add(duplicate_atom) residue.flag_disordered() if __debug__: warnings.warn("WARNING: disordered atom found " "with blank altloc before line %i.\n" % self.line_counter, PDBConstructionWarning) else: # The residue does not contain this disordered atom # so we create a new one. disordered_atom=DisorderedAtom(name) residue.add(disordered_atom) # Add the real atom to the disordered atom, and the # disordered atom to the residue disordered_atom.disordered_add(atom) residue.flag_disordered() else: # The atom is not disordered residue.add(atom) def set_anisou(self, anisou_array): "Set anisotropic B factor of current Atom." self.atom.set_anisou(anisou_array) def set_siguij(self, siguij_array): "Set standard deviation of anisotropic B factor of current Atom." self.atom.set_siguij(siguij_array) def set_sigatm(self, sigatm_array): "Set standard deviation of atom position of current Atom." self.atom.set_sigatm(sigatm_array) def get_structure(self): "Return the structure." # first sort everything # self.structure.sort() # Add the header dict self.structure.header=self.header return self.structure def set_symmetry(self, spacegroup, cell): pass
class StructureBuilder: """ Deals with contructing the Structure object. The StructureBuilder class is used by the PDBParser classes to translate a file to a Structure object. """ def __init__(self): self.line_counter = 0 self.header = {} def _is_completely_disordered(self, residue): "Return 1 if all atoms in the residue have a non blanc altloc." atom_list = residue.get_unpacked_list() for atom in atom_list: altloc = atom.get_altloc() if altloc == " ": return 0 return 1 # Public methods called by the Parser classes def set_header(self, header): self.header = header def set_line_counter(self, line_counter): """ The line counter keeps track of the line in the PDB file that is being parsed. Arguments: o line_counter - int """ self.line_counter = line_counter def init_structure(self, structure_id): """Initiate a new Structure object with given id. Arguments: o id - string """ self.structure = Structure(structure_id) def init_model(self, model_id): """Initiate a new Model object with given id. Arguments: o id - int """ self.model = Model(model_id) self.structure.add(self.model) def init_chain(self, chain_id): """Initiate a new Chain object with given id. Arguments: o chain_id - string """ if self.model.has_id(chain_id): self.chain = self.model[chain_id] if __debug__: sys.stderr.write( "WARNING: Chain %s is discontinuous at line %i.\n" % (chain_id, self.line_counter)) else: self.chain = Chain(chain_id) self.model.add(self.chain) def init_seg(self, segid): """Flag a change in segid. Arguments: o segid - string """ self.segid = segid def init_residue(self, resname, field, resseq, icode): """ Initiate a new Residue object. Arguments: o resname - string, e.g. "ASN" o field - hetero flag, "W" for waters, "H" for hetero residues, otherwise blanc. o resseq - int, sequence identifier o icode - string, insertion code """ if field != " ": if field == "H": # The hetero field consists of H_ + the residue name (e.g. H_FUC) field = "H_" + resname res_id = (field, resseq, icode) if field == " ": if self.chain.has_id(res_id): # There already is a residue with the id (field, resseq, icode). # This only makes sense in the case of a point mutation. if __debug__: sys.stderr.write( "WARNING: Residue ('%s', %i, '%s') redefined at line %i.\n" % (field, resseq, icode, self.line_counter)) duplicate_residue = self.chain[res_id] if duplicate_residue.is_disordered() == 2: # The residue in the chain is a DisorderedResidue object. # So just add the last Residue object. if duplicate_residue.disordered_has_id(resname): # The residue was already made self.residue = duplicate_residue duplicate_residue.disordered_select(resname) else: # Make a new residue and add it to the already # present DisorderedResidue new_residue = Residue(res_id, resname, self.segid) duplicate_residue.disordered_add(new_residue) self.residue = duplicate_residue return else: # Make a new DisorderedResidue object and put all # the Residue objects with the id (field, resseq, icode) in it. # These residues each should have non-blanc altlocs for all their atoms. # If not, the PDB file probably contains an error. if not self._is_completely_disordered(duplicate_residue): # if this exception is ignored, a residue will be missing self.residue = None raise PDBConstructionException(\ "Blank altlocs in duplicate residue %s ('%s', %i, '%s')" \ % (resname, field, resseq, icode)) self.chain.detach_child(res_id) new_residue = Residue(res_id, resname, self.segid) disordered_residue = DisorderedResidue(res_id) self.chain.add(disordered_residue) disordered_residue.disordered_add(duplicate_residue) disordered_residue.disordered_add(new_residue) self.residue = disordered_residue return residue = Residue(res_id, resname, self.segid) self.chain.add(residue) self.residue = residue def init_atom(self, name, coord, b_factor, occupancy, altloc, fullname, serial_number=None): """ Initiate a new Atom object. Arguments: o name - string, atom name, e.g. CA, spaces should be stripped o coord - Numeric array (Float0, size 3), atomic coordinates o b_factor - float, B factor o occupancy - float o altloc - string, alternative location specifier o fullname - string, atom name including spaces, e.g. " CA " """ residue = self.residue # if residue is None, an exception was generated during # the construction of the residue if residue is None: return # First check if this atom is already present in the residue. # If it is, it might be due to the fact that the two atoms have atom # names that differ only in spaces (e.g. "CA.." and ".CA.", # where the dots are spaces). If that is so, use all spaces # in the atom name of the current atom. if residue.has_id(name): duplicate_atom = residue[name] # atom name with spaces of duplicate atom duplicate_fullname = duplicate_atom.get_fullname() if duplicate_fullname != fullname: # name of current atom now includes spaces name = fullname if __debug__: sys.stderr.write( "WARNING: atom names %s and %s differ only in spaces at line %i.\n" % (duplicate_fullname, fullname, self.line_counter)) atom = self.atom = Atom(name, coord, b_factor, occupancy, altloc, fullname, serial_number) if altloc != " ": # The atom is disordered if residue.has_id(name): # Residue already contains this atom duplicate_atom = residue[name] if duplicate_atom.is_disordered() == 2: duplicate_atom.disordered_add(atom) else: # This is an error in the PDB file: # a disordered atom is found with a blanc altloc # Detach the duplicate atom, and put it in a # DisorderedAtom object together with the current # atom. residue.detach_child(name) disordered_atom = DisorderedAtom(name) residue.add(disordered_atom) disordered_atom.disordered_add(atom) disordered_atom.disordered_add(duplicate_atom) residue.flag_disordered() if __debug__: sys.stderr.write( "WARNING: disordered atom found with blanc altloc before line %i.\n" % self.line_counter) else: # The residue does not contain this disordered atom # so we create a new one. disordered_atom = DisorderedAtom(name) residue.add(disordered_atom) # Add the real atom to the disordered atom, and the # disordered atom to the residue disordered_atom.disordered_add(atom) residue.flag_disordered() else: # The atom is not disordered residue.add(atom) def set_anisou(self, anisou_array): "Set anisotropic B factor of current Atom." self.atom.set_anisou(anisou_array) def set_siguij(self, siguij_array): "Set standard deviation of anisotropic B factor of current Atom." self.atom.set_siguij(siguij_array) def set_sigatm(self, sigatm_array): "Set standard deviation of atom position of current Atom." self.atom.set_sigatm(sigatm_array) def get_structure(self): "Return the structure." # first sort everything # self.structure.sort() # Add the header dict self.structure.header = self.header return self.structure def set_symmetry(self, spacegroup, cell): pass
from Layers import Dense_layer, Conv_layer, Pooling_layer, Dropout_layer from Activation_Functions import ReLU, Softmax from Loss_functions import Loss, Softmax, CategoricalCrossentropy, Act_Softmax_Loss_CCentropy from Optimizers import SGD, Adam from Model import Model, Accuracy, Accuracy_Categorical # Exempel model hugh = Model() hugh.add( Dense_layer(2, 512, weight_regularizer_l2=0.0004, bias_regularizer_l2=0.0004)) hugh.add(ReLU()) hugh.add(Dropout_layer(0.2)) hugh.add(Dense_layer(512, 3)) hugh.add(Softmax()) hugh.setters(loss=CategoricalCrossentropy(), optimizer=Adam(learning_rate=0.05, decay=0.00005), accuracy=Accuracy_Categorical()) hugh.finalize() hugh.train(X_train, y_train, validation_data=(X_val, y_val), epochs=1000, print_every=100)
from Model import Model from layers.Layers import Dense, Conv, Flatten from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("../data", one_hot=True) x_train = np.reshape(mnist.train.images[:800], (800, 28, 28, 1)) y_train = mnist.train.labels[:800] x_test = np.reshape(mnist.test.images[:1000], (1000, 28, 28, 1)) y_test = mnist.test.labels[:1000] # print(x_train.shape) model = Model() model.add(Conv(16, (7, 7), (28, 28, 1), strides=(2, 2), padding="VALID", activation='tanh')) model.add(Conv(32, (5, 5), (11, 11, 16), strides=(2, 2), padding="VALID", activation='tanh')) # model.add(Conv(64, (3, 3), (4, 4, 32), strides=(1, 1), padding="VALID", activation='tanh')) model.add(Flatten()) model.add(Dense((512, 64), activation='tanh', name='dense')) model.add(Dense((64, 10), activation='softmax', name='dense2')) # model.add(Dense((64, 1), activation='none', name='dense2')) model.compile('cross_entropy') # cross_entropy # model.compile('mse') # cross_entropy model.fit_eval(x_train, y_train, 0.005, 20, x_test, y_test) res = model.predict(x_test) n = 0 for i, y in enumerate(y_test): pred = np.argmax(res[i]) y_ = np.argmax(y) # print("预测", pred, "真实", y_)
from Model import Model model = Model("html") body = Model("body") body.add(Model("h1")) body.add(Model("p")) body.add(Model("div", "hello")) model.add(body) print(model.html())
input_shape = tuple(input_shape[:-1]) y = y.ravel() if not num_classes: num_classes = np.max(y) + 1 n = y.shape[0] categorical = np.zeros((n, num_classes), dtype=dtype) categorical[np.arange(n), y] = 1 output_shape = input_shape + (num_classes, ) categorical = np.reshape(categorical, output_shape) return categorical # Flatten the images to vectors x_train = x_train.reshape(60000, 784) x_test = x_test.reshape(10000, 784) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 print(x_train.shape, 'train samples') print(x_test.shape, 'test samples') # convert class vectors to binary class matrices y_train = to_categorical(y_train, num_classes) y_test = to_categorical(y_test, num_classes) model = Model() model.add(Dense(10, input_shape=784)) model.train(x_train, y_train) model.predict(x_test, y_test)