def sparse_weighted_loss(target, output, weights):
return tf.multiply(tf.keras.backend.sparse_categorical_crossentropy(target, output), weights)
weights_tensor = Input(shape=(None,), dtype='float32', name='weights_input')
lossFct = partial(sparse_weighted_loss, weights=weights_tensor)
update_wrapper(lossFct, sparse_weighted_loss)
def sparse_weighted_loss(target, output, weights):
return tf.multiply(tf.keras.backend.sparse_categorical_crossentropy(target, output), weights)
custom_obj = {}
custom_obj['sparse_weighted_loss'] = sparse_weighted_loss
model = keras.models.load_model(modelPath, custom_objects=custom_obj)
Traceback (most recent call last):
File "Train_Product_NER_weighted_softmax.py", line 112, in <module>
model = BiLSTM.loadModel(sys.argv[2])
File "/BiLSTM_weightedloss.py", line 653, in loadModel
model = keras.models.load_model(modelPath, custom_objects=custom_obj)
File "/usr/local/lib/python3.4/site-packages/Keras-2.1.6-py3.4.egg/keras/models.py", line 388, in load_model
File "/usr/local/lib/python3.4/site-packages/Keras-2.1.6-py3.4.egg/keras/engine/training.py", line 837, in compile
File "/usr/local/lib/python3.4/site-packages/Keras-2.1.6-py3.4.egg/keras/engine/training.py", line 429, in weighted
TypeError: sparse_weighted_loss() missing 1 required positional argument: 'weights'
--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/2f73c3a1-33bb-4532-a7c6-77aad5511f77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
weights_tensor = Input(shape=(None,), dtype='float32', name='weights_input')
inputNodes.append(weights_tensor)
def sparse_weighted_loss(target, output):
return tf.multiply(tf.keras.backend.sparse_categorical_crossentropy(target, output), weights_tensor)
def sparse_weighted_loss(target, output):
return tf.multiply(tf.keras.backend.sparse_categorical_crossentropy(target, output), weights_tensor)
custom_obj = {}
custom_obj['sparse_weighted_loss'] = sparse_weighted_loss
model = keras.models.load_model(modelPath, custom_objects=custom_obj)
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/3b5441da-19dc-43c7-a575-421a8955b0ad%40googlegroups.com.
@staticmethod
def loadModel(modelPath):
import h5py
import json
from .keraslayers.ChainCRF import create_custom_objects
# Define weighted loss
def sparse_weighted_loss(target, output):
return tf.multiply(tf.keras.backend.sparse_categorical_crossentropy(target, output), weights_tensor)
custom_obj = {}
custom_obj['sparse_weighted_loss'] = sparse_weighted_loss
model = keras.models.load_model(modelPath, custom_objects=custom_obj)
with h5py.File(modelPath, 'r') as f:
mappings = json.loads(f.attrs['mappings'])
params = json.loads(f.attrs['params'])
modelName = f.attrs['modelName']
labelKey = f.attrs['labelKey']
bilstm = BiLSTM(params)
bilstm.setMappings(mappings, None)
bilstm.models = {modelName: model}
bilstm.labelKeys = {modelName: labelKey}
bilstm.idx2Labels = {}
bilstm.idx2Labels[modelName] = {v: k for k, v in bilstm.mappings[labelKey].items()}
return bilstm
def saveModel(self, modelName):
import json
import h5py
if self.modelSavePath == None:
raise ValueError('modelSavePath not specified.')
directory = os.path.dirname(savePath)
if not os.path.exists(directory):
os.makedirs(directory)
if os.path.isfile(savePath):
logging.info("Model "+savePath+" already exists. Model will be overwritten")
self.models[modelName].save(savePath, True)
with h5py.File(savePath, 'a') as h5file:
h5file.attrs['mappings'] = json.dumps(self.mappings)
h5file.attrs['params'] = json.dumps(self.params)
h5file.attrs['modelName'] = modelName
h5file.attrs['labelKey'] = self.datasets[modelName]['label']
from neuralnets.BiLSTM_weightedloss import BiLSTM
model = BiLSTM.loadModel(sys.argv[2])
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/59e9f533-cd4d-420e-b021-d364119c28c5%40googlegroups.com.
def my_init(shape, dtype=None):
mean_file=str('means')
mean_gmm = np.loadtxt(mean_file, dtype=np.object, delimiter=" ")
mean_gmm = mean_gmm.astype(np.float)
eq_norm = np.sqrt(np.sum(mean_gmm**2, axis=1))
norm_mean = np.transpose(mean_gmm/eq_norm[:, np.newaxis])
kvar = K.variable(value=norm_mean, dtype='float32')
return kvar
model.add(Dense(256, input_dim=60, activation='relu', kernel_initializer=my_init))
custom_obj = {}
custom_obj['my_init'] = my_init
saved_nn = load_model('model_saved.h5',custom_objects=custom_obj)
Traceback (most recent call last):
File "zstat_compute_DNN_withgmm_means.py", line 57, in <module>
saved_nn = load_model('tr_mfcc_zstat_ht_3layers_256_512_256_assign_mean_asWeights.h5',custom_objects=custom_obj)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/models.py", line 243, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/models.py", line 317, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 143, in deserialize_keras_object
list(custom_objects.items())))
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/models.py", line 1352, in from_config
layer = layer_module.deserialize(conf, custom_objects=custom_objects)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
return cls.from_config(config['config'])
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1269, in from_config
return cls(**config)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/layers/core.py", line 824, in __init__
self.kernel_initializer = initializers.get(kernel_initializer)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/initializers.py", line 498, in get
return deserialize(config)
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/initializers.py", line 490, in deserialize
printable_module_name='initializer')
File "/home/shareef/miniconda2/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 152, in deserialize_keras_object
return cls(**config['config'])
TypeError: my_init() takes at least 1 argument (0 given)