Search code examples
tensorflowkerasscipytensorflow2.0scipy-optimize

Scipy Optimizer Interface for Keras


I tried to use the implementation of Scipy optimizer interface for keras (https://github.com/pedro-r-marques/keras-opt) like its example but I got the following error:

'Sequential' object has no attribute '_configure_steps_per_execution'

I've searched a lot but I couldn't find anything related to _configure_steps_per_execution function.

Can anyone help me?


Solution

  • Works fine. Make sure you fully fill this 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)>]