MCPcopy
hub / github.com/tensorlayer/TensorLayer / save_model

Method save_model

tensorlayer/db.py:111–169  ·  view source on GitHub ↗

Save model architecture and parameters into database, timestamp will be added automatically. Parameters ---------- network : TensorLayer Model TensorLayer Model instance. model_name : str The name/key of model. kwargs : other events

(self, network=None, model_name='model', **kwargs)

Source from the content-addressed store, hash-verified

109
110 # =========================== MODELS ================================
111 def save_model(self, network=None, model_name='model', **kwargs):
112 """Save model architecture and parameters into database, timestamp will be added automatically.
113
114 Parameters
115 ----------
116 network : TensorLayer Model
117 TensorLayer Model instance.
118 model_name : str
119 The name/key of model.
120 kwargs : other events
121 Other events, such as name, accuracy, loss, step number and etc (optinal).
122
123 Examples
124 ---------
125 Save model architecture and parameters into database.
126 >>> db.save_model(net, accuracy=0.8, loss=2.3, name='second_model')
127
128 Load one model with parameters from database (run this in other script)
129 >>> net = db.find_top_model(accuracy=0.8, loss=2.3)
130
131 Find and load the latest model.
132 >>> net = db.find_top_model(sort=[("time", pymongo.DESCENDING)])
133 >>> net = db.find_top_model(sort=[("time", -1)])
134
135 Find and load the oldest model.
136 >>> net = db.find_top_model(sort=[("time", pymongo.ASCENDING)])
137 >>> net = db.find_top_model(sort=[("time", 1)])
138
139 Get model information
140 >>> net._accuracy
141 ... 0.8
142
143 Returns
144 ---------
145 boolean : True for success, False for fail.
146 """
147 kwargs.update({'model_name': model_name})
148 self._fill_project_info(kwargs) # put project_name into kwargs
149
150 # params = network.get_all_params()
151 params = network.all_weights
152
153 s = time.time()
154
155 # kwargs.update({'architecture': network.all_graphs, 'time': datetime.utcnow()})
156 kwargs.update({'architecture': network.config, 'time': datetime.utcnow()})
157
158 try:
159 params_id = self.model_fs.put(self._serialization(params))
160 kwargs.update({'params_id': params_id, 'time': datetime.utcnow()})
161 self.db.Model.insert_one(kwargs)
162 print("[Database] Save model: SUCCESS, took: {}s".format(round(time.time() - s, 2)))
163 return True
164 except Exception as e:
165 exc_type, exc_obj, exc_tb = sys.exc_info()
166 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
167 logging.info("{} {} {} {} {}".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))
168 print("[Database] Save model: FAIL")

Callers 1

task_script.pyFile · 0.80

Calls 3

_fill_project_infoMethod · 0.95
_serializationMethod · 0.95
updateMethod · 0.45

Tested by

no test coverage detected