from keras.models import Sequential from keras.layers import Dense # Create a simple linear model with one input and one output model = Sequential() model.add(Dense(1, input_shape=(1,))) # Compile the model using SGD optimizer model.compile(optimizer='sgd', loss='mse') # Train the model on some random data X = np.random.randn(100, 1) y = X * 2 + 1 model.fit(X, y, epochs=100, batch_size=10)
from keras.models import Sequential from keras.layers import Dense # Create a simple model with two hidden layers model = Sequential() model.add(Dense(10, input_shape=(2,), activation='relu')) model.add(Dense(10, activation='relu')) model.add(Dense(1, activation='relu')) # Compile the model using SGD optimizer with momentum optimizer = keras.optimizers.SGD(lr=0.01, momentum=0.9) model.compile(optimizer=optimizer, loss='mse') # Train the model on some random data X = np.random.randn(100, 2) y = np.sum(X, axis=1, keepdims=True) model.fit(X, y, epochs=100, batch_size=10)The package library for the keras.optimizers SGD is Keras, which is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.