python - 为什么在 Keras 中包裹一层会移除另一层的内核属性?
问题描述
我目前正在为时间序列预测创建一个基于 LSTM 的网络,我想尝试使用 Keras 的Bidirectional
包装器来看看它是否可以提高我的准确性。
但是,添加包装器会导致我的输出层丢失其kernel
属性,这是有问题的,因为我的优化器尝试访问它,导致编译期间崩溃。
也就是说,当我这样做时:
model = Sequential()
model.add(LSTM(
100,
batch_input_shape=(batch_size, look_back, features),
))
model.add(Dense(1))
print(hasattr(model.layers[-1], 'kernel'))
真的
但是当像这样包装 LSTM 时:
model = Sequential()
model.add(Bidirectional(LSTM(
100,
batch_input_shape=(batch_size, look_back, features),
)))
model.add(Dense(1))
print(hasattr(model.layers[-1], 'kernel'))
错误的
解决方案
解决方案是让您的网络预测某些内容,然后使用您的自定义优化器对其进行编译:
model = Sequential()
model.add(Bidirectional(LSTM(
100,
batch_input_shape=(batch_size, look_back, features),
)))
model.add(Dense(1))
model.predict(np.zeros((batch_size, look_back, features)))
print(hasattr(model.layers[-1], 'kernel'))
model.compile(optimizer=CustomOptimizer(), loss='mse')
真的
推荐阅读
- php - 选项可以从php中选择
- javascript - 将 Promise 解析/拒绝处理程序传递给子函数是错误的吗?
- airflow - 将运算符融合在一起
- vue.js - 将 Vuelidate 与 v-for 和 props 一起使用
- highcharts - 想要在图表加载时仅绘制一条样条线,并在加载后进一步添加其他样条线
- python - 带有两个参数的 defaultdict lambda(由于缺少单词,它本身不是参数)
- php - Symfony fzaninotto/faker,错误:找不到类“Faker\Factory”
- python - 如何使用 Windows 批处理文件运行 R 脚本,然后运行 Python 脚本,然后运行 T-SQL 查询?
- concurrency - 将循环分成线程的最佳方法?
- sql-server - 数据总是从以前的数据库中获取,而不是从新的数据库中获取