Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
model_list.sort(
key=lambda m_file: float(m_file.split("_")[3].replace(".hdf5", "")),
reverse=True,
)
model_file = os.path.join(
warm_start, "kfold_{}".format(fold), "model", model_list[-1]
)
# Load model from file
if learning_rate is None:
full_model = load_model(
model_file,
custom_objects={
"softplus2": softplus2,
"Set2Set": Set2Set,
"mean_squared_error_with_scale": mean_squared_error_with_scale,
"MEGNetLayer": MEGNetLayer,
},
)
learning_rate = K.get_value(full_model.optimizer.lr)
# Set up model
model = MEGNetModel(
100,
2,
nblocks=args.n_blocks,
nvocal=95,
npass=args.n_pass,
lr=learning_rate,
loss=args.loss,
graph_convertor=cg,
k_folds = list(map(int, args.k_folds.split(",")))
print("args is : {}".format(args))
print(
"Local devices are : {}, \n\n Available gpus are : {}".format(
device_lib.list_local_devices(), K.tensorflow_backend._get_available_gpus()
)
)
# prepare output path
if not os.path.exists(output_path):
os.makedirs(output_path, exist_ok=True)
# Get a crystal graph with cutoff radius A
cg = CrystalGraph(
bond_convertor=GaussianDistance(np.linspace(0, radius + 1, 100), 0.5),
cutoff=radius,
)
if graph_file is not None:
# load graph data
with gzip.open(graph_file, "rb") as f:
valid_graph_dict = pickle.load(f)
idx_list = list(range(len(valid_graph_dict)))
valid_idx_list = [
idx for idx, graph in valid_graph_dict.items() if graph is not None
]
else:
# load structure data
with gzip.open(args.input_file, "rb") as f:
df = pd.DataFrame(pickle.load(f))[["structure", prop_col]]
idx_list = list(range(len(df)))
# Load model from file
if learning_rate is None:
full_model = load_model(
model_file,
custom_objects={
"softplus2": softplus2,
"Set2Set": Set2Set,
"mean_squared_error_with_scale": mean_squared_error_with_scale,
"MEGNetLayer": MEGNetLayer,
},
)
learning_rate = K.get_value(full_model.optimizer.lr)
# Set up model
model = MEGNetModel(
100,
2,
nblocks=args.n_blocks,
nvocal=95,
npass=args.n_pass,
lr=learning_rate,
loss=args.loss,
graph_convertor=cg,
is_classification=True if args.type == "classification" else False,
nfeat_node=None if embedding_file is None else 16,
)
model.load_weights(model_file)
initial_epoch = int(model_list[-1].split("_")[2])
print(
"warm start from : {}, \nlearning_rate is {}.".format(
model_file, learning_rate
else:
model_list.sort(
key=lambda m_file: float(m_file.split("_")[3].replace(".hdf5", "")),
reverse=True,
)
model_file = os.path.join(
warm_start, "kfold_{}".format(fold), "model", model_list[-1]
)
# Load model from file
if learning_rate is None:
full_model = load_model(
model_file,
custom_objects={
"softplus2": softplus2,
"Set2Set": Set2Set,
"mean_squared_error_with_scale": mean_squared_error_with_scale,
"MEGNetLayer": MEGNetLayer,
},
)
learning_rate = K.get_value(full_model.optimizer.lr)
# Set up model
model = MEGNetModel(
100,
2,
nblocks=args.n_blocks,
nvocal=95,
npass=args.n_pass,
lr=learning_rate,
loss=args.loss,
https://github.com/materialsvirtuallab/megnet/blob/master/mvl_models/mp-2019.4.1/formation_energy.hdf5
Args:
url: (str) url link of the model
Returns:
GraphModel
"""
import urllib.request
fname = url.split("/")[-1]
urllib.request.urlretrieve(url, fname)
urllib.request.urlretrieve(url + ".json", fname + ".json")
return cls.from_file(fname)
class MEGNetModel(GraphModel):
"""
Construct a graph network model with or without explicit atom features
if n_feature is specified then a general graph model is assumed,
otherwise a crystal graph model with z number as atom feature is assumed.
"""
def __init__(self,
nfeat_edge: int = None,
nfeat_global: int = None,
nfeat_node: int = None,
nblocks: int = 3,
lr: float = 1e-3,
n1: int = 64,
n2: int = 32,
n3: int = 16,
nvocal: int = 95,
https://github.com/materialsvirtuallab/megnet/blob/master/mvl_models/mp-2019.4.1/formation_energy.hdf5
Args:
url: (str) url link of the model
Returns:
GraphModel
"""
import urllib.request
fname = url.split("/")[-1]
urllib.request.urlretrieve(url, fname)
urllib.request.urlretrieve(url + ".json", fname + ".json")
return cls.from_file(fname)
class MEGNetModel(GraphModel):
"""
Construct a graph network model with or without explicit atom features
if n_feature is specified then a general graph model is assumed,
otherwise a crystal graph model with z number as atom feature is assumed.
"""
def __init__(self,
nfeat_edge: int = None,
nfeat_global: int = None,
nfeat_node: int = None,
nblocks: int = 3,
lr: float = 1e-3,
n1: int = 64,
n2: int = 32,
n3: int = 16,
nvocal: int = 95,
embedding_file = args.embedding_file
k_folds = list(map(int, args.k_folds.split(",")))
print("args is : {}".format(args))
print(
"Local devices are : {}, \n\n Available gpus are : {}".format(
device_lib.list_local_devices(), K.tensorflow_backend._get_available_gpus()
)
)
# prepare output path
if not os.path.exists(output_path):
os.makedirs(output_path, exist_ok=True)
# Get a crystal graph with cutoff radius A
cg = CrystalGraph(
bond_convertor=GaussianDistance(np.linspace(0, radius + 1, 100), 0.5),
cutoff=radius,
)
if graph_file is not None:
# load graph data
with gzip.open(graph_file, "rb") as f:
valid_graph_dict = pickle.load(f)
idx_list = list(range(len(valid_graph_dict)))
valid_idx_list = [
idx for idx, graph in valid_graph_dict.items() if graph is not None
]
else:
# load structure data
with gzip.open(args.input_file, "rb") as f:
df = pd.DataFrame(pickle.load(f))[["structure", prop_col]]
# Get the inputs for each batch
inputs = self._generate_inputs(batch_index)
# Make the graph data
inputs = self._combine_graph_data(*inputs)
# Return the batch
if self.targets is None:
return inputs
else:
# get targets
it = itemgetter(*batch_index)
target_temp = itemgetter_list(self.targets, batch_index)
target_temp = np.atleast_2d(target_temp)
return inputs, expand_1st(target_temp)
# assemble bond indices
index1 = []
index2 = []
offset_ind = 0
for ind1, ind2 in zip(index1_temp, index2_temp):
index1 += [i + offset_ind for i in ind1]
index2 += [i + offset_ind for i in ind2]
offset_ind += (max(ind1) + 1)
# Compile the inputs in needed order
inputs = [expand_1st(feature_list_temp),
expand_1st(connection_list_temp),
expand_1st(global_list_temp),
expand_1st(index1),
expand_1st(index2),
expand_1st(gnode),
expand_1st(gbond)]
return inputs
def graph_to_input(self, graph: Dict) -> List[np.ndarray]:
"""
Turns a graph into model input
Args:
(dict): Dictionary description of the graph
Return:
([np.ndarray]): Inputs in the form needed by MEGNet
"""
gnode = [0] * len(graph['atom'])
gbond = [0] * len(graph['index1'])
return [expand_1st(self.atom_converter.convert(graph['atom'])),
expand_1st(self.bond_converter.convert(graph['bond'])),
expand_1st(np.array(graph['state'])),
expand_1st(np.array(graph['index1'])),
expand_1st(np.array(graph['index2'])),
expand_1st(np.array(gnode)),
expand_1st(np.array(gbond))]