微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Keras 的 Scipy 优化器界面

如何解决Keras 的 Scipy 优化器界面

我尝试将 Scipy optimizer 接口的实现用于 keras (https://github.com/pedro-r-marques/keras-opt) 就像其示例一样,但出现以下错误

'Sequential' object has no attribute '_configure_steps_per_execution'

搜索了很多,但找不到与 _configure_steps_per_execution 函数相关的任何内容

有人可以帮我吗?

解决方法

工作正常。确保您完全填写此 requirement

!git clone https://github.com/pedro-r-marques/keras-opt.git
import sys 
sys.path.insert(0,"/content/keras-opt")

from keras_opt import scipy_optimizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,InputLayer

model = Sequential()
model.add(InputLayer(input_shape=(4,)))
model.add(Dense(1,use_bias=False))
model.compile(loss='mse')

#%%
# Generate test data

import numpy as np

np.random.seed(42)
X = np.random.uniform(size=40).reshape(10,4)
y = np.dot(X,np.array([1,2,3,4])[:,np.newaxis])

#%%
# Use scipy.optimize to minimize the cost
model.train_function = scipy_optimizer.make_train_function(
            model,maxiter=20)
history = model.fit(X,y)

#%%
# Show weights.
model.trainable_weights
0/Unknown - 0s 0s/step - loss: 2.1390e-11Optimization terminated successfully.
Current function value: 0.000000
Iterations: 6
Function evaluations: 14
Gradient evaluations: 14
1/1 [==============================] - 0s 313ms/step - loss: 2.1390e-11
[<tf.Variable 'dense_1/kernel:0' shape=(4,1) dtype=float32,numpy=
 array([[1.0000011],[2.000016 ],[2.9999967],[3.9999871]],dtype=float32)>]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。