![](/img/trans.png)
[英]click TypeError: __init__() missing 1 required positional argument: 'name'
[英]TypeError: __init__() missing 1 required positional argument: 'name'
目前我正在研究揚聲器分類,我必須執行 LSTM。 我想在 keras 中制作自定義優化器。 為此,我以自定義方式重新實現了 SMORMS3,我的意思是我為 LSTM 定義了 class。 部分代碼:我從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'
當您創建優化器 class 的實例時,您必須為其命名。 這在tf.keras.optimizers.Optimizer`的文檔中進行了解釋。
在這種情況下,修改此行以包含名稱:
super(SMORMS3, self).__init__(name="SMORMS3", **kwargs)
一個更靈活的選擇是修改您的__init__
簽名以包含帶有默認參數的name
變量,這將允許您根據需要覆蓋名稱,就像您可以為預定義的優化器一樣:
def __init__(self, lr=0.001, epsilon=1e-16, decay=0., name="SMORMS3"
**kwargs):
super(SMORMS3, self).__init__(name=name, **kwargs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.