python - 类型错误:__init__() 缺少 1 个必需的位置参数:“名称”
问题描述
目前我正在研究扬声器分类,我必须执行 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'
解决方案
当您创建 Optimizer 类的实例时,您必须为其命名。这在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)
推荐阅读
- vba - 检查空数组返回 -400877505
- go - 在 jwt-go 中解析 JWT Auth 令牌时,密钥的类型无效
- javascript - 在可调整大小的表格上使用固定标题滚动
- javascript - 用多个 OR 条件编写 if 语句的更短方法?
- python - 有没有另一种方法可以在没有内存问题的情况下在 python 中读取巨大的 XML 数据集,我也使用了迭代
- mvvm - 如何在 Prism 7.2.0.1367 的 IDialogAware 中为 Xamarin 表单使用 RequestClose
- sql-server - 如何在 SQL SERVER 的列中列出所有可能的组合?
- amazon-web-services - 基于过滤器比较 S3 存储桶
- php - 在返回语句上使用 `foobar or die` - 意外结果
- linux - 如何构建 1999 年的老式软件以在当前的 Ubuntu 上运行?