【发布时间】:2020-07-23 22:14:31
【问题描述】:
目前我正在研究扬声器分类,我必须执行 LSTM。 我想在 keras 中制作自定义优化器。为此,我以自定义方式重新实现了 SMORMS3,我的意思是我为 LSTM 定义了类。部分代码: 我从https://gist.github.com/hbredin/89b2249504a62712441e7ffec7de9518获取代码
from keras.legacy import interfaces
from keras.optimizers import Optimizer
import keras.backend as K
class SMORMS3(Optimizer):
def __init__(self, lr=0.001, epsilon=1e-16, decay=0.,
**kwargs):
super(SMORMS3, self).__init__(**kwargs)
with K.name_scope(self.__class__.__name__):
self.lr = K.variable(lr, name='lr')
self.decay = K.variable(decay, name='decay')
self.iterations = K.variable(0, dtype='int64', name='iterations')
self.epsilon = epsilon
self.initial_decay = decay
@interfaces.legacy_get_updates_support
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
shapes = [K.get_variable_shape(p) for p in params]
ms = [K.zeros(shape) for shape in shapes]
vs = [K.zeros(shape) for shape in shapes]
mems = [K.zeros(shape) for shape in shapes]
self.weights = [self.iterations] + ms + vs + mems
self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr *= (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay))))
for p, g, m, v, mem in zip(params, grads, ms, vs, mems):
r = 1. / (1. + mem)
new_m = (1. - r) * m + r * g
new_v = (1. - r) * v + r * K.square(g)
denoise = K.square(new_m) / (new_v + self.epsilon)
new_p = p - g * K.minimum(lr, denoise) / (K.sqrt(new_v) + self.epsilon)
new_mem = 1. + mem * (1. - denoise)
self.updates.append(K.update(m, new_m))
self.updates.append(K.update(v, new_v))
self.updates.append(K.update(mem, new_mem))
# Apply constraints.
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates
def get_config(self):
config = {'lr': float(K.get_value(self.lr)),
'decay': float(K.get_value(self.decay)),
'epsilon': self.epsilon}
base_config = super(SMORMS3, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
这是我的 lstm 代码的一部分:
from keras.layers.core import Dense
from keras.models import Sequential
from keras.layers import Bidirectional, TimeDistributed, Dropout
from keras.layers import LSTM
import numpy as np
import keras
def train_bilstm():
model = Sequential()
model.add(Bidirectional(LSTM(128, return_sequences=True)))
model.add(Dropout(0.3))
model.add(Bidirectional(LSTM(128, return_sequences=True)))
model.add(Dropout(0.3))
model.add(TimeDistributed(Dense(32)))
model.add(TimeDistributed(Dense(32)))
model.add(TimeDistributed(Dense(1, activation='sigmoid')))
model.build(input_shape=(None, 137, 35))
model.compile(loss=keras.losses.binary_crossentropy, optimizer=SMORMS3(0.001, 1e-16, 0.), metrics=['accuracy'])
model.summary()
print("subhash")
all_x, all_y = load_dataset()
print(all_y.shape, np.sum(all_y))
subsample_all_x = []
subsample_all_y = []
for index in range(all_y.shape[0]):
class_positive = sum(all_y[index])
if class_positive > 5:
subsample_all_x.append(all_x[index][np.newaxis, :, :])
subsample_all_y.append(all_y[index])
all_x = np.vstack(subsample_all_x)
all_y = np.vstack(subsample_all_y)
print(all_y.shape, np.sum(all_y))
all_y = all_y[:, :, np.newaxis]
indices = np.random.permutation(all_x.shape[0])
all_x_random = all_x[indices]
all_y_random = all_y[indices]
datasize = all_x_random.shape[0]
train_size = int(datasize*0.97)
train_x = all_x_random[0:train_size]
valid_x = all_x_random[train_size:]
train_y = all_y_random[0:train_size]
valid_y = all_y_random[train_size:]
print('train over')
my = model.fit(x=train_x, y=train_y, batch_size=256, epochs=50,
validation_data=(valid_x, valid_y), shuffle=True)
model.save('model_hindi_2.h5')
def save_model(model, json_model_file, h5_model_file):
# serialize model to JSON
model_json = model.to_json()
with open(json_model_file, "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights(h5_model_file)
print("Saved model to disk")
# model_name = 'speech_seg1'
# json_model_file = '/content/drive/My Drive/SRU/model_hindi_2'+'.json'
# h5_model_file = '/content/drive/My Drive/SRU/model_hindi_2'+'.h5'
# save_model(model, json_model_file, h5_model_file)
我遇到了一个错误:
TypeError Traceback (most recent call last)
<ipython-input-133-e28aeaab5d03> in <module>
----> 1 train_bilstm()
<ipython-input-132-f20f9c93ac69> in train_bilstm()
20 model.build(input_shape=(None, 137, 35))
21
---> 22 model.compile(loss=keras.losses.binary_crossentropy, optimizer=SMORMS3(), metrics=['accuracy'])
23 model.summary()
24 print("subhash")
<ipython-input-127-f43b8a02704f> in __init__(self, lr, epsilon, decay, **kwargs)
20 def __init__(self, lr=0.001, epsilon=1e-16, decay=0.,
21 **kwargs):
---> 22 super(SMORMS3, self).__init__(**kwargs)
23 with K.name_scope(self.__class__.__name__):
24 self.lr = K.variable(lr, name='lr')
TypeError: __init__() missing 1 required positional argument: 'name'
【问题讨论】:
-
请发布完整的回溯(作为文本),以便我们查看您的代码的哪一行导致了错误。
-
我编辑了我的问题。
-
您的编辑不包括完整的错误回溯(TypeError 行之前的所有内容)。
-
OKK,,,我又编辑了。
-
它应该告诉你所有你需要知道的,
Optimizer.__init__()期望一个namekwarg,但你没有通过一个。试试SMORMS3(name='foo')或类似的。
标签: python machine-learning keras