Skip to content

Instantly share code, notes, and snippets.

@napsternxg
Created July 31, 2015 16:22
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napsternxg/723a39071659bb763cff to your computer and use it in GitHub Desktop.
Save napsternxg/723a39071659bb763cff to your computer and use it in GitHub Desktop.
Implementing linear regression in keras
"""
Author: Shubhanshu Mishra
Posted this on the keras issue tracker at: https://github.com/fchollet/keras/issues/108
Implementing a linear regression using Keras.
"""
from keras.models import Sequential
from keras.layers.core import Dense, Activation
model = Sequential()
model.add(Dense(2,1,init='uniform', activation='linear'))
model.compile(loss='mse', optimizer='rmsprop')
model.fit(X_train, y_train, nb_epoch=1000, batch_size=16,verbose=0)
model.fit(X_train, y_train, nb_epoch=1, batch_size=16,verbose=1)
score = model.evaluate(X_test, y_test, batch_size=16)
@scottlawsonbc
Copy link

No longer works on the latest version of Keras

@riders994
Copy link

@scottlawsonbc This will work if you reformat the Dense layer to the new format.

@qks1lver
Copy link

Hello, I'm knew to Keras. Can you show me how to write it with the new Dense format? Thanks!

@peachthiefmedia
Copy link

@qks1lver it should be model.add(Dense(1, input_dim=2)) for the new api I think assuming 1 output from two input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment