Hi,
I am using ml4t environment installed with instruction in official githup (using conda, mamba). I got error massage below while running the code (17_deep_learning/04_optimizing_a_NN_architecture_for_trading) . Could you anyone please let me know how to solve it?
Thanks,
Bumju
ic = []
scaler = StandardScaler()
for params in param_grid:
dense_layers, activation, dropout = params
for batch_size in [64, 256]:
print(dense_layers, activation, dropout, batch_size)
checkpoint_dir = checkpoint_path / str(dense_layers) / activation / str(dropout) / str(batch_size)
if not checkpoint_dir.exists():
checkpoint_dir.mkdir(parents=True, exist_ok=True)
start = time()
for fold, (train_idx, test_idx) in enumerate(cv.split(X_cv)):
x_train, y_train, x_val, y_val = get_train_valid_data(X_cv, y_cv, train_idx, test_idx)
x_train = scaler.fit_transform(x_train)
x_val = scaler.transform(x_val)
preds = y_val.to_frame('actual')
r = pd.DataFrame(index=y_val.groupby(level='date').size().index)
model = make_model(dense_layers, activation, dropout)
for epoch in range(20):
model.fit(x_train,
y_train,
batch_size=batch_size,
epochs=1,
verbose=0,
shuffle=True,
validation_data=(x_val, y_val))
model.save_weights((checkpoint_dir / f'ckpt_{fold}_{epoch}').as_posix())
preds[epoch] = model.predict(x_val).squeeze()
r[epoch] = preds.groupby(level='date').apply(lambda x: spearmanr(x.actual, x[epoch])[0]).to_frame(epoch)
print(format_time(time()-start), f'{fold + 1:02d} | {epoch + 1:02d} | {r[epoch].mean():7.4f} | {r[epoch].median():7.4f}')
ic.append(r.assign(dense_layers=str(dense_layers),
activation=activation,
dropout=dropout,
batch_size=batch_size,
fold=fold))
t = time()-start
pd.concat(ic).to_hdf('scores.h5', 'ic_by_day')
(64, 32) tanh 0.1 64
UnicodeDecodeError Traceback (most recent call last)
Input In [24], in
20 for epoch in range(20):
21 model.fit(x_train,
22 y_train,
23 batch_size=batch_size,
(…)
26 shuffle=True,
27 validation_data=(x_val, y_val))
—> 28 model.save_weights((checkpoint_dir / f’ckpt_{fold}_{epoch}’).as_posix())
29 preds[epoch] = model.predict(x_val).squeeze()
30 r[epoch] = preds.groupby(level=‘date’).apply(lambda x: spearmanr(x.actual, x[epoch])[0]).to_frame(epoch)
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\keras\engine\training.py:2224, in Model.save_weights(self, filepath, overwrite, save_format, options)
2222 else:
2223 session = backend.get_session()
→ 2224 self._trackable_saver.save(filepath, session=session, options=options)
2225 # Record this checkpoint so it’s visible from tf.train.latest_checkpoint.
2226 checkpoint_management.update_checkpoint_state_internal(
2227 save_dir=os.path.dirname(filepath),
2228 model_checkpoint_path=filepath,
2229 save_relative_paths=True,
2230 all_model_checkpoint_paths=[filepath])
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\training\tracking\util.py:1218, in TrackableSaver.save(self, file_prefix, checkpoint_number, session, options)
1215 object_graph_tensor = None
1217 file_io.recursive_create_dir(os.path.dirname(file_prefix))
→ 1218 save_path, new_feed_additions = self._save_cached_when_graph_building(
1219 file_prefix_tensor, object_graph_tensor, options)
1220 if new_feed_additions:
1221 feed_dict.update(new_feed_additions)
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\training\tracking\util.py:1164, in TrackableSaver._save_cached_when_graph_building(self, file_prefix, object_graph_tensor, options)
1157 if (self._last_save_object_graph != graph_proto
1158 # When executing eagerly, we need to re-create SaveableObjects each time
1159 # save() is called so they pick up new Tensors passed to their
1160 # constructors. That means the Saver needs to be copied with a new
1161 # var_list.
1162 or context.executing_eagerly() or ops.inside_function()):
1163 saver = functional_saver.MultiDeviceSaver(named_saveable_objects)
→ 1164 save_op = saver.save(file_prefix, options=options)
1165 with ops.device("/cpu:0"):
1166 with ops.control_dependencies([save_op]):
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\training\saving\functional_saver.py:300, in MultiDeviceSaver.save(self, file_prefix, options)
298 tf_function_save()
299 else:
→ 300 return save_fn()
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\training\saving\functional_saver.py:274, in MultiDeviceSaver.save..save_fn()
269 sharded_prefixes.append(shard_prefix)
270 with ops.device(device):
271 # _SingleDeviceSaver will use the CPU device when necessary, but
272 # initial read operations should be placed on the SaveableObject’s
273 # device.
→ 274 sharded_saves.append(saver.save(shard_prefix, options))
276 with ops.control_dependencies(sharded_saves):
277 # Merge on the io_device if specified, otherwise co-locates the merge op
278 # with the last device used.
279 merge_device = (
280 options.experimental_io_device or
281 saveable_object_util.set_cpu0(last_device))
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\training\saving\functional_saver.py:83, in _SingleDeviceSaver.save(self, file_prefix, options)
81 save_device = options.experimental_io_device or “cpu:0”
82 with ops.device(save_device):
—> 83 return io_ops.save_v2(file_prefix, tensor_names, tensor_slices, tensors)
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\ops\gen_io_ops.py:1695, in save_v2(prefix, tensor_names, shape_and_slices, tensors, name)
1693 pass
1694 try:
→ 1695 return save_v2_eager_fallback(
1696 prefix, tensor_names, shape_and_slices, tensors, name=name,
1697 ctx=_ctx)
1698 except _core._SymbolicException:
1699 pass # Add nodes to the TensorFlow graph.
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\ops\gen_io_ops.py:1716, in save_v2_eager_fallback(prefix, tensor_names, shape_and_slices, tensors, name, ctx)
1714 _inputs_flat = [prefix, tensor_names, shape_and_slices] + list(tensors)
1715 _attrs = (“dtypes”, _attr_dtypes)
→ 1716 _result = _execute.execute(b"SaveV2", 0, inputs=_inputs_flat, attrs=_attrs,
1717 ctx=ctx, name=name)
1718 _result = None
1719 return _result
File ~\anaconda3\envs\ml4t\lib\site-packages\tensorflow\python\eager\execute.py:59, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
—> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xc1 in position 156: invalid start byte