我正在使用命令'model.save(“ name_of_model”)'将Keras模型保存在Python脚本中。它可以在我的固定PC(Windows)上完美运行。但不在Azure中(Microsoft Machine Learning / Windows / JupyterLab)。
我收到很长的错误消息(见下文)。
怎么了?
到目前为止,我的代码非常简单:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout
from tensorflow import keras
def create_model():
model = Sequential()
model.add(Dense(128, input_shape=(4,), activation="relu"))
model.add(Dropout(0.6))
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.6))
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.6))
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.6))
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.6))
model.add(Dense(2, activation="softmax"))
model.compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
return model
trainingX = np.loadtxt("TraindataX.csv")
trainingY = np.loadtxt("TraindataY.csv")
model = create_model()
model.fit(trainingX, trainingY, epochs=1)
model.save("my_testmodel")
错误消息:
**--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-1a17397cf7f6> in <module>
36
37 model.fit(trainingX, trainingY, epochs=1)
---> 38 model.save("my_testmodel3.h5")
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer)
1150 raise NotImplementedError
1151 from ..models import save_model
-> 1152 save_model(self, filepath, overwrite, include_optimizer)
1153
1154 @saving.allow_write_to_gcs
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/engine/saving.py in save_wrapper(obj, filepath, overwrite, *args, **kwargs)
447 os.remove(tmp_filepath)
448 else:
--> 449 save_function(obj, filepath, overwrite, *args, **kwargs)
450
451 return save_wrapper
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/engine/saving.py in save_model(model, filepath, overwrite, include_optimizer)
539 return
540 with H5Dict(filepath, mode='w') as h5dict:
--> 541 _serialize_model(model, h5dict, include_optimizer)
542 elif hasattr(filepath, 'write') and callable(filepath.write):
543 # write as binary stream
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/engine/saving.py in _serialize_model(model, h5dict, include_optimizer)
159 layer_group['weight_names'] = weight_names
160 for name, val in zip(weight_names, weight_values):
--> 161 layer_group[name] = val
162 if include_optimizer and model.optimizer:
163 if isinstance(model.optimizer, optimizers.TFOptimizer):
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/utils/io_utils.py in __setitem__(self, attr, val)
231 'Group with name "{}" exists.'.format(attr))
232 if is_np:
--> 233 dataset = self.data.create_dataset(attr, val.shape, dtype=val.dtype)
234 if not val.shape:
235 # scalar
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/h5py/_hl/group.py in create_dataset(self, name, shape, dtype, data, **kwds)
141 group = self
142 if name:
--> 143 if '/' in name:
144 h5objects = [obj for obj in name.split('/') if len(obj)]
145 name = h5objects[-1]
TypeError: a bytes-like object is required, not 'str'**